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
@@ -2,10 +2,10 @@
2
2
 
3
3
  [dependencies.pigeonhole-format]
4
4
  path = "../format"
5
- version = "=3.0.2"
5
+ version = "=3.1.0"
6
6
  [dependencies.pigeonhole-io]
7
7
  path = "../io"
8
- version = "=3.0.2"
8
+ version = "=3.1.0"
9
9
  [features]
10
10
  sim-hooks = []
11
11
  [lints.rust.unexpected_cfgs]
@@ -22,4 +22,4 @@ name = "pigeonhole-wal"
22
22
  readme = "README.md"
23
23
  repository = "https://github.com/CodingAnarchy/pigeonhole"
24
24
  rust-version = "1.96"
25
- version = "3.0.2"
25
+ version = "3.1.0"
@@ -6,7 +6,7 @@ use std::fmt;
6
6
  use std::path::{Path, PathBuf};
7
7
  use std::sync::Arc;
8
8
 
9
- use pigeonhole_format::wal::{Decoded, FRAME_SIZE, FrameDecoder, FrameEncoder, WalRecord};
9
+ use pigeonhole_format::wal::{Decoded, FRAME_SIZE, FrameDecoder, FrameEncoder, WalRecord, Within};
10
10
  use pigeonhole_format::{Lsn, StreamId};
11
11
  use pigeonhole_io::{FileRef, OpenOptions, VfsRef};
12
12
 
@@ -16,18 +16,25 @@ use crate::{FeedMarks, Result, stream_path};
16
16
  /// Frames read from the file at a time.
17
17
  const CHUNK_FRAMES: u64 = 32;
18
18
 
19
+ /// A resume's first tail read (#1901) reaches at least this far past where the cursor
20
+ /// stopped... These two only size that read: what's trusted is `fresh_end`, and a fragment
21
+ /// past it is read before it's decided (`Within::More`).
22
+ const TAIL_MIN: u64 = 512;
23
+ /// ...to the next multiple of this (a page), so a record of a typical commit is read in one.
24
+ const TAIL_PAGE: u64 = 4096;
25
+
19
26
  /// What [`FeedCursor::step`] found.
20
27
  #[derive(Debug)]
21
28
  pub enum Step<'a> {
22
29
  /// A complete record.
