Improvements due to the review
MODIFY
dd-trace/src/main/java/com/datadoghq/trace/writer/DDAgentWriter.java
dd-trace/src/main/java/com/datadoghq/trace/writer/DDAgentWriter.java
@@ -38,7 +38,7 @@ public class DDAgentWriter implements Writer {
/** Flush interval for the API in seconds */
private static final long FLUSH_TIME_SECONDS = 1;
- /** Scheduled thread pool, it' acting like a cron */
+ /** Scheduled thread pool, acting like a cron */
private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
/** Effective thread pool, where real logic is done */
@@ -70,7 +70,7 @@ public class DDAgentWriter implements Writer {
final List<DDBaseSpan<?>> removed = traces.add(trace);
if (removed != null && !queueFullReported) {
- log.warn("Queue is full, dropping one trace, queue size: {}", DEFAULT_MAX_TRACES);
+ log.debug("Queue is full, traces will be discarded, queue size: {}", DEFAULT_MAX_TRACES);
queueFullReported = true;
return;
}
@@ -107,7 +107,7 @@ public class DDAgentWriter implements Writer {
}
/** Infinite tasks blocking until some spans come in the blocking queue. */
- private class TracesSendingTask implements Runnable {
+ class TracesSendingTask implements Runnable {
@Override
public void run() {
@@ -122,8 +122,6 @@ public class DDAgentWriter implements Writer {
}
}
- public void size() {}
-
class SendingTask implements Callable<Long> {
@Override

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/writer/WriterQueue.java
dd-trace/src/main/java/com/datadoghq/trace/writer/WriterQueue.java
package com.datadoghq.trace.writer;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
@@ -21,7 +20,6 @@ class WriterQueue<T> {
private final int capacity;
private volatile ArrayList<T> list;
- private volatile int elementCount = 0;
/**
* Default construct, a capacity must be provided
@@ -32,7 +30,7 @@ class WriterQueue<T> {
if (capacity < 1) {
throw new IllegalArgumentException("Capacity couldn't be 0");
}
- list = new ArrayList<>(capacity);
+ this.list = emptyList(capacity);
this.capacity = capacity;
}
@@ -43,10 +41,8 @@ class WriterQueue<T> {
* @return a list contain all elements
*/
public synchronized List<T> getAll() {
- List<T> all = Collections.emptyList();
- all = list;
- list = new ArrayList<>(capacity);
- elementCount = 0;
+ final List<T> all = list;
+ list = emptyList(capacity);
return all;
}
@@ -60,11 +56,10 @@ class WriterQueue<T> {
public synchronized T add(final T element) {
T removed = null;
- if (elementCount < capacity) {
+ if (list.size() < capacity) {
list.add(element);
- ++elementCount;
} else {
- final int index = ThreadLocalRandom.current().nextInt(0, elementCount);
+ final int index = ThreadLocalRandom.current().nextInt(0, list.size());
removed = list.set(index, element);
}
return removed;
@@ -78,7 +73,7 @@ class WriterQueue<T> {
* @return the current size of the queue
*/
public int size() {
- return elementCount;
+ return list.size();
}
/**
@@ -87,6 +82,10 @@ class WriterQueue<T> {
* @return true if the queue is empty
*/
public boolean isEmpty() {
- return elementCount == 0;
+ return list.isEmpty();
+ }
+
+ private ArrayList<T> emptyList(final int capacity) {
+ return new ArrayList<>(capacity);
}
}

Improvements due to the review