sdjournal 0.1.20

Pure Rust systemd journal reader and query engine
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
mod support;

use sdjournal::{
    Journal, JournalConfig, LiveJournal, LiveQueueFullPolicy, LiveSubscription, MmapPolicy,
    SubscriptionOptions,
};
use std::fs;
use std::path::Path;
use std::sync::mpsc::TryRecvError;
use std::thread;
use std::time::Duration;
use support::synthetic_journal::{
    SyntheticJournalFile, synthetic_message, write_synthetic_journal_file,
};

#[test]
fn shared_live_engine_dispatches_appended_entries_to_matching_subscriptions() {
    let initial_units = ["alpha.service", "beta.service"];
    let layout = SyntheticJournalFile::new(&initial_units);
    let journal = Journal::open_dir_with_config(layout.root(), live_test_config())
        .expect("open synthetic journal");
    let mut live = journal.live().expect("create live engine");

    let alpha = subscribe_unit(&mut live, "alpha.service");
    let beta = subscribe_unit(&mut live, "beta.service");
    let alpha_by_message = subscribe_message(&mut live, &synthetic_message(7, 2, "alpha.service"));
    let absent = subscribe_unit(&mut live, "absent.service");

    assert_subscription_empty(&alpha);
    assert_subscription_empty(&beta);
    assert_subscription_empty(&alpha_by_message);
    assert_subscription_empty(&absent);

    let rewritten_units = [
        "alpha.service",
        "beta.service",
        "alpha.service",
        "beta.service",
        "gamma.service",
    ];
    layout.rewrite(&rewritten_units);

    let deliveries = poll_until_delivered(&mut live, 3);
    assert_eq!(deliveries, 3);

    let alpha_entry = recv_ready(&alpha);
    let beta_entry = recv_ready(&beta);
    let alpha_message_entry = recv_ready(&alpha_by_message);

    assert_entry(
        &alpha_entry,
        "alpha.service",
        &synthetic_message(7, 2, "alpha.service"),
    );
    assert_entry(
        &beta_entry,
        "beta.service",
        &synthetic_message(7, 3, "beta.service"),
    );
    assert_eq!(
        alpha_entry.cursor().expect("alpha cursor"),
        alpha_message_entry.cursor().expect("alpha message cursor"),
        "the same appended entry should fan out to both matching subscriptions"
    );

    assert_subscription_empty(&alpha);
    assert_subscription_empty(&beta);
    assert_subscription_empty(&alpha_by_message);
    assert_subscription_empty(&absent);
}

#[test]
fn direct_live_engine_open_dir_dispatches_appended_entries() {
    let layout = SyntheticJournalFile::new(&["alpha.service"]);
    let mut live = LiveJournal::open_dir_with_config(layout.root(), live_test_config())
        .expect("open live engine without historical journal");

    let alpha = subscribe_unit(&mut live, "alpha.service");
    assert_subscription_empty(&alpha);

    layout.rewrite(&["alpha.service", "alpha.service"]);

    let deliveries = poll_until_delivered(&mut live, 1);
    assert_eq!(deliveries, 1);

    let alpha_entry = recv_ready(&alpha);
    assert_entry(
        &alpha_entry,
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );
}

#[test]
fn live_engine_initial_recheck_does_not_replay_existing_entries() {
    let layout = SyntheticJournalFile::new(&["alpha.service"]);
    let mut live = LiveJournal::open_dir_with_config(layout.root(), live_test_config())
        .expect("open live engine");

    let alpha = subscribe_unit(&mut live, "alpha.service");

    assert_eq!(live.poll_once().expect("initial live recheck"), 0);
    assert_subscription_empty(&alpha);
}

