-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathcommitlog.rs
More file actions
1434 lines (1278 loc) · 48.3 KB
/
commitlog.rs
File metadata and controls
1434 lines (1278 loc) · 48.3 KB
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{
fmt::Debug,
io,
marker::PhantomData,
mem,
ops::{Range, RangeBounds},
vec,
};
use itertools::Itertools;
use log::{debug, error, info, trace, warn};
use crate::{
commit::StoredCommit,
error::{self, source_chain},
index::IndexError,
payload::Decoder,
repo::{self, Repo, SegmentLen as _, TxOffsetIndex},
segment::{self, FileLike, Transaction, Writer},
Commit, Encode, Options, DEFAULT_LOG_FORMAT_VERSION,
};
pub use crate::segment::Committed;
/// A commitlog generic over the storage backend as well as the type of records
/// its [`Commit`]s contain.
#[derive(Debug)]
pub struct Generic<R: Repo, T> {
/// The storage backend.
pub(crate) repo: R,
/// The segment currently being written to.
///
/// If we squint, all segments in a log are a non-empty linked list, the
/// head of which is the segment open for writing.
pub(crate) head: Writer<R::SegmentWriter>,
/// The tail of the non-empty list of segments.
///
/// We only retain the min transaction offset of each, from which the
/// segments can be opened for reading when needed.
///
/// This is a `Vec`, not a linked list, so the last element is the newest
/// segment (after `head`).
tail: Vec<u64>,
/// Configuration options.
opts: Options,
/// Type of a single record in this log's [`Commit::records`].
_record: PhantomData<T>,
/// Tracks panics/errors to control what happens on drop.
///
/// Set to `true` before any I/O operation, and back to `false` after it
/// succeeded. This way, we won't try to perform I/O on drop when it is
/// unlikely to succeed, or even has a chance to panic.
panicked: bool,
}
impl<R: Repo, T> Generic<R, T> {
pub fn open(repo: R, opts: Options) -> io::Result<Self> {
let mut tail = repo.existing_offsets()?;
if !tail.is_empty() {
debug!("segments: {tail:?}");
}
// Resume the last segment for writing, or
// create a new segment starting from the last good commit + 1.
let head = loop {
if let Some(last) = tail.pop() {
info!("repo {}: resuming last segment: {}", repo, last);
match repo::resume_segment_writer(&repo, opts, last)? {
repo::ResumedSegment::Empty => {
repo.remove_segment(last)?;
continue;
}
repo::ResumedSegment::Resumed(writer) => break writer,
repo::ResumedSegment::Sealed(meta) | repo::ResumedSegment::Corrupted(meta) => {
tail.push(meta.tx_range.start);
break repo::create_segment_writer(&repo, opts, meta.max_epoch, meta.tx_range.end)?;
}
}
} else {
info!("repo {}: starting fresh log", repo);
break repo::create_segment_writer(&repo, opts, Commit::DEFAULT_EPOCH, 0)?;
}
};
Ok(Self {
repo,
head,
tail,
opts,
_record: PhantomData,
panicked: false,
})
}
/// Get the current epoch.
///
/// See also: [`Commit::epoch`].
pub fn epoch(&self) -> u64 {
self.head.commit.epoch
}
/// Update the current epoch.
///
/// Does nothing if the given `epoch` is equal to the current epoch.
///
/// # Errors
///
/// If `epoch` is smaller than the current epoch, an error of kind
/// [`io::ErrorKind::InvalidInput`] is returned.
pub fn set_epoch(&mut self, epoch: u64) -> io::Result<()> {
if epoch < self.head.epoch() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"new epoch is smaller than current epoch",
));
}
self.head.set_epoch(epoch);
Ok(())
}
/// Force the currently active segment to be flushed to storage.
///
/// Using a filesystem backend, this means to call `fsync(2)`.
///
/// **Note** that this does not flush the buffered data from calls to
/// [Self::commit], it only instructs the underlying storage to flush its
/// buffers. Call [Self::flush] prior to this method to ensure data from
/// all previous [Self::commit] calls is flushed to the underlying storage.
///
/// # Panics
///
/// As an `fsync` failure leaves a file in a more of less undefined state,
/// this method panics in this case, thereby preventing any further writes
/// to the log and forcing the user to re-read the state from disk.
pub fn sync(&mut self) {
self.panicked = true;
if let Err(e) = self.head.fsync() {
panic!("Failed to fsync segment: {e}");
}
self.panicked = false;
}
/// Flush the buffered data from previous calls to [Self::commit] to the
/// underlying storage.
///
/// Call [Self::sync] to instruct the underlying storage to flush its
/// buffers as well.
pub fn flush(&mut self) -> io::Result<()> {
self.head.flush()
}
/// Calls [Self::flush] and then [Self::sync].
fn flush_and_sync(&mut self) -> io::Result<()> {
self.flush()?;
self.sync();
Ok(())
}
/// The last transaction offset written to disk, or `None` if nothing has
/// been written yet.
///
/// Note that this does not imply durability: [`Self::sync`] may not have
/// been called at this offset.
pub fn max_committed_offset(&self) -> Option<u64> {
// Naming is hard: the segment's `next_tx_offset` indicates how many
// txs are already in the log (it's the next commit's min-tx-offset).
// If the value is zero, however, the initial commit hasn't been
// committed yet.
self.head.next_tx_offset().checked_sub(1)
}
/// The first transaction offset written to disk, or `None` if nothing has
/// been written yet.
pub fn min_committed_offset(&self) -> Option<u64> {
self.tail
.first()
.copied()
.or_else(|| (!self.head.is_empty()).then(|| self.head.min_tx_offset()))
}
// Helper to obtain a list of the segment offsets which include transaction
// offset `offset`.
//
// The returned `Vec` is sorted in **ascending** order, such that the first
// element is the segment which contains `offset`.
//
// The offset of `self.head` is always included, regardless of how many
// entries it actually contains.
fn segment_offsets_from(&self, offset: u64) -> Vec<u64> {
if offset >= self.head.min_tx_offset {
vec![self.head.min_tx_offset]
} else {
let mut offs = Vec::with_capacity(self.tail.len() + 1);
if let Some(pos) = self.tail.iter().rposition(|off| off <= &offset) {
offs.extend_from_slice(&self.tail[pos..]);
offs.push(self.head.min_tx_offset);
}
offs
}
}
pub fn commits_from(&self, offset: u64) -> Commits<R> {
let offsets = self.segment_offsets_from(offset);
let segments = Segments {
offs: offsets.into_iter(),
repo: self.repo.clone(),
max_log_format_version: self.opts.log_format_version,
};
Commits {
inner: None,
segments,
last_commit: CommitInfo::Initial { next_offset: offset },
last_error: None,
}
}
pub fn reset(mut self) -> io::Result<Self> {
info!("hard reset");
self.panicked = true;
self.tail.reserve(1);
self.tail.push(self.head.min_tx_offset);
for segment in self.tail.iter().rev() {
debug!("removing segment {segment}");
self.repo.remove_segment(*segment)?;
}
// Prevent finalizer from running by not updating self.panicked.
Self::open(self.repo.clone(), self.opts)
}
pub fn reset_to(mut self, offset: u64) -> io::Result<Self> {
info!("reset to {offset}");
self.panicked = true;
self.tail.reserve(1);
self.tail.push(self.head.min_tx_offset);
reset_to_internal(&self.repo, &self.tail, offset)?;
// Prevent finalizer from running by not updating self.panicked.
Self::open(self.repo.clone(), self.opts)
}
/// Start a new segment, preserving the current head's `Commit`.
///
/// The caller must ensure that the current head is synced to disk as
/// appropriate. It is not appropriate to sync after a write error, as that
/// is likely to return an error as well: the `Commit` will be written to
/// the new segment anyway.
fn start_new_segment(&mut self) -> io::Result<&mut Writer<R::SegmentWriter>> {
debug!(
"starting new segment offset={} prev-offset={}",
self.head.next_tx_offset(),
self.head.min_tx_offset()
);
let new = repo::create_segment_writer(&self.repo, self.opts, self.head.epoch(), self.head.next_tx_offset())?;
let old = mem::replace(&mut self.head, new);
self.tail.push(old.min_tx_offset());
self.head.commit = old.commit;
Ok(&mut self.head)
}
}
impl<R: Repo, T: Encode> Generic<R, T> {
/// Write `transactions` to the log.
///
/// This will store all `transactions` as a single [Commit]
/// (note that `transactions` must not yield more than [u16::MAX] elements).
///
/// Data is buffered by the underlying segment [Writer].
/// Call [Self::flush] to force flushing to the underlying storage.
///
/// If, after writing the transactions, the writer's total written bytes
/// exceed [Options::max_segment_size], the current segment is flushed,
/// `fsync`ed and closed, and a new segment is created.
///
/// Returns `Ok(None)` if `transactions` was empty, otherwise [Committed],
/// which contains the offset range and checksum of the commit.
///
/// Note that supplying empty `transactions` may cause the current segment
/// to be rotated.
///
/// # Errors
///
/// An `Err` value is returned in the following cases:
///
/// - if the transaction sequence is invalid, e.g. because the transaction
/// offsets are not contiguous.
///
/// In this case, **none** of the `transactions` will be written.
///
/// - if creating the new segment fails due to an I/O error.
///
/// # Panics
///
/// The method panics if:
///
/// - `transactions` exceeds [u16::MAX] elements
///
/// - [Self::flush] or writing to the underlying [Writer] fails
///
/// This is likely caused by some storage issue. As we cannot tell with
/// certainty how much data (if any) has been written, the internal state
/// becomes invalid and thus a panic is raised.
///
/// - [Self::sync] panics (called when rotating segments)
pub fn commit<U: Into<Transaction<T>>>(
&mut self,
transactions: impl IntoIterator<Item = U>,
) -> io::Result<Option<Committed>> {
self.panicked = true;
let writer = &mut self.head;
let committed = writer.commit(transactions)?;
if writer.len() >= self.opts.max_segment_size {
self.flush().expect("failed to flush segment upon rotation");
self.sync();
self.start_new_segment()?;
}
self.panicked = false;
Ok(committed)
}
pub fn transactions_from<'a, D>(
&self,
offset: u64,
decoder: &'a D,
) -> impl Iterator<Item = Result<Transaction<T>, D::Error>> + 'a + use<'a, D, R, T>
where
D: Decoder<Record = T>,
D::Error: From<error::Traversal>,
R: 'a,
T: 'a,
{
transactions_from_internal(self.commits_from(offset).with_log_format_version(), offset, decoder)
}
pub fn fold_transactions_from<D>(&self, offset: u64, decoder: D) -> Result<(), D::Error>
where
D: Decoder,
D::Error: From<error::Traversal>,
{
fold_transactions_internal(self.commits_from(offset).with_log_format_version(), decoder, offset..)
}
pub fn fold_transaction_range<D>(&self, range: impl RangeBounds<u64>, decoder: D) -> Result<(), D::Error>
where
D: Decoder,
D::Error: From<error::Traversal>,
{
use std::ops::Bound::*;
let start = match range.start_bound() {
Included(x) => *x,
Excluded(x) => x + 1,
Unbounded => 0,
};
fold_transactions_internal(self.commits_from(start).with_log_format_version(), decoder, range)
}
}
impl<R: Repo, T> Drop for Generic<R, T> {
fn drop(&mut self) {
if !self.panicked
&& let Err(e) = self.flush_and_sync()
{
error!("failed to commit on drop: {e}");
}
}
}
/// The most recent non empty segment in repo `R`.
///
/// Created by [open_newest_non_empty_segment].
struct MostRecentNonEmptySegment<R> {
/// Number of empty segments that were ignored.
empty_segments: usize,
/// Offset of the non-empty segment.
segment_offset: u64,
/// [Repo::SegmentReader] for the non-empty segment.
segment_reader: R,
}
/// Open the most recent segment in `repo` that is larger than
/// [segment::Header::LEN].
///
/// Note that there should be at most one empty segment in the log. We may,
/// however, want to be lenient on this read-only path, so the number of
/// empty segments is tracked in the returned type rather than returning an
/// error.
fn open_newest_non_empty_segment<R: Repo>(repo: R) -> io::Result<Option<MostRecentNonEmptySegment<R::SegmentReader>>> {
let mut segments = repo.existing_offsets()?;
let mut empty_segments = 0;
let mut segment_offset;
let mut segment_reader;
loop {
let Some(last) = segments.pop() else {
return Ok(None);
};
segment_offset = last;
segment_reader = repo.open_segment_reader(segment_offset)?;
if segment_reader.segment_len()? > segment::Header::LEN as u64 {
break;
} else {
empty_segments += 1;
}
}
Ok(Some(MostRecentNonEmptySegment {
empty_segments,
segment_offset,
segment_reader,
}))
}
/// The most recently written [segment::Metadata] for a given [Repo].
///
/// The type preserves the error information in case the most recent segment
/// contains corrupted data at the end (typically due to a torn write).
///
/// Created by [committed_meta].
pub enum CommittedMeta {
/// The most recent segment could not be traversed successfully until the
/// end, i.e. there is trailing garbage in the segment.
///
/// This variant is also returned in case [open_newest_non_empty_segment]
/// finds more than a single empty segment at the end of the log.
Prefix {
/// The metadata of the prefix that could be traversed successfully.
///
/// It is guaranteed that the metadata spans at least one commit.
metadata: segment::Metadata,
/// The error encountered.
error: io::Error,
},
/// The most recent segment could be traversed successfully until the end.
Complete {
/// The segment metadata.
///
/// It is guaranteed that the metadata spans at least one commit.
metadata: segment::Metadata,
},
}
impl CommittedMeta {
pub fn metadata(&self) -> &segment::Metadata {
let (Self::Prefix { metadata, .. } | Self::Complete { metadata }) = self;
metadata
}
fn extract(repo: impl Repo) -> io::Result<Option<Self>> {
let Some(MostRecentNonEmptySegment {
empty_segments,
segment_offset,
mut segment_reader,
}) = open_newest_non_empty_segment(&repo)?
else {
return Ok(None);
};
let offset_index = repo.get_offset_index(segment_offset).ok();
match segment::Metadata::extract(segment_offset, &mut segment_reader, offset_index.as_ref()) {
// Segment is intact.
Ok(metadata) if empty_segments <= 1 => {
assert!(
!metadata.tx_range.is_empty(),
"segment was promised to be non-empty but contains zero transactions"
);
Ok(Some(CommittedMeta::Complete { metadata }))
}
// Segment is good, but there are too many empty segments.
Ok(metadata) => Ok(Some(CommittedMeta::Prefix {
metadata,
error: io::Error::new(
io::ErrorKind::InvalidData,
format!("repo {}: too many empty segments: {}", repo, empty_segments),
),
})),
// Segment is non-empty, but first commit is corrupt.
Err(error::SegmentMetadata::InvalidCommit { sofar, source }) if sofar.tx_range.is_empty() => {
Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"repo {}: first commit in the most recent segment is corrupt: {}",
repo, source
),
))
}
// Some prefix of the segment is good.
Err(error::SegmentMetadata::InvalidCommit { sofar, source }) => Ok(Some(CommittedMeta::Prefix {
metadata: sofar,
error: source,
})),
// Something went wrong, including out-of-order errors and such.
Err(error::SegmentMetadata::Io(e)) => Err(e),
}
}
}
impl From<CommittedMeta> for segment::Metadata {
fn from(meta: CommittedMeta) -> Self {
let (CommittedMeta::Prefix { metadata, .. } | CommittedMeta::Complete { metadata }) = meta;
metadata
}
}
/// Extract the most recently written [CommittedMeta] from the commitlog
/// in `repo`.
///
/// Returns `None` if the commitlog is empty.
///
/// Note that this function validates the most recent segment, which entails
/// traversing it from the start.
///
/// The function can be used instead of the pattern:
///
/// ```ignore
/// let log = Commitlog::open(..)?;
/// let max_offset = log.max_committed_offset();
/// ```
///
/// like so:
///
/// ```ignore
/// let max_offset = committed_meta(..)?.map(|meta| meta.metadata().tx_range.end);
/// ```
///
/// Unlike `open`, no segment will be created in an empty `repo`.
pub fn committed_meta(repo: impl Repo) -> io::Result<Option<CommittedMeta>> {
CommittedMeta::extract(repo)
}
pub fn commits_from<R: Repo>(repo: R, max_log_format_version: u8, offset: u64) -> io::Result<Commits<R>> {
let mut offsets = repo.existing_offsets()?;
if let Some(pos) = offsets.iter().rposition(|&off| off <= offset) {
offsets = offsets.split_off(pos);
}
let segments = Segments {
offs: offsets.into_iter(),
repo,
max_log_format_version,
};
Ok(Commits {
inner: None,
segments,
last_commit: CommitInfo::Initial { next_offset: offset },
last_error: None,
})
}
pub fn transactions_from<'a, R, D, T>(
repo: R,
max_log_format_version: u8,
offset: u64,
de: &'a D,
) -> io::Result<impl Iterator<Item = Result<Transaction<T>, D::Error>> + 'a>
where
R: Repo + 'a,
D: Decoder<Record = T>,
D::Error: From<error::Traversal>,
T: 'a,
{
commits_from(repo, max_log_format_version, offset)
.map(|commits| transactions_from_internal(commits.with_log_format_version(), offset, de))
}
pub fn fold_transactions_from<R, D>(repo: R, max_log_format_version: u8, offset: u64, de: D) -> Result<(), D::Error>
where
R: Repo,
D: Decoder,
D::Error: From<error::Traversal> + From<io::Error>,
{
fold_transaction_range(repo, max_log_format_version, offset.., de)
}
pub fn fold_transaction_range<R, D>(
repo: R,
max_log_format_version: u8,
range: impl RangeBounds<u64>,
de: D,
) -> Result<(), D::Error>
where
R: Repo,
D: Decoder,
D::Error: From<error::Traversal> + From<io::Error>,
{
use std::ops::Bound::*;
let start = match range.start_bound() {
Included(x) => *x,
Excluded(x) => x + 1,
Unbounded => 0,
};
let commits = commits_from(repo, max_log_format_version, start)?;
fold_transactions_internal(commits.with_log_format_version(), de, range)
}
fn transactions_from_internal<'a, R, D, T>(
commits: CommitsWithVersion<R>,
offset: u64,
de: &'a D,
) -> impl Iterator<Item = Result<Transaction<T>, D::Error>> + 'a
where
R: Repo + 'a,
D: Decoder<Record = T>,
D::Error: From<error::Traversal>,
T: 'a,
{
commits
.map(|x| x.map_err(D::Error::from))
.map_ok(move |(version, commit)| commit.into_transactions(version, offset, de))
.flatten_ok()
.map(|x| x.and_then(|y| y))
}
fn fold_transactions_internal<R, D>(
mut commits: CommitsWithVersion<R>,
de: D,
range: impl RangeBounds<u64>,
) -> Result<(), D::Error>
where
R: Repo,
D: Decoder,
D::Error: From<error::Traversal>,
{
use std::ops::Bound::*;
// Avoid reading the first commit if it wouldn't be in the range anyway.
if range_is_empty(&range) {
return Ok(());
}
// `true` if `offset` is outside `range`, s.t. it is smaller than the start
// bound.
let before_start = |offset: &u64| match range.start_bound() {
Included(x) => offset < x,
Excluded(x) => offset <= x,
Unbounded => false,
};
// `true` if `offset` is outside `range`, s.t. it is greater than the end
// bound.
let past_end = |offset: &u64| match range.end_bound() {
Included(x) => offset > x,
Excluded(x) => offset >= x,
Unbounded => false,
};
while let Some(commit) = commits.next() {
let (version, commit) = match commit {
Ok(version_and_commit) => version_and_commit,
Err(e) => {
// Ignore it if the very last commit in the log is broken.
// The next `append` will fix the log, but the `decoder`
// has no way to tell whether we're at the end or not.
// This is unlike the consumer of an iterator, which can
// perform below check itself.
if commits.next().is_none() {
return Ok(());
}
return Err(e.into());
}
};
trace!("commit {} n={} version={}", commit.min_tx_offset, commit.n, version);
let max_tx_offset = commit.min_tx_offset + commit.n as u64;
// Skip if no transaction in the commit is in range.
if before_start(&max_tx_offset) {
continue;
}
let records = &mut commit.records.as_slice();
for n in 0..commit.n {
let tx_offset = commit.min_tx_offset + n as u64;
if before_start(&tx_offset) {
de.skip_record(version, tx_offset, records)?;
} else if past_end(&tx_offset) {
return Ok(());
} else {
de.consume_record(version, tx_offset, records)?;
}
}
}
Ok(())
}
/// Remove all data past the given transaction `offset`.
///
/// The function deletes log segments starting from the newest. As multiple
/// segments cannot be deleted atomically, the log may be left longer than
/// `offset` if the function does not return successfully.
///
/// If the function returns successfully, the most recent [`Commit`] in the
/// log will contain the transaction at `offset`.
///
/// The log must be re-opened if it is to be used after calling this function.
pub fn reset_to(repo: &impl Repo, offset: u64) -> io::Result<()> {
let segments = repo.existing_offsets()?;
reset_to_internal(repo, &segments, offset)
}
fn reset_to_internal(repo: &impl Repo, segments: &[u64], offset: u64) -> io::Result<()> {
for segment in segments.iter().copied().rev() {
if segment > offset {
// Segment is outside the offset, so remove it wholesale.
debug!("removing segment {segment}");
repo.remove_segment(segment)?;
} else {
// Read commit-wise until we find the byte offset.
let mut reader = repo::open_segment_reader(repo, DEFAULT_LOG_FORMAT_VERSION, segment)?;
let (index_file, mut byte_offset) = try_seek_using_offset_index(repo, &mut reader, offset)
.map(|(index_file, byte_offset)| (Some(index_file), byte_offset))
.unwrap_or((None, segment::Header::LEN as u64));
let commits = reader.commits();
for commit in commits {
let commit = commit?;
if commit.min_tx_offset > offset {
break;
}
byte_offset += Commit::from(commit).encoded_len() as u64;
}
if byte_offset == segment::Header::LEN as u64 {
// Segment is empty, just remove it.
repo.remove_segment(segment)?;
} else {
debug!("truncating segment {segment} to {offset} at {byte_offset}");
let mut file = repo.open_segment_writer(segment)?;
if let Some(mut index_file) = index_file {
let index_file = index_file.as_mut();
// Note: The offset index truncates equal or greater,
// inclusive. We'd like to retain `offset` in the index, as
// the commit is also retained in the log.
index_file.ftruncate(offset + 1, byte_offset).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Failed to truncate offset index: {e}"),
)
})?;
index_file.async_flush()?;
}
file.ftruncate(offset, byte_offset)?;
// Some filesystems require fsync after ftruncate.
file.fsync()?;
break;
}
}
}
Ok(())
}
pub struct Segments<R> {
repo: R,
offs: vec::IntoIter<u64>,
max_log_format_version: u8,
}
impl<R: Repo> Iterator for Segments<R> {
type Item = io::Result<segment::Reader<R::SegmentReader>>;
fn next(&mut self) -> Option<Self::Item> {
let off = self.offs.next()?;
debug!("iter segment {off}");
Some(repo::open_segment_reader(&self.repo, self.max_log_format_version, off))
}
}
/// Helper for the [`Commits`] iterator.
enum CommitInfo {
/// Constructed in [`Generic::commits_from`], specifying the offset the next
/// commit should have.
Initial { next_offset: u64 },
/// The last commit seen by the iterator.
///
/// Stores the range of transaction offsets, where `tx_range.end` is the
/// offset the next commit is expected to have. Also retains the checksum
/// needed to detect duplicate commits.
LastSeen { tx_range: Range<u64>, checksum: u32 },
}
impl CommitInfo {
/// `true` if the last seen commit in self and the provided one have the
/// same `min_tx_offset`.
fn same_offset_as(&self, commit: &StoredCommit) -> bool {
let Self::LastSeen { tx_range, .. } = self else {
return false;
};
tx_range.start == commit.min_tx_offset
}
/// `true` if the last seen commit in self and the provided one have the
/// same `checksum`.
fn same_checksum_as(&self, commit: &StoredCommit) -> bool {
let Some(checksum) = self.checksum() else { return false };
checksum == &commit.checksum
}
fn checksum(&self) -> Option<&u32> {
match self {
Self::Initial { .. } => None,
Self::LastSeen { checksum, .. } => Some(checksum),
}
}
fn expected_offset(&self) -> &u64 {
match self {
Self::Initial { next_offset } => next_offset,
Self::LastSeen { tx_range, .. } => &tx_range.end,
}
}
// If initial offset falls within a commit, adjust it to the commit boundary.
//
// Returns `true` if the initial offset is past `commit`.
// Returns `false` if `self` isn't `Self::Initial`,
// or the initial offset has been adjusted to the starting offset of `commit`.
//
// For iteration, `true` means to skip the commit, `false` to yield it.
fn adjust_initial_offset(&mut self, commit: &StoredCommit) -> bool {
if let Self::Initial { next_offset } = self {
let last_tx_offset = commit.min_tx_offset + commit.n as u64 - 1;
if *next_offset > last_tx_offset {
return true;
} else {
*next_offset = commit.min_tx_offset;
}
}
false
}
}
pub struct Commits<R: Repo> {
inner: Option<segment::Commits<R::SegmentReader>>,
segments: Segments<R>,
last_commit: CommitInfo,
last_error: Option<error::Traversal>,
}
impl<R: Repo> Commits<R> {
fn current_segment_header(&self) -> Option<&segment::Header> {
self.inner.as_ref().map(|segment| &segment.header)
}
/// Turn `self` into an iterator which pairs the log format version of the
/// current segment with the [`Commit`].
pub fn with_log_format_version(self) -> CommitsWithVersion<R> {
CommitsWithVersion { inner: self }
}
/// Advance the current-segment iterator to yield the next commit.
///
/// Checks that the offset sequence is contiguous, and may skip commits
/// until the requested offset.
///
/// Returns `None` if the segment iterator is exhausted or returns an error.
fn next_commit(&mut self) -> Option<Result<StoredCommit, error::Traversal>> {
loop {
match self.inner.as_mut()?.next()? {
Ok(commit) => {
// Pop the last error. Either we'll return it below, or it's no longer
// interesting.
let prev_error = self.last_error.take();
// Skip entries before the initial commit.
if self.last_commit.adjust_initial_offset(&commit) {
trace!("adjust initial offset");
continue;
// Same offset: ignore if duplicate (same crc), else report a "fork".
} else if self.last_commit.same_offset_as(&commit) {
if !self.last_commit.same_checksum_as(&commit) {
warn!(
"forked: commit={:?} last-error={:?} last-crc={:?}",
commit,
prev_error,
self.last_commit.checksum()
);
return Some(Err(error::Traversal::Forked {
offset: commit.min_tx_offset,
}));
} else {
trace!("ignore duplicate");
continue;
}
// Not the expected offset: report out-of-order.
} else if self.last_commit.expected_offset() != &commit.min_tx_offset {
warn!("out-of-order: commit={commit:?} last-error={prev_error:?}");
return Some(Err(error::Traversal::OutOfOrder {
expected_offset: *self.last_commit.expected_offset(),
actual_offset: commit.min_tx_offset,
prev_error: prev_error.map(Box::new),
}));
// Seems legit, record info.
} else {
self.last_commit = CommitInfo::LastSeen {
tx_range: commit.tx_range(),
checksum: commit.checksum,
};
return Some(Ok(commit));
}
}
Err(e) => {
warn!("error reading next commit: {e}");
// Stop traversing this segment here.
//
// If this is just a partial write at the end of the segment,
// we may be able to obtain a commit with right offset from
// the next segment.
//
// If we don't, the error here is likely more helpful, but
// would be clobbered by `OutOfOrder`. Therefore we store it
// here.
self.set_last_error(e);
return None;
}
}
}
}
/// Store `e` has the last error for delayed reporting.
fn set_last_error(&mut self, e: io::Error) {
// Recover a checksum mismatch.
let last_error = if e.kind() == io::ErrorKind::InvalidData && e.get_ref().is_some() {
e.into_inner()
.unwrap()
.downcast::<error::ChecksumMismatch>()
.map(|source| error::Traversal::Checksum {
offset: *self.last_commit.expected_offset(),
source: *source,
})
.unwrap_or_else(|e| io::Error::new(io::ErrorKind::InvalidData, e).into())
} else {
error::Traversal::from(e)
};
self.last_error = Some(last_error);
}
/// If we're still looking for the initial commit, try to use the offset
/// index to advance the segment reader.
fn try_seek_to_initial_offset(&self, segment: &mut segment::Reader<R::SegmentReader>) {
if let CommitInfo::Initial { next_offset } = &self.last_commit {
try_seek_using_offset_index(&self.segments.repo, segment, *next_offset);
}
}
}
impl<R: Repo> Iterator for Commits<R> {
type Item = Result<StoredCommit, error::Traversal>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(item) = self.next_commit() {
return Some(item);
}
match self.segments.next() {
// When there is no more data, the last commit being bad is an error
None => self.last_error.take().map(Err),
Some(segment) => segment.map_or_else(
|e| Some(Err(e.into())),
|mut segment| {
self.try_seek_to_initial_offset(&mut segment);
self.inner = Some(segment.commits());
self.next()
},
),
}
}
}
pub struct CommitsWithVersion<R: Repo> {
inner: Commits<R>,
}
impl<R: Repo> Iterator for CommitsWithVersion<R> {
type Item = Result<(u8, Commit), error::Traversal>;
fn next(&mut self) -> Option<Self::Item> {
let next = self.inner.next()?;
match next {
Ok(commit) => {
let version = self
.inner
.current_segment_header()
.map(|hdr| hdr.log_format_version)
.expect("segment header none even though segment yielded a commit");
Some(Ok((version, commit.into())))
}
Err(e) => Some(Err(e)),