shardlog 0.2.0

Embedded crash-recoverable append log with durable extent packs
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use std::collections::HashSet;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use crate::{
    BatchId, LogicalOffset, LogicalPartitionId, PlacementSequence, RecordId, RingEpoch, ShardId,
    TopicId, TopicPartition,
};

use crate::codec::{Decoder, push_u8, push_u32, push_u64, push_u128};
use crate::{StorageError, StorageResult};

const JOURNAL_MAGIC: &[u8; 4] = b"SSJ3";
const JOURNAL_VERSION: u8 = 3;
const PREFIX_LEN: usize = 4 + 1 + 1 + 2 + 4;
const CRC_LEN: usize = 4;
const MAX_EVENT_BYTES: usize = 1024 * 1024;

const CONFIGURE_KIND: u8 = 1;
const TRUNCATE_KIND: u8 = 6;
const GROUP_COMMIT_KIND: u8 = 7;
const GROUP_ABORT_KIND: u8 = 8;
const CREATE_PARTITION_KIND: u8 = 9;
const UPDATE_REPLICATION_POLICY_KIND: u8 = 10;
const TRUNCATE_CHECKPOINT_KIND: u8 = 11;

pub const DIGEST_XXH3_128: u8 = 1;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProducerSequence {
    pub producer_id: u128,
    pub epoch: u32,
    pub first_sequence: u64,
    pub event_id: Option<RecordId>,
    pub digest_algorithm: u8,
    pub payload_digest: [u8; 16],
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AtomicGroupSequence {
    pub transaction_id: u128,
    pub chunk_sequence: u64,
    pub payload_digest: [u8; 16],
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JournalEvent {
    /// Legacy partition configuration record. New partitions use
    /// `CreatePartition`, which atomically persists their durability policy.
    Configure {
        topic_partition: TopicPartition,
        ring_epoch: RingEpoch,
        shards: Vec<ShardId>,
    },
    CreatePartition {
        topic_partition: TopicPartition,
        ring_epoch: RingEpoch,
        shards: Vec<ShardId>,
        policy_generation: u64,
        replication_factor: u32,
        min_in_sync_replicas: u32,
    },
    UpdateReplicationPolicy {
        topic_partition: TopicPartition,
        expected_policy_generation: u64,
        policy_generation: u64,
        replication_factor: u32,
        min_in_sync_replicas: u32,
    },
    Truncate {
        topic_partition: TopicPartition,
        log_start: LogicalOffset,
    },
    TruncateCheckpoint {
        topic_partition: TopicPartition,
        log_start: LogicalOffset,
        next_batch_id: BatchId,
        next_offset: LogicalOffset,
        next_placement_sequence: PlacementSequence,
    },
    GroupCommit {
        topic_partition: TopicPartition,
        transaction_id: u128,
    },
    GroupAbort {
        topic_partition: TopicPartition,
        transaction_id: u128,
    },
}

#[derive(Debug, Clone, Default)]
pub struct RecoveredJournal {
    pub events: Vec<JournalEvent>,
    pub repaired_partial_tail: bool,
}

#[derive(Debug)]
pub struct CoordinatorJournal {
    path: PathBuf,
    file: File,
}

impl CoordinatorJournal {
    pub fn open(path: impl AsRef<Path>) -> StorageResult<(Self, RecoveredJournal)> {
        let path = path.as_ref().to_path_buf();
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let mut file = OpenOptions::new()
            .create(true)
            .truncate(false)
            .read(true)
            .write(true)
            .open(&path)?;
        let recovered = scan_and_repair(&path, &mut file)?;
        file.seek(SeekFrom::End(0))?;
        Ok((Self { path, file }, recovered))
    }

    pub fn append(&mut self, event: &JournalEvent, sync: bool) -> StorageResult<()> {
        let encoded = encode_event(event)?;
        let start = self.file.seek(SeekFrom::End(0))?;
        if let Err(error) = self.file.write_all(&encoded) {
            let _ = self.file.set_len(start);
            return Err(error.into());
        }
        self.file.flush()?;
        if sync {
            self.file.sync_data()?;
        }
        Ok(())
    }

    pub fn sync(&mut self) -> StorageResult<()> {
        self.file.flush()?;
        self.file.sync_data()?;
        Ok(())
    }

    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }
}

fn scan_and_repair(path: &Path, file: &mut File) -> StorageResult<RecoveredJournal> {
    let file_len = file.metadata()?.len();
    let mut offset = 0u64;
    let mut events = Vec::new();
    let mut repaired_partial_tail = false;
    while offset < file_len {
        if file_len - offset < PREFIX_LEN as u64 {
            file.set_len(offset)?;
            repaired_partial_tail = true;
            break;
        }
        file.seek(SeekFrom::Start(offset))?;
        let mut prefix = [0u8; PREFIX_LEN];
        file.read_exact(&mut prefix)?;
        validate_prefix(path, offset, &prefix)?;
        let body_len = u32::from_le_bytes(prefix[8..12].try_into().expect("body length")) as usize;
        if body_len > MAX_EVENT_BYTES {
            return Err(StorageError::corrupt(
                path,
                offset,
                "journal event exceeds maximum size",
            ));
        }
        let frame_len = PREFIX_LEN
            .checked_add(body_len)
            .and_then(|value| value.checked_add(CRC_LEN))
            .ok_or_else(|| StorageError::corrupt(path, offset, "journal length overflow"))?;
        if offset
            .checked_add(frame_len as u64)
            .is_none_or(|end| end > file_len)
        {
            file.set_len(offset)?;
            repaired_partial_tail = true;
            break;
        }
        let mut frame = vec![0u8; frame_len];
        frame[..PREFIX_LEN].copy_from_slice(&prefix);
        file.read_exact(&mut frame[PREFIX_LEN..])?;
        let expected_crc =
            u32::from_le_bytes(frame[frame_len - CRC_LEN..].try_into().expect("CRC width"));
        if crc32fast::hash(&frame[..frame_len - CRC_LEN]) != expected_crc {
            return Err(StorageError::corrupt(
                path,
                offset,
                "journal checksum mismatch",
            ));
        }
        let kind = prefix[5];
        events.push(decode_event(
            path,
            offset,
            kind,
            &frame[PREFIX_LEN..frame_len - CRC_LEN],
        )?);
        offset += frame_len as u64;
    }
    Ok(RecoveredJournal {
        events,
        repaired_partial_tail,
    })
}

fn encode_event(event: &JournalEvent) -> StorageResult<Vec<u8>> {
    let (kind, body) = match event {
        JournalEvent::Configure {
            topic_partition,
            ring_epoch,
            shards,
        } => {
            if shards.is_empty() {
                return Err(StorageError::InvalidInput(
                    "configured shard ring cannot be empty".into(),
                ));
            }
            let shard_count = u32::try_from(shards.len())
                .map_err(|_| StorageError::LimitExceeded("too many shards".into()))?;
            let mut body = Vec::with_capacity(16 + 4 + 8 + 4 + shards.len() * 4);
            push_topic_partition(&mut body, *topic_partition);
            push_u64(&mut body, ring_epoch.get());
            push_u32(&mut body, shard_count);
            for shard in shards {
                push_u32(&mut body, shard.get());
            }
            (CONFIGURE_KIND, body)
        }
        JournalEvent::CreatePartition {
            topic_partition,
            ring_epoch,
            shards,
            policy_generation,
            replication_factor,
            min_in_sync_replicas,
        } => {
            validate_replication_policy(
                *policy_generation,
                *replication_factor,
                *min_in_sync_replicas,
            )?;
            if shards.is_empty() {
                return Err(StorageError::InvalidInput(
                    "configured shard ring cannot be empty".into(),
                ));
            }
            let shard_count = u32::try_from(shards.len())
                .map_err(|_| StorageError::LimitExceeded("too many shards".into()))?;
            let mut body = Vec::with_capacity(16 + 4 + 8 + 4 + shards.len() * 4 + 8 + 4 + 4);
            push_topic_partition(&mut body, *topic_partition);
            push_u64(&mut body, ring_epoch.get());
            push_u32(&mut body, shard_count);
            for shard in shards {
                push_u32(&mut body, shard.get());
            }
            push_u64(&mut body, *policy_generation);
            push_u32(&mut body, *replication_factor);
            push_u32(&mut body, *min_in_sync_replicas);
            (CREATE_PARTITION_KIND, body)
        }
        JournalEvent::UpdateReplicationPolicy {
            topic_partition,
            expected_policy_generation,
            policy_generation,
            replication_factor,
            min_in_sync_replicas,
        } => {
            validate_replication_policy(
                *policy_generation,
                *replication_factor,
                *min_in_sync_replicas,
            )?;
            if *expected_policy_generation == 0 || *policy_generation <= *expected_policy_generation
            {
                return Err(StorageError::InvalidInput(
                    "replication policy generation must advance".into(),
                ));
            }
            let mut body = Vec::with_capacity(16 + 4 + 8 + 8 + 4 + 4);
            push_topic_partition(&mut body, *topic_partition);
            push_u64(&mut body, *expected_policy_generation);
            push_u64(&mut body, *policy_generation);
            push_u32(&mut body, *replication_factor);
            push_u32(&mut body, *min_in_sync_replicas);
            (UPDATE_REPLICATION_POLICY_KIND, body)
        }
        JournalEvent::Truncate {
            topic_partition,
            log_start,
        } => {
            let mut body = Vec::with_capacity(28);
            push_topic_partition(&mut body, *topic_partition);
            push_u64(&mut body, log_start.get());
            (TRUNCATE_KIND, body)
        }
        JournalEvent::TruncateCheckpoint {
            topic_partition,
            log_start,
            next_batch_id,
            next_offset,
            next_placement_sequence,
        } => {
            if next_offset < log_start
                || next_batch_id.get() != u128::from(next_placement_sequence.get())
            {
                return Err(StorageError::InvalidInput(
                    "truncate checkpoint sequencer boundary is invalid".into(),
                ));
            }
            let mut body = Vec::with_capacity(60);
            push_topic_partition(&mut body, *topic_partition);
            push_u64(&mut body, log_start.get());
            push_u128(&mut body, next_batch_id.get());
            push_u64(&mut body, next_offset.get());
            push_u64(&mut body, next_placement_sequence.get());
            (TRUNCATE_CHECKPOINT_KIND, body)
        }
        JournalEvent::GroupCommit {
            topic_partition,
            transaction_id,
        } => (
            GROUP_COMMIT_KIND,
            encode_group_marker(*topic_partition, *transaction_id),
        ),
        JournalEvent::GroupAbort {
            topic_partition,
            transaction_id,
        } => (
            GROUP_ABORT_KIND,
            encode_group_marker(*topic_partition, *transaction_id),
        ),
    };
    if body.len() > MAX_EVENT_BYTES {
        return Err(StorageError::LimitExceeded(
            "journal event exceeds maximum size".into(),
        ));
    }
    let body_len = u32::try_from(body.len())
        .map_err(|_| StorageError::LimitExceeded("journal body exceeds u32".into()))?;
    let mut frame = Vec::with_capacity(PREFIX_LEN + body.len() + CRC_LEN);
    frame.extend_from_slice(JOURNAL_MAGIC);
    push_u8(&mut frame, JOURNAL_VERSION);
    push_u8(&mut frame, kind);
    frame.extend_from_slice(&0u16.to_le_bytes());
    push_u32(&mut frame, body_len);
    frame.extend_from_slice(&body);
    let checksum = crc32fast::hash(&frame);
    push_u32(&mut frame, checksum);
    Ok(frame)
}

fn decode_event(path: &Path, offset: u64, kind: u8, body: &[u8]) -> StorageResult<JournalEvent> {
    let mut decoder = Decoder::new(body);
    let event = match kind {
        CONFIGURE_KIND => {
            let topic_partition = decode_topic_partition(&mut decoder)?;
            let ring_epoch = RingEpoch::new(decoder.u64()?);
            let shards = decode_shards(path, offset, &mut decoder)?;
            JournalEvent::Configure {
                topic_partition,
                ring_epoch,
                shards,
            }
        }
        CREATE_PARTITION_KIND => {
            let topic_partition = decode_topic_partition(&mut decoder)?;
            let ring_epoch = RingEpoch::new(decoder.u64()?);
            let shards = decode_shards(path, offset, &mut decoder)?;
            let policy_generation = decoder.u64()?;
            let replication_factor = decoder.u32()?;
            let min_in_sync_replicas = decoder.u32()?;
            validate_decoded_replication_policy(
                path,
                offset,
                policy_generation,
                replication_factor,
                min_in_sync_replicas,
            )?;
            JournalEvent::CreatePartition {
                topic_partition,
                ring_epoch,
                shards,
                policy_generation,
                replication_factor,
                min_in_sync_replicas,
            }
        }
        UPDATE_REPLICATION_POLICY_KIND => {
            let topic_partition = decode_topic_partition(&mut decoder)?;
            let expected_policy_generation = decoder.u64()?;
            let policy_generation = decoder.u64()?;
            let replication_factor = decoder.u32()?;
            let min_in_sync_replicas = decoder.u32()?;
            validate_decoded_replication_policy(
                path,
                offset,
                policy_generation,
                replication_factor,
                min_in_sync_replicas,
            )?;
            if expected_policy_generation == 0 || policy_generation <= expected_policy_generation {
                return Err(StorageError::corrupt(
                    path,
                    offset,
                    "replication policy generation does not advance",
                ));
            }
            JournalEvent::UpdateReplicationPolicy {
                topic_partition,
                expected_policy_generation,
                policy_generation,
                replication_factor,
                min_in_sync_replicas,
            }
        }
        TRUNCATE_KIND => JournalEvent::Truncate {
            topic_partition: decode_topic_partition(&mut decoder)?,
            log_start: LogicalOffset::new(decoder.u64()?),
        },
        TRUNCATE_CHECKPOINT_KIND => {
            let topic_partition = decode_topic_partition(&mut decoder)?;
            let log_start = LogicalOffset::new(decoder.u64()?);
            let next_batch_id = BatchId::new(decoder.u128()?);
            let next_offset = LogicalOffset::new(decoder.u64()?);
            let next_placement_sequence = PlacementSequence::new(decoder.u64()?);
            if next_offset < log_start
                || next_batch_id.get() != u128::from(next_placement_sequence.get())
            {
                return Err(StorageError::corrupt(
                    path,
                    offset,
                    "truncate checkpoint sequencer boundary is invalid",
                ));
            }
            JournalEvent::TruncateCheckpoint {
                topic_partition,
                log_start,
                next_batch_id,
                next_offset,
                next_placement_sequence,
            }
        }
        GROUP_COMMIT_KIND => JournalEvent::GroupCommit {
            topic_partition: decode_topic_partition(&mut decoder)?,
            transaction_id: decoder.u128()?,
        },
        GROUP_ABORT_KIND => JournalEvent::GroupAbort {
            topic_partition: decode_topic_partition(&mut decoder)?,
            transaction_id: decoder.u128()?,
        },
        _ => {
            return Err(StorageError::corrupt(
                path,
                offset,
                format!("unknown journal event kind {kind}"),
            ));
        }
    };
    if !decoder.is_empty() {
        return Err(StorageError::corrupt(
            path,
            offset,
            "journal event has trailing bytes",
        ));
    }
    Ok(event)
}

fn encode_group_marker(topic_partition: TopicPartition, transaction_id: u128) -> Vec<u8> {
    let mut body = Vec::with_capacity(36);
    push_topic_partition(&mut body, topic_partition);
    push_u128(&mut body, transaction_id);
    body
}

fn push_topic_partition(bytes: &mut Vec<u8>, topic_partition: TopicPartition) {
    push_u128(bytes, topic_partition.topic_id.get());
    push_u32(bytes, topic_partition.partition_id.get());
}

fn decode_topic_partition(decoder: &mut Decoder<'_>) -> StorageResult<TopicPartition> {
    Ok(TopicPartition::new(
        TopicId::new(decoder.u128()?),
        LogicalPartitionId::new(decoder.u32()?),
    ))
}

fn decode_shards(
    path: &Path,
    offset: u64,
    decoder: &mut Decoder<'_>,
) -> StorageResult<Vec<ShardId>> {
    let shard_count = usize::try_from(decoder.u32()?)
        .map_err(|_| StorageError::corrupt(path, offset, "shard count overflow"))?;
    if shard_count == 0 {
        return Err(StorageError::corrupt(
            path,
            offset,
            "empty configured shard ring",
        ));
    }
    let mut shards = Vec::with_capacity(shard_count);
    let mut unique = HashSet::with_capacity(shard_count);
    for _ in 0..shard_count {
        let shard = ShardId::new(decoder.u32()?);
        if !unique.insert(shard) {
            return Err(StorageError::corrupt(
                path,
                offset,
                "duplicate configured shard",
            ));
        }
        shards.push(shard);
    }
    Ok(shards)
}

fn validate_replication_policy(
    policy_generation: u64,
    replication_factor: u32,
    min_in_sync_replicas: u32,
) -> StorageResult<()> {
    if policy_generation == 0 {
        return Err(StorageError::InvalidInput(
            "policy generation must be nonzero".into(),
        ));
    }
    if replication_factor == 0
        || replication_factor > 64
        || min_in_sync_replicas == 0
        || min_in_sync_replicas > replication_factor
    {
        return Err(StorageError::InvalidInput(
            "invalid replication policy".into(),
        ));
    }
    Ok(())
}

fn validate_decoded_replication_policy(
    path: &Path,
    offset: u64,
    policy_generation: u64,
    replication_factor: u32,
    min_in_sync_replicas: u32,
) -> StorageResult<()> {
    validate_replication_policy(policy_generation, replication_factor, min_in_sync_replicas)
        .map_err(|_| StorageError::corrupt(path, offset, "invalid replication policy"))
}

fn validate_prefix(path: &Path, offset: u64, prefix: &[u8; PREFIX_LEN]) -> StorageResult<()> {
    if &prefix[..4] != JOURNAL_MAGIC {
        return Err(StorageError::corrupt(path, offset, "invalid journal magic"));
    }
    if prefix[4] != JOURNAL_VERSION {
        return Err(StorageError::corrupt(
            path,
            offset,
            format!("unsupported journal version {}", prefix[4]),
        ));
    }
    if prefix[6..8] != [0, 0] {
        return Err(StorageError::corrupt(
            path,
            offset,
            "journal reserved bytes are nonzero",
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::time::{SystemTime, UNIX_EPOCH};

    use super::*;

    struct TempFile(PathBuf);

    impl TempFile {
        fn new(name: &str) -> Self {
            let nonce = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("clock")
                .as_nanos();
            Self(std::env::temp_dir().join(format!(
                "shard-stream-journal-{name}-{}-{nonce}.ssj",
                std::process::id()
            )))
        }
    }

    impl Drop for TempFile {
        fn drop(&mut self) {
            let _ = fs::remove_file(&self.0);
        }
    }

    #[test]
    fn round_trips_every_event_and_repairs_partial_tail() {
        let temp = TempFile::new("roundtrip");
        let events = vec![
            JournalEvent::CreatePartition {
                topic_partition: TopicPartition::new(TopicId::new(8), LogicalPartitionId::new(1)),
                ring_epoch: RingEpoch::new(1),
                shards: vec![ShardId::new(0)],
                policy_generation: 3,
                replication_factor: 3,
                min_in_sync_replicas: 2,
            },
            JournalEvent::Configure {
                topic_partition: TopicPartition::new(TopicId::new(9), LogicalPartitionId::new(2)),
                ring_epoch: RingEpoch::new(7),
                shards: vec![ShardId::new(1), ShardId::new(2)],
            },
            JournalEvent::UpdateReplicationPolicy {
                topic_partition: TopicPartition::new(TopicId::new(8), LogicalPartitionId::new(1)),
                expected_policy_generation: 3,
                policy_generation: 4,
                replication_factor: 2,
                min_in_sync_replicas: 2,
            },
            JournalEvent::Truncate {
                topic_partition: TopicPartition::new(TopicId::new(9), LogicalPartitionId::new(2)),
                log_start: LogicalOffset::new(13),
            },
            JournalEvent::TruncateCheckpoint {
                topic_partition: TopicPartition::new(TopicId::new(9), LogicalPartitionId::new(2)),
                log_start: LogicalOffset::new(13),
                next_batch_id: BatchId::new(7),
                next_offset: LogicalOffset::new(13),
                next_placement_sequence: PlacementSequence::new(7),
            },
            JournalEvent::GroupCommit {
                topic_partition: TopicPartition::new(TopicId::new(9), LogicalPartitionId::new(2)),
                transaction_id: 14,
            },
            JournalEvent::GroupAbort {
                topic_partition: TopicPartition::new(TopicId::new(9), LogicalPartitionId::new(2)),
                transaction_id: 15,
            },
        ];
        {
            let (mut journal, recovered) = CoordinatorJournal::open(&temp.0).expect("open journal");
            assert!(recovered.events.is_empty());
            for event in &events {
                journal.append(event, true).expect("append");
            }
        }
        OpenOptions::new()
            .append(true)
            .open(&temp.0)
            .expect("open tail")
            .write_all(b"SS")
            .expect("partial tail");

        let (_, recovered) = CoordinatorJournal::open(&temp.0).expect("recover");
        assert_eq!(recovered.events, events);
        assert!(recovered.repaired_partial_tail);
    }

    #[test]
    fn checksum_corruption_is_an_error() {
        let temp = TempFile::new("crc");
        {
            let (mut journal, _) = CoordinatorJournal::open(&temp.0).expect("open");
            journal
                .append(
                    &JournalEvent::Configure {
                        topic_partition: TopicPartition::new(
                            TopicId::new(9),
                            LogicalPartitionId::new(2),
                        ),
                        ring_epoch: RingEpoch::new(7),
                        shards: vec![ShardId::new(1), ShardId::new(2)],
                    },
                    true,
                )
                .expect("append");
        }
        let mut file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(&temp.0)
            .expect("open corrupt");
        file.seek(SeekFrom::Start(PREFIX_LEN as u64 + 1))
            .expect("seek");
        file.write_all(b"X").expect("corrupt");
        file.sync_data().expect("sync");

        assert!(matches!(
            CoordinatorJournal::open(&temp.0),
            Err(StorageError::Corrupt { .. })
        ));
    }
}