storage: do not restart transaction if a near-future write *intent* is encountered
Transactions are currently restarted if a reader encounters a write or write intent in the near future. From the design docs:
Reader encounters write intent or value with newer timestamp in the near future: In this case, we have to be careful. The newer intent may, in absolute terms, have happened in our read's past if the clock of the writer is ahead of the node serving the values. In that case, we would need to take this value into account, but we just don't know. Hence the transaction restarts, using instead a future timestamp (but remembering a maximum timestamp used to limit the uncertainty window to the maximum clock skew).
This condition ensures that the active transaction (A) cannot be written with an earlier timestamp than another transaction (B) which was written with a later timestamp, but committed before A started in absolute time.
However, if a write intent is encountered then B may not yet have committed. This can be verified by checking the transaction table; if B has not yet committed, and it's perfectly valid for A to have the lower timestamp and proceed without pushing itself.
To summarize: Let A and B be transactions where 'A' has selected a lower timestamp than 'B'.
- If A encounters a committed write from B in the 'near future' uncertainty window, then B may have committed before A started. Transaction A must thus increase it's timestamp and retry.
- If A encounters a write intent from B, then the transaction table must be checked. If B has committed, then the same logic as case 1 applies and Transaction A restarts.
- If the write intent from B has NOT committed, then transaction B definitely did not commit before A started. In this case, it is perfectly valid for A to have the lower timestamp and ignore B's write; A can continue without restarting, and can ignore any future read/write conflicts with B.
I believe the reason why we've implemented it the way we do today is because of linearizability on a single key:value
See http://www.bailis.org/blog/linearizability-versus-serializability/
I was not aware that serializability applied to transactions, while linearizability applied to an object (key:value).
A thing to keep in mind: Writes are assumed to be instantaneous. Meaning a write intent with a timestamp is the same as a committed write as far as linearizability is concerned, but not as far as the A and the I in ACID transactions are concerned (the write might be retried to enforce A and I).
If we only cared about serializability we could take your approach with a change, that we would treat the write intent the same way we treat a committed value. I believe there is no reason to differentiate between the two.
On Wed, Jun 10, 2015 at 11:09 AM mrtracy [email protected] wrote:
Transactions are currently restarted if a reader encounters a write or write intent in the near future. From the design docs:
Reader encounters write intent or value with newer timestamp in the near future: In this case, we have to be careful. The newer intent may, in absolute terms, have happened in our read's past if the clock of the writer is ahead of the node serving the values. In that case, we would need to take this value into account, but we just don't know. Hence the transaction restarts, using instead a future timestamp (but remembering a maximum timestamp used to limit the uncertainty window to the maximum clock skew).
This condition ensures that the active transaction (A) cannot be written with an earlier timestamp than another transaction (B) which was written with a later timestamp, but committed before A started in absolute time.
However, if a write intent is encountered then B may not yet have committed. This can be verified by checking the transaction table; if B has not yet committed, and it's perfectly valid for A to have the lower timestamp and proceed without pushing itself.
To summarize: Let A and B be transactions where 'A' has selected a lower timestamp than 'B'.
- If A encounters a committed write from B in the 'near future' uncertainty window, then B may have committed before A started. Transaction A must thus increase it's timestamp and retry.
- If A encounters a write intent from B, then the transaction table must be checked. If B has committed, then the same logic as case 1 applies and Transaction A restarts.
- If the write intent from B has NOT committed, then transaction B definitely did not commit before A started. In this case, it is perfectly valid for A to have the lower timestamp and ignore B's write; A can continue without restarting, and can ignore any future read/write conflicts with B.
— Reply to this email directly or view it on GitHub https://github.com/cockroachdb/cockroach/issues/1329.
Another way to think about this:
- The transaction wants to see all values written in the past (as far as the real time clock is concerned, or the time recorded when the transaction started). This is a consistency (C) constraint on the transaction.
- The transaction wants to be A (atomic) and I (isolated, serializable)
So would skipping values that are written but not yet committed in the database constitute a violation of consistency. We'd decide in the affirmative since we think of writes at a key:value happening when a write intent is written and not when the write intent has been upgraded to committed.
On Wed, Jun 10, 2015 at 11:40 AM Vivek Menezes [email protected] wrote:
I believe the reason why we've implemented it the way we do today is because of linearizability on a single key:value
See http://www.bailis.org/blog/linearizability-versus-serializability/
I was not aware that serializability applied to transactions, while linearizability applied to an object (key:value).
A thing to keep in mind: Writes are assumed to be instantaneous. Meaning a write intent with a timestamp is the same as a committed write as far as linearizability is concerned, but not as far as the A and the I in ACID transactions are concerned (the write might be retried to enforce A and I).
If we only cared about serializability we could take your approach with a change, that we would treat the write intent the same way we treat a committed value. I believe there is no reason to differentiate between the two.
On Wed, Jun 10, 2015 at 11:09 AM mrtracy [email protected] wrote:
Transactions are currently restarted if a reader encounters a write or write intent in the near future. From the design docs:
Reader encounters write intent or value with newer timestamp in the near future: In this case, we have to be careful. The newer intent may, in absolute terms, have happened in our read's past if the clock of the writer is ahead of the node serving the values. In that case, we would need to take this value into account, but we just don't know. Hence the transaction restarts, using instead a future timestamp (but remembering a maximum timestamp used to limit the uncertainty window to the maximum clock skew).
This condition ensures that the active transaction (A) cannot be written with an earlier timestamp than another transaction (B) which was written with a later timestamp, but committed before A started in absolute time.
However, if a write intent is encountered then B may not yet have committed. This can be verified by checking the transaction table; if B has not yet committed, and it's perfectly valid for A to have the lower timestamp and proceed without pushing itself.
To summarize: Let A and B be transactions where 'A' has selected a lower timestamp than 'B'.
- If A encounters a committed write from B in the 'near future' uncertainty window, then B may have committed before A started. Transaction A must thus increase it's timestamp and retry.
- If A encounters a write intent from B, then the transaction table must be checked. If B has committed, then the same logic as case 1 applies and Transaction A restarts.
- If the write intent from B has NOT committed, then transaction B definitely did not commit before A started. In this case, it is perfectly valid for A to have the lower timestamp and ignore B's write; A can continue without restarting, and can ignore any future read/write conflicts with B.
— Reply to this email directly or view it on GitHub https://github.com/cockroachdb/cockroach/issues/1329.
Unfortunately we can't check the transaction table when we encounter a write intent. This happens post-raft in the range_command and mvcc layers, which means the behavior must be a deterministic function of the range's local state. If we did a transaction lookup here then the replicas of a range could come to different conclusions about whether the transaction had committed based on the time at which they applied the command.
Writes are only assumed to be instantaneous after they are committed. An SSI transaction selects a timestamp when it starts, and begins reading and writing (later than the chosen timestamp in absolute time); when the transaction commits, it is only allowed to commit if the resulting database state is equivalent to the result of an instantaneous write at the originally chosen timestamp (assuming that all other previously committed transactions meet the same requirement). Because it is equivalent, it is recorded as if it were an instantaneous transaction.
Cockroach detects when a transaction does not meet that requirements - for instance, if the transaction tries to write to a key but another transaction has read that key later than our chosen timestamp, then the transaction may no longer be equivalent to an instantaneous write and one of the two transactions must be restarted.
The "Near future" conflict detection is actually making a guarantee greater than serializability - it is actually guaranteeing that the transaction history is linearizable for transactions which may be causally related (two transactions are possibly causally related if they both involve a common key K1, and at least one of the transactions writes K1).
The requirement of a linearizable history is thus:
- The history is serializable.
- If transaction T1 commits before T2 starts in absolute time, then T2 has a later timestamp than T1.
Note that if T2 starts after T1 but before T1 commits, that the two operations are considered "concurrent" and the linearizable property provides no requirement about their timestamp relationship. As long as the result of those two transactions is serializable, it is also linearizable.
Note that we are not globally linearizable; if two transactions do not interact causally from cockroach's perspective, linearizability is not enforced.
Ben, I suppose this comment in the design doc:
"In the event the proxy restarts after a transaction commits but before the asynchronous resolution is complete, the dangling intents are upgraded when encountered by future readers and writers and the system does not depend on their timely resolution for correctness."
only applies to write intents in the far past (> 5 seconds or whatever), and not for write intents in the near future.
On Wed, Jun 10, 2015 at 12:48 PM Ben Darnell [email protected] wrote:
Unfortunately we can't check the transaction table when we encounter a write intent. This happens post-raft in the range_command and mvcc layers, which means the behavior must be a deterministic function of the range's local state. If we did a transaction lookup here then the replicas of a range could come to different conclusions about whether the transaction had committed based on the time at which they applied the command.
— Reply to this email directly or view it on GitHub https://github.com/cockroachdb/cockroach/issues/1329#issuecomment-110829641 .
Write intents are not resolved at the MVCC layer. Instead, a write intent error is returned, the intent is resolved outside of raft, and then the command is retried. It might be possible to do the same thing with a ReadWithinUncertaintyInterval error, but I think there would still be races (what if the other transaction commits in between our check of the transaction table and the re-submitted raft command?).
@bdarnell is right about the need to be careful, but on first sight it looks like races aren't going to be an issue with what @mrtracy suggests. Fundamentally, once at some point during your transaction you've seen that a certain other transaction hasn't committed yet, it's safe to ignore its writes for uncertainty purposes for the duration of the active transaction, even if the other transaction commits immediately after you check - all that counts is that it hasn't committed before your transaction got started.
So after catching a ReadWithinUncertaintyIntervalError (which would be updated to contain the conflicting writer's txn, if any, and the value which would have been read if not for uncertainty) the store could lookup that transaction and, if uncommitted, turn the error into an honest value to be passed to the reader.
Whether this optimization is useful depends on how often that additional check actually finds a running transaction. That in turn depends on the average lifespan of a transaction compared to the uncertainty interval - if both are at about the same magnitude, you'd expect a transaction which writes into your uncertainty interval to still be active while you check its status, which means you would reliably save yourself transaction restarts for just a single extra round-trip.
Seems to me that we should keep this one on the radar for when we've got a fairly optimized system and are ready to incur additional complexity to optimize for this scenario.
I agree with Tobias' analysis. And I also agree that this kind of work will be best left until we have some transactional use cases which are running against a real cluster so we can benchmark the potential performance improvements. While these restarts seem just terrible when you think about them happening in theory, I think you may in practice find that it's actually difficult to construct a use case which trips them enough that this kind of optimization (and concomitant complexity) would make sense.
Moved to 1.0, but I hope that after some optimizations I'm doing in #2861 we can actually close this early.
This is no longer an issue with #13501