23
30
  Record {
24
31
  /// Where to read again from to get this record: the cursor's position before it, the
25
- /// end of the record before. That's the record's first fragment, or, when a skipped
26
- /// frame tail (FORMAT §10.2) lies between, the position just before it, which a
27
- /// cursor opened there steps over. A resume position (a feed token's hint) is then
28
- /// never past where a recovery may cut the log before a record the cursor never read:
29
- /// with the record lost to a power loss, the cut is the end of the record before the
30
- /// tail, and the hint is too (#1937).
32
+ /// end of the record before. That's the record's first fragment, or, when padding
33
+ /// (D327) or a skipped frame tail lies between, the position just before it, which
34
+ /// a cursor opened there steps over. A resume position (a feed token's hint) is
35
+ /// then never past where a recovery may cut the log before a record the cursor never
36
+ /// read: with a pad's record lost to a power loss, the cut is the end of the record
37
+ /// before the pad, and the hint is too.
31
38
  start: Lsn,
32
39
  /// Just past the record: where the next one's `start` is.
33
40
  end: Lsn,
@@ -140,8 +147,16 @@ pub struct FeedCursor {
140
147
  chunk_slot: usize,
141
148
  /// Segment offset of `chunk[0]`.
142
149
  chunk_start: u64,
150
+ /// Segment offset below which `chunk`'s bytes are known current. A resume re-reads only
151
+ /// past it (#1901): bytes a cursor already decoded don't change while their segment is
152
+ /// in the file, and a frame's bytes past it may predate the writer.
153
+ fresh_end: u64,
154
+ /// The next tail read after a resume is a small window, not the rest of the frame.
155
+ tail_window: bool,
143
156
  /// A stop short of its successor's recorded end, deferred once (#1370): `(epoch, stop)`.
144
157
  deferred: Option<(u32, u64)>,
158
+ /// Whether a feed-silent batch's mark is taken (D331, [`FeedCursor::accept_silent`]).
159
+ silent_ok: bool,
145
160
  }
146
161
 
147
162
  #[derive(Debug)]
@@ -195,7 +210,10 @@ impl FeedCursor {
195
210
  chunk: Vec::new(),
196
211
  chunk_slot: usize::MAX,
197
212
  chunk_start: 0,
213
+ fresh_end: 0,
214
+ tail_window: false,
198
215
  deferred: None,
216
+ silent_ok: false,
199
217
  };
200
218
  this.cur = this.place(at);
201
219
  Ok(this)
@@ -230,6 +248,14 @@ impl FeedCursor {
230
248
  self.marks = Some(marks);
231
249
  }
232
250
 
251
+ /// Takes feed-silent batches' mark (D331, ICR 0141); without it a marked batch is
252
+ /// corrupt. The engine's change feed takes it and checks `incompat` bit 3 itself where it
253
+ /// looks at each batch (a writer may set the bit after the cursor opened). The cursor only
254
+ /// decodes the mark: whether a silent commit is delivered is its reader's call.
255
+ pub fn accept_silent(&mut self, ok: bool) {
256
+ self.silent_ok = ok;
257
+ }
258
+
233
259
  /// The stream's segments as their headers were last read, by epoch: `(epoch,
234
260
  /// prev_epoch, feed_session)` (FORMAT §10.2). A segment whose `prev_epoch` is 0 or whose
235
261
  /// epoch isn't one above it is the first of an open: a reopened stream starts above
@@ -257,6 +283,35 @@ impl FeedCursor {
257
283
  .map_or(Lsn::default(), |c| Lsn::new(c.epoch, c.last_end as u32))
258
284
  }
259
285
 
286
+ /// Moves the cursor to `at`, a record boundary in this stream that a cursor read up to
287
+ /// before, without reading or checking the records between (#1827: a feed reader
288
+ /// passing over records it indexed on an earlier read). They were checked when first
289
+ /// read, and a record below the readable end doesn't change while its segment is in the
290
+ /// file. `at` may be in a later segment than the cursor's. Returns `false`, the cursor
291
+ /// then gone as [`Step::Gone`] reads, once `at`'s segment isn't in the file (recycled).
292
+ ///
293
+ /// Nothing is read unless `at`'s segment isn't in the headers as last read, which are
294
+ /// then read again. The cached frames are dropped: past where this cursor decoded, they
295
+ /// may have been read before the writer finished them.
296
+ pub fn skip_to(&mut self, at: Lsn) -> Result<bool> {
297
+ if self.cur.is_none() {
298
+ return Ok(false);
299
+ }
300
+ self.chunk_slot = usize::MAX;
301
+ self.stopped = false;
302
+ let mut placed = self.place(at);
303
+ if placed.is_none() {
304
+ self.grid = Grid::read(&self.file, self.stream, self.db_id)?;
305
+ #[cfg(test)]
306
+ {
307
+ self.reads.0 += 1;
308
+ }
309
+ placed = self.place(at);
310
+ }
311
+ self.cur = placed;
312
+ Ok(self.cur.is_some())
313
+ }
314
+
260
315
  /// Where the successor of segment `epoch` says it ends, as the headers were last read.
261
316
  fn cut_of(&self, epoch: u32) -> Option<u64> {
262
317
  self.grid.as_ref().and_then(|g| cut_of_grid(g, epoch))
@@ -271,7 +326,16 @@ impl FeedCursor {
271
326
  let have = self.chunk_slot == slot
272
327
  && frame >= self.chunk_start
273
328
  && frame + FRAME <= self.chunk_start + self.chunk.len() as u64;
274
- if have {
329
+ // Cached: decoded from as far as `fresh_end`, and the decoder asks (`Within::More`)
330
+ // when a fragment reaches past it. A resume's frame (#1901) is current up to where the
331
+ // cursor stopped, so its first load reads only a window past that: to the next 4 KiB
332
+ // page, at least 512 bytes on.
333
+ if have && self.fresh_end >= frame {
334
+ if self.tail_window {
335
+ self.tail_window = false;
336
+ let window = (self.fresh_end + TAIL_MIN).next_multiple_of(TAIL_PAGE);
337
+ return self.read_tail(slot, window.min(frame + FRAME));
338
+ }
275
339
  return Ok(());
276
340
  }
277
341
  let segment_size = self.segment_size();
@@ -290,6 +354,24 @@ impl FeedCursor {
290
354
  }
291
355
  self.chunk_slot = slot;
292
356
  self.chunk_start = frame;
357
+ self.fresh_end = frame + len as u64;
358
+ Ok(())
359
+ }
360
+
361
+ /// Reads `chunk`'s current frame from `fresh_end` to `to` (both in it), making them fresh.
362
+ fn read_tail(&mut self, slot: usize, to: u64) -> Result<()> {
363
+ let from = self.fresh_end;
364
+ if to > from {
365
+ let at = (from - self.chunk_start) as usize;
366
+ let len = (to - from) as usize;
367
+ let pos = slot as u64 * self.segment_size() + from;
368
+ self.file.read_at(&mut self.chunk[at..at + len], pos)?;
369
+ #[cfg(test)]
370
+ {
371
+ self.reads.1 += len as u64;
372
+ }
373
+ self.fresh_end = to;
374
+ }
293
375
  Ok(())
294
376
  }
295
377
 
@@ -305,8 +387,6 @@ impl FeedCursor {
305
387
  {
306
388
  self.reads.0 += 1;
307
389
  }
308
- // Cached frames may predate a rewrite of the slot.
309
- self.chunk_slot = usize::MAX;
310
390
  self.short_load = true;
311
391
  let here = self
312
392
  .grid
@@ -314,8 +394,12 @@ impl FeedCursor {
314
394
  .and_then(|g| g.headers.get(slot).copied().flatten())
315
395
  .is_some_and(|h| h.epoch == epoch);
316
396
  if !here {
397
+ // Cached frames may predate a rewrite of the slot.
398
+ self.chunk_slot = usize::MAX;
317
399
  self.cur = None;
318
400
  }
401
+ // Still here: the cached frames are this segment's, current up to `fresh_end`, which
402
+ // a resume keeps (#1901).
319
403
  Ok(here)
320
404
  }
321
405
 
@@ -378,8 +462,22 @@ impl FeedCursor {
378
462
  // carry another epoch and read as a stop, which reads them (below), and a
379
463
  // successor is looked for at the stop too.
380
464
  self.stopped = false;
381
- self.chunk_slot = usize::MAX;
382
- self.short_load = true;
465
+ // Its bytes up to where the cursor stopped are kept (#1901): only the tail is read
466
+ // again. Without the frame cached, the frame is read whole, as before.
467
+ match &self.cur {
468
+ Some(cur)
469
+ if self.chunk_slot == cur.slot
470
+ && cur.frame >= self.chunk_start
471
+ && cur.frame + FRAME <= self.chunk_start + self.chunk.len() as u64 =>
472
+ {
473
+ self.fresh_end = self.fresh_end.min(cur.last_end).max(cur.frame);
474
+ self.tail_window = true;
475
+ }
476
+ _ => {
477
+ self.chunk_slot = usize::MAX;
478
+ self.short_load = true;
479
+ }
480
+ }
383
481
  }
384
482
  loop {
385
483
  let Some(cur) = &self.cur else {
@@ -402,8 +500,19 @@ impl FeedCursor {
402
500
  self.load(slot, frame)?;
403
501
  let at = (frame - self.chunk_start) as usize;
404
502
  let bytes = &self.chunk[at..at + FRAME_SIZE];
503
+ // `load` left the frame fresh at least to its start.
504
+ let fresh = (self.fresh_end - frame) as usize;
505
+ let cur = self.cur.as_mut().expect("checked above");
506
+ let decoded = match cur.dec.decode_within(bytes, fresh)? {
507
+ Within::Decoded(d) => d,
508
+ // A fragment reaches past what was read again: the rest of the frame.
509
+ Within::More => {
510
+ self.read_tail(slot, frame + FRAME)?;
511
+ continue;
512
+ }
513
+ };
405
514
  let cur = self.cur.as_mut().expect("checked above");
406
- match cur.dec.decode(bytes)? {
515
+ match decoded {
407
516
  Some(Decoded::Record { offset }) if cut.is_some_and(|c| offset >= c) => {
408
517
  // A frame past the cut (above): never delivered.
409
518
  self.cur = None;
@@ -417,7 +526,7 @@ impl FeedCursor {
417
526
  let start = Lsn::new(epoch, last_end as u32);
418
527
  let session = cur.session;
419
528
  let cur = self.cur.as_ref().expect("checked above");
420
- let record = WalRecord::decode(cur.dec.record())?;
529
+ let record = WalRecord::decode_with(cur.dec.record(), self.silent_ok)?;
421
530
  return Ok(Step::Record {
422
531
  start,
423
532
  end: Lsn::new(epoch, end as u32),
@@ -450,7 +559,10 @@ impl FeedCursor {
450
559
  }
451
560
  Chained::No => {}
452
561
  }
453
- // Not moved: resume just past the last complete record next time.
562
+ // Not moved: resume just past the last complete record next time. Every re-decode of
563
+ // this segment after a stop comes back through `stopped`'s resume, which trusts the
564
+ // cached frame only below `last_end` (#1901), so a deferred stop's second look
565
+ // (#1370) reads the gap fresh rather than stopping again on bytes read before it.
454
566
  let cur = self.cur.as_mut().expect("refresh kept it");
455
567
  cur.dec = FrameDecoder::new(epoch, cur.last_end);
456
568
  cur.frame = cur.last_end - cur.last_end % FRAME;
@@ -524,11 +636,114 @@ mod read_tests {
524
636
  }
525
637
  }
526
638
 
639
+ /// D327 (feed1248's S1 on #1925): a cursor over a hand-written pad. Records appended
640
+ /// through a stream, then `[pad, record]` written straight into the segment past them,
641
+ /// as the writer will frame them (nothing writes a pad before D327's writer PR). The
642
+ /// cursor reads the padded record in order, reporting its resume position before the pad
643
+ /// (its fragment starts past it); a cursor
644
+ /// opened at the record before's end or at the padded record's start reads it; and a
645
+ /// pad whose record isn't written yet (a live tail seen between the pad's page and the
646
+ /// record's) stops at the last record's end, then resumes to the record once it is; and a
647
+ /// cursor moved by `skip_to` to the pre-pad end steps over the pad to it (#1827).
648
+ #[test]
649
+ fn a_cursor_steps_over_a_pad_and_resumes_on_either_side_of_it() {
650
+ // The padded record's bytes, framed from the stream's end (the first segment is
651
+ // slot 0, at the file's start).
652
+ let record = |seqno: u64| {
653
+ let mut b = BatchBuilder::new();
654
+ b.push(
655
+ TableId(1),
656
+ FamilyId(2),
657
+ Kind::Put,
658
+ b"row",
659
+ b"q",
660
+ Some(seqno),
661
+ b"v",
662
+ )
663
+ .unwrap();
664
+ let mut out = Vec::new();
665
+ WalRecord::Batch {
666
+ seqno,
667
+ commit_ts: seqno,
668
+ batch: b.batch(),
669
+ }
670
+ .encode(&mut out);
671
+ out
672
+ };
673
+ for unit in [4096usize, FRAME as usize] {
674
+ let vfs: VfsRef = SimVfs::new(10);
675
+ let mut wal = stream(&vfs, 64);
676
+ append(&mut wal, 1, 8);
677
+ append(&mut wal, 2, 8);
678
+ let tail = wal.written();
679
+ drop(wal);
680
+ let mut enc = FrameEncoder::new(tail.epoch(), u64::from(tail.offset()));
681
+ let mut pad = Vec::new();
682
+ let pad_len = enc.pad(unit, &mut pad);
683
+ assert!(pad_len > 0);
684
+ let mut rec = Vec::new();
685
+ let rec_end = enc.encode(&record(3), &mut rec);
686
+ let rec_start = u64::from(tail.offset()) + pad_len as u64;
687
+ assert_eq!(rec_start % unit as u64, 0, "{unit}");
688
+ let file = vfs
689
+ .open(
690
+ &stream_path(db(), StreamId(0)),
691
+ OpenOptions::read_write_create(),
692
+ )
693
+ .unwrap();
694
+ // (c) The pad alone: the live tail stops at the last record's end.
695
+ file.write_at(&pad, u64::from(tail.offset())).unwrap();
696
+ let mut c = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, Lsn::default()).unwrap();
697
+ assert_eq!(drain(&mut c), [1, 2], "{unit}");
698
+ assert_eq!(c.position(), tail, "stopped before the pad");
699
+ // Then the record: the same cursor resumes over the pad to it.
700
+ file.write_at(&rec, rec_start).unwrap();
701
+ match c.step().unwrap() {
702
+ Step::Record {
703
+ start,
704
+ end,
705
+ record: WalRecord::Batch { seqno: 3, .. },
706
+ ..
707
+ } => {
708
+ // `start` is the resume position, before the pad (a power loss that took
709
+ // the record cuts the log there too, #1929's feed_model sweep).
710
+ assert_eq!(start, tail, "{unit}");
711
+ assert_eq!(end, Lsn::new(tail.epoch(), rec_end as u32));
712
+ }
713
+ other => panic!("{unit}: {other:?}"),
714
+ }
715
+ assert!(drain(&mut c).is_empty());
716
+ // (a) A fresh cursor reads all three in order.
717
+ let mut c = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, Lsn::default()).unwrap();
718
+ assert_eq!(drain(&mut c), [1, 2, 3], "{unit}");
719
+ // (b) Opened at the record before's end (before the pad) or at the padded
720
+ // record's start (past it), a cursor reads it.
721
+ for at in [tail, Lsn::new(tail.epoch(), rec_start as u32)] {
722
+ let mut c = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, at).unwrap();
723
+ assert_eq!(drain(&mut c), [3], "{unit} from {at:?}");
724
+ }
725
+ // (d) A cursor moved with `skip_to` to the record before's end (#1827's skip index
726
+ // jumps to a chunk's end, before the next record's pad) steps over the pad to it.
727
+ let mut c = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, Lsn::default()).unwrap();
728
+ assert!(c.skip_to(tail).unwrap(), "{unit}");
729
+ match c.step().unwrap() {
730
+ Step::Record {
731
+ start,
732
+ record: WalRecord::Batch { seqno: 3, .. },
733
+ ..
734
+ } => assert_eq!(start, tail, "{unit}"),
735
+ other => panic!("{unit}: {other:?}"),
736
+ }
737
+ }
738
+ }
739
+
527
740
  /// D310 PR 5b (the subscriber cost on the AX102): a stop reads the segment headers once,
528
- /// not on resuming and again at the stop, and a resume reads one frame, not a 1 MiB
529
- /// chunk. Without the writer's marks (a reader process) every stop reads the headers.
741
+ /// not on resuming and again at the stop. A resume reads only past where it stopped, a
742
+ /// window to the next 4 KiB page at least 512 bytes on (#1901: it was the whole 32 KiB
743
+ /// frame, half of a hub thread in g111), not a frame or a 1 MiB chunk. Without the
744
+ /// writer's marks (a reader process) every stop reads the headers.
530
745
  #[test]
531
- fn a_stop_reads_the_headers_once_and_a_resume_one_frame() {
746
+ fn a_stop_reads_the_headers_once_and_a_resume_only_the_tail() {
532
747
  let vfs: VfsRef = SimVfs::new(5);
533
748
  let mut wal = stream(&vfs, 64);
534
749
  append(&mut wal, 1, 8);
@@ -538,7 +753,11 @@ mod read_tests {
538
753
  let before = c.reads;
539
754
  assert!(drain(&mut c).is_empty());
540
755
  assert_eq!(c.reads.0 - before.0, 1, "one header read per idle stop");
541
- assert_eq!(c.reads.1 - before.1, FRAME, "one frame per resume");
756
+ let tail = c.reads.1 - before.1;
757
+ assert!(
758
+ (TAIL_MIN..=TAIL_MIN + TAIL_PAGE).contains(&tail),
759
+ "a resume read {tail} bytes: one tail window, not a frame"
760
+ );
542
761
  }
543
762
  for s in 2..=4 {
544
763
  append(&mut wal, s, 8);
@@ -552,6 +771,40 @@ mod read_tests {
552
771
  );
553
772
  }
554
773
 
774
+ /// #1901: a record that runs past a resume's tail window is read whole: the decoder asks
775
+ /// for more rather than stopping on the cached bytes past the window (zeros when they were
776
+ /// read, the record now), and the rest of the frame is read once. A small record after it
777
+ /// costs one window again.
778
+ #[test]
779
+ fn a_resume_reads_a_record_past_its_tail_window_whole() {
780
+ let vfs: VfsRef = SimVfs::new(9);
781
+ let mut wal = stream(&vfs, 64);
782
+ append(&mut wal, 1, 8);
783
+ let mut c = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, Lsn::default()).unwrap();
784
+ assert_eq!(drain(&mut c), [1]);
785
+ // Cached past the stop: the frame as it was, zeros after record 1.
786
+ assert!(drain(&mut c).is_empty());
787
+ // A record of 12 KB starts right where the cursor stopped: past any window.
788
+ append(&mut wal, 2, 12_000);
789
+ let before = c.reads;
790
+ assert_eq!(drain(&mut c), [2], "read whole, not stopped at the window");
791
+ let read = c.reads.1 - before.1;
792
+ assert!(
793
+ read > TAIL_PAGE && read <= FRAME,
794
+ "the window, then the rest of the frame once: {read} bytes"
795
+ );
796
+ append(&mut wal, 3, 8);
797
+ let before = c.reads;
798
+ assert_eq!(drain(&mut c), [3]);
799
+ let n = c.reads.1 - before.1;
800
+ assert!(
801
+ n <= TAIL_MIN + TAIL_PAGE,
802
+ "one window for a small record: {n} bytes, {} header reads, at {:?}",
803
+ c.reads.0 - before.0,
804
+ c.position()
805
+ );
806
+ }
807
+
555
808
  /// With the writer's marks (`follow_head`), a stop in the head segment reads no headers,
556
809
  /// idle or with new records.
557
810
  #[test]
@@ -614,4 +867,45 @@ mod read_tests {
614
867
  }
615
868
  assert!(wal.written().epoch() > 5, "the stream rolled over");
616
869
  }
870
+
871
+ /// #1827: a cursor moved to a record boundary another cursor reported, in its own
872
+ /// segment or a later one, reads on from there. (A skip into a recycled segment is the
873
+ /// engine's `it::feed_reader` T3.)
874
+ #[test]
875
+ fn a_skip_reads_on_from_a_boundary_in_this_or_a_later_segment() {
876
+ let vfs: VfsRef = SimVfs::new(8);
877
+ // 20 KB records, one a 32 KB frame and 4 frames a segment: 3 records a segment.
878
+ let mut wal = stream(&vfs, 4);
879
+ wal.spares().prepare(4).unwrap();
880
+ for seqno in 1..=9 {
881
+ append(&mut wal, seqno, 20_000);
882
+ }
883
+ let mut first = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, Lsn::default()).unwrap();
884
+ let mut ends = vec![first.position()];
885
+ loop {
886
+ match first.step().unwrap() {
887
+ Step::Record { end, .. } => ends.push(end),
888
+ Step::End => break,
889
+ other => panic!("{other:?}"),
890
+ }
891
+ }
892
+ assert_eq!(ends.len(), 10);
893
+ assert_ne!(
894
+ ends[1].epoch(),
895
+ ends[7].epoch(),
896
+ "the records span segments"
897
+ );
898
+ let mut c = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, Lsn::default()).unwrap();
899
+ // Within the first segment: past seqno 1, to 2.
900
+ assert!(c.skip_to(ends[1]).unwrap());
901
+ assert_eq!(
902
+ drain(&mut c).first(),
903
+ Some(&2),
904
+ "read on from the boundary after 1"
905
+ );
906
+ // Back to the start's segment, then ahead into a later one: past 7, to 8 and 9.
907
+ let mut c = FeedCursor::open(&vfs, db(), StreamId(0), DB_ID, Lsn::default()).unwrap();
908
+ assert!(c.skip_to(ends[7]).unwrap());
909
+ assert_eq!(drain(&mut c), [8, 9]);
910
+ }
617
911
  }
@@ -92,6 +92,10 @@ pub struct Recovery {
92
92
  chunk_start: u64,
93
93
  /// Opened by [`Recovery::open`] (else [`Recovery::open_read_only`]).
94
94
  writable: bool,
95
+ /// Whether a feed-silent batch's mark is taken ([`Recovery::accept_silent`]).
96
+ silent_ok: bool,
97
+ /// Whether a batch's xid trailer is taken (D329 2b, [`Recovery::accept_xid`]).
98
+ xid_ok: bool,
95
99
  }
96
100
 
97
101
  #[derive(Debug)]
@@ -126,6 +130,15 @@ fn seqno_of(rec: &WalRecord<'_>) -> Seqno {
126
130
  WalRecord::Decision {
127
131
  seqno, apply_seqno, ..
128
132
  } => (*seqno).max(*apply_seqno),
133
+ // An external PREPARE's id, and an external abort's own seqno (D329, ICR 0140: E1, E2),
134
+ // so a reopen never hands either out again.
135
+ WalRecord::ExternalPrepare { seqno, .. } => *seqno,
136
+ WalRecord::ExternalAbort {
137
+ seqno, abort_seqno, ..
138
+ } => (*seqno).max(*abort_seqno),
139
+ // A range drop's own seqno (D331): nothing applies there, and a reopen never hands it
140
+ // out again.
141
+ WalRecord::RangeDrop { seqno, .. } => *seqno,
129
142
  // `WalRecord::decode` returns no other type in this build (a newer one is corrupt).
130
143
  _ => 0,
131
144
  }
@@ -214,6 +227,8 @@ impl Recovery {
214
227
  chunk_slot: usize::MAX,
215
228
  chunk_start: 0,
216
229
  writable,
230
+ silent_ok: false,
231
+ xid_ok: true,
217
232
  };
218
233
  if missing {
219
234
  // The checkpoint's segment never reached the disk: everything before the
@@ -319,11 +334,26 @@ impl Recovery {
319
334
  }
320
335
  }
321
336
  let cur = self.cur.as_ref().expect("a record was just decoded");
322
- let record = WalRecord::decode(cur.dec.record())?;
337
+ let record = WalRecord::decode_flags(cur.dec.record(), self.silent_ok, self.xid_ok)?;
323
338
  self.max_seqno = self.max_seqno.max(seqno_of(&record));
324
339
  Ok(Some((self.end, record)))
325
340
  }
326
341
 
342
+ /// Takes feed-silent batches' mark (D331, ICR 0141): set when the superblock has
343
+ /// `incompat` bit 3 ([`INCOMPAT_FEED_SILENT`]). Without it a marked batch is corrupt.
344
+ ///
345
+ /// [`INCOMPAT_FEED_SILENT`]: pigeonhole_format::wal::INCOMPAT_FEED_SILENT
346
+ pub fn accept_silent(&mut self, ok: bool) {
347
+ self.silent_ok = ok;
348
+ }
349
+
350
+ /// Takes a batch's xid trailer (D329 2b, `XID_BATCH`): set when the superblock has
351
+ /// `incompat` bit 2. Without it a tagged batch is corrupt. On by default, for a reader
352
+ /// that doesn't know the database's bits.
353
+ pub fn accept_xid(&mut self, ok: bool) {
354
+ self.xid_ok = ok;
355
+ }
356
+
327
357
  /// The stream file's slot size, `None` while it holds no segment. An existing file keeps
328
358
  /// its slot size: [`Recovery::into_stream`] uses `opts.segment_size` only without one, so
329
359
  /// a retention in bytes is counted in this size's segments (#1471).
@@ -460,6 +460,9 @@ pub struct FeedMarks {
460
460
  /// The retention in effect: retained segments times the stream file's own slot size.
461
461
  retention: AtomicU64,
462
462
  checkpoint: AtomicU64,
463
+ /// The oldest epoch a retention hold keeps (D331, ICR 0141): retention trims nothing from
464
+ /// it on while it's set. 0 (no epoch is 0) when nothing holds the stream.
465
+ hold: AtomicU32,
463
466
  /// Where the marks are published for other processes (D310 PR 4), once set.
464
467
  mirror: std::sync::OnceLock<Box<dyn MarkMirror>>,
465
468
  }
@@ -531,6 +534,26 @@ impl FeedMarks {
531
534
  }
532
535
  }
533
536
 
537
+ /// Keeps retention from trimming the segment of `epoch` or any later one (D331, ICR 0141:
538
+ /// a range move's catch-up reads the source's history from its copy's first snapshot),
539
+ /// from the stream's next retention pass on; `None` lets it go. Take it at an epoch the
540
+ /// stream still has: the engine takes its checkpoint's, which retention never trims,
541
+ /// since a snapshot's feed token starts the stream's scan there (#2107's tests). A held
542
+ /// stream's file grows by what its writes add meanwhile, so the holder bounds it
543
+ /// ([`retained_bytes`](Self::retained_bytes)). Without retention, nothing is trimmed for a
544
+ /// feed and the hold changes nothing.
545
+ pub fn set_hold(&self, epoch: Option<u32>) {
546
+ self.hold.store(epoch.unwrap_or(0), Ordering::Release);
547
+ }
548
+
549
+ /// The epoch [`set_hold`](Self::set_hold) holds from, if any.
550
+ pub fn hold(&self) -> Option<u32> {
551
+ match self.hold.load(Ordering::Acquire) {
552
+ 0 => None,
553
+ e => Some(e),
554
+ }
555
+ }
556
+
534
557
  fn set_head(&self, epoch: u32) {
535
558
  self.head.store(epoch, Ordering::Release);
536
559
  }
@@ -1812,6 +1835,11 @@ impl WalStream {
1812
1835
  }
1813
1836
  }
1814
1837
  }
