Elffuss BraidHub

kikocis/java-driver public

Surface number of retries in metrics (JAVA-329).
Changesetf1f9cbc93507cac73c15befc9ce53121b9df85ae687a69c2ef523e408927ecbd
Authorolim7t <omichallat+github@gmail.com> · 2014-08-26 10:29 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-08-26 11:01 +02:00
Parents6d3e2a121092

Browse the files at this changeset

4 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +91 −5
@@ -18,6 +18,8 @@
1818 import java.util.HashSet;
1919 import java.util.Set;
2020
21+import com.codahale.metrics.Counter;
22+
2123 import com.codahale.metrics.*;
2224
2325 /**
@@ -114,16 +116,16 @@
114116 }
115117
116118 /**
117- * Returns an object regrouping metrics related to the errors encountered.
119+ * Returns an object grouping metrics related to the errors encountered.
118120 *
119- * @return an object regrouping metrics related to the errors encountered.
121+ * @return an object grouping metrics related to the errors encountered.
120122 */
121123 public Errors getErrorMetrics() {
122124 return errors;
123125 }
124126
125127 /**
126- * Returns the number of Cassandra hosts currently known by the driver (that is
128+ * Returns the number of Cassandra hosts currently known by the driver (that is
127129 * whether they are currently considered up or down).
128130 *
129131 * @return the number of Cassandra hosts currently known by the driver.
@@ -170,7 +172,13 @@
170172 private final Counter otherErrors = registry.counter("other-errors");
171173
172174 private final Counter retries = registry.counter("retries");
175+ private final Counter retriesOnWriteTimeout = registry.counter("retries-on-write-timeout");
176+ private final Counter retriesOnReadTimeout = registry.counter("retries-on-read-timeout");
177+ private final Counter retriesOnUnavailable = registry.counter("retries-on-unavailable");
173178 private final Counter ignores = registry.counter("ignores");
179+ private final Counter ignoresOnWriteTimeout = registry.counter("ignores-on-write-timeout");
180+ private final Counter ignoresOnReadTimeout = registry.counter("ignores-on-read-timeout");
181+ private final Counter ignoresOnUnavailable = registry.counter("ignores-on-unavailable");
174182
175183 /**
176184 * Returns the number of connection to Cassandra nodes errors.
@@ -211,7 +219,7 @@
211219
212220 /**
213221 * Returns the number of requests that returned an unavailable exception
214- * (independently of the final decision taken by the
222+ * (independently of the final decision taken by the
215223 * {@link com.datastax.driver.core.policies.RetryPolicy}).
216224 *
217225 * @return the number of unavailable exceptions.
@@ -235,13 +243,52 @@
235243 * Returns the number of times a request was retried due to the
236244 * {@link com.datastax.driver.core.policies.RetryPolicy}.
237245 *
238- * @return the number of times a requests was retried due to the
246+ * @return the number of times a requests was retried due to the
239247 * {@link com.datastax.driver.core.policies.RetryPolicy}.
240248 */
241249 public Counter getRetries() {
242250 return retries;
243251 }
244252
253+ /**
254+ * Returns the number of times a request was retried due to the
255+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
256+ * read timed out.
257+ *
258+ * @return the number of times a requests was retried due to the
259+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
260+ * read timed out.
261+ */
262+ public Counter getRetriesOnReadTimeout() {
263+ return retriesOnReadTimeout;
264+ }
265+
266+ /**
267+ * Returns the number of times a request was retried due to the
268+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
269+ * write timed out.
270+ *
271+ * @return the number of times a requests was retried due to the
272+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
273+ * write timed out.
274+ */
275+ public Counter getRetriesOnWriteTimeout() {
276+ return retriesOnWriteTimeout;
277+ }
278+
279+ /**
280+ * Returns the number of times a request was retried due to the
281+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after an
282+ * unavailable exception.
283+ *
284+ * @return the number of times a requests was retried due to the
285+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after an
286+ * unavailable exception.
287+ */
288+ public Counter getRetriesOnUnavailable() {
289+ return retriesOnUnavailable;
290+ }
291+
245292 /**
246293 * Returns the number of times a request was ignored
247294 * due to the {@link com.datastax.driver.core.policies.RetryPolicy}, for
@@ -253,5 +300,44 @@
253300 public Counter getIgnores() {
254301 return ignores;
255302 }
303+
304+ /**
305+ * Returns the number of times a request was ignored due to the
306+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
307+ * read timed out.
308+ *
309+ * @return the number of times a requests was ignored due to the
310+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
311+ * read timed out.
312+ */
313+ public Counter getIgnoresOnReadTimeout() {
314+ return ignoresOnReadTimeout;
315+ }
316+
317+ /**
318+ * Returns the number of times a request was ignored due to the
319+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
320+ * write timed out.
321+ *
322+ * @return the number of times a requests was ignored due to the
323+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after a
324+ * write timed out.
325+ */
326+ public Counter getIgnoresOnWriteTimeout() {
327+ return ignoresOnWriteTimeout;
328+ }
329+
330+ /**
331+ * Returns the number of times a request was ignored due to the
332+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after an
333+ * unavailable exception.
334+ *
335+ * @return the number of times a requests was ignored due to the
336+ * {@link com.datastax.driver.core.policies.RetryPolicy}, after an
337+ * unavailable exception.
338+ */
339+ public Counter getIgnoresOnUnavailable() {
340+ return ignoresOnUnavailable;
341+ }
256342 }
257343 }
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +24 −1
@@ -37,7 +37,9 @@
3737 import com.datastax.driver.core.exceptions.ReadTimeoutException;
3838 import com.datastax.driver.core.exceptions.UnavailableException;
3939 import com.datastax.driver.core.exceptions.WriteTimeoutException;
40-import com.datastax.driver.core.policies.RetryPolicy;
40+import com.datastax.driver.core.policies.*;
41+import com.datastax.driver.core.policies.RetryPolicy.RetryDecision;
42+import com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type;
4143
4244 /**
4345 * Handles a request to cassandra, dealing with host failover and retries on
@@ -277,6 +279,13 @@
277279 rte.getReceivedAcknowledgements(),
278280 rte.wasDataRetrieved(),
279281 queryRetries);
282+
283+ if (metricsEnabled()) {
284+ if (retry.getType() == Type.RETRY)
285+ metrics().getErrorMetrics().getRetriesOnReadTimeout().inc();
286+ if (retry.getType() == Type.IGNORE)
287+ metrics().getErrorMetrics().getIgnoresOnReadTimeout().inc();
288+ }
280289 break;
281290 case WRITE_TIMEOUT:
282291 assert err.infos instanceof WriteTimeoutException;
@@ -290,6 +299,13 @@
290299 wte.getRequiredAcknowledgements(),
291300 wte.getReceivedAcknowledgements(),
292301 queryRetries);
302+
303+ if (metricsEnabled()) {
304+ if (retry.getType() == Type.RETRY)
305+ metrics().getErrorMetrics().getRetriesOnWriteTimeout().inc();
306+ if (retry.getType() == Type.IGNORE)
307+ metrics().getErrorMetrics().getIgnoresOnWriteTimeout().inc();
308+ }
293309 break;
294310 case UNAVAILABLE:
295311 assert err.infos instanceof UnavailableException;
@@ -302,6 +318,13 @@
302318 ue.getRequiredReplicas(),
303319 ue.getAliveReplicas(),
304320 queryRetries);
321+
322+ if (metricsEnabled()) {
323+ if (retry.getType() == Type.RETRY)
324+ metrics().getErrorMetrics().getRetriesOnUnavailable().inc();
325+ if (retry.getType() == Type.IGNORE)
326+ metrics().getErrorMetrics().getIgnoresOnUnavailable().inc();
327+ }
305328 break;
306329 case OVERLOADED:
307330 // Try another node
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +11 −2
@@ -15,9 +15,11 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import com.datastax.driver.core.Cluster.Builder;
1819 import com.datastax.driver.core.exceptions.AlreadyExistsException;
1920 import com.datastax.driver.core.exceptions.DriverException;
2021 import com.datastax.driver.core.exceptions.NoHostAvailableException;
22+
2123 import com.google.common.io.Files;
2224 import org.slf4j.Logger;
2325 import org.slf4j.LoggerFactory;
@@ -227,16 +229,23 @@
227229
228230 protected abstract Collection<String> getTableDefinitions();
229231
232+ // Give individual tests a chance to customize the cluster configuration
233+ protected Cluster.Builder configure(Cluster.Builder builder) {
234+ return builder;
235+ }
236+
230237 public void errorOut() {
231238 erroredOut = true;
232239 }
233240
234- public static void createCluster() {
241+ public void createCluster() {
235242 erroredOut = false;
236243 schemaCreated = false;
237244 cassandraCluster = CCMBridge.create("test", 1);
238245 try {
239- cluster = Cluster.builder().addContactPoints(IP_PREFIX + '1').build();
246+ Builder builder = Cluster.builder();
247+ builder = configure(builder);
248+ cluster = builder.addContactPoints(IP_PREFIX + '1').build();
240249 session = cluster.connect();
241250 } catch (NoHostAvailableException e) {
242251 erroredOut = true;
added driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +63 −0
@@ -0,0 +1,63 @@
1+package com.datastax.driver.core;
2+
3+import java.util.Collection;
4+
5+import com.google.common.collect.Lists;
6+import org.testng.annotations.Test;
7+
8+import static org.testng.Assert.assertEquals;
9+
10+import com.datastax.driver.core.Metrics.Errors;
11+import com.datastax.driver.core.policies.RetryPolicy;
12+import com.datastax.driver.core.policies.RetryPolicy.RetryDecision;
13+
14+public class MetricsTest extends CCMBridge.PerClassSingleNodeCluster {
15+ private volatile RetryDecision retryDecision;
16+
17+ @Override
18+ protected Cluster.Builder configure(Cluster.Builder builder) {
19+ return builder.withRetryPolicy(new RetryPolicy() {
20+ @Override
21+ public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) {
22+ return retryDecision;
23+ }
24+
25+ @Override
26+ public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) {
27+ return retryDecision;
28+ }
29+
30+ @Override
31+ public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) {
32+ return retryDecision;
33+ }
34+ });
35+ }
36+
37+ @Override
38+ protected Collection<String> getTableDefinitions() {
39+ return Lists.newArrayList("CREATE TABLE test (k int primary key, v int)",
40+ "INSERT INTO test (k, v) VALUES (1, 1)");
41+ }
42+
43+ @Test(groups = "short")
44+ public void retriesTest() {
45+ retryDecision = RetryDecision.retry(ConsistencyLevel.ONE);
46+
47+ // We only have one node, this will throw an unavailable exception
48+ Statement statement = new SimpleStatement("SELECT v FROM test WHERE k = 1").setConsistencyLevel(ConsistencyLevel.TWO);
49+ session.execute(statement);
50+
51+ Errors errors = cluster.getMetrics().getErrorMetrics();
52+ assertEquals(errors.getUnavailables().getCount(), 1);
53+ assertEquals(errors.getRetries().getCount(), 1);
54+ assertEquals(errors.getRetriesOnUnavailable().getCount(), 1);
55+
56+ retryDecision = RetryDecision.ignore();
57+ session.execute(statement);
58+
59+ assertEquals(errors.getUnavailables().getCount(), 2);
60+ assertEquals(errors.getIgnores().getCount(), 1);
61+ assertEquals(errors.getIgnoresOnUnavailable().getCount(), 1);
62+ }
63+}