Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make access to ArrayDeque synchronized #7027

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.opentelemetry.sdk.trace.data.SpanData;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.Queue;
import java.util.function.BiFunction;

/**
Expand All @@ -20,7 +20,8 @@
*/
public class SpanReusableDataMarshaler {

private final Deque<LowAllocationTraceRequestMarshaler> marshalerPool = new ArrayDeque<>();
private final SynchronizedQueue<LowAllocationTraceRequestMarshaler> marshalerPool =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same construct is also used in

private final Deque<LowAllocationLogsRequestMarshaler> marshalerPool = new ArrayDeque<>();
and
private final Deque<LowAllocationMetricsRequestMarshaler> marshalerPool = new ArrayDeque<>();

did you consider just using a different queue implementation such as ConcurrentLinkedDeque?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this.

Also, this change is applicable to MetricReusableDataMarshaler and LogReusableDataMarshaler as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you consider just using a different queue implementation such as ConcurrentLinkedDeque?

No, I didn't even remember ConcurrentLinkedDeque existed, and I'm not familiar with its guarantees and performance characteristics. Might well be a better and lighter option.

new SynchronizedQueue<>(new ArrayDeque<>());

private final MemoryMode memoryMode;
private final BiFunction<Marshaler, Integer, CompletableResultCode> doExport;
Expand Down Expand Up @@ -55,4 +56,21 @@ public CompletableResultCode export(Collection<SpanData> spans) {
TraceRequestMarshaler request = TraceRequestMarshaler.create(spans);
return doExport.apply(request, spans.size());
}

private static class SynchronizedQueue<T> {
private final Queue<T> queue;

private SynchronizedQueue(Queue<T> queue) {
this.queue = queue;
}

@SuppressWarnings("UnusedReturnValue")
public synchronized boolean add(T t) {
return queue.add(t);
}

public synchronized T poll() {
return queue.poll();
}
}
}
Loading