balius_runtime/
store.rs

1use itertools::Itertools;
2use prost::Message;
3use redb::{ReadableTable as _, TableDefinition, WriteTransaction};
4use std::{path::Path, sync::Arc};
5use tracing::warn;
6
7use crate::{Block, ChainPoint, Error};
8
9pub type WorkerId = String;
10pub type LogSeq = u64;
11
12#[derive(Message)]
13pub struct LogEntry {
14    #[prost(bytes, tag = "1")]
15    pub next_block: Vec<u8>,
16    #[prost(bytes, repeated, tag = "2")]
17    pub undo_blocks: Vec<Vec<u8>>,
18}
19
20impl redb::Value for LogEntry {
21    type SelfType<'a>
22        = LogEntry
23    where
24        Self: 'a;
25
26    type AsBytes<'a>
27        = Vec<u8>
28    where
29        Self: 'a;
30
31    fn fixed_width() -> Option<usize> {
32        None
33    }
34
35    fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
36    where
37        Self: 'a,
38    {
39        prost::Message::decode(data).unwrap()
40    }
41
42    fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
43    where
44        Self: 'a,
45        Self: 'b,
46    {
47        value.encode_to_vec()
48    }
49
50    fn type_name() -> redb::TypeName {
51        redb::TypeName::new("LogEntry")
52    }
53}
54
55const CURSORS: TableDefinition<WorkerId, LogSeq> = TableDefinition::new("cursors");
56const WAL: TableDefinition<LogSeq, LogEntry> = TableDefinition::new("wal");
57
58const DEFAULT_CACHE_SIZE_MB: usize = 50;
59
60pub struct AtomicUpdate {
61    wx: WriteTransaction,
62    log_seq: LogSeq,
63}
64
65impl AtomicUpdate {
66    pub fn update_worker_cursor(&mut self, id: &str) -> Result<(), super::Error> {
67        let mut table = self.wx.open_table(CURSORS)?;
68        table.insert(id.to_owned(), self.log_seq)?;
69
70        Ok(())
71    }
72
73    pub fn commit(self) -> Result<(), super::Error> {
74        self.wx.commit()?;
75        Ok(())
76    }
77}
78
79#[derive(Clone)]
80pub struct Store {
81    db: Arc<redb::Database>,
82    log_seq: LogSeq,
83}
84
85impl Store {
86    pub fn in_memory() -> Result<Self, super::Error> {
87        let db = Arc::new(
88            redb::Database::builder().create_with_backend(redb::backends::InMemoryBackend::new())?,
89        );
90        Ok(Self { db, log_seq: 0 })
91    }
92
93    pub fn open(path: impl AsRef<Path>, cache_size: Option<usize>) -> Result<Self, super::Error> {
94        let inner = redb::Database::builder()
95            .set_repair_callback(|x| {
96                warn!(progress = x.progress() * 100f64, "balius db is repairing")
97            })
98            .set_cache_size(1024 * 1024 * cache_size.unwrap_or(DEFAULT_CACHE_SIZE_MB))
99            .create(path)?;
100
101        let log_seq = Self::load_log_seq(&inner)?.unwrap_or_default();
102
103        let out = Self {
104            db: Arc::new(inner),
105            log_seq,
106        };
107
108        Ok(out)
109    }
110
111    pub fn into_ephemeral(&mut self) -> Result<Self, super::Error> {
112        let new_db =
113            redb::Database::builder().create_with_backend(redb::backends::InMemoryBackend::new())?;
114
115        let rx = self.db.begin_read()?;
116        let wx = new_db.begin_write()?;
117
118        {
119            let source = rx.open_table(WAL)?;
120            let mut target = wx.open_table(WAL)?;
121
122            for entry in source.iter()? {
123                let (k, v) = entry?;
124                target.insert(k.value(), v.value())?;
125            }
126
127            let source = rx.open_table(CURSORS)?;
128            let mut target = wx.open_table(CURSORS)?;
129
130            for entry in source.iter()? {
131                let (k, v) = entry?;
132                target.insert(k.value(), v.value())?;
133            }
134        }
135
136        wx.commit()?;
137
138        let log_seq = Self::load_log_seq(&new_db)?.unwrap_or_default();
139        let new = Store {
140            db: Arc::new(new_db),
141            log_seq,
142        };
143
144        Ok(new)
145    }
146
147    fn load_log_seq(db: &redb::Database) -> Result<Option<LogSeq>, Error> {
148        let rx = db.begin_read()?;
149
150        match rx.open_table(WAL) {
151            Ok(table) => {
152                let last = table.last()?;
153                Ok(last.map(|(k, _)| k.value()))
154            }
155            Err(redb::TableError::TableDoesNotExist(_)) => Ok(None),
156            Err(e) => Err(e.into()),
157        }
158    }
159
160    fn get_entry(&self, seq: LogSeq) -> Result<Option<LogEntry>, Error> {
161        let rx = self.db.begin_read()?;
162        let table = rx.open_table(WAL)?;
163        let entry = table.get(seq)?;
164        Ok(entry.map(|x| x.value()))
165    }
166
167    pub fn find_chain_point(&self, seq: LogSeq) -> Result<Option<ChainPoint>, Error> {
168        let entry = self.get_entry(seq)?;
169        let block = Block::from_bytes(&entry.unwrap().next_block);
170
171        Ok(Some(block.chain_point()))
172    }
173
174    pub fn write_ahead(
175        &mut self,
176        undo_blocks: &[Block],
177        next_block: &Block,
178    ) -> Result<LogSeq, Error> {
179        self.log_seq += 1;
180
181        let wx = self.db.begin_write()?;
182        {
183            wx.open_table(WAL)?.insert(
184                self.log_seq,
185                LogEntry {
186                    next_block: next_block.to_bytes(),
187                    undo_blocks: undo_blocks.iter().map(|x| x.to_bytes()).collect(),
188                },
189            )?;
190        }
191
192        wx.commit()?;
193        Ok(self.log_seq)
194    }
195
196    // TODO: see if loading in batch is worth it
197    pub fn get_worker_cursor(&self, id: &str) -> Result<Option<LogSeq>, super::Error> {
198        let rx = self.db.begin_read()?;
199
200        let table = match rx.open_table(CURSORS) {
201            Ok(table) => table,
202            Err(redb::TableError::TableDoesNotExist(_)) => return Ok(None),
203            Err(e) => return Err(e.into()),
204        };
205
206        let cursor = table.get(id.to_owned())?;
207        Ok(cursor.map(|x| x.value()))
208    }
209
210    pub fn start_atomic_update(&self, log_seq: LogSeq) -> Result<AtomicUpdate, super::Error> {
211        let wx = self.db.begin_write()?;
212        Ok(AtomicUpdate { wx, log_seq })
213    }
214
215    // TODO: I don't think we need this since we're going to load each cursor as
216    // part of the loaded worker
217    pub fn lowest_cursor(&self) -> Result<Option<LogSeq>, super::Error> {
218        let rx = self.db.begin_read()?;
219
220        let table = rx.open_table(CURSORS)?;
221
222        let cursors: Vec<_> = table
223            .iter()?
224            .map_ok(|(_, value)| value.value())
225            .try_collect()?;
226
227        let lowest = cursors.iter().fold(None, |all, item| all.min(Some(*item)));
228
229        Ok(lowest)
230    }
231}