Elffuss BraidHub

kikocis/java-driver public

JAVA-626 Add metrics to track queue backlog in Cluster executors.

Adds 4 new metrics:
  - executor-queue-depth
  - blocking-executor-queue-depth
  - reconnection-scheduler-task-count
  - task-scheduler-task-count

These can be used for monitoring whether or not a Cluster's executors
are becoming backlogged which could help understand abnormal behavior of
the driver.
Changeset1805c994c4a95c44a8f7498c4fac723dfa5905f3f2575b77045916a8b98cf828
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-01-28 22:20 -06:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-18 11:13 -05:00
Parents6091a1724bd4

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +12 −6
@@ -1081,12 +1081,12 @@
10811081 return "cluster" + CLUSTER_ID.incrementAndGet();
10821082 }
10831083
1084- private static ListeningExecutorService makeExecutor(int threads, String name) {
1084+ private static ListeningExecutorService makeExecutor(int threads, String name, LinkedBlockingQueue<Runnable> workQueue) {
10851085 ThreadPoolExecutor executor = new ThreadPoolExecutor(threads,
10861086 threads,
10871087 DEFAULT_THREAD_KEEP_ALIVE,
10881088 TimeUnit.SECONDS,
1089- new LinkedBlockingQueue<Runnable>(),
1089+ workQueue,
10901090 threadFactory(name));
10911091
10921092 executor.allowCoreThreadTimeOut(true);
@@ -1119,17 +1119,23 @@
11191119
11201120 final ConvictionPolicy.Factory convictionPolicyFactory = new ConvictionPolicy.Simple.Factory();
11211121
1122- final ScheduledExecutorService reconnectionExecutor = Executors.newScheduledThreadPool(2, threadFactory("Reconnection-%d"));
1122+ final ScheduledThreadPoolExecutor reconnectionExecutor = new ScheduledThreadPoolExecutor(2, threadFactory("Reconnection-%d"));
11231123 // scheduledTasksExecutor is used to process C* notifications. So having it mono-threaded ensures notifications are
11241124 // applied in the order received.
1125- final ScheduledExecutorService scheduledTasksExecutor = Executors.newScheduledThreadPool(1, threadFactory("Scheduled Tasks-%d"));
1125+ final ScheduledThreadPoolExecutor scheduledTasksExecutor = new ScheduledThreadPoolExecutor(1, threadFactory("Scheduled Tasks-%d"));
11261126
11271127 // Executor used for tasks that shouldn't be executed on an IO thread. Used for short-lived, generally non-blocking tasks
11281128 final ListeningExecutorService executor;
11291129
1130+ // Work Queue used by executor.
1131+ final LinkedBlockingQueue<Runnable> executorQueue = new LinkedBlockingQueue<Runnable>();
1132+
11301133 // An executor for tasks that might block some time, like creating new connection, but are generally not too critical.
11311134 final ListeningExecutorService blockingExecutor;
11321135
1136+ // Work Queue used by blockingExecutor.
1137+ final LinkedBlockingQueue<Runnable> blockingExecutorQueue = new LinkedBlockingQueue<Runnable>();
1138+
11331139 final ConnectionReaper reaper;
11341140
11351141 final AtomicReference<CloseFuture> closeFuture = new AtomicReference<CloseFuture>();
@@ -1150,8 +1156,8 @@
11501156 this.configuration = configuration;
11511157 this.configuration.register(this);
11521158
1153- this.executor = makeExecutor(NON_BLOCKING_EXECUTOR_SIZE, "Cassandra Java Driver worker-%d");
1154- this.blockingExecutor = makeExecutor(2, "Cassandra Java Driver blocking tasks worker-%d");
1159+ this.executor = makeExecutor(NON_BLOCKING_EXECUTOR_SIZE, "Cassandra Java Driver worker-%d", executorQueue);
1160+ this.blockingExecutor = makeExecutor(2, "Cassandra Java Driver blocking tasks worker-%d", blockingExecutorQueue);
11551161
11561162 this.reaper = new ConnectionReaper();
11571163
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +58 −0
@@ -70,6 +70,34 @@
7070 }
7171 });
7272
73+ private final Gauge<Integer> executorQueueDepth = registry.register("executor-queue-depth", new Gauge<Integer>() {
74+ @Override
75+ public Integer getValue() {
76+ return manager.executorQueue.size();
77+ }
78+ });
79+
80+ private final Gauge<Integer> blockingExecutorQueueDepth = registry.register("blocking-executor-queue-depth", new Gauge<Integer>() {
81+ @Override
82+ public Integer getValue() {
83+ return manager.blockingExecutorQueue.size();
84+ }
85+ });
86+
87+ private final Gauge<Integer> reconnectionSchedulerQueueSize= registry.register("reconnection-scheduler-task-count", new Gauge<Integer>() {
88+ @Override
89+ public Integer getValue() {
90+ return manager.reconnectionExecutor.getQueue().size();
91+ }
92+ });
93+
94+ private final Gauge<Integer> taskSchedulerQueueSize = registry.register("task-scheduler-task-count", new Gauge<Integer>() {
95+ @Override
96+ public Integer getValue() {
97+ return manager.scheduledTasksExecutor.getQueue().size();
98+ }
99+ });
100+
73101 Metrics(Cluster.Manager manager) {
74102 this.manager = manager;
75103 if (manager.configuration.getMetricsOptions().isJMXReportingEnabled()) {
@@ -177,6 +205,36 @@
177205 return openConnections;
178206 }
179207
208+ /**
209+ * @return The number of queued up tasks in the non-blocking executor (Cassandra Java Driver workers).
210+ */
211+ public Gauge<Integer> getExecutorQueueDepth() {
212+ return executorQueueDepth;
213+ }
214+
215+ /**
216+ * @return The number of queued up tasks in the blocking executor (Cassandra Java Driver blocking tasks worker).
217+ */
218+ public Gauge<Integer> getBlockingExecutorQueueDepth() {
219+ return blockingExecutorQueueDepth;
220+ }
221+
222+ /**
223+ * @return The size of the work queue for the reconnection scheduler (Reconnection). A queue size > 0 does not
224+ * necessarily indicate a backlog as some tasks may not have been scheduled to execute yet.
225+ */
226+ public Gauge<Integer> getReconnectionSchedulerQueueSize() {
227+ return reconnectionSchedulerQueueSize;
228+ }
229+
230+ /**
231+ * @return The size of the work queue for the task scheduler (Scheduled Tasks). A queue size > 0 does not
232+ * necessarily indicate a backlog as some tasks may not have been scheduled to execute yet.
233+ */
234+ public Gauge<Integer> getTaskSchedulerQueueSize() {
235+ return taskSchedulerQueueSize;
236+ }
237+
180238 void shutdown() {
181239 if (jmxReporter != null)
182240 jmxReporter.stop();