c# - RethinkDB 中的原子条件更新

标签 c# concurrency rethinkdb upsert atomic

RethinkDB 为我们提供了一些 atomicity guarantees under certain conditions 。我试图利用它使用修订字段来实现乐观并发(c#)。这个想法是,如果在客户端编辑自己的修订版本时数据已被修改,则更新应该失败。应插入新条目。

var id = "some unique id";
var rev = "the old known revision";
var doc = new {id, Revision = Guid.NewGuid().ToString("N")};
var result = await R.Db("test").Table("table")
    .Get(id)
    .Replace(found => 
        R.Branch(
            found.Eq(null), doc,
            found["Revision"].Eq(rev), doc,
            found
        )
    ).RunResultAsync(connection);

上面的代码似乎可以用于此目的,但我想知道它是否确实会消除严格并发访问的竞争条件。换句话说,这个更新/插入或更新插入是原子的吗?如果可能的话,请指导我查看一些文档。

文档提到了一个检查和设置寄存器(也可能是 CAS,比较和交换),我认为它对于解释哪些操作实际上是原子的和哪些操作没有帮助我们如何保证它(例如使用集成测试)。

我的印象是,如果满足以下条件:

  • write_acks:多数
  • 耐用性:硬
  • 读取模式:多数

并且如果写入不涉及二次查询或其他文档,那么写入是原子的。我喜欢那里有太多的信念,那些如果。我希望有一个标志或其他操作必须是原子操作的东西,否则会破坏它。好吧,在那之前,我可以相信我的印象吗? 有confusion on SO也是如此。

最佳答案

您的示例查询是原子的。它是原子的,因为它使用 Replace 修改单个文档。如果它修改了多个文档,则整个查询将不是原子的,但对每个文档的修改将是单独的原子操作。

有一个Replace标志,nonAtomic,默认为true。除非您将 nonAtomic 参数传递给 Replace,否则传递给 Replace 的函数将以原子方式执行。

没有整个查询atomicnonAtomic标志。

当您尝试以原子方式执行非确定性操作时,RethinkDB 将抛出错误:

Could not prove argument deterministic. Maybe you want to use the non_atomic flag?

documentation you linked to提到:

To read and modify a document in a single atomic operation, use the update or replace commands.

检查和设置寄存器只是可以原子执行的操作的一个示例。它还说明了原子操作的注意事项:只有传递给 ReplaceUpdate 的函数才是原子的。周围的查询不是,例如 FilterGetAll

RethinkDB operations are never atomic across multiple keys

阅读discussion you linked to ,困惑似乎源于文档中的错误。以前常说

If the user runs a query that cannot be executed atomically, by default RethinkDB will throw an error.

但被更正说

If an update or replace query cannot be executed atomically, by default RethinkDB will throw an error

另请参阅How does the atomicity model work?

Write atomicity is supported on a per-document basis – updates to a single JSON document are guaranteed to be atomic. RethinkDB is different from other NoSQL systems in that atomic document updates aren’t limited to a small subset of possible operations – any combination of operations that can be performed on a single document is guaranteed to update the document atomically.

关于c# - RethinkDB 中的原子条件更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44440401/

相关文章:

c# - 如何标记一个类是线程安全的(或不是)?

c# - 如何结合 Service Fabric Remoting 进行分区

python - 如何将任务分配给特定的 CPU 内核?

MySQL 多个事务递增同一行

Angular 2 : removeAll() in RethinkDB with Horizon API

rethinkdb - 执行嵌套过滤/连接

c# - 如何在 Visual Studio 中确定当前插入符号位置位于注释 block 中?

c# - Gridview 应用程序中的错误

java - java 中打印中间步骤卡住

java - 从 RethinkDB Count 对象中提取数据