cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

A write followed by another write can very easily get aborted.

Open vivekmenezes opened this issue 11 years ago • 13 comments

Take a look at this test

https://github.com/cockroachdb/cockroach/blob/master/sql/driver/driver_test.go#L236

The second transaction aborts the first one. Is this because it came with a higher priority? or did some retry logic get triggered that made it retry with a higher priority? If it did retry then the behavior is probably okay.

vivekmenezes avatar Aug 24 '15 18:08 vivekmenezes

Here's what postgres does: https://gist.github.com/tamird/0662f3b0997a89b37a12

Note that in Postgres the second write blocks until the first transaction commits; I think the only option for us is to abort immediately rather than block.

tamird avatar Aug 24 '15 18:08 tamird

The second transaction aborting the first one is part of our lock-free transaction model. We shouldn't attempt to fix this at the SQL layer. I don't know if it needs fixing at all.

petermattis avatar Aug 24 '15 20:08 petermattis

We have to fix it somewhere, or else ship with big flashing warnings that say "never use values obtained from a transaction that failed to commit".

The implementation is not incorrect, but it will violate the principle of least surprise for many.

tamird avatar Aug 24 '15 20:08 tamird

"Fixing" this part of our system is a non goal at the moment. Let's not confuse things. The choice of txn models was made for reasons of efficiency with distributed txn's 2nd phase in the common case of low contention. Introducing a pessimistic txn model with locks so that our system behaves more like existing RDBMS systems would substitute the goal of efficient two phase commits for least surprise.

I do agree that people who are used to txns not generally retrying will be surprised by the need to put txn processing in a loop. As Pete formerly suggested, we might provide some helpful shorthand for this which hides the need to manually implement the retries. The principle of least surprise is a guideline, not an absolute requirement afterall.

On Mon, Aug 24, 2015 at 4:10 PM, Tamir Duberstein [email protected] wrote:

We have to fix it somewhere, or else ship with big flashing warnings that say "never use values obtained from a transaction that failed to commit".

The implementation is not incorrect, but it will violate the principle of least surprise for many.

— Reply to this email directly or view it on GitHub https://github.com/cockroachdb/cockroach/issues/2231#issuecomment-134362554 .

spencerkimball avatar Aug 24 '15 21:08 spencerkimball

I think you misunderstood. The observation here is that the aborted transaction is able to continue reading, which we should do better to disallow. This violates the "I" in "ACID" when the user neglects to commit her write-then-read transaction before acting on the read value.

tamird avatar Aug 24 '15 22:08 tamird

While I agree that we don't necessarily have to fix this, if we do, it will involve every read write checking the transaction table to see if the transaction has aborted and report it to the user. That will slow down things. So I suppose this is infeasible given our architecture.

On Mon, Aug 24, 2015, 6:33 PM Tamir Duberstein [email protected] wrote:

I think you misunderstood. The observation here is that the aborted transaction is able to continue reading, which we should do better to disallow. This violates the "I" in "ACID" when the user neglects to commit her write-then-read transaction before acting on the read value.

— Reply to this email directly or view it on GitHub https://github.com/cockroachdb/cockroach/issues/2231#issuecomment-134400541 .

vivekmenezes avatar Aug 25 '15 00:08 vivekmenezes

The line of code in the original comment is no longer correct, here is a permanent link:

https://github.com/cockroachdb/cockroach/blob/00bfb87fdb690ecd24dfc03339bdf8850a88d7d5/sql/driver/driver_test.go#L285

mrtracy avatar Aug 31 '15 18:08 mrtracy

My first question would be, do we have examples of this pattern in wide usage:

the user neglects to commit her write-then-read transaction before acting on the read value.

That pattern itself violates the "A" in ACID. Even in Postgres, it seems that a transaction might be eventually aborted due to a future action; it would always be unsafe to use read values before committing.

But more than that, I don't think that this pattern does violate the "I" in ACID unless it also somehow violates the sanctity of the snapshot, and I don't think it does.

Lets assume the first transaction (A) gets aborted by the second transaction (B) via a write/write conflict, but is still reading values until it attempts to commit (at which time it discovers it has been pushed). To preserve the snapshot, A cannot read anything written by B if it would violate the integrity of A's snapshot.

  1. If B has a later timestamp than A:
    1. If B's timestamp is beyond the uncertainty threshold of A, then A will never read anything written by B.
    2. If B's timestamp is within A's uncertainty threshold, then if A attempts to read something written by B if will abort itself at that time.
  2. If B has a lower timestamp:
    1. If B is still running, it will abort itself if it tries to read any value that was previously read by A (due to the read timestamp cache). If A reads a value written by B, it will see B's intent and attempt to push it (and fail).
    2. If B has committed, then we know if never wrote to a value after A read from it. The transactions are still isolated; if A reads a value written by the committed B, that's completely valid.

Therefore, I suggest that Isolation is maintained because the snapshot is still correct; any time that A might return data that violates the snapshot, it does detect that it has been aborted and returns an error immediately.

mrtracy avatar Aug 31 '15 19:08 mrtracy

I'm also not quite sure what the issue here is. You're running a transaction, and until you commit it, why would you assume that your data has been written and that you can use any of it outside your Txn? Sure, Postgres tells you as early as the conflict happens (which is nice for the user), but we obviously can't do that and I don't see why we must. In Postgres, I can also end up writing a value successfully in my Txn only to get aborted on the next, so the situation looks the same to me. Am I missing something?

tbg avatar Sep 01 '15 07:09 tbg

You're running a transaction, and until you commit it, why would you assume that your data has been written and that you can use any of it outside your Txn?

That's not the issue here. The issue here is that you can write a value, then proceed to read it in the same transaction: if you got pushed, you will read the original value rather than whatever you just wrote.

This transaction is doomed and won't commit, but the behaviour is still surprising.

tamird avatar Sep 01 '15 11:09 tamird

Oh, I see. Thanks for the explanation. Doesn't seem like something we need to fix other than for performance (but the performance gain is worth thinking about because it'll hit the cluster pretty bad especially when there's contention). The client gateway (which tracks the intents) could short-circuit the txn in such a case, at least for point requests, but I doubt the overhead will be worth it. For long-running transactions, the heartbeat goroutine (which will always read the true txn entry) can inject an error to the client basically for free, though.

tbg avatar Sep 01 '15 11:09 tbg

just a small note that this phenomenon can have unintended consequences because it can mean that instead of a transaction error (which the client retries) you get a ConditionFailedError which the client doesn't handle. Not an issue for sql usage I think (because the client doesn't know the difference between being aborted and having to restart), but for any kv-style transaction that doesn't expect a real "error" this comes unexpectedly. Consider code like this:

func retryable(txn *Txn) error {
  txn.Put("a", "b")
  return txn.CPut("a", "b", "c")
}

where of course the use of CPut is redundant - still you would expect this code to go through or run forever, not error out with a ConditionFailedError. If you're using CPut to compare against data you didn't write, of course you should be prepared for the thing to go bust.

tbg avatar Oct 06 '15 17:10 tbg

Clients can't currently tell the difference between different error modes, but I think we'll need to expose some sort of error code so they can distinguish retryable and non-retryable cases. We use CPut to enforce unique indices (so a "redundant" CPut is perhaps more likely than it would be in native KV code), and it seems bad if we report a non-retryable unique constraint violation instead of a retryable transaction push.

bdarnell avatar Oct 06 '15 18:10 bdarnell