- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我很好奇 FoundationDB 如何处理多个事务试图更新同一个 key 的情况?
如果一个客户执行这个交易:
db.run((Transaction tr) -> {
tr.set(Tuple.from("key").pack(), Tuple.from("valueA").pack());
return null;
});
当另一个客户端执行冲突事务时:
db.run((Transaction tr) -> {
tr.set(Tuple.from("key").pack(), Tuple.from("valueB").pack());
return null;
});
FoundationDB 内部会发生什么来解决这个冲突?
最佳答案
最近我一直在探索和测试 FoundationDB(我想现在每个人都在玩它)并且作为我探索的一部分,我做了一些简单的测试。其中一个应该回答您的问题:
所以,下面是一个示例(希望您不会介意 Scala):
import com.apple.foundationdb._
import com.apple.foundationdb.tuple._
import resource.managed
import scala.collection.mutable
import scala.util.Random
object Example {
val THREAD_COUNT = 1
@volatile var v0: Long = 0
@volatile var v1: Long = 0
@volatile var v2: Long = 0
@volatile var v3: Long = 0
@volatile var v4: Long = 0
def doJob(db: Database, x: Int): Unit = {
db.run((tr) => {
val key = Tuple.from("OBJ", Long.box(100)).pack()
val current = Tuple.fromBytes(tr.get(key).join())
if (Random.nextInt(100) < 2) {
out(current)
}
val next = mutable.ArrayBuffer(current.getLong(0), current.getLong(1), current.getLong(2), current.getLong(3), current.getLong(4))
if (x == 1 && v1 == next(1)) { println(s"again: $v1, v0=$v0, 0=${next(0)}")}
if (x == 0 && v0 > next(0)) { out(current); ??? } else { v0 = next(0)}
if (x == 1 && v1 > next(1)) { out(current); ??? } else { v1 = next(1)}
if (x == 2 && v2 > next(2)) { out(current); ??? } else { v2 = next(2)}
if (x == 3 && v3 > next(3)) { out(current); ??? } else { v3 = next(3)}
if (x == 4 && v4 > next(4)) { out(current); ??? } else { v4 = next(4)}
next.update(x, next(x) + 1)
val nv = Tuple.from(next.map(v => Long.box(v)) :_*)
tr.set(key, nv.pack())
})
}
def main(args: Array[String]): Unit = {
if (THREAD_COUNT > 5) {
throw new IllegalArgumentException("")
}
val fdb: FDB = FDB.selectAPIVersion(510)
for (db <- managed(fdb.open())) {
// Run an operation on the database
db.run((tr) => {
for (x <- 0 to 10000) {
val k = Tuple.from(s"OBJ", x.toLong.underlying()).pack()
val v = Tuple.from(Long.box(0), Long.box(0), Long.box(0), Long.box(0), Long.box(0)).pack()
tr.set(k, v)
null
}
})
val threads = (0 to THREAD_COUNT).map { x =>
new Thread(new Runnable {
override def run(): Unit = {
while (true) {
try {
doJob(db, x)
} catch {
case t: Throwable =>
t.printStackTrace()
}
}
}
})
}
threads.foreach(_.start())
threads.foreach(_.join())
}
}
private def out(current: Tuple) = {
println("===")
println((v0, v1, v2, v3, v4))
println((Thread.currentThread().getId, current))
}
}
所以,这个东西允许你启动多个线程写入同一个对象。其他实验遗留了一些不需要的代码,忽略它(或用于您自己的实验)。
此代码生成您的线程,然后每个线程从键 ("OBJ", 100) 读取一个包含五个 long 的元组,例如
,然后递增与线程号对应的值然后将其写回并递增其中一个 volatile 计数器。(0,1,0,0,0)
这些是我的观察:
println(s"again: $v1, v0=$v0, 0=${next(0)}")
因此,从本质上讲,当发生冲突时,FoundationDB 客户端会尝试提交事务,直到成功为止。您可以在 this chapter 中找到更多详细信息的文档。然后看architecture overview diagram.
另请注意,您的交易只是功能。希望 - idempotent functions .
而且您应该知道,在许多情况下,您可以通过使用 atomic operations 来避免冲突。关于你的值(value)。
希望这能回答您的问题。
我建议您阅读所有 official docs通过这样你可能会发现很多有趣的事情包括数据库开发人员如何consider CAP theorem , 很好的例子 cool distributed data structures以及许多其他技术细节和有趣的事情。
关于java - FoundationDB 如何处理冲突事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50519331/
我刚开始查看基础数据库,但在理解各层的工作方式时遇到了一些困难。foundationdb 层是否可以互操作?如果我使用 sql 添加数据,我可以使用图形层查询该数据吗?该转换/映射如何工作?问候奥斯卡
有没有什么快速的方法可以从本地数据库中删除所有数据?像 SQL '删除数据库'? 我正在浏览文档,但还没有发现任何有趣的东西。 最佳答案 “CLI”方式 使用提供的 fdbcli界面,您可以使用单个
FoundationDB 有版本戳,十字节,唯一,单调(但不是顺序)增加每个提交事务的值。 我可以从中获取时间戳(事务提交的时间)吗? 最佳答案 在“正常”(非故障)条件下,FoundationDB
我很好奇 FoundationDB 如何处理多个事务试图更新同一个 key 的情况? 如果一个客户执行这个交易: db.run((Transaction tr) -> { tr.set(Tuple
我正在查看 FoundationDB 的网站.它说它是用新的编程语言开发的Flow .在网站上它说: Flow is a new programming language design -- in f
foundationdb 声明在 consistency 上吗?有效的? FoundationDB provides the strongest possible consistency model,
我试图使用 getKey 和 KeySelector 在 FoundationDB 的某个子空间中找到一个键。如果结果存在于子空间中,它会工作得很好。 val key = new Tuple
我正在使用 foundationDB SQL 解析器 ( https://github.com/FoundationDB/sql-parser ) 来解析 Java 中的查询,但我不太熟悉访问者设计模
交易的 mongoc 文档使用 session 。因此,我尝试在与 session 关联的事务中插入文档。但我收到了错误 [ 3666]: ERROR: mongoc: Failed
我是一名优秀的程序员,十分优秀!