Golang cassandra gocql transaction need a delay time to executed

258 Views Asked by At

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

There are 1 best solutions below

0
Erick Ramirez On BEST ANSWER

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!