|
| 1 | +/* |
| 2 | +package com.aws.ssa.keyspaces.retry; |
| 3 | +
|
| 4 | +import com.datastax.driver.core.*; |
| 5 | +import com.datastax.driver.core.exceptions.DriverException; |
| 6 | +import com.datastax.driver.core.policies.RetryPolicy; |
| 7 | +
|
| 8 | +// ** Commented out so that it will not break compile. For v3 you should use the appropriate maven dependency. |
| 9 | +// Taken from https://github.com/aws-samples/amazon-keyspaces-examples/ |
| 10 | +// The Amazon Keyspaces Retry Policy is an alternative to the DefaultRetryPolicy for the Cassandra 3.x Driver. |
| 11 | +// |
| 12 | +// The main difference between the DefaultRetryPolicy and the AmazonKeyspacesRetryPolicy is that |
| 13 | +// the AmazonKeyspacesRetryPolicy will retry request a configuable number of times. By default we take a conservative |
| 14 | +// approach of 3 retry attempts. Additionally, this policy will not retry on the nexthost which can result in |
| 15 | +// NoHostAvailableExceptions |
| 16 | +// |
| 17 | +
|
| 18 | +
|
| 19 | +
|
| 20 | +public class AmazonKeyspacesRetryPolicy implements RetryPolicy { |
| 21 | +
|
| 22 | + final int maxNumberOfRetries; |
| 23 | +
|
| 24 | + public AmazonKeyspacesRetryPolicy() { |
| 25 | + maxNumberOfRetries = 3; |
| 26 | + } |
| 27 | +
|
| 28 | + public AmazonKeyspacesRetryPolicy(int numberOfRetries) { |
| 29 | + this.maxNumberOfRetries = numberOfRetries; |
| 30 | + } |
| 31 | +
|
| 32 | + protected RetryDecision makeDecisionBasedOnNumberOfConfiguredRetries(int nbRetry, ConsistencyLevel cl){ |
| 33 | + if(nbRetry > maxNumberOfRetries){ |
| 34 | + return RetryDecision.rethrow(); |
| 35 | + } |
| 36 | +
|
| 37 | + return RetryDecision.retry(cl); |
| 38 | + } |
| 39 | +
|
| 40 | + public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) { |
| 41 | + return makeDecisionBasedOnNumberOfConfiguredRetries(nbRetry, cl); |
| 42 | + } |
| 43 | +
|
| 44 | + public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) { |
| 45 | + return makeDecisionBasedOnNumberOfConfiguredRetries(nbRetry, cl); |
| 46 | + } |
| 47 | +
|
| 48 | + public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) { |
| 49 | + return makeDecisionBasedOnNumberOfConfiguredRetries(nbRetry, (ConsistencyLevel)null); |
| 50 | + } |
| 51 | +
|
| 52 | + public RetryDecision onRequestError(Statement statement, ConsistencyLevel cl, DriverException e, int nbRetry) { |
| 53 | + return makeDecisionBasedOnNumberOfConfiguredRetries(nbRetry, cl); |
| 54 | + } |
| 55 | +
|
| 56 | + public void init(Cluster cluster) { |
| 57 | + } |
| 58 | +
|
| 59 | + public void close() { |
| 60 | + } |
| 61 | +
|
| 62 | +
|
| 63 | +}*/ |
0 commit comments