Cassandra Datastax驱动程序重试策略

时间:2015-05-19 15:30:34

标签: cassandra

我们可以像这样创建一个集群实例。

 cluster = Cluster
.builder()
.addContactPoint("192.168.0.30")
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.build();

我们是否会在查询时提供重试请求的时间信息。

感谢任何建议。

由于

2 个答案:

答案 0 :(得分:18)

为了指定这些值,您必须创建自己的RetryPolicy interface实现。

以下只是一个示例 - 根据您的规则制作您自己的实现:

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();
    }

}

这很容易......然后你将它传递到Cluster ...

RetryPolicy rc = new CustomRetryPolicy(3, 3, 2);
Cluster cluster = Cluster.builder().addContactPoint("192.168.0.30").withRetryPolicy(rc).build();

HTH, 卡罗

答案 1 :(得分:0)

对于使用.NET的CassandraCSharpDriver NuGet软件包的C#开发人员,我无耻地复制了Carlo Bertuccini的出色答案,并将其转换为C#语法:

CustomRetryPolicy customRetryPolicy = new CustomRetryPolicy(10, 10, 10);
cluster = Cluster.Builder().WithRetryPolicy(customRetryPolicy).WithCredentials(USER, PASS).AddContactPoint(NODE).WithCompression(CompressionType.Snappy).Build();

班级:

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();
    }
}