pigeonhole-db 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +16 -16
  3. data/Cargo.toml +5 -5
  4. data/lib/pigeonhole/version.rb +1 -1
  5. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/IO_Event.so +0 -0
  6. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/gem_make.out +6 -6
  7. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/io-event-1.22.3/mkmf.log +26 -26
  8. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/gem_make.out +6 -6
  9. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/generator.so +0 -0
  10. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/json/ext/parser.so +0 -0
  11. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/json-3.1.0/mkmf.log +19 -19
  12. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/gem_make.out +9 -9
  13. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/mkmf.log +6 -6
  14. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/prism-1.9.0/prism/prism.so +0 -0
  15. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/gem_make.out +6 -6
  16. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/mkmf.log +7 -7
  17. data/vendor/bundle/ruby/3.4.0/extensions/x86_64-linux/3.4.0/rbs-4.2.0/rbs_extension.so +0 -0
  18. data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/ext/Makefile +3 -3
  19. data/vendor/bundle/ruby/3.4.0/gems/io-event-1.22.3/lib/IO_Event.so +0 -0
  20. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/generator/Makefile +3 -3
  21. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/ext/json/ext/parser/Makefile +3 -3
  22. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/generator.so +0 -0
  23. data/vendor/bundle/ruby/3.4.0/gems/json-3.1.0/lib/json/ext/parser.so +0 -0
  24. data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/ext/prism/Makefile +3 -3
  25. data/vendor/bundle/ruby/3.4.0/gems/prism-1.9.0/lib/prism/prism.so +0 -0
  26. data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/ext/rbs_extension/Makefile +3 -3
  27. data/vendor/bundle/ruby/3.4.0/gems/rbs-4.2.0/lib/rbs_extension.so +0 -0
  28. data/vendor/cache/Cargo.toml +2 -2
  29. data/vendor/cache/src/lib.rs +37 -0
  30. data/vendor/cache/src/loom_tests.rs +32 -0
  31. data/vendor/cache/src/s3fifo.rs +239 -11
  32. data/vendor/cache/src/sync.rs +4 -4
  33. data/vendor/compaction/Cargo.toml +6 -6
  34. data/vendor/compaction/src/cursor.rs +86 -4
  35. data/vendor/compaction/src/job.rs +24 -2
  36. data/vendor/engine/Cargo.toml +11 -11
  37. data/vendor/engine/src/admission.rs +3 -1
  38. data/vendor/engine/src/catalog.rs +39 -4
  39. data/vendor/engine/src/check.rs +59 -20
  40. data/vendor/engine/src/compact.rs +2 -2
  41. data/vendor/engine/src/engine/hooks.rs +144 -2
  42. data/vendor/engine/src/engine/info.rs +6 -0
  43. data/vendor/engine/src/engine.rs +1492 -50
  44. data/vendor/engine/src/external_registry.rs +447 -0
  45. data/vendor/engine/src/feed.rs +313 -84
  46. data/vendor/engine/src/feed_reader/catalog.rs +490 -0
  47. data/vendor/engine/src/feed_reader/history.rs +236 -0
  48. data/vendor/engine/src/feed_reader/read_ahead.rs +241 -0
  49. data/vendor/engine/src/feed_reader/token.rs +232 -0
  50. data/vendor/engine/src/feed_reader.rs +970 -852
  51. data/vendor/engine/src/feed_stop_loom.rs +73 -0
  52. data/vendor/engine/src/flush.rs +29 -4
  53. data/vendor/engine/src/io_count.rs +190 -0
  54. data/vendor/engine/src/large.rs +4 -1
  55. data/vendor/engine/src/lib.rs +16 -2
  56. data/vendor/engine/src/maintenance.rs +2 -2
  57. data/vendor/engine/src/manifest.rs +38 -6
  58. data/vendor/engine/src/nonblocking.rs +3 -3
  59. data/vendor/engine/src/options.rs +26 -0
  60. data/vendor/engine/src/punch.rs +282 -0
  61. data/vendor/engine/src/range_copy.rs +365 -0
  62. data/vendor/engine/src/read/keys.rs +37 -5
  63. data/vendor/engine/src/read/reduce.rs +1 -0
  64. data/vendor/engine/src/read.rs +7 -0
  65. data/vendor/engine/src/row_cache.rs +24 -0
  66. data/vendor/engine/src/shard/tablets.rs +127 -31
  67. data/vendor/engine/src/shard.rs +1006 -33
  68. data/vendor/engine/src/source.rs +105 -12
  69. data/vendor/engine/src/write.rs +310 -4
  70. data/vendor/format/Cargo.toml +1 -1
  71. data/vendor/format/src/shm.rs +10 -0
  72. data/vendor/format/src/superblock.rs +9 -2
  73. data/vendor/format/src/wal.rs +818 -16
  74. data/vendor/io/Cargo.toml +3 -3
  75. data/vendor/io/src/count.rs +76 -0
  76. data/vendor/io/src/file.rs +70 -0
  77. data/vendor/io/src/lib.rs +2 -0
  78. data/vendor/io/src/os/unix.rs +124 -0
  79. data/vendor/io/src/os/windows.rs +51 -0
  80. data/vendor/io/src/pread.rs +26 -0
  81. data/vendor/io/src/sim.rs +81 -9
  82. data/vendor/io/src/sys.rs +53 -0
  83. data/vendor/io/src/uring.rs +28 -0
  84. data/vendor/io/src/vfs.rs +4 -0
  85. data/vendor/memory/Cargo.toml +4 -4
  86. data/vendor/memory/src/hooks.rs +6 -0
  87. data/vendor/memory/src/ivf.rs +67 -1
  88. data/vendor/memory/src/lib.rs +4 -1
  89. data/vendor/memory/src/recall/pool.rs +872 -0
  90. data/vendor/memory/src/recall.rs +351 -273
  91. data/vendor/memory/src/store.rs +15 -0
  92. data/vendor/memtable/Cargo.toml +3 -3
  93. data/vendor/pager/Cargo.toml +3 -3
  94. data/vendor/pager/src/alloc.rs +430 -0
  95. data/vendor/pager/src/lib.rs +114 -0
  96. data/vendor/pigeonhole/Cargo.toml +6 -5
  97. data/vendor/pigeonhole/src/db.rs +91 -2
  98. data/vendor/pigeonhole/src/error.rs +2 -1
  99. data/vendor/pigeonhole/src/feed.rs +210 -6
  100. data/vendor/pigeonhole/src/lib.rs +3 -0
  101. data/vendor/pigeonhole/src/metrics.rs +37 -0
  102. data/vendor/pigeonhole/src/nonblocking.rs +94 -111
  103. data/vendor/pigeonhole/src/options.rs +69 -0
  104. data/vendor/pigeonhole/src/replication.rs +601 -11
  105. data/vendor/pigeonhole/src/write.rs +81 -0
  106. data/vendor/runtime/Cargo.toml +2 -2
  107. data/vendor/shm/Cargo.toml +3 -3
  108. data/vendor/shm/src/region.rs +20 -2
  109. data/vendor/sst/Cargo.toml +4 -4
  110. data/vendor/sst/src/blob.rs +85 -7
  111. data/vendor/sst/src/iter.rs +98 -2
  112. data/vendor/sst/src/lib.rs +11 -2
  113. data/vendor/sst/src/writer.rs +12 -2
  114. data/vendor/text/Cargo.toml +2 -2
  115. data/vendor/text/src/block.rs +638 -0
  116. data/vendor/text/src/error.rs +8 -3
  117. data/vendor/text/src/fuzzy.rs +1 -1
  118. data/vendor/text/src/index.rs +1055 -80
  119. data/vendor/text/src/layout.rs +63 -2
  120. data/vendor/text/src/lib.rs +5 -1
  121. data/vendor/text/src/maxscore.rs +481 -94
  122. data/vendor/text/src/merged.rs +470 -0
  123. data/vendor/text/src/search.rs +1278 -341
  124. data/vendor/text/src/stats.rs +431 -0
  125. data/vendor/vector/Cargo.toml +2 -2
  126. data/vendor/vector/src/hooks.rs +33 -4
  127. data/vendor/vector/src/index.rs +498 -69
  128. data/vendor/wal/Cargo.toml +3 -3
  129. data/vendor/wal/src/feed.rs +313 -19
  130. data/vendor/wal/src/recovery.rs +31 -1
  131. data/vendor/wal/src/stream.rs +28 -0
  132. metadata +15 -1
