java - 使用 Java API 的 Couchbase 回退

标签 java couchbase nosql

我正在尝试了解增量退避在 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/

相关文章:

java - apache ignite 是否在启动时创建 cassandra 表还是我们需要运行任何命令?

memcached - 是否有带有 Couchbase memcached 存储桶的代理?

c - 分配从指针目标类型中丢弃 ‘const’ 限定符 - 尝试检索值时

mongodb - NoSQL数据库引擎如何选择?

ruby - 我可以继续使用 MongoDB v2.2.0rc 使用 1.6.4 Ruby 驱动程序吗?

mysql - 如何存储前30天和后30天的数据?

java - Suppressionfilter 在 checkstyle 配置文件中不起作用

java.net.ProtocolException : Server redirected too many times (20)

java - Android 服务变红

java - Spring JPA 和 Couchbase - com.couchbase.client.java.error.ViewDoesNotExistException : View cat/all does not exist