-
Notifications
You must be signed in to change notification settings - Fork 319
Simplified ListWriter await logic. #8963
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ca4d65b
Simplified ListWriter await logic.
AlexeyKuznetsov-DD ef302ee
Fixed counter state.
AlexeyKuznetsov-DD 675e3cf
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD 45aabcb
Fixed counter state.
AlexeyKuznetsov-DD d1fdec0
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD ab6b5cf
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD c062ad7
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD c29d38a
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD 28088ae
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD ca2c2ce
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD 967749d
Test SSH signing.
AlexeyKuznetsov-DD 3b857fc
Test SSH signing.
AlexeyKuznetsov-DD 73ad88c
Merge branch 'master' into alexeyk/fixed-flaky-pekko-test
AlexeyKuznetsov-DD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,27 @@ | ||
| package datadog.trace.common.writer; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
|
||
| import datadog.trace.core.DDSpan; | ||
| import datadog.trace.core.MetadataConsumer; | ||
| import datadog.trace.core.tagprocessor.PeerServiceCalculator; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.BooleanSupplier; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** List writer used by tests mostly */ | ||
| public class ListWriter extends CopyOnWriteArrayList<List<DDSpan>> implements Writer { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(ListWriter.class); | ||
| private static final Filter ACCEPT_ALL = trace -> true; | ||
|
|
||
| public static final Filter ACCEPT_ALL = | ||
| new Filter() { | ||
| @Override | ||
| public boolean accept(List<DDSpan> trace) { | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| private final List<CountDownLatch> latches = new ArrayList<>(); | ||
| private final AtomicInteger traceCount = new AtomicInteger(); | ||
| private final TraceStructureWriter structureWriter = new TraceStructureWriter(true); | ||
| private final Object monitor = new Object(); | ||
|
|
||
| private final PeerServiceCalculator peerServiceCalculator = new PeerServiceCalculator(); | ||
| private Filter filter = ACCEPT_ALL; | ||
|
|
||
| public List<DDSpan> firstTrace() { | ||
|
|
@@ -47,30 +38,41 @@ public void write(List<DDSpan> trace) { | |
| // remotely realistic so the test actually test something | ||
| span.processTagsAndBaggage(MetadataConsumer.NO_OP); | ||
| } | ||
|
|
||
| add(trace); | ||
| structureWriter.write(trace); | ||
|
|
||
| traceCount.incrementAndGet(); | ||
| synchronized (latches) { | ||
| add(trace); | ||
| for (final CountDownLatch latch : latches) { | ||
| if (size() >= latch.getCount()) { | ||
| while (latch.getCount() > 0) { | ||
| latch.countDown(); | ||
| } | ||
| } | ||
| } | ||
| synchronized (monitor) { | ||
| monitor.notifyAll(); | ||
| } | ||
| structureWriter.write(trace); | ||
| } | ||
|
|
||
| public boolean waitForTracesMax(final int number, int seconds) | ||
| throws InterruptedException, TimeoutException { | ||
| final CountDownLatch latch = new CountDownLatch(number); | ||
| synchronized (latches) { | ||
| if (size() >= number) { | ||
| private boolean awaitUntilDeadline(long timeout, TimeUnit unit, BooleanSupplier predicate) | ||
| throws InterruptedException { | ||
| long deadline = System.currentTimeMillis() + unit.toMillis(timeout); | ||
|
|
||
| while (true) { | ||
| if (predicate.getAsBoolean()) { | ||
| return true; | ||
| } | ||
| latches.add(latch); | ||
|
|
||
| long now = System.currentTimeMillis(); | ||
|
Comment on lines
+53
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Shouldn't we use |
||
| long waitTime = deadline - now; | ||
| if (waitTime <= 0) { | ||
| break; | ||
| } | ||
|
|
||
| synchronized (monitor) { | ||
| monitor.wait(waitTime); | ||
| } | ||
| } | ||
| return latch.await(seconds, TimeUnit.SECONDS); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public boolean waitForTracesMax(final int number, int seconds) throws InterruptedException { | ||
| return awaitUntilDeadline(seconds, SECONDS, () -> traceCount.get() >= number); | ||
| } | ||
|
|
||
| public void waitForTraces(final int number) throws InterruptedException, TimeoutException { | ||
|
|
@@ -88,24 +90,17 @@ public void waitForTraces(final int number) throws InterruptedException, Timeout | |
| } | ||
|
|
||
| public void waitUntilReported(final DDSpan span) throws InterruptedException, TimeoutException { | ||
| waitUntilReported(span, 20, TimeUnit.SECONDS); | ||
| waitUntilReported(span, 20, SECONDS); | ||
| } | ||
|
|
||
| public void waitUntilReported(final DDSpan span, int timeout, TimeUnit unit) | ||
| throws InterruptedException, TimeoutException { | ||
| while (true) { | ||
| final CountDownLatch latch = new CountDownLatch(size() + 1); | ||
| synchronized (latches) { | ||
| latches.add(latch); | ||
| } | ||
| if (isReported(span)) { | ||
| return; | ||
| } | ||
| if (!latch.await(timeout, unit)) { | ||
| String msg = "Timeout waiting for span to be reported: " + span; | ||
| log.warn(msg); | ||
| throw new TimeoutException(msg); | ||
| } | ||
| boolean reported = awaitUntilDeadline(timeout, unit, () -> isReported(span)); | ||
|
|
||
| if (!reported) { | ||
| String msg = "Timeout waiting for span to be reported: " + span; | ||
| log.warn(msg); | ||
| throw new TimeoutException(msg); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -142,17 +137,16 @@ public boolean flush() { | |
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| super.clear(); | ||
|
|
||
| traceCount.set(0); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| clear(); | ||
| synchronized (latches) { | ||
| for (final CountDownLatch latch : latches) { | ||
| while (latch.getCount() > 0) { | ||
| latch.countDown(); | ||
| } | ||
| } | ||
| latches.clear(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.