kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f1f9cbc93507cac73c15befc9ce53121b9df85ae687a69c2ef523e408927ecbd |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-08-26 10:29 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-08-26 11:01 +02:00 |
| Parents | 6d3e2a121092 |
4 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +91 −5
| @@ -18,6 +18,8 @@ | ||
| 18 | 18 | import java.util.HashSet; |
| 19 | 19 | import java.util.Set; |
| 20 | 20 | |
| 21 | +import com.codahale.metrics.Counter; | |
| 22 | + | |
| 21 | 23 | import com.codahale.metrics.*; |
| 22 | 24 | |
| 23 | 25 | /** |
| @@ -114,16 +116,16 @@ | ||
| 114 | 116 | } |
| 115 | 117 | |
| 116 | 118 | /** |
| 117 | - * Returns an object regrouping metrics related to the errors encountered. | |
| 119 | + * Returns an object grouping metrics related to the errors encountered. | |
| 118 | 120 | * |
| 119 | - * @return an object regrouping metrics related to the errors encountered. | |
| 121 | + * @return an object grouping metrics related to the errors encountered. | |
| 120 | 122 | */ |
| 121 | 123 | public Errors getErrorMetrics() { |
| 122 | 124 | return errors; |
| 123 | 125 | } |
| 124 | 126 | |
| 125 | 127 | /** |
| 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 | |
| 127 | 129 | * whether they are currently considered up or down). |
| 128 | 130 | * |
| 129 | 131 | * @return the number of Cassandra hosts currently known by the driver. |
| @@ -170,7 +172,13 @@ | ||
| 170 | 172 | private final Counter otherErrors = registry.counter("other-errors"); |
| 171 | 173 | |
| 172 | 174 | 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"); | |
| 173 | 178 | 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"); | |
| 174 | 182 | |
| 175 | 183 | /** |
| 176 | 184 | * Returns the number of connection to Cassandra nodes errors. |
| @@ -211,7 +219,7 @@ | ||
| 211 | 219 | |
| 212 | 220 | /** |
| 213 | 221 | * 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 | |
| 215 | 223 | * {@link com.datastax.driver.core.policies.RetryPolicy}). |
| 216 | 224 | * |
| 217 | 225 | * @return the number of unavailable exceptions. |
| @@ -235,13 +243,52 @@ | ||
| 235 | 243 | * Returns the number of times a request was retried due to the |
| 236 | 244 | * {@link com.datastax.driver.core.policies.RetryPolicy}. |
| 237 | 245 | * |
| 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 | |
| 239 | 247 | * {@link com.datastax.driver.core.policies.RetryPolicy}. |
| 240 | 248 | */ |
| 241 | 249 | public Counter getRetries() { |
| 242 | 250 | return retries; |
| 243 | 251 | } |
| 244 | 252 | |
| 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 | + | |
| 245 | 292 | /** |
| 246 | 293 | * Returns the number of times a request was ignored |
| 247 | 294 | * due to the {@link com.datastax.driver.core.policies.RetryPolicy}, for |
| @@ -253,5 +300,44 @@ | ||
| 253 | 300 | public Counter getIgnores() { |
| 254 | 301 | return ignores; |
| 255 | 302 | } |
| 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 | + } | |
| 256 | 342 | } |
| 257 | 343 | } |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +24 −1
| @@ -37,7 +37,9 @@ | ||
| 37 | 37 | import com.datastax.driver.core.exceptions.ReadTimeoutException; |
| 38 | 38 | import com.datastax.driver.core.exceptions.UnavailableException; |
| 39 | 39 | 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; | |
| 41 | 43 | |
| 42 | 44 | /** |
| 43 | 45 | * Handles a request to cassandra, dealing with host failover and retries on |
| @@ -277,6 +279,13 @@ | ||
| 277 | 279 | rte.getReceivedAcknowledgements(), |
| 278 | 280 | rte.wasDataRetrieved(), |
| 279 | 281 | 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 | + } | |
| 280 | 289 | break; |
| 281 | 290 | case WRITE_TIMEOUT: |
| 282 | 291 | assert err.infos instanceof WriteTimeoutException; |
| @@ -290,6 +299,13 @@ | ||
| 290 | 299 | wte.getRequiredAcknowledgements(), |
| 291 | 300 | wte.getReceivedAcknowledgements(), |
| 292 | 301 | 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 | + } | |
| 293 | 309 | break; |
| 294 | 310 | case UNAVAILABLE: |
| 295 | 311 | assert err.infos instanceof UnavailableException; |
| @@ -302,6 +318,13 @@ | ||
| 302 | 318 | ue.getRequiredReplicas(), |
| 303 | 319 | ue.getAliveReplicas(), |
| 304 | 320 | 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 | + } | |
| 305 | 328 | break; |
| 306 | 329 | case OVERLOADED: |
| 307 | 330 | // Try another node |
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +11 −2
| @@ -15,9 +15,11 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.Cluster.Builder; | |
| 18 | 19 | import com.datastax.driver.core.exceptions.AlreadyExistsException; |
| 19 | 20 | import com.datastax.driver.core.exceptions.DriverException; |
| 20 | 21 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 22 | + | |
| 21 | 23 | import com.google.common.io.Files; |
| 22 | 24 | import org.slf4j.Logger; |
| 23 | 25 | import org.slf4j.LoggerFactory; |
| @@ -227,16 +229,23 @@ | ||
| 227 | 229 | |
| 228 | 230 | protected abstract Collection<String> getTableDefinitions(); |
| 229 | 231 | |
| 232 | + // Give individual tests a chance to customize the cluster configuration | |
| 233 | + protected Cluster.Builder configure(Cluster.Builder builder) { | |
| 234 | + return builder; | |
| 235 | + } | |
| 236 | + | |
| 230 | 237 | public void errorOut() { |
| 231 | 238 | erroredOut = true; |
| 232 | 239 | } |
| 233 | 240 | |
| 234 | - public static void createCluster() { | |
| 241 | + public void createCluster() { | |
| 235 | 242 | erroredOut = false; |
| 236 | 243 | schemaCreated = false; |
| 237 | 244 | cassandraCluster = CCMBridge.create("test", 1); |
| 238 | 245 | 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(); | |
| 240 | 249 | session = cluster.connect(); |
| 241 | 250 | } catch (NoHostAvailableException e) { |
| 242 | 251 | 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 | +} |