@@ -0,0 +1,236 @@
1
+ use super::*;
2
+
3
+ /// The sessions whose segments the feed's history runs through (the mark rule), newest
4
+ /// first ([`chain`]'s order).
5
+ #[derive(Debug)]
6
+ pub(super) struct History {
7
+ pub(super) sessions: Vec<u64>,
8
+ }
9
+
10
+ impl History {
11
+ pub(super) fn holds(&self, session: u64) -> bool {
12
+ session != 0 && self.sessions.contains(&session)
13
+ }
14
+
15
+ /// Whether `session` came after `than` in the history: by the chain's order, never by
16
+ /// value, since session ids are random (#1810). Any held session is later than none
17
+ /// (0, a feed that has read nothing yet) and than one the history doesn't hold.
18
+ pub(super) fn later(&self, session: u64, than: u64) -> bool {
19
+ let at = |s: u64| self.sessions.iter().position(|&x| x == s);
20
+ match (at(session), at(than)) {
21
+ (Some(a), Some(b)) => a < b,
22
+ (Some(_), None) => true,
23
+ (None, _) => false,
24
+ }
25
+ }
26
+ }
27
+
28
+ /// In a reader process: the writer's session and fed tables, from its entry in the sidecar
29
+ /// (the last session entry: a writer writes it before its shared memory exists).
30
+ /// `InvalidArgument` when the writer doesn't feed (its open recorded `fed: none`).
31
+ pub(super) fn writer_session(shared: &Shared) -> Result<(u64, FeedTables)> {
32
+ let off = || {
33
+ Error::InvalidArgument(
34
+ "the writer's change feed is off: subscribe to a writer that feeds".into(),
35
+ )
36
+ };
37
+ let entries = match crate::feed::read_sidecar(&shared.vfs, &shared.path, shared.pager.db_id()) {
38
+ Ok(Some(entries)) => entries,
39
+ Ok(None) => return Err(off()),
40
+ Err(Error::Io(e)) if e.kind == pigeonhole_io::ErrorKind::NotFound => return Err(off()),
41
+ Err(e) => return Err(e),
42
+ };
43
+ for e in entries.iter().rev() {
44
+ match e {
45
+ FeedEntry::Session {
46
+ session,
47
+ streams,
48
+ tables,
49
+ ..
50
+ } => {
51
+ if *streams as usize != shared.shards {
52
+ // A new writer with another shard count is starting: this attachment is
53
+ // the old one's (simp724's nit). Subscribe again once it runs.
54
+ return Err(Error::SnapshotExpired);
55
+ }
56
+ return Ok((*session, tables.clone()));
57
+ }
58
+ FeedEntry::Unfed { .. } => return Err(off()),
59
+ _ => {}
60
+ }
61
+ }
62
+ Err(off())
63
+ }
64
+
65
+ /// The sessions whose segments hold the feed's history: the current one, and each one
66
+ /// before it while it fed every table in `tables`, had the same stream count, and chains.
67
+ /// A session chains when its `opened_at` is the previous session's last recorded
68
+ /// `sequence` and every sequence between was recorded: the previous session's Root
69
+ /// entries run contiguously from its own `opened_at` (simp724 on fix A: a lost middle
70
+ /// entry could hide another session's root commits, so a gap breaks the chain), with no
71
+ /// feed-off open between.
72
+ pub(super) fn read_history(shared: &Shared, current: u64, tables: &[TableId]) -> Result<History> {
73
+ let entries = crate::feed::read_sidecar(&shared.vfs, &shared.path, shared.pager.db_id())?
74
+ .unwrap_or_default();
75
+ Ok(History {
76
+ sessions: chain(&entries, current, tables),
77
+ })
78
+ }
79
+
80
+ /// [`read_history`]'s rule over the sidecar's entries.
81
+ pub(super) fn chain(entries: &[FeedEntry], current: u64, tables: &[TableId]) -> Vec<u64> {
82
+ let mut sessions: Vec<Link> = Vec::new();
83
+ let mut last_seq: Option<u64> = None;
84
+ let mut broken = true;
85
+ for e in entries {
86
+ match e {
87
+ FeedEntry::Session {
88
+ session,
89
+ opened_at,
90
+ streams,
91
+ tables: fed,
92
+ ..
93
+ } => {
94
+ // A feed of every table (an empty list, D315) needs a session that fed every
95
+ // table: one with a list may have left out any table created since.
96
+ let feeds = if tables.is_empty() {
97
+ matches!(fed, FeedTables::All)
98
+ } else {
99
+ tables.iter().all(|&t| fed.contains(t))
100
+ };
101
+ let chained = !broken && last_seq == Some(*opened_at);
102
+ sessions.push(Link {
103
+ session: *session,
104
+ feeds,
105
+ streams: *streams,
106
+ chained,
107
+ });
108
+ last_seq = Some(*opened_at);
109
+ broken = false;
110
+ }
111
+ FeedEntry::Root { session, sequence } => {
112
+ let ours = sessions.last().is_some_and(|s| s.session == *session);
113
+ if !ours || last_seq.map(|l| l + 1) != Some(*sequence) {
114
+ broken = true;
115
+ }
116
+ last_seq = Some(*sequence);
117
+ }
118
+ FeedEntry::Unfed { .. } => broken = true,
119
+ // A catalog change (D315) is appended just before its root commit's Root entry,
120
+ // which carries the chain: one of another session breaks it.
121
+ FeedEntry::Catalog { session, .. } => {
122
+ if sessions.last().is_none_or(|s| s.session != *session) {
123
+ broken = true;
124
+ }
125
+ }
126
+ // A root commit's intent (D315 amendment) is overwritten by its Root entry once
127
+ // the commit is durable, and an open resolves one a crash left at the end. One
128
+ // still here is either the live session's commit in progress, after everything
129
+ // the chain reads, or an older one no open could decide: fail closed.
130
+ FeedEntry::RootIntent { .. } => broken = true,
131
+ // A newer build's entry may mean a break: fail closed (simp724's nit on #1316).
132
+ FeedEntry::Unknown(_) | _ => broken = true,
133
+ }
134
+ }
135
+ let Some(pos) = sessions.iter().rposition(|s| s.session == current) else {
136
+ return vec![current];
137
+ };
138
+ let streams = sessions[pos].streams;
139
+ let mut run = Vec::new();
140
+ for (i, s) in sessions[..=pos].iter().enumerate().rev() {
141
+ if !s.feeds || s.streams != streams {
142
+ break;
143
+ }
144
+ run.push(s.session);
145
+ if !s.chained || i == 0 {
146
+ break;
147
+ }
148
+ }
149
+ run
150
+ }
151
+
152
+ /// One session in [`chain`]'s walk of the sidecar.
153
+ pub(super) struct Link {
154
+ pub(super) session: u64,
155
+ /// It fed every table the feed follows.
156
+ pub(super) feeds: bool,
157
+ pub(super) streams: u32,
158
+ /// It chains to the session before it.
159
+ pub(super) chained: bool,
160
+ }
161
+
162
+ #[cfg(test)]
163
+ mod tests {
164
+ use super::*;
165
+
166
+ fn session(session: u64, opened_at: u64) -> FeedEntry {
167
+ FeedEntry::Session {
168
+ session,
169
+ opened_at,
170
+ retention_bytes: 1 << 20,
171
+ retention_age_secs: 0,
172
+ streams: 2,
173
+ tables: FeedTables::All,
174
+ }
175
+ }
176
+
177
+ fn root(session: u64, sequence: u64) -> FeedEntry {
178
+ FeedEntry::Root { session, sequence }
179
+ }
180
+
181
+ #[test]
182
+ fn sessions_chain_only_through_contiguous_sequences() {
183
+ let t = [TableId(1)];
184
+ let whole = [
185
+ session(1, 1),
186
+ root(1, 2),
187
+ root(1, 3),
188
+ root(1, 4),
189
+ session(2, 4),
190
+ root(2, 5),
191
+ ];
192
+ assert_eq!(chain(&whole, 2, &t), [2, 1]);
193
+ // A lost middle entry: the last still matches the next open, but the root commit at
194
+ // 3 is unaccounted for, so session 1's history ends there (simp724 on fix A).
195
+ let gap = [
196
+ session(1, 1),
197
+ root(1, 2),
198
+ root(1, 4),
199
+ session(2, 4),
200
+ root(2, 5),
201
+ ];
202
+ assert_eq!(chain(&gap, 2, &t), [2]);
203
+ // A lost last entry: the next open is ahead.
204
+ let ahead = [session(1, 1), root(1, 2), session(2, 3)];
205
+ assert_eq!(chain(&ahead, 2, &t), [2]);
206
+ // A feed-off open between.
207
+ let unfed = [
208
+ session(1, 1),
209
+ root(1, 2),
210
+ FeedEntry::Unfed { sequence: 2 },
211
+ session(2, 2),
212
+ ];
213
+ assert_eq!(chain(&unfed, 2, &t), [2]);
214
+ // A session with another stream count, or not feeding the table, ends it too.
215
+ let narrower = [
216
+ FeedEntry::Session {
217
+ session: 1,
218
+ opened_at: 1,
219
+ retention_bytes: 1 << 20,
220
+ retention_age_secs: 0,
221
+ streams: 2,
222
+ tables: FeedTables::Only(vec![TableId(9)]),
223
+ },
224
+ session(2, 1),
225
+ ];
226
+ assert_eq!(chain(&narrower, 2, &t), [2]);
227
+ // An entry this build doesn't know may be a break: it ends the chain too.
228
+ let unknown = [
229
+ session(1, 1),
230
+ root(1, 2),
231
+ FeedEntry::Unknown(0xee),
232
+ session(2, 2),
233
+ ];
234
+ assert_eq!(chain(&unknown, 2, &t), [2]);
235
+ }
236
+ }
@@ -0,0 +1,241 @@
1
+ use super::*;
2
+
3
+ /// A reader's read-ahead budget (D320): the bytes of records read but not delivered that it
4
+ /// keeps (`ready` and `held`). Past it, a pass keeps only the smallest keys.
5
+ pub(super) const READ_AHEAD_BYTES: u64 = 64 << 20;
6
+
7
+ /// What one entry of `ready` costs in its B-tree, with its start's entry in `starts`: each
8
+ /// key and value twice over, since a node of entries appended in order (keys only grow
9
+ /// there) is about half full (#1637).
10
+ pub(super) const READY_ENTRY: u64 =
11
+ 2 * (std::mem::size_of::<(Seqno, Pending)>() + START_ENTRY) as u64;
12
+
13
+ /// The same for one share in `held`.
14
+ pub(super) const HELD_ENTRY: u64 =
15
+ 2 * (std::mem::size_of::<((Seqno, usize), Held)>() + START_ENTRY) as u64;
16
+
17
+ /// One entry of a stream's `starts`.
18
+ pub(super) const START_ENTRY: usize = std::mem::size_of::<(Lsn, u32)>();
19
+
20
+ /// The read-ahead bytes a record kept in `ready` counts for: what it really holds, its map
21
+ /// entry and its allocations (#1637; a decided round's shares count in `held`).
22
+ pub(super) fn pending_bytes(p: &Pending) -> u64 {
23
+ READY_ENTRY
24
+ + match p {
25
+ Pending::Single { changes, .. } => changes.memory(),
26
+ Pending::Decided { participants, .. } => {
27
+ alloc_size(participants.capacity() * std::mem::size_of::<StreamId>())
28
+ }
29
+ }
30
+ }
31
+
32
+ /// The read-ahead bytes a held share counts for, as for [`pending_bytes`].
33
+ pub(super) fn held_bytes(h: &Held) -> u64 {
34
+ if h.external {
35
+ return 0;
36
+ }
37
+ HELD_ENTRY + h.changes.memory()
38
+ }
39
+
40
+ /// The read-ahead cap's state (D320). A pass reads every stream to its end, as without a
41
+ /// cap, but keeps only records with keys at or below the cutoff `C`, which falls whenever
42
+ /// what's kept passes the budget: the largest keys go first. Every record with a key at or
43
+ /// below `C` read since the last rewind is kept, so the reader delivers up to the smaller
44
+ /// of the frontier and `C`. A PREPARE's key is its round's apply seqno, above its id (both
45
+ /// come from the one allocator, the id first), so a share counts at `id + 1`, a lower bound.
46
+ #[derive(Debug)]
47
+ pub(super) struct ReadAhead {
48
+ /// The bytes kept now, over `ready` and `held`.
49
+ pub(super) bytes: u64,
50
+ /// The budget for the next eviction: `base`, doubled after a pass that left nothing to
51
+ /// deliver below a cutoff under the frontier (liveness), and back to `base` on a delivery.
52
+ pub(super) budget: u64,
53
+ pub(super) base: u64,
54
+ /// `C`: every record with a key at or below it read since the last rewind is kept.
55
+ /// `Seqno::MAX` while nothing was passed over.
56
+ pub(super) cutoff: Seqno,
57
+ /// Per stream, the earliest start of a record passed over since the last rewind: where
58
+ /// the stream must be read again from (and the token's hint at most this).
59
+ pub(super) resume: Vec<Option<Lsn>>,
60
+ /// Rounds passed over since the last rewind (their Decision skipped, or their shares or
61
+ /// Decision evicted): their shares read later are passed over too, never held. Rule C
62
+ /// would otherwise drop a held share whose Decision this pass skipped, and the Decision
63
+ /// read after the rewind would then expire the feed. Only ids below the cutoff are kept
64
+ /// (simp724's S1 on #1622): a share with an id at or above it is passed over by its id
65
+ /// anyway, and the cutoff only falls until the rewind. So it holds only rounds in flight
66
+ /// across the cutoff, not every round of a backlog.
67
+ pub(super) skipped_rounds: std::collections::BTreeSet<Seqno>,
68
+ /// The decided rounds in `ready`: id to apply seqno.
69
+ pub(super) decided: HashMap<Seqno, Seqno>,
70
+ }
71
+
72
+ impl ReadAhead {
73
+ pub(super) fn new(streams: usize, base: u64) -> Self {
74
+ Self {
75
+ bytes: 0,
76
+ budget: base,
77
+ base,
78
+ cutoff: Seqno::MAX,
79
+ resume: vec![None; streams],
80
+ skipped_rounds: std::collections::BTreeSet::new(),
81
+ decided: HashMap::new(),
82
+ }
83
+ }
84
+
85
+ /// Stream `s`'s record at `start` was passed over.
86
+ pub(super) fn skip(&mut self, s: usize, start: Lsn) {
87
+ let r = &mut self.resume[s];
88
+ *r = Some(r.map_or(start, |r| r.min(start)));
89
+ }
90
+
91
+ /// Lowers the cutoff to at most `c`, forgetting the passed-over rounds its id test now
92
+ /// covers.
93
+ pub(super) fn lower_cutoff(&mut self, c: Seqno) {
94
+ if c < self.cutoff {
95
+ self.cutoff = c;
96
+ self.skipped_rounds.split_off(&c);
97
+ }
98
+ }
99
+
100
+ /// Whether anything was passed over since the last rewind.
101
+ pub(super) fn capped(&self) -> bool {
102
+ self.cutoff != Seqno::MAX
103
+ }
104
+ }
105
+
106
+ impl FeedReader {
107
+ /// D320: reads every stream again from its hint (the earliest record not delivered,
108
+ /// kept or passed over), dropping what's kept: the reads of a resume from this reader's
109
+ /// token. Returns `false` once the feed expired (retention passed a hint).
110
+ pub(super) fn rewind(&mut self) -> Result<bool> {
111
+ let hints: Vec<Lsn> = (0..self.streams.len()).map(|s| self.hint(s)).collect();
112
+ for (s, &at) in hints.iter().enumerate() {
113
+ if at.epoch() != 0 && at.epoch() < self.floor(s) {
114
+ self.expired = Some(expiry(
115
+ "retention passed the feed: a segment it was reading was recycled",
116
+ ));
117
+ return Ok(false);
118
+ }
119
+ }
120
+ for (s, at) in hints.into_iter().enumerate() {
121
+ self.streams[s].cursor = self.open_cursor(s, at)?;
122
+ self.streams[s].index.trim(at);
123
+ }
124
+ self.ready.clear();
125
+ self.held.clear();
126
+ self.silent_seen.clear();
127
+ self.xids.clear();
128
+ self.starts = Starts::default();
129
+ let a = &mut self.ahead;
130
+ a.bytes = 0;
131
+ a.cutoff = Seqno::MAX;
132
+ a.resume.iter_mut().for_each(|r| *r = None);
133
+ a.skipped_rounds.clear();
134
+ a.decided.clear();
135
+ self.read_through = None;
136
+ Ok(true)
137
+ }
138
+
139
+ /// D320: evicts the largest-keyed records kept while the read-ahead is over its budget,
140
+ /// or past the cutoff, lowering the cutoff below each. A held share counts at its id + 1
141
+ /// (its round's apply seqno is above its id), and goes with every share of its round; a
142
+ /// decided round goes with its shares. Returns whether it evicted anything.
143
+ ///
144
+ /// Run after every record a pass keeps: the check stays inline and the eviction is out
145
+ /// of line, so a reader that keeps up pays only the check (callgrind's feed-drain on
146
+ /// x86, #1922: with the eviction in it, `trim` stopped being inlined).
147
+ #[inline]
148
+ pub(super) fn trim(&mut self) -> bool {
149
+ let (top_ready, top_held) = self.trim_tops();
150
+ if !self.trim_due(top_ready, top_held) {
151
+ return false;
152
+ }
153
+ self.evict_over();
154
+ true
155
+ }
156
+
157
+ /// The largest key kept, and the largest held share's id + 1 (not a share of a round
158
+ /// whose id + 1 is at or below `delivered`, simp724's N1 on #1622: a rewind can re-read a
159
+ /// delivered round's share without its Decision, and evicting it would put the cutoff at
160
+ /// or below `delivered` for nothing; rule C drops it).
161
+ #[inline]
162
+ pub(super) fn trim_tops(&self) -> (Option<Seqno>, Option<Seqno>) {
163
+ let top_ready = self.ready.last_key_value().map(|(&k, _)| k);
164
+ let top_held = self
165
+ .held
166
+ .range((self.delivered, 0)..)
167
+ .rev()
168
+ .find(|(_, h)| !h.external)
169
+ .map(|(&(id, _), _)| id.saturating_add(1));
170
+ (top_ready, top_held)
171
+ }
172
+
173
+ /// Whether [`trim`](Self::trim) has anything to evict.
174
+ #[inline]
175
+ pub(super) fn trim_due(&self, top_ready: Option<Seqno>, top_held: Option<Seqno>) -> bool {
176
+ let c = self.ahead.cutoff;
177
+ self.ahead.bytes > self.ahead.budget
178
+ || top_ready.is_some_and(|k| k > c)
179
+ || top_held.is_some_and(|k| k > c)
180
+ }
181
+
182
+ /// [`trim`](Self::trim)'s eviction, once it's due.
183
+ #[cold]
184
+ #[inline(never)]
185
+ pub(super) fn evict_over(&mut self) {
186
+ // The cutoff falls only here: the streams' skip indexes are built from now on
187
+ // (#1827), and a reader that never gets here pays nothing for them per record.
188
+ self.indexing = true;
189
+ loop {
190
+ let (top_ready, top_held) = self.trim_tops();
191
+ if !self.trim_due(top_ready, top_held) {
192
+ return;
193
+ }
194
+ match (top_ready, top_held) {
195
+ (Some(k), None) => self.evict_ready(k),
196
+ (Some(k), Some(h)) if k >= h => self.evict_ready(k),
197
+ (_, Some(h)) => {
198
+ let id = h - 1;
199
+ // A decided round's apply seqno is in `ready`, at least id + 1: the arms
200
+ // above evict it first, with its shares.
201
+ debug_assert!(!self.ahead.decided.contains_key(&id), "round {id} decided");
202
+ self.evict_round(id);
203
+ self.ahead.lower_cutoff(id);
204
+ }
205
+ (None, None) => return,
206
+ }
207
+ }
208
+ }
209
+
210
+ /// Evicts `ready`'s record at `key` (and a decided round's shares): passed over, to be
211
+ /// read again after a rewind.
212
+ pub(super) fn evict_ready(&mut self, key: Seqno) {
213
+ let Some(p) = self.ready.remove(&key) else {
214
+ return;
215
+ };
216
+ self.forget_ready(&p);
217
+ let (stream, start) = p.at();
218
+ self.ahead.skip(stream, start);
219
+ match p {
220
+ // A round's note goes with its shares (`remove_held`).
221
+ Pending::Decided { id, .. } => self.evict_round(id),
222
+ // A single's goes here: it's read again, and noted again, past the cutoff
223
+ // (ai1249's nit on #2068).
224
+ Pending::Single { .. } => {
225
+ self.silent_seen.remove(&key);
226
+ self.xids.remove(&key);
227
+ }
228
+ }
229
+ self.ahead.lower_cutoff(key - 1);
230
+ }
231
+
232
+ /// Evicts round `id`'s held shares, and passes over any read later in this pass.
233
+ pub(super) fn evict_round(&mut self, id: Seqno) {
234
+ for h in self.remove_held(id) {
235
+ self.ahead.skip(h.stream, h.start);
236
+ }
237
+ if id < self.ahead.cutoff {
238
+ self.ahead.skipped_rounds.insert(id);
239
+ }
240
+ }
241
+ }