sincekit / examples /replicate.cpp
pgg-engineering's picture
Release SinceKit 0.1.0 with native benchmarks and trace replay
ef8334d verified
Raw History Blame Contribute Delete
5.81 kB
// c++ -std=c++17 -O2 -Iinclude examples/replicate.cpp -o /tmp/replicate
#include "sincekit_batch.h"
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
using Stamp = std::uint64_t;
using Value = std::optional<std::uint32_t>;
using State = std::vector<Value>;
struct World {
Stamp epoch; // Use a fresh epoch for every new authoritative Index.
sincekit::Index index;
State values;
World(Stamp e, std::size_t n) : epoch(e), index(n), values(n) {}
void write(std::uint32_t id, Value value) {
if (value) index.touch(id);
else index.erase(id);
values[id] = value;
}
};
struct Row { std::uint32_t id; Value value; };
struct Peer { Stamp boot; Stamp acked; }; // Sender's per-reader progress.
struct Ack { Stamp epoch; Stamp boot; Stamp revision; };
struct Frame {
Stamp epoch, boot, base, target;
std::size_t count;
std::vector<Row> rows;
};
struct Prepared {
Stamp epoch, target;
std::vector<Peer> peers;
std::vector<std::size_t> lengths;
std::vector<Row> rows;
Frame unicast(std::size_t i) const {
const auto peer = peers.at(i);
const auto count = lengths[i];
return {epoch, peer.boot, peer.acked, target, count,
std::vector<Row>(rows.begin(), rows.begin() + count)};
}
};
// Freeze the index and its values together here. One thread owns this world.
// Each trusted reader sees the same slots, but receives only its own prefix.
Prepared prepare(const World& w, const std::vector<Peer>& peers) {
std::vector<Stamp> cursors;
for (const auto& peer : peers) cursors.push_back(peer.acked);
auto plan = sincekit::since_many(w.index, cursors);
Prepared p{w.epoch, plan.revision, peers, std::move(plan.prefix_lengths), {}};
p.rows.reserve(plan.rows.size());
for (const auto& row : plan.rows) {
p.rows.push_back({row.id, row.deleted ? std::nullopt : w.values[row.id]});
}
return p;
}
struct Mirror {
Stamp epoch, boot, applied;
State values;
std::optional<Ack> receive(const Frame& frame) {
if (frame.epoch != epoch || frame.boot != boot || frame.target < frame.base ||
frame.count != frame.rows.size()) return std::nullopt;
if (applied != frame.base && applied != frame.target) return std::nullopt;
if (std::any_of(frame.rows.begin(), frame.rows.end(),
[&](const Row& r) { return r.id >= values.size(); })) {
return std::nullopt;
}
if (applied != frame.target) {
// All rows are present. Assignments are nonthrowing in this example.
for (const auto& r : frame.rows) values[r.id] = r.value;
applied = frame.target;
}
return Ack{epoch, boot, applied};
}
};
bool confirm(Peer& peer, Stamp world_epoch, const Frame& sent, const Ack& ack) {
if (ack.epoch != world_epoch || ack.epoch != sent.epoch || ack.boot != peer.boot ||
ack.boot != sent.boot || ack.revision != sent.target || sent.base != peer.acked) {
return false;
}
peer.acked = ack.revision;
return true;
}
void check(bool ok) {
if (!ok) throw std::runtime_error("replication invariant failed");
}
int main() {
World w(100, 5);
std::vector<Peer> peers{{10, 0}, {20, 0}};
Mirror fast{w.epoch, 10, 0, State(5)}, slow{w.epoch, 20, 0, State(5)};
w.write(0, 42); w.write(1, 8); w.write(2, 31);
auto initial = prepare(w, peers);
auto first = initial.unicast(0);
auto ack = fast.receive(first);
check(ack && confirm(peers[0], w.epoch, first, *ack));
check(fast.values == w.values && slow.applied == 0);
w.write(0, 45); w.write(0, 46); w.write(1, std::nullopt);
auto round = prepare(w, peers);
const auto oracle = w.values;
w.write(3, 77); // The prepared records own their values at the earlier cut.
auto a = round.unicast(0), b = round.unicast(1);
check(a.count == 2 && b.count == 3);
auto truncated = b;
truncated.rows.pop_back();
check(!slow.receive(truncated) && slow.applied == 0);
ack = slow.receive(b);
check(ack && confirm(peers[1], w.epoch, b, *ack));
check(fast.receive(a).has_value()); // Simulate losing this ACK.
check(peers[0].acked == first.target);
ack = fast.receive(a); // Retry the same prepared frame.
check(ack && confirm(peers[0], w.epoch, a, *ack));
check(fast.values == oracle && slow.values == oracle);
// A restarting reader handshakes with a fresh boot ID and an empty mirror.
fast = {w.epoch, 11, 0, State(5)};
peers[0] = {fast.boot, 0};
check(!fast.receive(a) && !confirm(peers[0], w.epoch, a, *ack));
auto catchup = prepare(w, peers);
auto fresh = catchup.unicast(0), lagged = catchup.unicast(1);
check(fresh.count == 4 && lagged.count == 1);
ack = fast.receive(fresh);
check(ack && confirm(peers[0], w.epoch, fresh, *ack));
ack = slow.receive(lagged);
check(ack && confirm(peers[1], w.epoch, lagged, *ack));
check(fast.values == w.values && slow.values == w.values);
// An authoritative restart also changes epoch and resets the mirror.
World replacement(101, 5);
replacement.write(4, 99);
slow = {replacement.epoch, slow.boot, 0, State(5)};
peers[1].acked = 0;
check(!slow.receive(lagged) && !confirm(peers[1], replacement.epoch, lagged, *ack));
auto rebuilt = prepare(replacement, {peers[1]}).unicast(0);
ack = slow.receive(rebuilt);
check(ack && confirm(peers[1], replacement.epoch, rebuilt, *ack));
check(slow.values == replacement.values);
std::cout << "Two mirrors converged. Verified truncation, lost ACK, reader restart, "
"and authority restart.\n";
}