[PR #1] Refactoring Thread > Executor
This commit is contained in:
parent
c9ea5c0c5d
commit
9e9e525535
|
@ -7,13 +7,11 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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
|
||||
*/
|
||||
private final Thread asyncWriterThread;
|
||||
private final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
/**
|
||||
* The DD agent api
|
||||
|
@ -68,9 +66,8 @@ public class DDAgentWriter implements Writer {
|
|||
tokens = new Semaphore(DEFAULT_MAX_SPANS);
|
||||
traces = new ArrayBlockingQueue<List<Span>>(DEFAULT_MAX_SPANS);
|
||||
|
||||
asyncWriterThread = new Thread(new SpansSendingTask(), "dd.DDAgentWriter-SpansSendingTask");
|
||||
asyncWriterThread.setDaemon(true);
|
||||
asyncWriterThread.start();
|
||||
executor.submit(new SpansSendingTask());
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
@ -80,9 +77,9 @@ public class DDAgentWriter implements Writer {
|
|||
//Try to add a new span in the queue
|
||||
boolean proceed = tokens.tryAcquire(trace.size());
|
||||
|
||||
if(proceed){
|
||||
if (proceed) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -91,9 +88,9 @@ public class DDAgentWriter implements Writer {
|
|||
* @see com.datadoghq.trace.Writer#close()
|
||||
*/
|
||||
public void close() {
|
||||
asyncWriterThread.interrupt();
|
||||
executor.shutdownNow();
|
||||
try {
|
||||
asyncWriterThread.join();
|
||||
executor.awaitTermination(500, TimeUnit.MILLISECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
logger.info("Writer properly closed and async writer interrupted.");
|
||||
}
|
||||
|
@ -122,8 +119,8 @@ public class DDAgentWriter implements Writer {
|
|||
|
||||
//Compute the number of spans sent
|
||||
int spansCount = 0;
|
||||
for(List<Span> trace:payload){
|
||||
spansCount+=trace.size();
|
||||
for (List<Span> trace : payload) {
|
||||
spansCount += trace.size();
|
||||
}
|
||||
logger.debug("Async writer just sent {} spans through {} traces", spansCount, payload.size());
|
||||
|
||||
|
|
Loading…
Reference in New Issue