We can create a cluster instance like this.
cluster = Cluster
.builder()
.addContactPoint("192.168.0.30")
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.build();
Were we will give the information for number of time a request has to be retried while querying.
Any advice is appreciated.
Thanks
In order to specify these values you have to create your own implementation of the RetryPolicy
interface.
The following is just an example -- make your own implementation following your rules:
public class CustomRetryPolicy implements RetryPolicy {
private final int readAttempts;
private final int writeAttempts;
private final int unavailableAttempts;
public CustomRetryPolicy(int readAttempts, int writeAttempts, int unavailableAttempts) {
this.readAttempts = readAttempts;
this.writeAttempts = writeAttempts;
this.unavailableAttempts = unavailableAttempts;
}
@Override
public RetryDecision onReadTimeout(Statement stmnt, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataReceived, int rTime) {
if (dataReceived) {
return RetryDecision.ignore();
} else if (rTime < readAttempts) {
return RetryDecision.retry(cl);
} else {
return RetryDecision.rethrow();
}
}
@Override
public RetryDecision onWriteTimeout(Statement stmnt, ConsistencyLevel cl, WriteType wt, int requiredResponses, int receivedResponses, int wTime) {
if (wTime < writeAttempts) {
return RetryDecision.retry(cl);
}
return RetryDecision.rethrow();
}
@Override
public RetryDecision onUnavailable(Statement stmnt, ConsistencyLevel cl, int requiredResponses, int receivedResponses, int uTime) {
if (uTime < unavailableAttempts) {
return RetryDecision.retry(ConsistencyLevel.ONE);
}
return RetryDecision.rethrow();
}
}
It is really easy to do ... then you pass it in your Cluster
...
RetryPolicy rc = new CustomRetryPolicy(3, 3, 2);
Cluster cluster = Cluster.builder().addContactPoint("192.168.0.30").withRetryPolicy(rc).build();
HTH, Carlo
For any C# developers using the CassandraCSharpDriver NuGet Package for .NET, I shamelessly copied Carlo Bertuccini's excellent answer and converted to the C# syntax:
CustomRetryPolicy customRetryPolicy = new CustomRetryPolicy(10, 10, 10);
cluster = Cluster.Builder().WithRetryPolicy(customRetryPolicy).WithCredentials(USER, PASS).AddContactPoint(NODE).WithCompression(CompressionType.Snappy).Build();
Class:
public class CustomRetryPolicy : IRetryPolicy
{
private int readAttempts;
private int writeAttempts;
private int unavailableAttempts;
public CustomRetryPolicy(int readAttempts, int writeAttempts, int unavailableAttempts)
{
this.readAttempts = readAttempts;
this.writeAttempts = writeAttempts;
this.unavailableAttempts = unavailableAttempts;
}
public RetryDecision OnReadTimeout(IStatement query, ConsistencyLevel cl, int requiredResponses, int receivedResponses, bool dataRetrieved, int nbRetry)
{
if (dataRetrieved)
{
return RetryDecision.Ignore();
}
else if (nbRetry < readAttempts)
{
return RetryDecision.Retry(cl);
}
else
{
return RetryDecision.Rethrow();
}
}
public RetryDecision OnUnavailable(IStatement query, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry)
{
if (nbRetry < unavailableAttempts)
{
return RetryDecision.Retry(ConsistencyLevel.One);
}
return RetryDecision.Rethrow();
}
public RetryDecision OnWriteTimeout(IStatement query, ConsistencyLevel cl, string writeType, int requiredAcks, int receivedAcks, int nbRetry)
{
if (nbRetry < writeAttempts)
{
return RetryDecision.Retry(cl);
}
return RetryDecision.Rethrow();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With