Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Navigating different consistency modes using epics

So far we have only explored epics under Snapshot Isolation. We lazily construct a snapshot by maintaining a list of version constraints for co-accessed objects; nanotransactions in an epic are only allowed to read versions compatible with this version constraint set. At the end of the epic we immediately commit changes to objects created by the nanotransactions of the epic being completed (since we know that they can't be responsible for a conflict with other concurrent operations since no external operation can name them). If the epic's write set contains pre-existing objects, we apply those changes to the authoritative copy, aborting the epic if a write conflict is detected.

The epic commit protocol is kind of ad hoc, and it has mostly worked because of our evaluation targets not exposing us to conflicts. This document proposes a new mechanism to embed the commit protocol into the epic task graph, and allow us to explore different consistency modes for epics, namely serializability.

Distributed Locking in magpie

We introduce a concept of logical locks, with each locks associated with an epic. Locks do not apply to individual nanotransactions (i.e. nanotransactions that do not execute as part of epics). A nanotransaction will still force its data dependencies to be co-located before it can execute.

A nanotransaction under an epic can request either a read or a write lock, depending on whether or not the object being accessed is mutable or not. Multiple epics can acquire a read lock at the same time, but if any nanotransaction acquires a write lock then all readers have to wait until the write lock is released. Locks can be upgraded.

New system nanotransactions

Locks are acquired before a nanotransaction can execute. Acquiring a lock is a system nanotransaction that has the user-spawned original nanotransaction as a downstream control dependency. The "locking" logic is executed by the owning worker's scheduling thread, which is responsible for maintaining the status of each object (lock status, epic ID associated with a lock etc.) and also maintaining some kind of fair queue for the epics/nanotransactions requesting a lock on an object.

We also hijack the epic task graph to include a downstream dependency of the epic's top level nanotransaction (the first nanotransaction that executes and kicks off an epic).

For each top-level nanotransaction of an epic, we will insert a downstream dependency wrapping a system task, finish(). This system nanotransaction is, at a minimum, responsible for performing "cleanup" actions. If an epic acquired locks, it is responsible for invoking appropriate actions to release the acquired locks across the cluster.

finish() needs to always run before an epic finishes, regardless of whether or not the epic ran to completion. By doing this we can hide arbitrary rollback logic under the finish() subgraph.

Simple NanotransactionLock Rewrite
Simple Lock Pre RewriteSimple Lock Pre Rewrite

Atomicity of effects

Acquiring a lock per nanotransaction is sometimes enough to provide us with serializable semantics, but it does not allow the programmer to express an intent for atomic visibility of effects across distinct objects. To do this, we should also introduce a way for users to specify that they want some kind of lock barrier -- for a set of spawned nanotransactions, first acquire all locks, and then proceed with executing all of them.

Without a barrier, the following operation (e.g. a two-key put) would look like this (before lock-based rewrites): Without Barrier or rewrite

while lock rewrites would transform the task graph to the following:

Without Barrier

If we wanted to ensure that both underlying objects are available before executing, we could instead set up a barrier like so:

With Barrier

finish() for SI epics

Epics running under SI (the default epic consistency mode) do not acquire locks as they execute. Instead, before a nanotransaction under an SI epic attempts to modify a pre-existing object (an object that existed before the epic kicked off), the system creates a shadow copy of the target object for the epic's modifications to happen in. After the business logic of the epic is done and finish() is invoked, it will kick off on the coordinator of the epic (the host that executed the epic's root nanotransaction), and receive as arguments the read and write set of the entire epic, including the versions at which each object was read at.

If we are running under snapshot isolation, this process will group the write set of the epic by owning host, and spawn two operations for each such group. The first operation locks (see below) all target (i.e. written-to) objects, and proceeds to check if they are still at the version that the epic read them in. If all lock-and-check operations return successfully, the coordinator then notifies downstream tasks that once again will run on the owners of the write sets and simply apply the changes from the shadow copies onto the primary, object-backed versions of these objects. As part of the commit operation of these nanotransactions, the log entries associated with the epic are moved from a tentative in-memory log to the worker's primary log.

If any of the lock-and-check operations fail (because a conflicting operation wrote to a member of our write set), we signal with an error that instead of invoking apply will simply drop all changes from the tentative logs and unlock all locked objects.

The logical task graph for SI finish operations is shown below.

finish SI

Open Questions

How does this interact with the existing MVCC mechanisms?

Since multiple nanotransactions can acquire a read lock, if there's no write locks on an object then readers can proceed in parallel. What happens if a nanotransaction in a given epic has acquired a write lock, and then some downstream nanotransactions want to read said object? Should the write lock within the epic only last for the duration of the write nanotransaction and then immediately get demoted to a read lock again within the scope of the current epic? For nanotransactions outside this epic, the object is still write-locked and so nobody else can read it.

Can we avoid the traditional pitfalls of 2PL?

What could ownership and transparent replication do for us here? Can we avoid stalls when we lose a participant?

How can we deal with deadlock?

How can we integrate an external deadlock detection mechanism? All workers need to expose some endpoint to i) fetch their current lock queue tables (which will return a list of pairs of epics (e1, e2) if one of epic e1's nanotransactions scheduled on the current worker is waiting on a nanotransaction of epic e2 also scheduled on the current worker) and ii) an endpoint to force an abort of an epic. Ideally, we should pick a victim based on other metrics (e.g. size of write set so far) to minimize the amount of wasted work.

Rollback could also be incremental -- we could abort all spawned nanotransactions up to a subgraph root (i.e. the parent of the nanotransactions that are part of the deadlock cycle), and then replay starting from that parent node.

For epics under SI, abort simply will drop all tentative updates that have already been made. For serializable epics, abort will have to trigger rollback of executed transactions. For epics with external effects, we should somehow encourage users to rely on the lock barrier mechanism to avoid having to implement compensating actions.