Diffinery

homeflare/celldpublic

176 lines · 5.7 KBRaw History
1//! A restore view over the epochs of one cell.
2//!
3//! A paged activation continues the chain it paged in: its first object
4//! follows the cut's last txid instead of opening the epoch with a
5//! whole-database snapshot (see [`crate::db::Db::seed_continuation`]). The
6//! objects a restore needs can therefore span epochs. The chain walks down
7//! from the newest epoch until one that opens with a snapshot, lists every
8//! level of each visited epoch once, clips an epoch to the txids before the
9//! next epoch's first (a fold that landed after the cut was never part of
10//! it), and routes every read by txid. The planner and the page map then
11//! run over it unchanged.
12use crate::client::ReplicaClient;
13use crate::compaction_level::SNAPSHOT_LEVEL;
14use crate::error::Error;
15use crate::error::Result;
16use crate::ltx::FileInfo;
17use crate::TXID;
18use async_trait::async_trait;
19use futures_util::stream;
20use futures_util::StreamExt;
21use futures_util::TryStreamExt;
22
23/// Listings per epoch in flight while a chain builds.
24const LIST_CONCURRENCY: usize = 4;
25
26struct Span<C> {
27 epoch: u64,
28 client: C,
29 /// The first txid this epoch serves; the span before it ends just short.
30 lo: TXID,
31 /// Files by level, clipped to this span.
32 levels: Vec<Vec<FileInfo>>,
33}
34
35pub struct EpochChain<C> {
36 spans: Vec<Span<C>>,
37}
38
39impl<C: ReplicaClient> EpochChain<C> {
40 /// Walks `epochs` (ascending) down from the newest until an epoch that
41 /// opens with a snapshot, listing every level of each visited epoch. The
42 /// link below a continued epoch is the epoch whose objects, clipped to
43 /// the txids before the continuation's first, end exactly at the cut; an
44 /// epoch below that ends elsewhere is skipped. A fenced owner's late
45 /// snapshot can land in an intermediate epoch, whole but older than the
46 /// cut, and taking it as the base loses the writes between it and the
47 /// cut (CelldPersistencePaged.tla, `BreakLinkByOrder`). An epoch without
48 /// objects contributes nothing; no chain at all is `TxNotAvailable`.
49 pub async fn build(mut epochs: Vec<(u64, C)>) -> Result<Self> {
50 let mut spans: Vec<Span<C>> = Vec::new();
51 while let Some((epoch, client)) = epochs.pop() {
52 let mut levels = list_levels(&client).await?;
53 if let Some(above) = spans.last() {
54 // Clip to the txids before the continuation's first, then
55 // the link must end at the cut.
56 for files in &mut levels {
57 files.retain(|f| f.max_txid < above.lo);
58 }
59 let ends = levels.iter().flatten().map(|f| f.max_txid).max();
60 if ends != Some(TXID(above.lo.0 - 1)) {
61 continue;
62 }
63 }
64 let Some(lo) = levels.iter().flatten().map(|f| f.min_txid).min() else {
65 continue;
66 };
67 spans.push(Span {
68 epoch,
69 client,
70 lo,
71 levels,
72 });
73 if lo == TXID(1) {
74 break;
75 }
76 }
77 if spans.is_empty() {
78 return Err(Error::TxNotAvailable);
79 }
80 spans.reverse();
81 Ok(Self { spans })
82 }
83
84 /// The epochs in the chain, oldest first, each with the first txid it
85 /// serves.
86 pub fn spans(&self) -> Vec<(u64, TXID)> {
87 self.spans.iter().map(|s| (s.epoch, s.lo)).collect()
88 }
89
90 /// The last txid of the chain: the cut a continuation starts after.
91 pub fn max_txid(&self) -> TXID {
92 self.spans
93 .iter()
94 .flat_map(|s| s.levels.iter().flatten())
95 .map(|f| f.max_txid)
96 .max()
97 .unwrap_or(TXID(0))
98 }
99
100 fn span(&self, txid: TXID) -> &Span<C> {
101 self.spans
102 .iter()
103 .rev()
104 .find(|s| s.lo <= txid)
105 .unwrap_or(&self.spans[0])
106 }
107}
108
109async fn list_levels<C: ReplicaClient>(client: &C) -> Result<Vec<Vec<FileInfo>>> {
110 stream::iter(0..=SNAPSHOT_LEVEL)
111 .map(|level| async move { client.ltx_files(level, TXID(0)).await })
112 .buffered(LIST_CONCURRENCY)
113 .try_collect()
114 .await
115}
116
117#[async_trait]
118impl<C: ReplicaClient> ReplicaClient for EpochChain<C> {
119 async fn ltx_files(&self, level: i32, seek: TXID) -> Result<Vec<FileInfo>> {
120 Ok(self
121 .spans
122 .iter()
123 .flat_map(|s| s.levels.get(level as usize).into_iter().flatten())
124 .filter(|f| f.min_txid >= seek)
125 .cloned()
126 .collect())
127 }
128
129 async fn open_ltx_file(&self, level: i32, min_txid: TXID, max_txid: TXID) -> Result<Vec<u8>> {
130 self.span(min_txid)
131 .client
132 .open_ltx_file(level, min_txid, max_txid)
133 .await
134 }
135
136 async fn read_range(
137 &self,
138 level: i32,
139 min_txid: TXID,
140 max_txid: TXID,
141 offset: u64,
142 len: u64,
143 ) -> Result<Vec<u8>> {
144 self.span(min_txid)
145 .client
146 .read_range(level, min_txid, max_txid, offset, len)
147 .await
148 }
149
150 async fn write_ltx_file(
151 &self,
152 level: i32,
153 min_txid: TXID,
154 max_txid: TXID,
155 data: &[u8],
156 ) -> Result<FileInfo> {
157 self.span(min_txid)
158 .client
159 .write_ltx_file(level, min_txid, max_txid, data)
160 .await
161 }
162
163 async fn delete_ltx_files(&self, files: &[FileInfo]) -> Result<()> {
164 for file in files {
165 self.span(file.min_txid)
166 .client
167 .delete_ltx_files(std::slice::from_ref(file))
168 .await?;
169 }
170 Ok(())
171 }
172
173 async fn delete_all(&self) -> Result<()> {
174 Err(Error::Other("an epoch chain is a restore view".into()))
175 }
176}