#[test]
fn live_engine_drains_large_appends_across_bounded_batches() {
    let layout = SyntheticJournalFile::new(&["alpha.service"]);
    let mut live = LiveJournal::open_dir_with_config(
        layout.root(),
        live_test_config_with_live_limits(1, 1, LiveQueueFullPolicy::Block),
    )
    .expect("open live engine with tiny batches");

    let alpha = subscribe_unit(&mut live, "alpha.service");
    layout.rewrite(&["alpha.service", "alpha.service", "alpha.service"]);

    assert_eq!(live.poll_once().expect("first bounded poll"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );

    assert_eq!(live.poll_once().expect("second bounded poll"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 2, "alpha.service"),
    );
}

#[test]
fn live_engine_disconnects_slow_subscription_when_queue_is_full() {
    let layout = SyntheticJournalFile::new(&["alpha.service"]);
    let mut live = LiveJournal::open_dir_with_config(
        layout.root(),
        live_test_config_with_live_limits(1, 2, LiveQueueFullPolicy::Disconnect),
    )
    .expect("open live engine with bounded disconnect policy");

    let alpha = subscribe_unit(&mut live, "alpha.service");
    layout.rewrite(&["alpha.service", "alpha.service", "alpha.service"]);

    assert_eq!(live.poll_once().expect("bounded poll"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );
    match alpha.try_recv() {
        Err(TryRecvError::Disconnected) => {}
        other => panic!("expected disconnected slow subscription, got {other:?}"),
    }
}

#[test]
fn live_replay_is_delivered_in_bounded_batches() {
    let layout = SyntheticJournalFile::new(&["alpha.service", "alpha.service", "alpha.service"]);
    let mut live = LiveJournal::open_dir_with_config(
        layout.root(),
        live_test_config_with_live_limits(1, 1, LiveQueueFullPolicy::Block),
    )
    .expect("open live engine with bounded replay");

    let mut filter = live.filter();
    filter.match_unit("alpha.service");
    let mut options = SubscriptionOptions::new(filter);
    options.since_realtime(0);
    let alpha = live
        .subscribe_with_options(options)
        .expect("subscribe with replay");

    assert_eq!(live.poll_once().expect("first replay batch"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 0, "alpha.service"),
    );

    assert_eq!(live.poll_once().expect("second replay batch"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );
}

#[test]
fn replay_subscription_does_not_advance_existing_live_tail() {
    let layout = SyntheticJournalFile::new(&["alpha.service"]);
    let mut live = LiveJournal::open_dir_with_config(layout.root(), live_test_config())
        .expect("open live engine");
    let alpha = subscribe_unit(&mut live, "alpha.service");

    layout.rewrite(&["alpha.service", "alpha.service"]);

    let mut replay_filter = live.filter();
    replay_filter.match_unit("absent.service");
    let mut options = SubscriptionOptions::new(replay_filter);
    options.since_realtime(0);
    let absent_replay = live
        .subscribe_with_options(options)
        .expect("subscribe replaying absent unit");

    let deliveries = poll_until_delivered(&mut live, 1);
    assert_eq!(deliveries, 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );
    assert_subscription_empty(&absent_replay);
}

#[test]
fn replay_snapshot_switches_to_live_entries_created_after_subscription() {
    let layout = SyntheticJournalFile::new(&["alpha.service", "alpha.service"]);
    let mut cfg = live_test_config_with_live_limits(4, 1, LiveQueueFullPolicy::Block);
    cfg.max_live_replay_entries = 2;
    let mut live = LiveJournal::open_dir_with_config(layout.root(), cfg).expect("open live engine");

    let mut filter = live.filter();
    filter.match_unit("alpha.service");
    let mut options = SubscriptionOptions::new(filter);
    options.since_realtime(0);
    let alpha = live
        .subscribe_with_options(options)
        .expect("subscribe replay");

    write_synthetic_journal_file(&layout.root().join("later.journal"), &["alpha.service"], 9);

    assert_eq!(live.poll_once().expect("first replay batch"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 0, "alpha.service"),
    );

    assert_eq!(live.poll_once().expect("second replay batch"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );

    let deliveries = poll_until_delivered(&mut live, 1);
    assert_eq!(deliveries, 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(9, 0, "alpha.service"),
    );
}

#[test]
fn live_only_subscription_is_not_blocked_by_another_subscription_replay() {
    let layout = SyntheticJournalFile::new(&["alpha.service", "alpha.service"]);
    let later = layout.root().join("later.journal");
    write_synthetic_journal_file(&later, &["gamma.service"], 9);
    let mut live = LiveJournal::open_dir_with_config(
        layout.root(),
        live_test_config_with_live_limits(4, 1, LiveQueueFullPolicy::Block),
    )
    .expect("open live engine");

    let mut replay_filter = live.filter();
    replay_filter.match_unit("alpha.service");
    let mut options = SubscriptionOptions::new(replay_filter);
    options.since_realtime(0);
    let alpha = live
        .subscribe_with_options(options)
        .expect("subscribe replaying alpha");
    let beta = subscribe_unit(&mut live, "beta.service");

    write_synthetic_journal_file(&later, &["gamma.service", "beta.service"], 9);

    assert_eq!(live.poll_once().expect("dispatch ready live entry"), 1);
    assert_entry(
        &recv_ready(&beta),
        "beta.service",
        &synthetic_message(9, 1, "beta.service"),
    );
    assert_subscription_empty(&alpha);

    assert_eq!(live.poll_once().expect("dispatch alpha replay"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 0, "alpha.service"),
    );
}

#[test]
fn replay_limit_counts_matching_entries_not_unrelated_entries() {
    let layout = SyntheticJournalFile::new(&["beta.service", "alpha.service"]);
    let mut cfg = live_test_config_with_live_limits(1, 1, LiveQueueFullPolicy::Block);
    cfg.max_live_replay_entries = 1;
    let mut live = LiveJournal::open_dir_with_config(layout.root(), cfg).expect("open live engine");

    let mut filter = live.filter();
    filter.match_unit("alpha.service");
    let mut options = SubscriptionOptions::new(filter);
    options.since_realtime(0);
    let alpha = live
        .subscribe_with_options(options)
        .expect("subscribe alpha replay");

    assert_eq!(live.poll_once().expect("single matching replay batch"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );
}

#[test]
fn live_replay_works_with_low_memory_journal_snapshot() {
    let layout = SyntheticJournalFile::new(&["alpha.service"]);
    write_synthetic_journal_file(
        &layout.root().join("later.journal"),
        &["beta.service", "alpha.service"],
        9,
    );
    let mut cfg = live_test_config_with_live_limits(1, 1, LiveQueueFullPolicy::Block);
    cfg.max_open_files = 1;
    cfg.mmap_policy = MmapPolicy::Never;
    let mut live =
        LiveJournal::open_dir_with_config(layout.root(), cfg).expect("open low-memory live engine");

    let mut filter = live.filter();
    filter.match_unit("alpha.service");
    let mut options = SubscriptionOptions::new(filter);
    options.since_realtime(0);
    let alpha = live
        .subscribe_with_options(options)
        .expect("subscribe with low-memory replay");

    assert_eq!(live.poll_once().expect("first low-memory replay batch"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(7, 0, "alpha.service"),
    );

    assert_eq!(live.poll_once().expect("second low-memory replay batch"), 1);
    assert_entry(
        &recv_ready(&alpha),
        "alpha.service",
        &synthetic_message(9, 1, "alpha.service"),
    );
}

#[test]
fn live_engine_skips_corrupt_tilde_journal_and_tracks_healthy_files() {
    let initial_units = ["alpha.service"];
    let layout = SyntheticJournalFile::new(&initial_units);
    write_corrupt_tilde_journal(layout.root());

    let journal = Journal::open_dir_with_config(layout.root(), live_test_config_with_tilde())
        .expect("open journal set containing a corrupt tilde file");
    let mut live = journal
        .live()
        .expect("live engine should skip the corrupt tilde file");

    let alpha = subscribe_unit(&mut live, "alpha.service");
    assert_subscription_empty(&alpha);

    let rewritten_units = ["alpha.service", "alpha.service"];
    layout.rewrite(&rewritten_units);

    let deliveries = poll_until_delivered(&mut live, 1);
    assert_eq!(deliveries, 1);

    let alpha_entry = recv_ready(&alpha);
    assert_entry(
        &alpha_entry,
        "alpha.service",
        &synthetic_message(7, 1, "alpha.service"),
    );
}

#[test]
fn live_engine_fails_when_all_journal_files_are_untrackable() {
    let layout = SyntheticJournalFile::new(&["alpha.service"]);
    write_corrupt_tilde_journal(layout.root());
    fs::remove_file(layout.root().join("synthetic.journal")).expect("remove healthy journal");

    let journal = Journal::open_dir_with_config(layout.root(), live_test_config_with_tilde())
        .expect("open corrupt journal header");

    match journal.live() {
        Err(sdjournal::SdJournalError::Corrupt { .. }) => {}
        Err(sdjournal::SdJournalError::Transient { .. }) => {}
        Err(err) => panic!("unexpected live error: {err}"),
        Ok(_) => panic!("expected live engine to fail without any trackable journal files"),
    }
}

fn live_test_config() -> JournalConfig {
    JournalConfig {
        poll_interval: Duration::from_millis(10),
        ..JournalConfig::default()
    }
}

fn live_test_config_with_tilde() -> JournalConfig {
    JournalConfig {
        include_journal_tilde: true,
        ..live_test_config()
    }
}

fn live_test_config_with_live_limits(
    live_channel_capacity: usize,
    max_live_batch_entries: usize,
    live_queue_full_policy: LiveQueueFullPolicy,
) -> JournalConfig {
    JournalConfig {
        live_channel_capacity,
        max_live_batch_entries,
        live_queue_full_policy,
        ..live_test_config()
    }
}

fn write_corrupt_tilde_journal(root: &Path) {
    let mut bytes = fs::read(root.join("synthetic.journal")).expect("read synthetic journal");

    bytes[24] = bytes[24].wrapping_add(101);
    bytes[288] = 0;

    fs::write(root.join("corrupt.journal~"), bytes).expect("write corrupt tilde journal");
}

fn subscribe_unit(live: &mut LiveJournal, unit: &str) -> LiveSubscription {
    let mut filter = live.filter();
    filter.match_unit(unit);
    live.subscribe(filter).expect("subscribe unit")
}

fn subscribe_message(live: &mut LiveJournal, message: &str) -> LiveSubscription {
    let mut filter = live.filter();
    filter.match_exact("MESSAGE", message.as_bytes());
    live.subscribe(filter).expect("subscribe message")
}

fn poll_until_delivered(live: &mut LiveJournal, expected: usize) -> usize {
    let mut delivered = 0usize;
    for _ in 0..10 {
        delivered = delivered.saturating_add(live.poll_once().expect("poll live engine"));
        if delivered >= expected {
            break;
        }
        thread::sleep(Duration::from_millis(10));
    }
    delivered
}

fn recv_ready(subscription: &LiveSubscription) -> sdjournal::LiveEntry {
    subscription
        .try_recv()
        .expect("subscription should have one ready item")
        .expect("live entry should decode")
}

fn assert_entry(entry: &sdjournal::LiveEntry, unit: &str, message: &str) {
    assert_eq!(field(entry, "_SYSTEMD_UNIT"), unit);
    assert_eq!(field(entry, "MESSAGE"), message);
}

fn field(entry: &sdjournal::LiveEntry, name: &str) -> String {
    String::from_utf8_lossy(entry.get(name).expect("entry field")).into_owned()
}

fn assert_subscription_empty(subscription: &LiveSubscription) {
    match subscription.try_recv() {
        Err(TryRecvError::Empty) => {}
        Err(TryRecvError::Disconnected) => panic!("subscription disconnected"),
        Ok(Ok(entry)) => panic!(
            "subscription unexpectedly received MESSAGE={}",
            String::from_utf8_lossy(entry.get("MESSAGE").unwrap_or(b"<missing>"))
        ),
        Ok(Err(err)) => panic!("subscription unexpectedly received error: {err}"),
    }
}