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

Actions at a distance

What does it mean to perform actions at a distance?

Let's assume we have two hosts, A and B, two objects o1 and o2 that are owned by each host respectively, and further assume that we want to run a nanotransaction n(o1, o2). Under regular magpie rules, we would move one object to the other host, for instance move o2 to A. Any subsequent nanotransaction that needed to operate on o2 would be submitted to A until we decided to move that object again.

Instead what we would like is to separate the effects of n() between the effects on o1 and those on o2, acquire a remote "lock" (i.e. temporarily shift ownership of o2 to A without actually moving any bytes), and then atomically submit those effects across both o1 and o2. To do this, somehow, A needs to figure out what its effects on o2 would be without reading any bytes. For blind writes, maybe this is easy. What happens if we want to make a conditional update on o1 based on a value we read in o2?

Through static analysis of the nanotransaction's body we can form an information and data flow graph. Using that graph, we could separate n into an ordered list of transactions that can happen as long as we hold a lock (see here) on o1 and o2. Basically, n() is split into N n_i() nanotransactions, each of which either receives o1 or o2 but never both. In addition to these, they also receive values that are produced by upstream nanotransactions, so for any k, j where k < j, n_j() can consume anything produced by nanotransactions 1 <= k < j. Any read value is replaced by a parameter, and causes the creation of one or more upstream nanotransactions that will read the value from the target object and return it.

This approach trades off latency of data movement for latency in coordination (where "coordination" here means progress over the epic task graph, including notification propagation etc.)

Code examples

The goal would be to split a nanotransaction that accesses multiple objects into a set of nanotransactions each one of which only needs to access one object. To do this, we would decompose the original nanotransaction's body into a set of blocks, grouping lines based on the object being mutated, and maintaining the sequential order of data accesses, preserving the original dataflow.

Let's look at some code examples.

No data dependencies

The simplest possible example here is a nanotransaction that performs blind writes to multiple objects. For example, a two-key put:

#![allow(unused)]
fn main() {
pub fn two_put::<V>(root_block: RootBlock<V>, kva: (String, V), kvb: (String, V)) {
    let object_a: IPtr = compute_bucket(root_block, kva.0).into();
    let object_b: IPtr = compute_bucket(root_block, kvb.0).into();

    nando_spawn!("two_put_internal<V>", object_a, object_b, kva, kvb);
}

pub fn two_put_internal::<V>(
    bucket_a: &mut Bucket,
    bucket_b: &mut Bucket,
    kva: (String, V),
    kvb: (String, V),
) {
    bucket_a.insert(kva.0, kva.1);
    bucket_b.insert(kvb.0, kvb.1);
}
}

Why would you implement this? I don't know. But a rewrite would break down two_put_internal() into the following:

#![allow(unused)]
fn main() {
pub fn two_put_rewrite::<V>(root_block: RootBlock<V>, kva: (String, V), kvb: (String, V)) {
    let object_a: IPtr = compute_bucket(root_block, kva.0).into();
    let object_b: IPtr = compute_bucket(root_block, kvb.0).into();

    nando_spawn!("two_put_internal_rewrite<V>", object_a, kva);
    nando_spawn!("two_put_internal_rewrite<V>", object_b, kvb);
}

pub fn two_put_internal_rewrite::<V>(bucket: &mut Bucket, kv: (String, V)) {
    bucket.insert(kv.0, kv.1);
}
}

For the semantics to be the same, two_put_internal_rewrite can only execute after the system acquires a "lock" on the underlying objects.

Object lists as parameters

Of course, there are more interesting patterns, for instance a true "multi-key" put:

