Golang cassandra gocql事务需要延迟时间来执行。

huangapple go评论95阅读模式
英文:

Golang cassandra gocql transaction need a delay time to executed

问题

这段代码没有删除数据,我不知道为什么。

s.Query(
    "INSERT INTO smth (id, name) VALUES (?,?)",
    data.ID,
    data.Name,
).ScanCAS(nil, nil, nil)

s.Query(
    "DELETE FROM smth WHERE id = ? AND name = ?",
    data.ID,
    data.Name,
).Exec()

但是如果我添加一些延迟,像这样:

s.Query(
    "INSERT INTO smth (id, name) VALUES (?,?)",
    data.ID,
    data.Name,
).ScanCAS(nil, nil, nil)

time.Sleep(time.Second)
s.Query(
    "DELETE FROM smth WHERE id = ? AND name = ?",
    data.ID,
    data.Name,
).Exec()

数据将被删除。

有人知道如何在不添加延迟的情况下修复吗?
我正在使用github.com/gocql/gocql

英文:

This block of code does not delete the data, I don't know why.

s.Query(
		"INSERT INTO smth (id, name) VALUES (?,?)",
		data.ID
		data.Name
	).ScanCAS(nil, nil, nil)

s.Query(
	"DELETE FROM smth WHERE id = ? AND name = ?",
	data.ID
	data.Name
).Exec()

but if I added some delay like so:

s.Query(
		"INSERT INTO smth (id, name) VALUES (?,?)",
		data.ID
		data.Name
	).ScanCAS(nil, nil, nil)

time.Sleep(time.Second)
s.Query(
	"DELETE FROM smth WHERE id = ? AND name = ?",
	data.ID
	data.Name
).Exec()

The data will be deleted.

Does anyone know how to fix it without adding the delay?
I'm using github.com/gocql/gocql

答案1

得分: 1

Gocql查询是异步执行的,因此不能保证您的DELETEINSERT之后执行。

无论如何,您的测试用例是无效的,因为在刚刚插入分区后立即删除它不是一个有效的用例。

另外,我觉得您的代码有问题。在插入数据时,您调用了Query.ScanCAS(),但查询不是轻量级事务--它缺少条件IF EXISTS/IF NOT EXISTS。谢谢!

英文:

Gocql queries are executed asynchronously so there are no guarantees that your DELETE is executed after the INSERT.

In any case, your test case is invalid because immediately deleting a partition after it has just been inserted isn't a valid use case.

As a side note, your code appears to be wrong to me. You are calling Query.ScanCAS() when you insert data but the query isn't a lightweight transaction -- it's missing the conditional IF EXISTS/IF NOT EXISTS. Cheers!

huangapple
  • 本文由 发表于 2022年7月17日 19:56:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/73011549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定