gpt4 book ai didi

java - 使用 Java API 的 Couchbase 回退

转载 作者:行者123 更新时间:2023-11-29 08:50:26 25 4
gpt4 key购买 nike

我正在尝试了解增量退避在 Java Couchbase API 中的工作原理。以下代码片段来自 Couchbase Java Tutorial (我添加了一些评论)。

public OperationFuture<Boolean> contSet(String key,
int exp,
Object value,
int tries) {
OperationFuture<Boolean> result = null;
OperationStatus status;
int backoffexp = 0;

try {
do {
if (backoffexp > tries) {
throw new RuntimeException("Could not perform a set after "
+ tries + " tries.");
}
result = cbc.set(key, exp, value);
status = result.getStatus(); // Is this a blocking call?
if (status.isSuccess()) {
break;
}
if (backoffexp > 0) {
double backoffMillis = Math.pow(2, backoffexp);
backoffMillis = Math.min(1000, backoffMillis); // 1 sec max
Thread.sleep((int) backoffMillis);
System.err.println("Backing off, tries so far: " + backoffexp);
}
backoffexp++;

// Why are we checking again if the operation previously failed
if (!status.isSuccess()) {
System.err.println("Failed with status: " + status.getMessage());
}

// If we break on success, why not do while(true)?
} while (status.getMessage().equals("Temporary failure"));
} catch (InterruptedException ex) {
System.err.println("Interrupted while trying to set. Exception:"
+ ex.getMessage());
}

if (result == null) {
throw new RuntimeException("Could not carry out operation.");
}

return result;
}

getStatus() 调用是否仅在操作成功或失败时返回? (即同步)。 Java Tutorial似乎说它是阻塞的,但是 Java API说:

Get the current status of this operation. Note that the operation status may change as the operation is tried and potentially retried against the servers specified by the NodeLocator.

为什么我们需要多次检查status.isSuccess()?如果它成功了,我们就会跳出循环,我们可以假设它失败了吗?

如果有任何理由执行 while (status.getMessage().equals("Temporary failure")) 而不是 while(true),因为我们调用 break 状态成功时?

谢谢

最佳答案

getStatus()强制Future完成(即内部调用get(),所以可以知道操作的状态。换句话说,有APU 中没有pending 状态,因此您必须强制 Future 完成才能确定状态。

参见Operation_Future:getStatus的源代码了解详情。

关于java - 使用 Java API 的 Couchbase 回退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22990196/

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