summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig81
1 files changed, 67 insertions, 14 deletions
diff --git a/core/replica.zig b/core/replica.zig
index 3a36a06b..040c33ca 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -40,8 +40,10 @@ const checked = builtin.mode == .debug or builtin.mode == .safe;
const head_len = 6; // origin or client: u16le, seq: u32le
/// A `stamp` frame (wire.zig): its header and the two clocks.
const stamp_len = wire.header_len + 16;
-/// Confirmed units fast-pathed under one copy before it is taken afresh:
-/// a rollback replays them all.
+/// Confirmed units kept under one copy before it is let go, at the next
+/// unit that leaves nothing applied on top (free: it is the confirmed
+/// state); a rollback replays them all, so with inputs applied on top all
+/// the while it is put back at twice as many.
const rebase_after = 32;
/// A client compares its core's digest with the session's at most once in
/// this many confirmed units (a unit that carries one, its state the
@@ -1037,22 +1039,23 @@ pub const Replica = struct {
if (r.diverged == null) r.diverged = r.gpa.dupe(u8, why) catch return;
}
- /// A unit kept to replay when the copy is next put back; past
- /// `rebase_after` of them, that is now (`toConfirmed`).
+ /// A unit kept to replay when the copy is next put back (`rebase_after`).
fn keepSince(r: *Replica, unit: []const u8) void {
const kept = r.gpa.dupe(u8, unit) catch return r.fail("out of memory");
r.since.append(r.gpa, kept) catch return r.fail("out of memory");
- if (r.since.items.len >= rebase_after) {
- // Nothing on top: it is the confirmed state already.
- if (r.applied == 0) {
- r.copy.release();
- rollback.Epoch.select(null);
- return r.clearSince();
- }
- r.stats.rebases += 1;
- r.toConfirmed();
- r.dirty = true;
+ if (r.since.items.len < rebase_after) return;
+ // Nothing on top: it is the confirmed state already.
+ if (r.applied == 0) {
+ r.copy.release();
+ rollback.Epoch.select(null);
+ return r.clearSince();
}
+ // An input applied on top (the session's pump unit before its own
+ // unit, the next to come): let go then, not replayed whole now.
+ if (r.since.items.len < 2 * rebase_after) return;
+ r.stats.rebases += 1;
+ r.toConfirmed();
+ r.dirty = true;
}
fn clearSince(r: *Replica) void {
@@ -3680,6 +3683,56 @@ test "the copy taken for an input of its own stays once the input is confirmed:
try std.testing.expectEqual(@as(u32, 1), r.stats.foreign);
}
+test "the session's pump units that moved nothing, kept under a key applied on top, reach the kept list's bound: it is let go at the confirm that leaves nothing applied, never replayed whole" {
+ // The live pair's order (core/replica.zig Peers.boundary): before it
+ // steps a client's key the session sends its pump's unit, which moved
+ // nothing; the client takes it with the key applied on top (`neutral`,
+ // kept), then the key's own unit (fast, kept). Two kept a key: which
+ // of them is the bound's is the arrivals' parity, and a pump's unit
+ // while idle turns it.
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), false);
+ defer r.deinit();
+ var sent: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (sent.items) |m| gpa.free(m);
+ sent.deinit(gpa);
+ }
+ freshSecond();
+ // A first key, confirmed: the copy held, one unit kept (the parity
+ // that puts the bound on a pump's unit).
+ try key(r, 'j', &sent);
+ r.confirm(try a.take(sent.items[0]));
+ for (1..rebase_after) |i| {
+ try key(r, if (i % 2 == 0) 'j' else 'k', &sent);
+ a.pump();
+ r.confirm((try a.pending()).?);
+ r.confirm(try a.take(sent.items[i]));
+ r.settle();
+ try std.testing.expect(r.since.items.len <= rebase_after);
+ }
+ try std.testing.expectEqual(@as(u32, rebase_after - 1), r.neutral);
+ try std.testing.expectEqual(@as(u32, 0), r.stats.rebases);
+ try agrees(a, r);
+ // Keys applied on top all the while (one ahead of the session): the
+ // list is put back and replayed at twice the bound, not let grow.
+ try key(r, 'j', &sent);
+ for (0..3 * rebase_after) |i| {
+ try key(r, 'k', &sent);
+ a.pump();
+ r.confirm((try a.pending()).?);
+ r.confirm(try a.take(sent.items[rebase_after + i]));
+ r.settle();
+ try std.testing.expect(r.since.items.len <= 2 * rebase_after);
+ }
+ try std.testing.expect(r.stats.rebases >= 1);
+ r.confirm(try a.take(sent.items[sent.items.len - 1]));
+ r.settle();
+ try agrees(a, r);
+}
+
test "a rollback over a Tab it applied measures tabs as its core has them again" {
// A tab's width is the core's (settings.tab, handed to every width
// function), which the copy holds: once the process's (config.tab_now),