[PR #1] Refactoring Thread > Executor

This commit is contained in:
Guillaume Polaert 2017-05-02 17:13:00 +02:00
parent c9ea5c0c5d
commit 9e9e525535
1 changed files with 108 additions and 111 deletions

View File

@ -7,13 +7,11 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;
/** /**
* This writer write provided traces to the a DD agent which is most of time located on the same host. * This writer write provided traces to the a DD agent which is most of time located on the same host.
* * <p>
* It handles writes asynchronuously so the calling threads are automatically released. However, if too much spans are collected * It handles writes asynchronuously so the calling threads are automatically released. However, if too much spans are collected
* the writers can reach a state where it is forced to drop incoming spans. * the writers can reach a state where it is forced to drop incoming spans.
*/ */
@ -50,7 +48,7 @@ public class DDAgentWriter implements Writer {
/** /**
* Async worker that posts the spans to the DD agent * Async worker that posts the spans to the DD agent
*/ */
private final Thread asyncWriterThread; private final ExecutorService executor = Executors.newSingleThreadExecutor();
/** /**
* The DD agent api * The DD agent api
@ -68,9 +66,8 @@ public class DDAgentWriter implements Writer {
tokens = new Semaphore(DEFAULT_MAX_SPANS); tokens = new Semaphore(DEFAULT_MAX_SPANS);
traces = new ArrayBlockingQueue<List<Span>>(DEFAULT_MAX_SPANS); traces = new ArrayBlockingQueue<List<Span>>(DEFAULT_MAX_SPANS);
asyncWriterThread = new Thread(new SpansSendingTask(), "dd.DDAgentWriter-SpansSendingTask"); executor.submit(new SpansSendingTask());
asyncWriterThread.setDaemon(true);
asyncWriterThread.start();
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -80,9 +77,9 @@ public class DDAgentWriter implements Writer {
//Try to add a new span in the queue //Try to add a new span in the queue
boolean proceed = tokens.tryAcquire(trace.size()); boolean proceed = tokens.tryAcquire(trace.size());
if(proceed){ if (proceed) {
traces.add(trace); traces.add(trace);
}else{ } else {
logger.warn("Cannot add a trace of {} as the async queue is full. Queue max size: {}", trace.size(), DEFAULT_MAX_SPANS); logger.warn("Cannot add a trace of {} as the async queue is full. Queue max size: {}", trace.size(), DEFAULT_MAX_SPANS);
} }
} }
@ -91,9 +88,9 @@ public class DDAgentWriter implements Writer {
* @see com.datadoghq.trace.Writer#close() * @see com.datadoghq.trace.Writer#close()
*/ */
public void close() { public void close() {
asyncWriterThread.interrupt(); executor.shutdownNow();
try { try {
asyncWriterThread.join(); executor.awaitTermination(500, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.info("Writer properly closed and async writer interrupted."); logger.info("Writer properly closed and async writer interrupted.");
} }
@ -122,8 +119,8 @@ public class DDAgentWriter implements Writer {
//Compute the number of spans sent //Compute the number of spans sent
int spansCount = 0; int spansCount = 0;
for(List<Span> trace:payload){ for (List<Span> trace : payload) {
spansCount+=trace.size(); spansCount += trace.size();
} }
logger.debug("Async writer just sent {} spans through {} traces", spansCount, payload.size()); logger.debug("Async writer just sent {} spans through {} traces", spansCount, payload.size());