1838
+ // A retention hold (D331) keeps its epoch on: the floor stops below it, and never goes
1839
+ // down (a segment already past it may be reused).
1840
+ if let Some(held) = self.marks.hold() {
1841
+ floor = floor.min(held).max(self.retain_floor);
1842
+ }
1815
1843
  self.retain_floor = floor;
1816
1844
  }
1817
1845
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pigeonhole-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Tanous
@@ -1295,12 +1295,19 @@ files:
1295
1295
  - vendor/engine/src/engine/hooks.rs
1296
1296
  - vendor/engine/src/engine/info.rs
1297
1297
  - vendor/engine/src/error.rs
1298
+ - vendor/engine/src/external_registry.rs
1298
1299
  - vendor/engine/src/feed.rs
1299
1300
  - vendor/engine/src/feed_reader.rs
1301
+ - vendor/engine/src/feed_reader/catalog.rs
1302
+ - vendor/engine/src/feed_reader/history.rs
1303
+ - vendor/engine/src/feed_reader/read_ahead.rs
1304
+ - vendor/engine/src/feed_reader/token.rs
1305
+ - vendor/engine/src/feed_stop_loom.rs
1300
1306
  - vendor/engine/src/flush.rs
1301
1307
  - vendor/engine/src/flushed_roots.rs
