about write conflict in SI

49 views
Skip to first unread message

RafaelYim

unread,
Jan 8, 2016, 3:54:57 AM1/8/16
to Cockroach DB
Hi all,
    I run two test cases with the same SQL in SI, but get different results. Here is my test cases

table t1 has two records(1, 'A'), (2,'B')

Case 1Case 2




Why Transaction 1 commit failed in Case 1, success in Case 2?

Tobias Schottdorf

unread,
Jan 8, 2016, 7:35:49 AM1/8/16
to RafaelYim, Cockroach DB
In the first case, your first transaction writes an intent and the second transaction runs into it (and wins) before number one commits.
In the second case, number one commits before number two, so the second transaction can't win but needs to go on top of the first.

In case you were running this in a shell, you'll find that the second transaction always wins in case one. The reason is that when you leave the first txn open and run the second one's conflicting command, it will internally bump against the first one repeatedly until it wins (based on random priorities).

--
You received this message because you are subscribed to the Google Groups "Cockroach DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cockroach-db...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Schottdorf

unread,
Jan 8, 2016, 7:37:54 AM1/8/16
to RafaelYim, Cockroach DB
One detail I should've mentioned is that the transaction internally only begins with the first write. So in your schemata, you can put `begin transaction; select c1 from t1; update t1 set c1 = 0 where c1=<i>` in the same line for the purpose of this exposition. So really there's no overlap in case two.

RafaelYim

unread,
Jan 8, 2016, 7:59:17 PM1/8/16
to Cockroach DB, rafa...@gmail.com

thanks for the info.
But in case 1, T1 update the first record, T2 update the second record. T2 haven't touched the first record. I think there should be no conflict between T1 and T2 in SI.

RafaelYim

unread,
Jan 8, 2016, 8:24:53 PM1/8/16
to Cockroach DB, rafa...@gmail.com
this case would failed too in SI

STEP 1: T1 begin;set transaction isolation level snapshot;
STEP 2: T2 begin;set transaction isolation level snapshot;
STEP 3: T1 select * from t1;update t1 set c1=0 where c1=1;
STEP 4: T2 select * from t1;insert into t1 values(3,'C');

both of the transactions commit failed with the following error message:
query error: kv/txn_coord_sender.go:377: transaction is already committed or aborted

Tobias Schottdorf

unread,
Jan 9, 2016, 7:56:22 AM1/9/16
to RafaelYim, Cockroach DB
What's the first and second record? You're only updating `c1`.
I'm wondering that maybe you're just experiencing transaction timeouts. You need activitiy once every 10 seconds or your transactions will time out.

I've translated your shell session into a test. Please see
and feel free to comment if that's not the session you had in mind.

RafaelYim

unread,
Jan 9, 2016, 8:42:47 PM1/9/16
to Cockroach DB, rafa...@gmail.com
Thanks very much. you are right. the transaction is aborted because of timeout. I put the sql in my test code, it runs fine now.

Tobias Schottdorf

unread,
Jan 9, 2016, 9:06:05 PM1/9/16
to RafaelYim, Cockroach DB
Great, thanks for testing.
Reply all
Reply to author
Forward
0 new messages