Skip to content

Commit

Permalink
VSR: Generalize FreeSetEncoded → CheckpointTrailer
Browse files Browse the repository at this point in the history
In preparation for moving client sessions data from the superblock trailer into the grid, generalize `FreeSetEncoded` into `CheckpointTrailer`.

Note that this commit doesn't touch the `schema.FreeSetNode` block type; that will be part of the next PR.
  • Loading branch information
sentientwaffle committed Dec 11, 2023
1 parent 06c5313 commit 281360c
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 360 deletions.
10 changes: 6 additions & 4 deletions docs/internals/data_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ of a couple of block references. These blocks, taken together, specify the manif

Superblock is located at a fixed position in the data file, so, when a replica starts up, it can
read the superblock, read root block indexes and hashes from the superblock, and through those get
access to the rest of the data in the grid. Besides the manifest, superblock also stores a
compressed bitset of all grid blocks which are not currently allocated.
access to the rest of the data in the grid. Besides the manifest, superblock also references a
compressed bitset, which is itself stored in the grid, of all grid blocks which are not currently
allocated.

```zig
pub const SuperBlock = struct {
manifest_oldest: BlockReference,
manifest_newest: BlockReference,
free_set: BitSet,
free_set: BlockReference,
};
```

Expand Down Expand Up @@ -191,7 +192,8 @@ const Superblock = {
manifest_block_oldest_checksum: u128,
manifest_block_newest_address: u64,
manifest_block_newest_checksum: u128,
free_set: BitSet,
free_set_last_address: u64,
free_set_last_checksum: u128,
};
```

Expand Down
17 changes: 7 additions & 10 deletions src/lsm/forest_fuzz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Forest = StateMachine.Forest;
const Grid = GridType(Storage);
const SuperBlock = vsr.SuperBlockType(Storage);
const FreeSet = vsr.FreeSet;
const FreeSetEncoded = vsr.FreeSetEncodedType(Storage);
const CheckpointTrailer = vsr.CheckpointTrailerType(Storage);

pub const tigerbeetle_config = @import("../config.zig").configs.fuzz_min;

Expand Down Expand Up @@ -171,11 +171,7 @@ const Environment = struct {
env.superblock.open(superblock_open_callback, &env.superblock_context);
try env.tick_until_state_change(.superblock_open, .free_set_open);

env.grid.free_set_encoded.open(
&env.grid,
env.superblock.working.free_set_reference(),
free_set_open_callback,
);
env.grid.open(grid_open_callback);
try env.tick_until_state_change(.free_set_open, .forest_init);

env.forest = try Forest.init(allocator, &env.grid, node_count, forest_options);
Expand All @@ -192,7 +188,7 @@ const Environment = struct {
}

/// Allocate a sparse subset of grid blocks to make sure that the encoded free set needs more
/// than one block to exercise the block linked list logic from FreeSetEncoded.
/// than one block to exercise the block linked list logic from CheckpointTrailer.
fn fragmentate_free_set(env: *Environment) void {
assert(env.grid.free_set.count_acquired() == 0);

Expand Down Expand Up @@ -222,8 +218,7 @@ const Environment = struct {
env.change_state(.superblock_open, .free_set_open);
}

fn free_set_open_callback(set: *FreeSetEncoded) void {
const grid = @fieldParentPtr(Grid, "free_set_encoded", set);
fn grid_open_callback(grid: *Grid) void {
const env = @fieldParentPtr(@This(), "grid", grid);
env.change_state(.free_set_open, .forest_init);
}
Expand Down Expand Up @@ -272,12 +267,14 @@ const Environment = struct {
}
env.superblock.checkpoint(superblock_checkpoint_callback, &env.superblock_context, .{
.manifest_references = env.forest.manifest_log.checkpoint_references(),
.free_set_reference = env.grid.free_set_encoded.checkpoint_reference(),
.free_set_reference = env.grid.free_set_checkpoint.checkpoint_reference(),
.commit_min_checksum = env.superblock.working.vsr_state.checkpoint.commit_min_checksum + 1,
.commit_min = env.checkpoint_op.?,
.commit_max = env.checkpoint_op.? + 1,
.sync_op_min = 0,
.sync_op_max = 0,
.storage_size = vsr.superblock.data_file_size_min +
(env.grid.free_set.highest_address_acquired() orelse 0) * constants.block_size,
});
try env.tick_until_state_change(.superblock_checkpoint, .fuzzing);

Expand Down
19 changes: 7 additions & 12 deletions src/lsm/manifest_log_fuzz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn run_fuzz(
env.open_superblock();
env.wait(&env.manifest_log);

env.open_free_set();
env.open_grid();
env.wait(&env.manifest_log);

env.open();
Expand Down Expand Up @@ -257,8 +257,6 @@ fn generate_events(
}

const Environment = struct {
const FreeSetEncoded = vsr.FreeSetEncodedType(Storage);

allocator: std.mem.Allocator,
storage: Storage,
storage_verify: Storage,
Expand Down Expand Up @@ -395,18 +393,13 @@ const Environment = struct {
env.pending -= 1;
}

fn open_free_set(env: *Environment) void {
fn open_grid(env: *Environment) void {
assert(env.pending == 0);
env.pending += 1;
env.grid.free_set_encoded.open(
&env.grid,
env.superblock.working.free_set_reference(),
open_free_set_callback,
);
env.grid.open(open_grid_callback);
}

fn open_free_set_callback(free_set_encoded: *FreeSetEncoded) void {
const grid = @fieldParentPtr(Grid, "free_set_encoded", free_set_encoded);
fn open_grid_callback(grid: *Grid) void {
const env = @fieldParentPtr(Environment, "grid", grid);
env.pending -= 1;
}
Expand Down Expand Up @@ -493,12 +486,14 @@ const Environment = struct {
&env.superblock_context,
.{
.manifest_references = env.manifest_log.checkpoint_references(),
.free_set_reference = env.grid.free_set_encoded.checkpoint_reference(),
.free_set_reference = env.grid.free_set_checkpoint.checkpoint_reference(),
.commit_min_checksum = vsr_state.checkpoint.commit_min_checksum + 1,
.commit_min = vsr.Checkpoint.checkpoint_after(vsr_state.checkpoint.commit_min),
.commit_max = vsr.Checkpoint.checkpoint_after(vsr_state.commit_max),
.sync_op_min = 0,
.sync_op_max = 0,
.storage_size = vsr.superblock.data_file_size_min +
(env.grid.free_set.highest_address_acquired() orelse 0) * constants.block_size,
},
);
env.wait(&env.manifest_log);
Expand Down
14 changes: 5 additions & 9 deletions src/lsm/tree_fuzz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const ManifestLog = @import("manifest_log.zig").ManifestLogType(Storage);

const Grid = GridType(Storage);
const SuperBlock = vsr.SuperBlockType(Storage);
const FreeSetEncoded = vsr.FreeSetEncodedType(Storage);

pub const tigerbeetle_config = @import("../config.zig").configs.fuzz_min;

Expand Down Expand Up @@ -190,11 +189,7 @@ fn EnvironmentType(comptime table_usage: TableUsage) type {
env.superblock.open(superblock_open_callback, &env.superblock_context);

env.tick_until_state_change(.superblock_open, .free_set_open);
env.grid.free_set_encoded.open(
&env.grid,
env.superblock.working.free_set_reference(),
free_set_open_callback,
);
env.grid.open(grid_open_callback);

env.tick_until_state_change(.free_set_open, .tree_init);
env.tree = try Tree.init(allocator, &env.node_pool, &env.grid, .{
Expand Down Expand Up @@ -224,8 +219,7 @@ fn EnvironmentType(comptime table_usage: TableUsage) type {
env.change_state(.superblock_open, .free_set_open);
}

fn free_set_open_callback(set: *FreeSetEncoded) void {
const grid = @fieldParentPtr(Grid, "free_set_encoded", set);
fn grid_open_callback(grid: *Grid) void {
const env = @fieldParentPtr(Environment, "grid", grid);
env.change_state(.free_set_open, .tree_init);
}
Expand Down Expand Up @@ -304,12 +298,14 @@ fn EnvironmentType(comptime table_usage: TableUsage) type {
const checkpoint_op = op - constants.lsm_batch_multiple;
env.superblock.checkpoint(superblock_checkpoint_callback, &env.superblock_context, .{
.manifest_references = std.mem.zeroes(vsr.SuperBlockManifestReferences),
.free_set_reference = env.grid.free_set_encoded.checkpoint_reference(),
.free_set_reference = env.grid.free_set_checkpoint.checkpoint_reference(),
.commit_min_checksum = env.superblock.working.vsr_state.checkpoint.commit_min_checksum + 1,
.commit_min = checkpoint_op,
.commit_max = checkpoint_op + 1,
.sync_op_min = 0,
.sync_op_max = 0,
.storage_size = vsr.superblock.data_file_size_min +
(env.grid.free_set.highest_address_acquired() orelse 0) * constants.block_size,
});

env.change_state(.fuzzing, .superblock_checkpoint);
Expand Down
4 changes: 2 additions & 2 deletions src/vsr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ pub const SlotRange = @import("vsr/journal.zig").SlotRange;
pub const SuperBlockType = superblock.SuperBlockType;
pub const SuperBlockTrailer = superblock.Trailer;
pub const SuperBlockManifestReferences = superblock.ManifestReferences;
pub const SuperBlockFreeSetReference = superblock.FreeSetReference;
pub const SuperBlockTrailerReference = superblock.TrailerReference;
pub const VSRState = superblock.SuperBlockHeader.VSRState;
pub const CheckpointState = superblock.SuperBlockHeader.CheckpointState;
pub const checksum = @import("vsr/checksum.zig").checksum;
pub const ChecksumStream = @import("vsr/checksum.zig").ChecksumStream;
pub const Header = @import("vsr/message_header.zig").Header;
pub const FreeSet = @import("vsr/free_set.zig").FreeSet;
pub const FreeSetEncodedType = @import("vsr/free_set_encoded.zig").FreeSetEncodedType;
pub const CheckpointTrailerType = @import("vsr/checkpoint_trailer.zig").CheckpointTrailerType;

/// The version of our Viewstamped Replication protocol in use, including customizations.
/// For backwards compatibility through breaking changes (e.g. upgrading checksums/ciphers).
Expand Down
Loading

0 comments on commit 281360c

Please sign in to comment.