doudejans / web-scale-data-management

Project work for IN4331 Web-scale Data Management
0 stars 2 forks source link

Change rollback routes #35

Closed leicmi closed 3 years ago

leicmi commented 4 years ago

This will make the service call Cassandra multiple times if someone else concurrently updated our row. Remember that rollbacks are important for consistency with SAGAs.

plammerts commented 4 years ago

Can you explain this a little more? I dont quite understand it. Suppose someone updated a row concurrently. In the code you only try the 'update' part multiple times. But then it will fail for all attempts because the "IF amount = %s;" will fail for the outdated value of 'amount', right?

leicmi commented 4 years ago

Can you explain this a little more? I dont quite understand it. Suppose someone updated a row concurrently. In the code you only try the 'update' part multiple times. But then it will fail for all attempts because the "IF amount = %s;" will fail for the outdated value of 'amount', right?

res = ... // get amount from database
for attempt in range(5):
    amount = res.amount
    new_amount = amount + number
    res = ... // UPDATE stock SET amount = {new_amount} IF amount = {amount}
    if res.applied:
        return True
return False

The interesting thing here is that the UPDATE query with IF returns the value of the failing column "amount" (accessed at the beginning of the next loop iteration) if the condition fails (res.appliedd==False).

https://docs.datastax.com/en/cql-oss/3.3/cql/cql_reference/cqlUpdate.html#Conditionallyupdatingcolumns

leicmi commented 4 years ago

hmm, looking again at the link, seems I was wrong? The documentation on the behavior for IF is really lacking...

leicmi commented 4 years ago

Found some documentation that explains this behavior with an example: https://docs.datastax.com/en/developer/python-driver/3.23/lwt/#named-tuple-factory-default

>>> session.execute("UPDATE t SET v = 1, x = 2 WHERE k = 0 IF v =0")
Row(applied=True)

>>> session.execute("UPDATE t SET v = 1, x = 2 WHERE k = 0 IF v =0 AND x = 1")
Row(applied=False, v=1, x=2)
plammerts commented 4 years ago

I think this will work then, but can you first test it?

leicmi commented 3 years ago

closed due to the project having ended