dovgqc2648 2016-04-29 20:51
浏览 133

CouchBase异常。 该文件发生了变异

I have CouchBase server.

And have a question about concurrent document mutations: http://developer.couchbase.com/documentation/server/4.0/developer-guide/cas-concurrency.html


Code example with saving data in 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) {

}

But less than 0.002% from all messages I receive Exception:

CouchBase exception. The document was mutated.

Find this in documentation:

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


But still can't understand, what this mutation means?

  • Why if CAS values match, then the mutation operation succeeds, isn't it just rewrite message data?

  • Why if values differ, then the mutation operation fails?

  • Why I receive this Exception?

  • 写回答

1条回答 默认 最新

  • duanliusong6395 2016-11-21 11:55
    关注

    You can think about CAS as "revision number", which describes the document, but these "revision numbers" are not ordered, you allowed only to tell if two revisions are the same or not. For every change of the document the server will generate new CAS value (even if you rewrite the body with the same contents, set expiration time or lock the key).

    So you might seen the CAS mismatch errors when document change occurred, but the content still the same, I can guess it from how you calculate md5 from the body, without CAS.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)