-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AsyncReporter/SpanHandler: make queuedMaxBytes=0 disable pre-flight s…
…ize checks (#260) Signed-off-by: Andriy Redko <drreta@gmail.com> Signed-off-by: Adrian Cole <adrian@tetrate.io> Co-authored-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
Showing
10 changed files
with
588 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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
146 changes: 146 additions & 0 deletions
146
benchmarks/src/test/java/zipkin2/reporter/internal/BoundedQueueBenchmarks.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright The OpenZipkin Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package zipkin2.reporter.internal; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.openjdk.jmh.annotations.AuxCounters; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Group; | ||
import org.openjdk.jmh.annotations.GroupThreads; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
@Measurement(iterations = 5, time = 1) | ||
@Warmup(iterations = 10, time = 1) | ||
@Fork(3) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@State(Scope.Group) | ||
public class BoundedQueueBenchmarks { | ||
static final byte ONE = 1; | ||
|
||
@Param( {"0", "10000"}) | ||
public int maxBytes; | ||
|
||
@AuxCounters | ||
@State(Scope.Thread) | ||
public static class OfferCounters { | ||
public int offersFailed; | ||
public int offersMade; | ||
|
||
@Setup(Level.Iteration) | ||
public void clean() { | ||
offersFailed = offersMade = 0; | ||
} | ||
} | ||
|
||
@AuxCounters | ||
@State(Scope.Thread) | ||
public static class DrainCounters { | ||
public int drained; | ||
|
||
@Setup(Level.Iteration) | ||
public void clean() { | ||
drained = 0; | ||
} | ||
} | ||
|
||
private static ThreadLocal<Object> marker = new ThreadLocal<>(); | ||
|
||
@State(Scope.Thread) | ||
public static class ConsumerMarker { | ||
public ConsumerMarker() { | ||
marker.set(this); | ||
} | ||
} | ||
|
||
BoundedQueue<Byte> q; | ||
|
||
@Setup | ||
public void setup() { | ||
q = BoundedQueue.create(null, null, null, 10000, 10000, maxBytes); | ||
} | ||
|
||
@Benchmark @Group("no_contention") @GroupThreads(1) | ||
public void no_contention_offer(OfferCounters counters) { | ||
if (q.offer(ONE, 1)) { | ||
counters.offersMade++; | ||
} else { | ||
counters.offersFailed++; | ||
} | ||
} | ||
|
||
@Benchmark @Group("no_contention") @GroupThreads(1) | ||
public void no_contention_drain(DrainCounters counters, ConsumerMarker cm) { | ||
q.drainTo((s, b) -> { | ||
counters.drained++; | ||
return true; | ||
}, 1000); | ||
} | ||
|
||
@Benchmark @Group("mild_contention") @GroupThreads(2) | ||
public void mild_contention_offer(OfferCounters counters) { | ||
if (q.offer(ONE, 1)) { | ||
counters.offersMade++; | ||
} else { | ||
counters.offersFailed++; | ||
} | ||
} | ||
|
||
@Benchmark @Group("mild_contention") @GroupThreads(1) | ||
public void mild_contention_drain(DrainCounters counters, ConsumerMarker cm) { | ||
q.drainTo((s, b) -> { | ||
counters.drained++; | ||
return true; | ||
}, 1000); | ||
} | ||
|
||
@Benchmark @Group("high_contention") @GroupThreads(8) | ||
public void high_contention_offer(OfferCounters counters) { | ||
if (q.offer(ONE, 1)) { | ||
counters.offersMade++; | ||
} else { | ||
counters.offersFailed++; | ||
} | ||
} | ||
|
||
@Benchmark @Group("high_contention") @GroupThreads(1) | ||
public void high_contention_drain(DrainCounters counters, ConsumerMarker cm) { | ||
q.drainTo((s, b) -> { | ||
counters.drained++; | ||
return true; | ||
}, 1000); | ||
} | ||
|
||
@TearDown(Level.Iteration) | ||
public void emptyQ() { | ||
// If this thread didn't drain, return | ||
if (marker.get() == null) return; | ||
q.clear(); | ||
} | ||
|
||
// Convenience main entry-point | ||
public static void main(String[] args) throws RunnerException { | ||
Options opt = new OptionsBuilder() | ||
.include(".*" + BoundedQueueBenchmarks.class.getSimpleName() + ".*") | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
} |
This file contains 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
This file contains 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
49 changes: 49 additions & 0 deletions
49
core/src/main/java/zipkin2/reporter/internal/BoundedQueue.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenZipkin Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package zipkin2.reporter.internal; | ||
|
||
import zipkin2.reporter.BytesEncoder; | ||
import zipkin2.reporter.BytesMessageSender; | ||
import zipkin2.reporter.ReporterMetrics; | ||
|
||
/** | ||
* Multi-producer, multi-consumer queue that could be bounded by count or/and size. | ||
*/ | ||
abstract class BoundedQueue<S> implements SpanWithSizeConsumer<S> { | ||
static <S> BoundedQueue<S> create(BytesEncoder<S> encoder, BytesMessageSender sender, | ||
ReporterMetrics metrics, int messageMaxBytes, int maxSize, int maxBytes) { | ||
if (maxBytes > 0) { | ||
return new ByteBoundedQueue<S>(encoder, sender, metrics, messageMaxBytes, maxSize, maxBytes); | ||
} else { | ||
return new CountBoundedQueue<S>(encoder, sender, metrics, messageMaxBytes, maxSize); | ||
} | ||
} | ||
|
||
/** | ||
* Max element's count of this bounded queue | ||
*/ | ||
abstract int maxSize(); | ||
|
||
/** | ||
* Clear this bounded queue | ||
*/ | ||
abstract int clear(); | ||
|
||
/** | ||
* Drains this bounded queue. Blocks for up to nanosTimeout for spans to appear. | ||
* Then, consume as many as possible. | ||
*/ | ||
abstract int drainTo(SpanWithSizeConsumer<S> bundler, long remainingNanos); | ||
|
||
/** Returns true if the element could be added or false if it could not. */ | ||
abstract boolean offer(S next); | ||
} | ||
|
||
interface SpanWithSizeConsumer<S> { | ||
/** Returns true if the element could be added or false if it could not due to its size. */ | ||
boolean offer(S next, int nextSizeInBytes); | ||
} | ||
|
Oops, something went wrong.