1302
1308
  - vendor/engine/src/flushed_through_loom.rs
1303
1309
  - vendor/engine/src/health.rs
1310
+ - vendor/engine/src/io_count.rs
1304
1311
  - vendor/engine/src/large.rs
1305
1312
  - vendor/engine/src/large_settle.rs
1306
1313
  - vendor/engine/src/lib.rs
@@ -1311,7 +1318,9 @@ files:
1311
1318
  - vendor/engine/src/nonblocking.rs
1312
1319
  - vendor/engine/src/options.rs
1313
1320
  - vendor/engine/src/position.rs
1321
+ - vendor/engine/src/punch.rs
1314
1322
  - vendor/engine/src/purge_guard.rs
1323
+ - vendor/engine/src/range_copy.rs
1315
1324
  - vendor/engine/src/read.rs
1316
1325
  - vendor/engine/src/read/keys.rs
1317
1326
  - vendor/engine/src/read/knn.rs
@@ -1367,6 +1376,7 @@ files:
1367
1376
  - vendor/io/src/async_poll.rs
1368
1377
  - vendor/io/src/buf.rs
1369
1378
  - vendor/io/src/completion.rs
1379
+ - vendor/io/src/count.rs
1370
1380
  - vendor/io/src/error.rs
1371
1381
  - vendor/io/src/file.rs
