foundationdb icon indicating copy to clipboard operation
foundationdb copied to clipboard

Adding an explicit read conflict range skips the keys in write cache

Open alecgrieser opened this issue 8 years ago • 6 comments

If the user adds an explicit read conflict range over one or more keys, then any keys in the write cache are skipped from the explicit conflict range insert. For example, suppose we have an increment counter method that looks like this:

private void incrementCounter(Transaction tr) {
    long counterValue = Tuple.fromBytes(tr.snapshot().get(COUNTER_KEY).join()).getLong(0);
    tr.set(COUNTER_KEY, Tuple.from(counterValue + 1).pack());
    tr.addReadConflictKey(COUNTER_KEY);
}

(Presumably, we are reading at snapshot and then writing and adding a conflict range because we only want to conditionally update the counter, but we always want to know the counter value. This is a little contrived, but there are less contrived examples out there as well.) Then suppose we start two transactions and commit them concurrently:

try (Transaction tr1 = db.createTransaction(); Transaction tr2 = db.createTransaction()) {
    CompletableFuture.allOf(tr1.getReadVersion(), tr2.getReadVersion()).join();
    incrementCounter(tr1);
    incrementCounter(tr2);
    tr1.commit().join();
    tr2.commit().join();
}

Then both commits succeed, but the counter still says "1" as its final value, despite two transactions concurrently incrementing it.

The user can fix this by correctly ordering their writes and read conflict range operations, but it probably isn't the expected semantics for such a call.

alecgrieser avatar Apr 03 '18 01:04 alecgrieser

How does that pass tests? If we don't have a test that checks isolation properties and generates random weird conflict ranges, that's a second bug!

davidscherer avatar Apr 19 '18 23:04 davidscherer

I am trying to correctly understand the implications of this issue - the description does not mention it explicitly, but is there any significance of the read operation being performed on a snapshot()?

I copied the code as is and was able to reproduce the error, but on removing the snapshot() part from the code, the transactions resulted in a conflict.

How I interpreted the statement If the user adds an explicit read conflict range over one or more keys, then any keys in the write cache are skipped from the explicit conflict range insert. was: if one adds a `ReadConflict` over one or more keys/ranges, then any explicit write for a key in that range is not added to the `WriteConflict` range of that transaction.

Clearly, I am missing something here.

gauravagarwal avatar Jan 26 '19 12:01 gauravagarwal

I think maybe this isn't truly a bug (although it's certainly confusing, and deserves explicit warnings in documentation). Doing a fully serializable read after the write wouldn't cause a conflict (since you are just reading the key you already wrote), and I think that a snapshot read and addReadConflictKey together should be a serializable read. But then composability implies that the addReadConflictKey after the write should do nothing. Imagine that instead of being done in the wrong order as part of this operation, the addReadConflictKey is part of a second transaction that gets composed together with this one. I think you would then expect the current behavior.

davidscherer avatar Jan 26 '19 16:01 davidscherer

@gauravvnera I think this issue is roughly saying that if you add a read conflict range that includes keys which have already been written in the same transaction, the read conflict range will exclude those already written keys.

Alec's example with the snapshot read shows a pattern which may be affected by this -- namely where you do a snapshot read to see what's in the database and then selectively apply conflict ranges afterward. For example, you may have a transaction that reads an entire prefix, picks one key, and modifies it. Sometimes you don't care what happens concurrently in the whole range, but instead just the key you modified. In this case, you could use Alec's pattern of snapshot reading the whole range, choosing your key, and then adding a read conflict range to the chosen key.

When using that pattern, the effect of this issue is that you'll need to add that read conflict range before you modify the key. Done in the reverse order, you won't get a read conflict range at all, which means a simultaneous write to this key in another transaction won't cause your transaction to fail. That's how Alec's example ended up with a counter value of 1 despite incrementing it twice. His two transactions were intended to have a level of isolation that they didn't actually get.

Changing it to a regular read adds a conflict range at the time of the read, so it avoids the issue entirely.

ajbeamon avatar Jan 26 '19 17:01 ajbeamon

Thanks, David and AJ. that is extremely helpful, and a good 'gotcha' to keep in mind. I think this statement (or some version of it), should be added to docs:

if you add a read conflict range that includes keys which have already been written in the same transaction, the read conflict range will exclude those already written keys... you'll need to add that read conflict range before you modify the key.

gauravagarwal avatar Jan 27 '19 06:01 gauravagarwal

If a write is dependent on a previous read, it is obvious that the read must come first, because you need the result of the read to do the write. The same is not true for ordering a write with add read conflict range. Because the name is so descriptive, it is not something you would think to look up the documentation on either. This means even very sophisticated user could get burned by the existing behavior.

If I could magically change it, I would probably name it something like ReadRangeWithoutResult to better describe the behavior. Changing the name now is impractical, so we can close this issue after updating the documentation.

etschannen avatar Jan 30 '19 00:01 etschannen