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

Fix span cleaner executor to use correct span cleaner reference #423

Merged
merged 1 commit into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you relying on the shutdown hook from DDTracer then?

Copy link
Contributor Author

@mar-kolya mar-kolya Aug 2, 2018

Choose a reason for hiding this comment

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

Yes, my understanding is that original problem might have been caused by us creating two SpanCleaners instead of one. Do you have any thoughts/objections on this approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I think it's fine. Just making sure I understood.

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