Cassandra Datastax驱动程序重试策略

Job*_*obs 4 cassandra

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

 cluster = Cluster
.builder()
.addContactPoint("192.168.0.30")
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.build();
Run Code Online (Sandbox Code Playgroud)

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

任何建议表示赞赏.

谢谢

Car*_*ini 18

要指定这些值,您必须创建自己的RetryPolicy接口实现.

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

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

}
Run Code Online (Sandbox Code Playgroud)

这真的很容易......然后你把它传递给你Cluster......

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

HTH,卡罗

  • 只有一次...据我所知,所有默认实现只重试一次...有一个默认实现,重试具有较低的一致性级别. (2认同)
  • 感谢您的帮助. (2认同)
  • 请考虑删除`if(dataRecieved){return RetryDecision.ignore(); 来自onReadTimeout的`语句.如果用户请求了仲裁,这将导致数据安静地通过,并且用户认为他们确实没有达到法定人数. (2认同)