5

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

Jobs
  • 1,257
  • 2
  • 14
  • 27

2 Answers2

21

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

Carlo Bertuccini
  • 19,615
  • 3
  • 28
  • 39
  • 1
    What is the default retry count for the retry policy that comes along with Cassandra.?? – Jobs May 20 '15 at 05:06
  • 2
    Only once ... As far as I know all default implementations retry only one time ... There is a default implementation that retry with a lower consistency level. – Carlo Bertuccini May 20 '15 at 05:11
  • 2
    Please consider removing the `if(dataRecieved) { return RetryDecision.ignore(); }` statement from your onReadTimeout. If the user requested quorum, this will cause the data to go through quietly, and the user think they had quorum when they really didn't. – riwalk Feb 17 '16 at 21:02
1

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();
    }
}
adinas
  • 4,150
  • 3
  • 37
  • 47