#![allow(unused)]
fn main() {
pub fn multi_put::<V>(root_block: RootBlock<V>, kvs: Vec<(String, V)>) {
    let kvs: Vec<(String, V, ObjectId)> = kvs.into_iter().map(|(k, v)| (k, v, compute_bucket(k))).collect();
    let objects: HashSet<ObjectId> = kvs.iter().map(|(_, _, oid)| oid).collect();
    let objects: Vec<IPtr> = objects.iter().map(|oid| oid.into()).collect();
    nando_spawn!("multi_put_internal<V>", objects, kvs);
}

pub fn multi_put_internal::<V>(buckets: Vec<&mut Bucket>, kvs: Vec<(String, V, ObjectId)>) {
    for (key, value, oid) in &kvs {
        let maybe_bucket = buckets.find(|bucket| bucket.object_id() == oid);
        let Some(bucket) = maybe_bucket else {
            continue;
        }
        bucket.insert(key, value);
    }
}
}

A naive rewrite of the above would rewrite the call-site of multi_put_internal in the body of multi_put into calls with only a single object as its argument:

#![allow(unused)]
fn main() {
pub fn multi_put_rewrite::<V>(root_block: RootBlock<V>, kvs: Vec<(String, V)>) {
    let kvs: Vec<(String, V, ObjectId)> = kvs.into_iter().map(|(k, v)| (k, v, compute_bucket(k))).collect();
    let objects: HashSet<ObjectId> = kvs.iter().map(|(_, _, oid)| oid).collect();
    let objects: Vec<IPtr> = objects.iter().map(|oid| oid.into()).collect();
    for object in objects {
        let local_objects = vec![object];
        nando_spawn!("multi_put_internal<V>", local_objects, kvs);
    }
}
}

To optimize this, we could instead group the objects argument that the multi_put_internal() nando is invoked with by the host they are resident on. To do this at runtime, we would use a combinator similar to fold() that simply performs an aggregation based on host ownership of the first argument of multi_put_internal. The important thing is to insert appropriate "locking" nanotransactions as upstream dependencies of the various multi_put_internal() calls

Data dependencies

What if we read a value from one object and make some data-dependent update to another object?

#![allow(unused)]
fn main() {
struct SomeStruct {
    a: u32,
    b: u32,
}

pub fn read_and_write(obj_a: &mut SomeStruct, obj_b: &SomeStruct) {
    obj_a.a = obj_b.a + 10;
    obj_a.b = obj_b.b + 52;
}
}

By analysing the body of read_and_write() we can see that we read values from obj_b and then use those to update values in obj_a. We can replace those reads with placeholders that will be parameters to the rewritten nanotransaction.

#![allow(unused)]
fn main() {
pub fn read_and_write_post(obj_a: &mut SomeStruct, val1: u32, val2: u32) {
    obj_a.a = val1 + 10;
    obj_a.b = val2 + 52;
}
}

Any occurence of the values of obj_b's fields can then be replaced by the new values (in conditionals etc.). We can then introduce an upstream nanotransaction to read the values we care about as below:

#![allow(unused)]
fn main() {
pub fn read_and_write_pre(obj_b: &SomeStruct, obj_a_oid: ObjectId) {
    let obj_a_iptr = obj_a_oid.into();
    nando_spawn!("read_and_write_post", obj_a_iptr, obj_b.a, obj_b.b);
}
}

Again, for the semantics to be the same, we need to take out locks on the two objects, since we are turning a single nanotransaction that would require co-location to an epic with constistuent nanotransaction that can each execute on separate hosts.

Both objects mutable

Let's now consider that we are mutating both objects, as below:

#![allow(unused)]
fn main() {
struct SomeStruct {
    a: u32,
    b: u32,
}

pub fn read_write_mix(obj_a: &mut SomeStruct, obj_b: &mut SomeStruct) {
    obj_a.a = obj_b.a + 10;
    if obj_a.a > 20 {
        return;
    }
    obj_b.b = obj_a.a + obj_a.b;
}
}

Based on our analyses, we can see two distinct blocks in read_write_mix: the first (consisting of lines 1-4) where we are reading a value from obj_b and only mutating obj_a, and another (consisting of line 5) where we are mutating only obj_b. Because of the read dependency on obj_b, we would therefore need to split the above nanotransaction into three new ones, based on the object under mutation in each block:

