11package com .aws .ssa .keyspaces .retry ;
22
33import com .datastax .oss .driver .api .core .ConsistencyLevel ;
4+ import com .datastax .oss .driver .api .core .config .DefaultDriverOption ;
45import com .datastax .oss .driver .api .core .config .DriverExecutionProfile ;
6+ import com .datastax .oss .driver .api .core .config .DriverOption ;
57import com .datastax .oss .driver .api .core .context .DriverContext ;
68import com .datastax .oss .driver .api .core .retry .RetryDecision ;
79import com .datastax .oss .driver .api .core .retry .RetryPolicy ;
1012import com .datastax .oss .driver .api .core .servererrors .WriteTimeoutException ;
1113import com .datastax .oss .driver .api .core .servererrors .WriteType ;
1214import com .datastax .oss .driver .api .core .session .Request ;
15+ import com .datastax .oss .driver .internal .core .retry .DefaultRetryPolicy ;
1316import com .datastax .oss .driver .shaded .guava .common .annotations .VisibleForTesting ;
1417import com .datastax .oss .driver .shaded .guava .common .util .concurrent .Uninterruptibles ;
1518import edu .umd .cs .findbugs .annotations .NonNull ;
19+ import jnr .ffi .annotations .In ;
1620import net .jcip .annotations .ThreadSafe ;
1721import org .slf4j .Logger ;
1822import org .slf4j .LoggerFactory ;
1923
24+ import java .time .Duration ;
25+ import java .time .Period ;
26+ import java .time .temporal .ChronoUnit ;
27+ import java .time .temporal .TemporalUnit ;
2028import java .util .concurrent .ThreadLocalRandom ;
2129import java .util .concurrent .TimeUnit ;
2230
3745 *
3846 * <pre>
3947 * datastax-java-driver {
40- * advanced.retry-policy {
41- * class = com.aws.ssa.keyspaces.retry.AmazonKeyspacesRetryPolicy
42- * max-attempts = 2
43- * }
48+ * basic.request.default-idempotence = true
49+ * advanced.retry-policy{
50+ * class = com.aws.ssa.keyspaces.retry.AmazonKeyspacesExponentialRetryPolicy
51+ * max-attempts = 3
52+ * min-wait = 10 mills
53+ * max-wait = 100 mills
54+ * }
4455 * }
4556 * </pre>
4657 */
@@ -64,6 +75,8 @@ public class AmazonKeyspacesExponentialRetryPolicy implements RetryPolicy {
6475 private final String logPrefix ;
6576
6677 private final Integer maxRetryCount ;
78+ private final Long minWaitTime ;
79+ private final Long maxWaitTime ;
6780
6881 //private final Integer maxTimeToWait;
6982
@@ -75,16 +88,40 @@ public AmazonKeyspacesExponentialRetryPolicy(DriverContext context, Integer maxR
7588
7689 String profileName = context .getConfig ().getDefaultProfile ().getName ();
7790
91+ DriverExecutionProfile retryExecutionProfile = context .getConfig ().getProfile (profileName );
92+
7893 this .maxRetryCount = maxRetryCount ;
94+ Duration minWaitDuration = retryExecutionProfile .getDuration (KeyspacesRetryOption .KEYSPACES_RETRY_MIN_WAIT , KeyspacesRetryOption .DEFAULT_KEYSPACES_RETRY_MIN_WAIT );
95+ Duration maxWaitDuration = retryExecutionProfile .getDuration (KeyspacesRetryOption .KEYSPACES_RETRY_MAX_WAIT , KeyspacesRetryOption .DEFAULT_KEYSPACES_RETRY_MAX_WAIT );
96+
97+ this .minWaitTime = minWaitDuration .toMillis ();
98+ this .maxWaitTime = maxWaitDuration .toMillis ();
99+
100+ this .logPrefix = (context != null ? context .getSessionName () : null ) + "|" + profileName ;
101+ }
102+ public AmazonKeyspacesExponentialRetryPolicy (DriverContext context , Integer maxRetryCount , Duration minWaitTime , Duration maxWaitTime ) {
103+
104+ String profileName = context .getConfig ().getDefaultProfile ().getName ();
105+
106+ this .maxRetryCount = maxRetryCount ;
107+ this .minWaitTime = minWaitTime .toMillis ();
108+ this .maxWaitTime = maxWaitTime .toMillis ();
79109
80110 this .logPrefix = (context != null ? context .getSessionName () : null ) + "|" + profileName ;
81111 }
82112
83113 public AmazonKeyspacesExponentialRetryPolicy (DriverContext context , String profileName ) {
84114 DriverExecutionProfile retryExecutionProfile = context .getConfig ().getProfile (profileName );
85115
116+
86117 maxRetryCount = retryExecutionProfile .getInt (KeyspacesRetryOption .KEYSPACES_RETRY_MAX_ATTEMPTS , KeyspacesRetryOption .DEFAULT_KEYSPACES_RETRY_MAX_ATTEMPTS );
87118
119+ Duration minWaitDuration = retryExecutionProfile .getDuration (KeyspacesRetryOption .KEYSPACES_RETRY_MIN_WAIT , KeyspacesRetryOption .DEFAULT_KEYSPACES_RETRY_MIN_WAIT );
120+ Duration maxWaitDuration = retryExecutionProfile .getDuration (KeyspacesRetryOption .KEYSPACES_RETRY_MAX_WAIT , KeyspacesRetryOption .DEFAULT_KEYSPACES_RETRY_MAX_WAIT );
121+
122+ this .minWaitTime = minWaitDuration .toMillis ();
123+ this .maxWaitTime = maxWaitDuration .toMillis ();
124+
88125 this .logPrefix = (context != null ? context .getSessionName () : null ) + "|" + profileName ;
89126 }
90127
@@ -103,9 +140,9 @@ protected RetryDecision determineRetryDecision(int retryCount) {
103140 }
104141 protected void timeToWait (int retryCount ){
105142
106- int timeToWaitCalculation = ( retryCount + 1 ) * ThreadLocalRandom .current ().nextInt (1 , 20 );
143+ long timeToWaitCalculation = minWaitTime + ThreadLocalRandom .current ().nextInt (retryCount , Double . valueOf ( Math . pow ( 2d , Integer . valueOf ( retryCount ). doubleValue ())). intValue () );
107144
108- int timeToWaitFinal = Math .min (500 , timeToWaitCalculation );
145+ long timeToWaitFinal = Math .min (maxWaitTime , timeToWaitCalculation );
109146
110147 Uninterruptibles .sleepUninterruptibly (timeToWaitFinal , TimeUnit .MILLISECONDS );
111148 }
0 commit comments