Skip to content

Commit 94bd2f6

Browse files
committed
feat: add the op log, undo and watch
Every node now keeps an op log under .origin/oplog/: size-capped segments of checked frames, one entry per record batch written (with what the index keeps of its records), landing left waiting, and working-copy move, each tagged with its workspace and command. The sequence number of an entry is the cursor of the event stream, and never changes. Every write syncs its op log entries with its own files, in place of the index log, which now catches up from the op log on open and stays rebuildable from the store: record batches with their records, waiting landings with their file, working-copy moves with the `workspace` pointer, and a target's move with its head, as a `target.advanced` entry. Each write holds a lock file of its own, `oplog/writers/<n>`, which its entries name: readers stop before the first entry whose writer still holds its lock, so no cursor is handed out that a power loss could take back, and a crashed writer stalls nothing (readers sync its entries, then remove its file). Damage in the middle of a segment is read past from both ends, reported with the number of entries lost, rebuilds the index, and starts a new segment for appends; a length running past the end of the segment is damage too, unless no complete frame ends it, so a settled entry is never cut off as a partial append. A move that checks files out first publishes a `moving` marker durably, naming the files it starts from, those it goes to and their revision, makes the files durable, writes the revision if it's new, then the pointer, and removes the marker durably. The next command finishes a move a crash cut short, or takes it back if its revision wasn't written, touching only the files still on one side of it: edits made since, and new files, stay for its snapshot to record. An unreadable marker leaves every file as it is. `origin op log` lists operations, `origin undo` moves only this workspace's working copy back one operation (records and targets stay), and `origin watch [--kind] [--subject] [--since]` prints events derived from the op log, the records it names and the waiting landings: statement.issued, revision.created, target.advanced and check.requested. `Events::after` and `Events::wait` are the reader API for the server's long poll and the CI worker.
1 parent 0e07c28 commit 94bd2f6

19 files changed

Lines changed: 3689 additions & 404 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ to a hot path (hashing, storage, snapshots, transfer) should come with a
2727
benchmark, and its numbers in the pull request.
2828

2929
- `just bench`: the divan micro-benchmarks (encoding, store writes,
30-
snapshots, input digests and promotion, landing).
30+
snapshots, input digests and promotion, landing, reading events).
3131
- `just bench-git`: times origin against git on a 20,000-file tree with
3232
hyperfine, and rewrites `docs/benchmarks.md`.
3333

‎README.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ origin pull # follow your change where it went, or onto
4343

