Skip to content

Commit

Permalink
Merge pull request #423 from DataDog/mar-kolya/fix-span-cleaner-bug
Browse files Browse the repository at this point in the history
Fix span cleaner executor to use correct span cleaner reference
  • Loading branch information
mar-kolya committed Aug 3, 2018
2 parents 911ad5f + e517080 commit 5289204
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
32 changes: 9 additions & 23 deletions dd-trace-ot/src/main/java/datadog/opentracing/PendingTrace.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@

@Slf4j
public class PendingTrace extends ConcurrentLinkedDeque<DDSpan> {
private static final SpanCleaner SPAN_CLEANER;

static {
SPAN_CLEANER = new SpanCleaner();
SPAN_CLEANER.start();
}
private static final SpanCleaner SPAN_CLEANER = new SpanCleaner();

private final DDTracer tracer;
private final String traceId;
Expand Down Expand Up @@ -56,8 +51,8 @@ public class PendingTrace extends ConcurrentLinkedDeque<DDSpan> {
this.traceId = traceId;
this.serviceNameMappings = serviceNameMappings;

this.startTimeNano = Clock.currentNanoTime();
this.startNanoTicks = Clock.currentNanoTicks();
startTimeNano = Clock.currentNanoTime();
startNanoTicks = Clock.currentNanoTicks();

SPAN_CLEANER.pendingTraces.add(this);
}
Expand Down Expand Up @@ -198,7 +193,7 @@ private void write() {
if (isWritten.compareAndSet(false, true)) {
SPAN_CLEANER.pendingTraces.remove(this);
if (!isEmpty()) {
log.debug("Writing {} spans to {}.", this.size(), tracer.writer);
log.debug("Writing {} spans to {}.", size(), tracer.writer);
tracer.write(this);
}
}
Expand Down Expand Up @@ -254,20 +249,8 @@ public Thread newThread(final Runnable r) {
private final Set<PendingTrace> pendingTraces =
Collections.newSetFromMap(new ConcurrentHashMap<PendingTrace, Boolean>());

void start() {
executorService.scheduleAtFixedRate(new SpanCleaner(), 0, CLEAN_FREQUENCY, TimeUnit.SECONDS);
try {
Runtime.getRuntime()
.addShutdownHook(
new Thread() {
@Override
public void run() {
PendingTrace.SpanCleaner.this.close();
}
});
} catch (final IllegalStateException ex) {
// The JVM is already shutting down.
}
public SpanCleaner() {
executorService.scheduleAtFixedRate(this, 0, CLEAN_FREQUENCY, TimeUnit.SECONDS);
}

@Override
Expand All @@ -285,6 +268,9 @@ public void close() {
} catch (final InterruptedException e) {
log.info("Writer properly closed and async writer interrupted.");
}

// Make sure that whatever was left over gets cleaned up
run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import spock.lang.Subject

import java.util.concurrent.TimeUnit


class PendingTraceTest extends Specification {
def writer = new ListWriter()
def tracer = new DDTracer(writer)
Expand Down

0 comments on commit 5289204

Please sign in to comment.