php - CouchBase 异常。文档被篡改

标签 php couchbase

我有 CouchBase 服务器。

还有关于并发文档突变的问题:http://developer.couchbase.com/documentation/server/4.0/developer-guide/cas-concurrency.html


在 CouchBase 中保存数据的代码示例:

try {
  Yii::$app->Couch->set($key, $data, 0, '', 1);
    
} catch (\Exception $e) {

    $already_saved = Yii::$app->Couch->get($key);

    Yii::$app->Logger->alert(
        'CouchBase exception',
        [
            'exception' => $e->getMessage(),
            'key' => $key,
            'need_saved' => $data,
            'already_saved' => $already_saved,
            'equal' => md5($already_saved)==md5(json_encode($data))
        ]
    );
}

/**
 * Store a document in the cluster.
 *
 * The set operation stores a document in the cluster. It differs from
 * add and replace in that it does not care for the presence of
 * the identifier in the cluster.
 *
 * If the $cas field is specified, set will <b>only</b> succeed if the
 * identifier exists in the cluster with the <b>exact</b> same cas value
 * as the one specified in this request.
 *
 * @param string $id the identifier to store the document under
 * @param object|string $document the document to store
 * @param integer $expiry the lifetime of the document (0 == infinite)
 * @param string $cas a cas identifier to restrict the store operation
 * @param integer $persist_to wait until the document is persisted to (at least)
 *                            this many nodes
 * @param integer $replicate_to wait until the document is replicated to (at least)
 *                            this many nodes
 * @return string the cas value of the object if success
 * @throws CouchbaseException if an error occurs
 */
function set($id, $document, $expiry = 0, $cas = "", $persist_to = 0, $replicate_to = 0) {

}

但我收到的所有消息中不到 0.002% 异常:

CouchBase exception. The document was mutated.

在文档中找到这个:

CAS is an acronym for Compare And Swap, and is known as a form of optimistic locking. The CAS can be supplied by applications to mutation operations ( insert, upsert, replace). When applications provide the CAS, server will check the application-provided version of CAS against its own version of the CAS:

  • If the two CAS values match (they compare successfully), then the mutation operation succeeds.

  • If the two CAS values differ, then the mutation operation fails


但是还是不明白,这个突变是什么意思?

  • 为什么CAS值匹配,那么变异操作就成功了,不就是重写了消息数据吗?

  • 为什么如果值不同,变异操作就会失败?

  • 为什么我会收到此异常?

最佳答案

你可以把CAS看成是描述文档的“修订号”,但是这些“修订号”是没有顺序的,你只允许判断两个修订版是否相同。对于文档的每次更改,服务器都会生成新的 CAS 值(即使您用相同的内容重写正文,设置过期时间或锁定 key )。

所以当文档发生变化时你可能会看到 CAS 不匹配错误,但内容仍然是一样的,我可以从你如何从正文中计算 md5 来猜测,没有 CAS。

关于php - CouchBase 异常。文档被篡改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36947242/

相关文章:

android - 在 Android 应用程序和服务器 Couchbase Lite 之间同步数据

java - 在代码 : First query returns 0 rows, 索引中创建 Couchbase View 在后台构建

amazon-web-services - 无法到达 erlang 端口映射器 - Couchbase 错误

php - 命名约定 - 使用 PHP 和 MySQL

php - 将WordPress Post Meta保存到数据库不起作用

php在linux中执行 Asterisk 命令

macos - 在不知道的情况下恢复 Couchbase 密码

php - SQL : How to reduce multiple time querying the same table ..查询优化

javascript - 计算网站上多个文件的下载次数的最佳方法

php - `sudo pecl install couchbase` 给出错误 `ERROR: failed to mkdir/usr/local/Cellar/php/7.3.8/pecl/20180731`