4444
origin workspace new [--change <change>] [<path>] # another working copy, sharing the repository
4545
origin workspace list | rm <path>
46+
47+
origin op log [-n <count>] # what this node did, operation by operation, in every workspace
48+
origin undo # move this workspace's working copy back one operation
49+
origin watch [--kind <kind>] [--subject <id>] [--since <cursor>] # events, as they happen
4650
```
4751

4852
Landing rebases a change onto the target's tip, file by file, checks the
@@ -66,6 +70,16 @@ refuse. `pull` also rebases the working copy onto its target's tip once it
6670
moved. Where your edits conflict, both versions are kept between conflict
6771
markers (see `converge` below).
6872

73+
Every node keeps an op log of what it did: records written, landings left
74+
waiting and working copies moved, numbered in order. `origin undo` only
75+
moves the working copy of the workspace it runs in back to where it was
76+
before its latest operation; records and targets stay as they are, so an
77+
undone landing still landed. `origin watch` prints events derived from the
78+
op log, one a line starting with a cursor: `statement.issued`,
79+
`revision.created`, `target.advanced` and `check.requested`. `--kind`
80+
takes an event name or a statement kind (`core.conflict`, `ci.*`), and
81+
`--since` resumes after a cursor.
82+
6983
Changes are named by a unique prefix of their id, or `@` for the working
7084
copy's. A revision is `@`, a change (for its head), or a unique prefix of
7185
the revision's id. Your name and node key live in `$ORIGIN_CONFIG_DIR`, or

‎apps/origin-src/benches/store.rs‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Store throughput: durable writes with and without batching, verified
2-
//! reads, target appends, and the record index. Run on a real disk: syncs
3-
//! cost nothing on a RAM-backed file system.
2+
//! reads, target appends, the record index, and reading events from the op
3+
//! log. Run on a real disk: syncs cost nothing on a RAM-backed file system.
44
55
use std::fs::File;
66
use std::io;
@@ -16,6 +16,7 @@ use origin_shared::{
1616
AcceptanceBody, Base, ChangeId, EncodedObject, Kind, NodeKey, Record, RecordId, Revision,
1717
Statement, TargetName, Tree, TreeId, Verdict,
1818
};
19+
use origin_src::events::{Events, Filter};
1920
use origin_src::index::Index;
2021
use origin_src::store::Store;
2122

@@ -262,3 +263,29 @@ fn rebuild_index(bencher: Bencher<'_, '_>) {
262263
.counter(ItemsCount::new(20_000_usize))
263264
.bench(|| Index::rebuild(&store).unwrap());
264265
}
266+
267+
/// Reading the events of 10k records, each written in its own batch, as
268+
/// issuing statements does: what a subscriber catching up pays.
269+
#[divan::bench(sample_count = 20)]
270+
fn read_events(bencher: Bencher<'_, '_>) {
271+
let (_dir, store) = store_on(NoSync);
272+
for record in records(10_000) {
273+
store.write_all(slice::from_ref(&record)).unwrap();
274+
}
275+
let events = Events::new(&store);
276+
bencher
277+
.counter(ItemsCount::new(10_000_usize))
278+
.bench(|| events.after(0, &Filter::default(), usize::MAX).unwrap());
279+
}
280+
281+
/// Polling for events when there are none, as `watch` does every 50 ms.
282+
#[divan::bench]
283+
fn poll_events(bencher: Bencher<'_, '_>) {
284+
let (_dir, store) = store_on(NoSync);
285+
for record in records(10_000) {
286+
store.write_all(slice::from_ref(&record)).unwrap();
287+
}
288+
let events = Events::new(&store);
289+
let end = events.end().unwrap();
290+
bencher.bench(|| events.after(end, &Filter::default(), usize::MAX).unwrap());
291+
}

‎apps/origin-src/src/cli.rs‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use clap::{Args, Parser, Subcommand};
1111
use origin_shared::{Digest, Inputs, Kind, RepoPath, TargetName, Verdict};
1212

1313
use crate::config::UserConfig;
14+
use crate::events::Filter;
15+
use crate::oplog::Actor;
16+
use crate::policy::KindPattern;
1417
use crate::workspace::{Payload, Workspace};
1518

1619
/// A modern, git-compatible source control system.
@@ -139,6 +142,37 @@ enum WorkspaceCommand {
139142
/// Manage workspaces: working copies sharing this repository.
140143
#[command(subcommand)]
141144
Workspace(WorkspacesCommand),
145+
/// Show the op log: what this node did, in every workspace.
146+
#[command(subcommand)]
147+
Op(OpCommand),
148+
/// Move this workspace's working copy back to where it was before its
149+
/// latest operation. Records and targets stay as they are.
150+
Undo,
151+
/// Print events as they happen, one a line, starting with the cursor to
152+
/// resume from. Runs until interrupted.
153+
Watch {
154+
/// Only events of this name, such as `check.requested`, or about
155+
/// statements of this kind, such as `core.conflict` or `ci.*`.
156+
#[arg(long)]
157+
kind: Vec<KindPattern>,
158+
/// Only events about this: a revision or change id prefix, or a
159+
/// target name.
160+
#[arg(long)]
161+
subject: Vec<String>,
162+
/// Start after this cursor instead of now: `0` for every event kept.
163+
#[arg(long)]
164+
since: Option<u64>,
165+
},
166+
}
167+
168+
#[derive(Debug, Subcommand)]
169+
enum OpCommand {
170+
/// List the latest operations, newest first.
171+
Log {
172+
/// Show at most this many operations.
173+
#[arg(short = 'n', long)]
174+
limit: Option<usize>,
175+
},
142176
}
143177

144178
#[derive(Debug, Args)]
@@ -314,20 +348,22 @@ impl Cli {
314348
/// Fails if the command fails; the error is meant for the user.
315349
pub fn run(self) -> Result<()> {
316350
let config = UserConfig::load()?;
351+
// The op log records the command line, without the program's name.
352+
let actor = Actor::new(env::args().skip(1).collect::<Vec<_>>().join(" "));
317353
match self.command {
318354
Command::Init { path } => {
319-
Workspace::init(&path, config)?;
355+
Workspace::init(&path, config, &actor)?;
320356
}
321-
Command::Workspace(command) => command.run(config)?,
357+
Command::Workspace(command) => command.run(config, &actor)?,
322358
}
323359
Ok(())
324360
}
325361
}
326362

327363
impl WorkspaceCommand {
328-
fn run(self, config: UserConfig) -> Result<()> {
364+
fn run(self, config: UserConfig, actor: &Actor) -> Result<()> {
329365
let cwd = env::current_dir().context("cannot read the current directory")?;
330-
let mut workspace = Workspace::discover(&cwd, config)?;
366+
let mut workspace = Workspace::discover(&cwd, config, actor)?;
331367
match self {
332368
Self::Status => workspace.status()?,
333369
Self::Diff => workspace.diff()?,
@@ -369,6 +405,19 @@ impl WorkspaceCommand {
369405
}
370406
Self::Workspace(WorkspacesCommand::List) => workspace.list_workspaces()?,
371407
Self::Workspace(WorkspacesCommand::Rm { path }) => workspace.remove_workspace(&path)?,
408+
Self::Op(OpCommand::Log { limit }) => workspace.op_log(limit)?,
409+
Self::Undo => workspace.undo()?,
410+
Self::Watch {
411+
kind,
412+
subject,
413+
since,
414+
} => workspace.watch(
415+
&Filter {
416+
kinds: kind,
417+
subjects: subject,
418+
},
419+
since,
420+
)?,
372421
}
373422
Ok(())
374423
}

0 commit comments

Comments
 (0)