Doxense / foundationdb-dotnet-client

C#/.NET Binding for FoundationDB Client API
BSD 3-Clause "New" or "Revised" License
149 stars 33 forks source link

Deferred Value Checks #104

Closed KrzysFR closed 4 years ago

KrzysFR commented 4 years ago

Prototype implementation of deferred value checks (as described in #103).

Add a set of APIs on transaction operation context, to hold a set of pending Task that will read the value of a key. Before "commit", all tasks are resolved, and their result is checked against the expected value (as provided by the application).

Each "check" is linked to a tag, so that multiple layers can all contribute to the same transaction, without interference.

If at least on check fails, a not_commited error is simulated, and the transaction will retry. Before it retries, an internal map is updated with all the tags of checks that passed/failed in the previous attempt.

Implementing metadata caching in a layer can use this API to protect against external change in the database.

The API is considered "expert level" and is not exposed directly on the transaction instance. Since metadata caching is a hard problem anyway, we don't expect this API to be called by top-level application code, but instead by layer implementors who need to have better performance.

Added APIs

public class FdbOperationContext
{
    //...

    public void AddValueCheck(string tag, Slice key, Slice expectedValue);
    public void AddValueChecks(string tag, IEnumerable<KeyValuePair<Slice, Slice>> items);
    public void AddValueChecks(string tag, KeyValuePair<Slice, Slice>[] items);
    public void AddValueChecks(string tag, ReadOnlySpan<KeyValuePair<Slice, Slice>> items);

    public bool HasAtLeastOneFailedValueCheck { get; }
    public bool? ValueCheckFailedInPreviousAttempt(string tag);
}