#![allow(unused)]
fn main() {
pub fn read_write_mix_1(obj_b: &SomeStruct, obj_a_oid: ObjectId) {
    let obj_a_iptr = obj_a_oid.into();
    let obj_b_oid = obj_b.iptr().object_id;
    nando_spawn!("read_write_mix_2", obj_a_iptr, obj_b.a, obj_b_oid);
}

pub fn read_write_mix_2(obj_a: &mut SomeStruct, val1: u32, obj_b_oid: ObjectId) {
    let obj_b_iptr = obj_b_oid.into();
    obj_a.a = val1 + 10;
    if obj_a.a > 20 {
        return;
    }
    nando_spawn!("read_write_mix_3", obj_b_iptr, obj_a.a, obj_a.b);
}

pub fn read_write_mix_3(obj_b: &mut SomeStruct, val1: u32, val2: u32) {
    obj_b.b = val1 + val2;
}
}

Could we also optimize the above by simply passing the result of the addition as opposed to the constituent values? Sure, but we are not an optimizing compiler, so we are not concerned with that for now.

More exotic transformations

This section covers some useful transformations that we could apply to existing programs that will be more complex to implement than what has been described so far, so if we were to proceed with the ideas sketched in this document, the transformations below would be part of a second phase, after we had convinced ourselves through some proof-of-concept that this whole idea is a) doable and b) useful.

Expressions

Some access patterns can be more complex than simply reading a field of a struct -- for instance, we might have code that iterates over multiple collections (e.g. vectors or multidimensional arrays). Since we are not really performing logic rewrites but rather replacing data sinks and sources, we should be careful with how we perform these rewrites to ensure functional equivalence and a simple syntactic rewrite that does not require us to analyze and reason about data access patterns.

For instance, consider the below function:

#![allow(unused)]
fn main() {
struct ArrayStruct {
    elems: PVec<u32>,
}

pub fn iterate(obj_a: &mut ArrayStruct, obj_b: &ArrayStruct) {
    for (idx, elem) in obj_b.elems.iter().enumerate() {
        // whatever serious computation here
        obj_a.elems[idx] = elem + idx;
    }
}
}

We would like to rewritte the above as two nanotransactions, one that extacts the contents of obj_b.elems into an array that can then be serialized, and another as the function that actually updates the values of obj_a.elems:

#![allow(unused)]
fn main() {
pub fn iterate_pre(obj_b: &ArrayStruct, obj_a_oid: ObjectId) {
    let elems: Vec<u32> = obj_b.elems.clone();
    let obj_a_iptr: IPtr = obj_a_oid.into();
    nando_spawn!("iterate_post", obj_a_iptr, elems);
}

pub fn iterate_post(obj_a: &mut ArrayStruct, elems: Vec<u32>) {
    for (idx, elem) in elems.iter().enumerate() {
        obj_a.elems[idx] = elem + idx;
    }
}
}

Non-serializable data

If we want to extract and transfer data that we cannot serialize as easily as numbers or strings (or simple collections of serializable items, like a small array of tuples) to send over the wire, we should also be able to construct intermediate objects that can hold the target data by copying some user definitions. These intermediate objects should be much smaller than the original objects that we avoided migrating and therefore cost much less to co-locate with the target objects of downstream nanotransactions.

Attacking this problem

First, we need an operational semantics for our rewrites; anything not covered by these semantics will be considered not safe to rewrite. These semantics will describe the set of potential rewrite rules and the conditions under which they can be applied. These semantics will form the basis of a new nandoize pass that will, for every external function being processed, generate a "separable" version of it (a set of chopped transactions).

The decision on which version of the nanotransaction to call can thus be made at runtime, either by programmer choice (by specifying the rewritten version) or by the system itself as a result of consulting its cost model.