gpt4 book ai didi

java - FoundationDB 如何处理冲突事务?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:24 24 4
gpt4 key购买 nike

我很好奇 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 的元组,例如 (0,1,0,0,0) ,然后递增与线程号对应的值然后将其写回并递增其中一个 volatile 计数器。

这些是我的观察:

  1. 当您运行这个配置了一个线程的示例时,您会发现它的写入速度非常快,
  2. 当您增加并发性时,您会注意到您的写入速度正在减慢(预期)...
  3. ...你会看到这段代码不时被执行: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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com