gpt4 book ai didi

Cassandra Datastax 驱动程序重试策略

转载 作者:行者123 更新时间:2023-12-03 15:09:45 28 4
gpt4 key购买 nike

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

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

我们是否会提供有关在查询时必须重试请求的次数的信息。

任何建议表示赞赏。

谢谢

最佳答案

为了指定这些值,您必须创建自己的 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();

哈,
卡罗

关于Cassandra Datastax 驱动程序重试策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329956/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com