1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Single-at-a-time operator lifecycle for timestamp imports.
//!
//! The durable current-job record is the cross-process authority. Submit
//! confines the path, atomically creates the record/current pointer, and runs
//! the real importer in a background thread. Startup adopts a nonterminal
//! current job; shutdown cancels and joins it so the next process can resume.
const std = @import("std");
const import_path = @import("import_path.zig");
const jobs_mod = @import("jobs.zig");
const runner = @import("runner.zig");
const observability = @import("../runtime/observability.zig");
const Allocator = std.mem.Allocator;
const Io = std.Io;
const log = std.log.scoped(.stream_timestamp_import);
pub const Config = struct {
allocator: Allocator,
io: Io,
import_dir: []const u8,
scratch_root: []const u8,
jobs: *jobs_mod.Store,
runner: runner.Config,
ready: ?*const std.atomic.Value(bool) = null,
};
pub const Manager = struct {
allocator: Allocator,
io: Io,
root: import_path.Root,
scratch_root: []u8,
jobs: *jobs_mod.Store,
ready: ?*const std.atomic.Value(bool),
runner_config: runner.Config,
mutex: Io.Mutex = .init,
thread: ?std.Thread = null,
active: bool = false,
active_id: ?[]u8 = null,
cancelled: std.atomic.Value(bool) = .init(false),
pub fn init(config: Config) !Manager {
const root = try import_path.Root.open(config.allocator, config.io, config.import_dir);
errdefer {
var owned = root;
owned.deinit();
}
try Io.Dir.cwd().createDirPath(config.io, config.scratch_root);
return .{
.allocator = config.allocator,
.io = config.io,
.root = root,
.scratch_root = try config.allocator.dupe(u8, config.scratch_root),
.jobs = config.jobs,
.ready = config.ready,
.runner_config = config.runner,
};
}
pub fn deinit(self: *Manager) void {
self.shutdown();
if (self.active_id) |id| self.allocator.free(id);
self.allocator.free(self.scratch_root);
self.root.deinit();
self.* = undefined;
}
/// Returns an owned job id immediately after its durable admission.
pub fn submit(self: *Manager, requested_path: []const u8) ![]u8 {
const csv_path = try self.root.resolve(requested_path);
defer self.allocator.free(csv_path);
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
self.reapLocked();
try self.ensureIdleLocked();
if (self.ready) |ready| if (!ready.load(.acquire)) return error.ImportNotReady;
const id = try newJobId(self.allocator, self.io);
defer self.allocator.free(id);
try self.jobs.begin(.{
.id = id,
.csv_path = csv_path,
.submitted_at_us = Io.Timestamp.now(self.io, .real).toMicroseconds(),
});
self.startLocked(id) catch |err| {
var parsed = (try self.jobs.get(id)).?;
defer parsed.deinit();
var failed = parsed.value;
failed.state = .failed;
failed.error_message = @errorName(err);
failed.finished_at_us = Io.Timestamp.now(self.io, .real).toMicroseconds();
try self.jobs.put(failed);
return err;
};
return try self.allocator.dupe(u8, id);
}
/// Adopt the persisted current job when it is nonterminal.
pub fn resumeIncomplete(self: *Manager) !bool {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
self.reapLocked();
if (self.active) return false;
const id = try self.jobs.current() orelse return false;
defer self.allocator.free(id);
var parsed = (try self.jobs.get(id)) orelse return error.ImportCurrentRecordMissing;
defer parsed.deinit();
if (parsed.value.state.terminal()) return false;
try self.startLocked(id);
return true;
}
pub fn status(self: *Manager, id: []const u8) !jobs_mod.ParsedRecord {
return (try self.jobs.get(id)) orelse error.ImportJobNotFound;
}
pub fn current(self: *Manager) !?jobs_mod.ParsedRecord {
const id = try self.jobs.current() orelse return null;
defer self.allocator.free(id);
return (try self.jobs.get(id)) orelse error.ImportCurrentRecordMissing;
}
pub fn wait(self: *Manager) void {
self.mutex.lockUncancelable(self.io);
const thread = self.thread;
self.thread = null;
self.mutex.unlock(self.io);
if (thread) |item| item.join();
self.mutex.lockUncancelable(self.io);
self.active = false;
if (self.active_id) |id| self.allocator.free(id);
self.active_id = null;
self.mutex.unlock(self.io);
}
pub fn shutdown(self: *Manager) void {
self.cancelled.store(true, .release);
self.wait();
}
pub fn setTraceContext(self: *Manager, trace_context: observability.Context) void {
self.runner_config.trace_context = trace_context;
}
fn ensureIdleLocked(self: *Manager) !void {
if (self.active) return error.ImportJobInProgress;
const current_id = try self.jobs.current() orelse return;
defer self.allocator.free(current_id);
var current_record = (try self.jobs.get(current_id)) orelse return error.ImportCurrentRecordMissing;
defer current_record.deinit();
if (!current_record.value.state.terminal()) return error.ImportJobInProgress;
}
fn startLocked(self: *Manager, id: []const u8) !void {
if (self.active or self.thread != null) return error.ImportJobInProgress;
self.cancelled.store(false, .release);
self.active_id = try self.allocator.dupe(u8, id);
errdefer {
self.allocator.free(self.active_id.?);
self.active_id = null;
}
self.active = true;
errdefer self.active = false;
self.thread = try std.Thread.spawn(.{}, worker, .{self});
}
fn worker(self: *Manager) void {
var config = self.runner_config;
config.scratch_root = self.scratch_root;
config.cancelled = &self.cancelled;
runner.run(config, self.active_id.?) catch |err| {
log.err("timestamp import {s} stopped: {s}", .{ self.active_id.?, @errorName(err) });
};
self.mutex.lockUncancelable(self.io);
self.active = false;
self.mutex.unlock(self.io);
}
fn reapLocked(self: *Manager) void {
if (self.active or self.thread == null) return;
self.thread.?.join();
self.thread = null;
if (self.active_id) |id| self.allocator.free(id);
self.active_id = null;
}
};
fn newJobId(allocator: Allocator, io: Io) ![]u8 {
var random: [8]u8 = undefined;
try io.randomSecure(&random);
return std.fmt.allocPrint(allocator, "{d}-{x}", .{
Io.Timestamp.now(io, .real).toMicroseconds(),
random,
});
}
test "manager submits real work and adopts a durable incomplete job" {
const testing = std.testing;
const archive_mod = @import("../storage/archive.zig");
const meta_store = @import("../storage/meta_store.zig");
const rules_mod = @import("rules.zig");
const metrics = @import("../runtime/metrics.zig");
var threaded: Io.Threaded = .init(testing.allocator, .{});
defer threaded.deinit();
const io = threaded.io();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDirPath(io, "imports");
try tmp.dir.writeFile(io, .{
.sub_path = "imports/job.csv",
.data = "uri,timestamp\n" ++
"at://did:plc:manager/app.bsky.feed.post/one,2024-01-02T03:04:05Z\n",
});
var root_buffer: [std.fs.max_path_bytes]u8 = undefined;
const root_len = try tmp.dir.realPath(io, &root_buffer);
const root = root_buffer[0..root_len];
const imports = try std.fs.path.join(testing.allocator, &.{ root, "imports" });
defer testing.allocator.free(imports);
const scratch = try std.fs.path.join(testing.allocator, &.{ root, "timestamp-jobs" });
defer testing.allocator.free(scratch);
const csv_path = try std.fs.path.join(testing.allocator, &.{ imports, "job.csv" });
defer testing.allocator.free(csv_path);
var meta = try meta_store.Store.open(testing.allocator, root);
defer meta.deinit();
var job_store = jobs_mod.Store.init(testing.allocator, &meta);
var rules = try rules_mod.Store.open(testing.allocator, io, root);
defer rules.deinit();
var archive = try archive_mod.Archive.init(testing.allocator, io, root);
defer archive.deinit();
archive.stamper_ctx = &rules;
archive.stamper = rules_mod.Store.stampOpaque;
var metric_stats: metrics.Stats = .{};
_ = try archive.append(.{
.seq = 0,
.witnessed_at = 1,
.indexed_at = 0,
.kind = .create,
.did = "did:plc:manager",
.collection = "app.bsky.feed.post",
.rkey = "one",
.rev = "3lmanager",
.payload = "payload",
}, 1);
var manager = try Manager.init(.{
.allocator = testing.allocator,
.io = io,
.import_dir = imports,
.scratch_root = scratch,
.jobs = &job_store,
.runner = .{
.allocator = testing.allocator,
.io = io,
.scratch_root = scratch,
.archive = &archive,
.rules = &rules,
.jobs = &job_store,
.stats = &metric_stats,
},
});
defer manager.deinit();
const id = try manager.submit("job.csv");
defer testing.allocator.free(id);
manager.wait();
var completed = try manager.status(id);
defer completed.deinit();
try testing.expectEqual(jobs_mod.State.complete, completed.value.state);
try testing.expectEqual(@as(u64, 1), completed.value.rows_mutated);
var current = (try manager.current()).?;
defer current.deinit();
try testing.expectEqualStrings(id, current.value.id);
// Model a hard process stop after admission: only the durable record and
// current pointer exist. It blocks a new submit and startup adopts it.
try job_store.begin(.{
.id = "adopt-me",
.csv_path = csv_path,
.submitted_at_us = 2,
});
try testing.expectError(error.ImportJobInProgress, manager.submit("job.csv"));
try testing.expect(try manager.resumeIncomplete());
manager.wait();
var adopted = try manager.status("adopt-me");
defer adopted.deinit();
try testing.expectEqual(jobs_mod.State.complete, adopted.value.state);
try testing.expect(!(try manager.resumeIncomplete()));
try testing.expectEqual(@as(u64, 2), metric_stats.import_jobs_ok_total.load(.monotonic));
try testing.expectEqual(@as(u64, 0), metric_stats.import_jobs_error_total.load(.monotonic));
try testing.expectEqual(@as(u64, 0), metric_stats.import_phase.load(.monotonic));
try testing.expectEqual(@as(u64, 2), metric_stats.import_rows_parsed_total.load(.monotonic));
try testing.expectEqual(@as(u64, 1), metric_stats.import_rows_mutated_total.load(.monotonic));
try testing.expectEqual(@as(u64, 2), metric_stats.import_segments_examined_total.load(.monotonic));
try testing.expect(metric_stats.import_bytes_rewritten_total.load(.monotonic) > 0);
}