1372
1382
  - vendor/io/src/lib.rs
@@ -1395,6 +1405,7 @@ files:
1395
1405
  - vendor/memory/src/layout.rs
1396
1406
  - vendor/memory/src/lib.rs
1397
1407
  - vendor/memory/src/recall.rs
1408
+ - vendor/memory/src/recall/pool.rs
1398
1409
  - vendor/memory/src/store.rs
1399
1410
  - vendor/memory/src/watch.rs
1400
1411
  - vendor/memtable/Cargo.toml
@@ -1462,6 +1473,7 @@ files:
1462
1473
  - vendor/text/Cargo.toml
1463
1474
  - vendor/text/LICENSE
1464
1475
  - vendor/text/README.md
1476
+ - vendor/text/src/block.rs
1465
1477
  - vendor/text/src/error.rs
1466
1478
  - vendor/text/src/fuzzy.rs
1467
1479
  - vendor/text/src/highlight.rs
@@ -1469,9 +1481,11 @@ files:
1469
1481
  - vendor/text/src/layout.rs
1470
1482
  - vendor/text/src/lib.rs
1471
1483
  - vendor/text/src/maxscore.rs
1484
+ - vendor/text/src/merged.rs
1472
1485
  - vendor/text/src/query.rs
1473
1486
  - vendor/text/src/search.rs
1474
1487
  - vendor/text/src/sloppy.rs
1488
+ - vendor/text/src/stats.rs
1475
1489
  - vendor/text/src/tokenizer.rs
1476
1490
  - vendor/vector/Cargo.toml
1477
1491
  - vendor/vector/LICENSE