tycho_core/storage/persistent_state/
mod.rs

1use std::collections::{BTreeMap, VecDeque};
2use std::fs::File;
3use std::io::{Seek, Write};
4use std::num::{NonZeroU32, NonZeroU64};
5use std::path::PathBuf;
6use std::sync::Arc;
7
8use anyhow::{Context, Result};
9use arc_swap::ArcSwapAny;
10use dashmap::DashMap;
11use parking_lot::Mutex;
12use tokio::sync::{Notify, Semaphore};
13use tokio::time::Instant;
14use tycho_block_util::block::BlockStuff;
15use tycho_block_util::queue::QueueStateHeader;
16use tycho_block_util::state::RefMcStateHandle;
17use tycho_storage::fs::{Dir, MappedFile};
18use tycho_types::models::{BlockId, PrevBlockRef};
19use tycho_util::FastHashSet;
20use tycho_util::sync::CancellationFlag;
21
22pub use self::queue_state::reader::{QueueDiffReader, QueueStateReader};
23pub use self::queue_state::writer::QueueStateWriter;
24pub use self::shard_state::reader::{BriefBocHeader, ShardStateReader};
25pub use self::shard_state::writer::ShardStateWriter;
26use super::{
27    BlockHandle, BlockHandleStorage, BlockStorage, CoreDb, KeyBlocksDirection, ShardStateStorage,
28};
29
30mod queue_state {
31    pub mod reader;
32    pub mod writer;
33}
34mod shard_state {
35    pub mod reader;
36    pub mod writer;
37}
38
39#[cfg(test)]
40mod tests;
41
42const BASE_DIR: &str = "states";
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45pub enum PersistentStateKind {
46    Shard,
47    Queue,
48}
49
50impl PersistentStateKind {
51    fn make_file_name(&self, block_id: &BlockId) -> PathBuf {
52        match self {
53            Self::Shard => ShardStateWriter::file_name(block_id),
54            Self::Queue => QueueStateWriter::file_name(block_id),
55        }
56    }
57
58    fn make_temp_file_name(&self, block_id: &BlockId) -> PathBuf {
59        match self {
60            Self::Shard => ShardStateWriter::temp_file_name(block_id),
61            Self::Queue => QueueStateWriter::temp_file_name(block_id),
62        }
63    }
64
65    fn from_extension(extension: &str) -> Option<Self> {
66        match extension {
67            ShardStateWriter::FILE_EXTENSION => Some(Self::Shard),
68            QueueStateWriter::FILE_EXTENSION => Some(Self::Queue),
69            _ => None,
70        }
71    }
72}
73
74#[derive(Debug, Eq, Hash, PartialEq)]
75struct CacheKey {
76    block_id: BlockId,
77    kind: PersistentStateKind,
78}
79
80#[derive(Clone)]
81pub struct PersistentStateStorage {
82    inner: Arc<Inner>,
83}
84
85impl PersistentStateStorage {
86    pub fn new(
87        db: CoreDb,
88        files_dir: &Dir,
89        block_handle_storage: Arc<BlockHandleStorage>,
90        block_storage: Arc<BlockStorage>,
91        shard_state_storage: Arc<ShardStateStorage>,
92    ) -> Result<Self> {
93        const MAX_PARALLEL_CHUNK_READS: usize = 20;
94
95        let storage_dir = files_dir.create_subdir(BASE_DIR)?;
96
97        Ok(Self {
98            inner: Arc::new(Inner {
99                db,
100                storage_dir,
101                block_handles: block_handle_storage,
102                blocks: block_storage,
103                shard_states: shard_state_storage,
104                descriptor_cache: Default::default(),
105                mc_seqno_to_block_ids: Default::default(),
106                chunks_semaphore: Semaphore::new(MAX_PARALLEL_CHUNK_READS),
107                handles_queue: Default::default(),
108                oldest_ps_changed: Default::default(),
109                oldest_ps_handle: Default::default(),
110            }),
111        })
112    }
113
114    pub fn load_oldest_known_handle(&self) -> Option<BlockHandle> {
115        self.inner.oldest_ps_handle.load_full()
116    }
117
118    pub fn oldest_known_handle_changed(&self) -> tokio::sync::futures::Notified<'_> {
119        self.inner.oldest_ps_changed.notified()
120    }
121
122    #[tracing::instrument(skip_all)]
123    pub async fn preload(&self) -> Result<()> {
124        self.preload_handles_queue()?;
125        self.preload_states().await
126    }
127
128    fn preload_handles_queue(&self) -> Result<()> {
129        let this = self.inner.as_ref();
130
131        let block_handles = this.block_handles.as_ref();
132
133        let mut changed = false;
134        let mut prev_utime = 0;
135        for block_id in block_handles.key_blocks_iterator(KeyBlocksDirection::ForwardFrom(0)) {
136            let block_handle = block_handles
137                .load_handle(&block_id)
138                .context("key block handle not found")?;
139
140            let gen_utime = block_handle.gen_utime();
141            if BlockStuff::compute_is_persistent(gen_utime, prev_utime) {
142                prev_utime = gen_utime;
143
144                let mut queue = this.handles_queue.lock();
145                if queue.push(block_handle) {
146                    this.oldest_ps_handle.store(queue.oldest_known().cloned());
147                    changed = true;
148                }
149            }
150        }
151
152        if changed {
153            this.oldest_ps_changed.notify_waiters();
154        }
155        Ok(())
156    }
157
158    async fn preload_states(&self) -> Result<()> {
159        // For each mc_seqno directory
160        let process_states = |this: &Inner, dir: &PathBuf, mc_seqno: u32| -> Result<()> {
161            'outer: for entry in std::fs::read_dir(dir)?.flatten() {
162                let path = entry.path();
163                // Skip subdirectories
164                if path.is_dir() {
165                    tracing::warn!(path = %path.display(), "unexpected directory");
166                    continue;
167                }
168
169                'file: {
170                    // Try to parse the file name as a block_id
171                    let Ok(block_id) = path
172                        // TODO should use file_prefix
173                        .file_stem()
174                        .unwrap_or_default()
175                        .to_str()
176                        .unwrap_or_default()
177                        .parse::<BlockId>()
178                    else {
179                        break 'file;
180                    };
181
182                    let extension = path
183                        .extension()
184                        .and_then(|ext| ext.to_str())
185                        .unwrap_or_default();
186
187                    let Some(cache_type) = PersistentStateKind::from_extension(extension) else {
188                        break 'file;
189                    };
190
191                    this.cache_state(mc_seqno, &block_id, cache_type)?;
192                    continue 'outer;
193                }
194                tracing::warn!(path = %path.display(), "unexpected file");
195            }
196            Ok(())
197        };
198
199        let this = self.inner.clone();
200        let span = tracing::Span::current();
201        tokio::task::spawn_blocking(move || {
202            let _span = span.enter();
203
204            // For each entry in the storage directory
205            'outer: for entry in this.storage_dir.entries()?.flatten() {
206                let path = entry.path();
207                // Skip files
208                if path.is_file() {
209                    tracing::warn!(path = %path.display(), "unexpected file");
210                    continue;
211                }
212
213                'dir: {
214                    // Try to parse the directory name as an mc_seqno
215                    let Ok(name) = entry.file_name().into_string() else {
216                        break 'dir;
217                    };
218                    let Ok(mc_seqno) = name.parse::<u32>() else {
219                        break 'dir;
220                    };
221
222                    // Try to load files in the directory as persistent states
223                    process_states(&this, &path, mc_seqno)?;
224                    continue 'outer;
225                }
226                tracing::warn!(path = %path.display(), "unexpected directory");
227            }
228
229            Ok(())
230        })
231        .await?
232    }
233
234    // NOTE: This is intentionally a method, not a constant because
235    // it might be useful to allow configure it during the first run.
236    pub fn state_chunk_size(&self) -> NonZeroU32 {
237        NonZeroU32::new(STATE_CHUNK_SIZE as _).unwrap()
238    }
239
240    pub fn state_exists(&self, block_id: &BlockId, kind: PersistentStateKind) -> bool {
241        self.inner.descriptor_cache.contains_key(&CacheKey {
242            block_id: *block_id,
243            kind,
244        })
245    }
246
247    pub fn get_state_info(
248        &self,
249        block_id: &BlockId,
250        kind: PersistentStateKind,
251    ) -> Option<PersistentStateInfo> {
252        self.inner
253            .descriptor_cache
254            .get(&CacheKey {
255                block_id: *block_id,
256                kind,
257            })
258            .and_then(|cached| {
259                let size = NonZeroU64::new(cached.file.length() as u64)?;
260                Some(PersistentStateInfo {
261                    size,
262                    chunk_size: self.state_chunk_size(),
263                })
264            })
265    }
266
267    pub async fn read_state_part(
268        &self,
269        block_id: &BlockId,
270        offset: u64,
271        state_kind: PersistentStateKind,
272    ) -> Option<Vec<u8>> {
273        // NOTE: Should be noop on x64
274        let offset = usize::try_from(offset).ok()?;
275        let chunk_size = self.state_chunk_size().get() as usize;
276        if offset % chunk_size != 0 {
277            return None;
278        }
279
280        let _permit = self.inner.chunks_semaphore.acquire().await.ok()?;
281
282        let key = CacheKey {
283            block_id: *block_id,
284            kind: state_kind,
285        };
286        let cached = self.inner.descriptor_cache.get(&key)?.clone();
287        if offset > cached.file.length() {
288            return None;
289        }
290
291        // NOTE: Cached file is a mapped file, therefore it can take a while to read from it.
292        // NOTE: `spawn_blocking` is called here because it is mostly IO-bound operation.
293        // TODO: Add semaphore to limit the number of concurrent operations.
294        tokio::task::spawn_blocking(move || {
295            let end = std::cmp::min(offset.saturating_add(chunk_size), cached.file.length());
296            cached.file.as_slice()[offset..end].to_vec()
297        })
298        .await
299        .ok()
300    }
301
302    #[tracing::instrument(skip_all, fields(mc_seqno, block_id = %handle.id()))]
303    pub async fn store_shard_state(
304        &self,
305        mc_seqno: u32,
306        handle: &BlockHandle,
307        tracker_handle: RefMcStateHandle,
308    ) -> Result<()> {
309        if self
310            .try_reuse_persistent_state(mc_seqno, handle, PersistentStateKind::Shard)
311            .await?
312        {
313            return Ok(());
314        }
315
316        let cancelled = CancellationFlag::new();
317        scopeguard::defer! {
318            cancelled.cancel();
319        }
320
321        let handle = handle.clone();
322        let this = self.inner.clone();
323        let cancelled = cancelled.clone();
324        let span = tracing::Span::current();
325
326        tokio::task::spawn_blocking(move || {
327            let _span = span.enter();
328
329            let guard = scopeguard::guard((), |_| {
330                tracing::warn!("cancelled");
331            });
332
333            // NOTE: Ensure that the tracker handle will outlive the state writer.
334            let _tracker_handle = tracker_handle;
335
336            let root_hash = this.shard_states.load_state_root(handle.id())?;
337
338            let states_dir = this.prepare_persistent_states_dir(mc_seqno)?;
339
340            let cell_writer = ShardStateWriter::new(&this.db, &states_dir, handle.id());
341            match cell_writer.write(&root_hash, Some(&cancelled)) {
342                Ok(()) => {
343                    this.block_handles.set_has_persistent_shard_state(&handle);
344                    tracing::info!("persistent shard state saved");
345                }
346                Err(e) => {
347                    // NOTE: We are ignoring an error here. It might be intentional
348                    tracing::error!("failed to write persistent shard state: {e:?}");
349                }
350            }
351
352            this.cache_state(mc_seqno, handle.id(), PersistentStateKind::Shard)?;
353
354            scopeguard::ScopeGuard::into_inner(guard);
355            Ok(())
356        })
357        .await?
358    }
359
360    #[tracing::instrument(skip_all, fields(mc_seqno, block_id = %handle.id()))]
361    pub async fn store_shard_state_file(
362        &self,
363        mc_seqno: u32,
364        handle: &BlockHandle,
365        file: File,
366    ) -> Result<()> {
367        if self
368            .try_reuse_persistent_state(mc_seqno, handle, PersistentStateKind::Shard)
369            .await?
370        {
371            return Ok(());
372        }
373
374        let cancelled = CancellationFlag::new();
375        scopeguard::defer! {
376            cancelled.cancel();
377        }
378
379        let handle = handle.clone();
380        let this = self.inner.clone();
381        let cancelled = cancelled.clone();
382        let span = tracing::Span::current();
383
384        tokio::task::spawn_blocking(move || {
385            let _span = span.enter();
386
387            let guard = scopeguard::guard((), |_| {
388                tracing::warn!("cancelled");
389            });
390
391            let states_dir = this.prepare_persistent_states_dir(mc_seqno)?;
392
393            let cell_writer = ShardStateWriter::new(&this.db, &states_dir, handle.id());
394            cell_writer.write_file(file, Some(&cancelled))?;
395            this.block_handles.set_has_persistent_shard_state(&handle);
396            this.cache_state(mc_seqno, handle.id(), PersistentStateKind::Shard)?;
397
398            scopeguard::ScopeGuard::into_inner(guard);
399            Ok(())
400        })
401        .await?
402    }
403
404    #[tracing::instrument(skip_all, fields(mc_seqno = mc_seqno, block_id = %block.id()))]
405    pub async fn store_queue_state(
406        &self,
407        mc_seqno: u32,
408        handle: &BlockHandle,
409        block: BlockStuff,
410    ) -> Result<()> {
411        if self
412            .try_reuse_persistent_state(mc_seqno, handle, PersistentStateKind::Queue)
413            .await?
414        {
415            return Ok(());
416        }
417
418        let this = self.inner.clone();
419
420        let shard_ident = handle.id().shard;
421
422        let mut queue_diffs = Vec::new();
423        let mut messages = Vec::new();
424
425        let mut top_block_handle = handle.clone();
426        let mut top_block = block;
427
428        let mut tail_len = top_block.block().out_msg_queue_updates.tail_len as usize;
429
430        while tail_len > 0 {
431            let queue_diff = this.blocks.load_queue_diff(&top_block_handle).await?;
432            let top_block_info = top_block.load_info()?;
433
434            let block_extra = top_block.load_extra()?;
435            let out_messages = block_extra.load_out_msg_description()?;
436
437            messages.push(queue_diff.zip(&out_messages));
438            queue_diffs.push(queue_diff.diff().clone());
439
440            if tail_len == 1 {
441                break;
442            }
443
444            let prev_block_id = match top_block_info.load_prev_ref()? {
445                PrevBlockRef::Single(block_ref) => block_ref.as_block_id(shard_ident),
446                PrevBlockRef::AfterMerge { .. } => anyhow::bail!("merge not supported yet"),
447            };
448
449            let Some(prev_block_handle) = this.block_handles.load_handle(&prev_block_id) else {
450                anyhow::bail!("prev block handle not found for: {prev_block_id}");
451            };
452            let prev_block = this.blocks.load_block_data(&prev_block_handle).await?;
453
454            top_block_handle = prev_block_handle;
455            top_block = prev_block;
456            tail_len -= 1;
457        }
458
459        let state = QueueStateHeader {
460            shard_ident,
461            seqno: handle.id().seqno,
462            queue_diffs,
463        };
464
465        let cancelled = CancellationFlag::new();
466        scopeguard::defer! {
467            cancelled.cancel();
468        }
469
470        let handle = handle.clone();
471        let cancelled = cancelled.clone();
472        let span = tracing::Span::current();
473
474        tokio::task::spawn_blocking(move || {
475            let _span = span.enter();
476
477            let guard = scopeguard::guard((), |_| {
478                tracing::warn!("cancelled");
479            });
480
481            let states_dir = this.prepare_persistent_states_dir(mc_seqno)?;
482            match QueueStateWriter::new(&states_dir, handle.id(), state, messages)
483                .write(Some(&cancelled))
484            {
485                Ok(()) => {
486                    this.block_handles.set_has_persistent_queue_state(&handle);
487                    tracing::info!("persistent queue state saved");
488                }
489                Err(e) => {
490                    tracing::error!("failed to write persistent queue state: {e:?}");
491                }
492            }
493
494            this.cache_state(mc_seqno, handle.id(), PersistentStateKind::Queue)?;
495
496            scopeguard::ScopeGuard::into_inner(guard);
497            Ok(())
498        })
499        .await?
500    }
501
502    pub async fn rotate_persistent_states(&self, top_handle: &BlockHandle) -> Result<()> {
503        anyhow::ensure!(
504            top_handle.is_masterchain(),
505            "top persistent state handle must be in the masterchain"
506        );
507
508        {
509            tracing::info!(
510                mc_block_id = %top_handle.id(),
511                "adding new persistent state to the queue"
512            );
513
514            let mut queue = self.inner.handles_queue.lock();
515            if queue.push(top_handle.clone()) {
516                self.inner
517                    .oldest_ps_handle
518                    .store(queue.oldest_known().cloned());
519                self.inner.oldest_ps_changed.notify_waiters();
520            }
521        }
522
523        tracing::info!("started clearing old persistent state directories");
524        let start = Instant::now();
525        scopeguard::defer! {
526            tracing::info!(
527                elapsed = %humantime::format_duration(start.elapsed()),
528                "clearing old persistent state directories completed"
529            );
530        }
531
532        let this = self.inner.clone();
533        let mut top_handle = top_handle.clone();
534        if top_handle.id().seqno == 0 {
535            // Nothing to clear for the zerostate
536            return Ok(());
537        }
538
539        let span = tracing::Span::current();
540        tokio::task::spawn_blocking(move || {
541            let _span = span.enter();
542
543            let block_handles = &this.block_handles;
544
545            let now_utime = top_handle.gen_utime();
546
547            // Find a state before the
548            let mut has_suitable = false;
549            loop {
550                match block_handles.find_prev_persistent_key_block(top_handle.id().seqno) {
551                    // Find the newest usable persistent state...
552                    Some(handle) if !has_suitable => {
553                        has_suitable |= BlockStuff::can_use_for_boot(handle.gen_utime(), now_utime);
554                        top_handle = handle;
555                    }
556                    // ...and return the previous one.
557                    Some(handle) => {
558                        top_handle = handle;
559                        break;
560                    }
561                    // Or do nothing if not found.
562                    None => return Ok(()),
563                }
564            }
565
566            // Remove cached states
567            let mut index = this.mc_seqno_to_block_ids.lock();
568            index.retain(|&mc_seqno, block_ids| {
569                if mc_seqno >= top_handle.id().seqno || mc_seqno == 0 {
570                    return true;
571                }
572
573                for block_id in block_ids.drain() {
574                    // TODO: Clear flag in block handle
575                    this.clear_cache(&block_id);
576                }
577                false
578            });
579
580            // Remove files
581            this.clear_outdated_state_entries(top_handle.id())
582        })
583        .await?
584    }
585
586    async fn try_reuse_persistent_state(
587        &self,
588        mc_seqno: u32,
589        handle: &BlockHandle,
590        kind: PersistentStateKind,
591    ) -> Result<bool> {
592        // Check if there is anything to reuse (return false if nothing)
593        match kind {
594            PersistentStateKind::Shard if !handle.has_persistent_shard_state() => return Ok(false),
595            PersistentStateKind::Queue if !handle.has_persistent_queue_state() => return Ok(false),
596            _ => {}
597        }
598
599        let block_id = *handle.id();
600
601        let Some(cached) = self
602            .inner
603            .descriptor_cache
604            .get(&CacheKey { block_id, kind })
605            .map(|r| r.clone())
606        else {
607            // Nothing to reuse
608            return Ok(false);
609        };
610
611        if cached.mc_seqno >= mc_seqno {
612            // We already have the recent enough state
613            return Ok(true);
614        }
615
616        let this = self.inner.clone();
617
618        let span = tracing::Span::current();
619        tokio::task::spawn_blocking(move || {
620            let _span = span.enter();
621
622            let states_dir = this.prepare_persistent_states_dir(mc_seqno)?;
623
624            let temp_file = states_dir.file(kind.make_temp_file_name(&block_id));
625            std::fs::write(temp_file.path(), cached.file.as_slice())?;
626            temp_file.rename(kind.make_file_name(&block_id))?;
627
628            drop(cached);
629
630            this.cache_state(mc_seqno, &block_id, kind)?;
631            Ok(true)
632        })
633        .await?
634    }
635}
636
637struct Inner {
638    db: CoreDb,
639    storage_dir: Dir,
640    block_handles: Arc<BlockHandleStorage>,
641    blocks: Arc<BlockStorage>,
642    shard_states: Arc<ShardStateStorage>,
643    descriptor_cache: DashMap<CacheKey, Arc<CachedState>>,
644    mc_seqno_to_block_ids: Mutex<BTreeMap<u32, FastHashSet<BlockId>>>,
645    chunks_semaphore: Semaphore,
646    handles_queue: Mutex<HandlesQueue>,
647    oldest_ps_changed: Notify,
648    oldest_ps_handle: ArcSwapAny<Option<BlockHandle>>,
649}
650
651impl Inner {
652    fn prepare_persistent_states_dir(&self, mc_seqno: u32) -> Result<Dir> {
653        let states_dir = self.mc_states_dir(mc_seqno);
654        if !states_dir.path().is_dir() {
655            tracing::info!(mc_seqno, "creating persistent state directory");
656            states_dir.create_if_not_exists()?;
657        }
658        Ok(states_dir)
659    }
660
661    fn mc_states_dir(&self, mc_seqno: u32) -> Dir {
662        Dir::new_readonly(self.storage_dir.path().join(mc_seqno.to_string()))
663    }
664
665    fn clear_outdated_state_entries(&self, recent_block_id: &BlockId) -> Result<()> {
666        let mut directories_to_remove: Vec<PathBuf> = Vec::new();
667        let mut files_to_remove: Vec<PathBuf> = Vec::new();
668
669        for entry in self.storage_dir.entries()?.flatten() {
670            let path = entry.path();
671
672            if path.is_file() {
673                files_to_remove.push(path);
674                continue;
675            }
676
677            let Ok(name) = entry.file_name().into_string() else {
678                directories_to_remove.push(path);
679                continue;
680            };
681
682            let is_recent = matches!(
683                name.parse::<u32>(),
684                Ok(seqno) if seqno >= recent_block_id.seqno || seqno == 0
685            );
686            if !is_recent {
687                directories_to_remove.push(path);
688            }
689        }
690
691        for dir in directories_to_remove {
692            tracing::info!(dir = %dir.display(), "removing an old persistent state directory");
693            if let Err(e) = std::fs::remove_dir_all(&dir) {
694                tracing::error!(dir = %dir.display(), "failed to remove an old persistent state: {e:?}");
695            }
696        }
697
698        for file in files_to_remove {
699            tracing::info!(file = %file.display(), "removing file");
700            if let Err(e) = std::fs::remove_file(&file) {
701                tracing::error!(file = %file.display(), "failed to remove file: {e:?}");
702            }
703        }
704
705        Ok(())
706    }
707
708    fn cache_state(
709        &self,
710        mc_seqno: u32,
711        block_id: &BlockId,
712        kind: PersistentStateKind,
713    ) -> Result<()> {
714        use std::collections::btree_map;
715
716        use dashmap::mapref::entry::Entry;
717
718        let key = CacheKey {
719            block_id: *block_id,
720            kind,
721        };
722
723        let load_mapped = || {
724            let mut file = self
725                .mc_states_dir(mc_seqno)
726                .file(kind.make_file_name(block_id))
727                .read(true)
728                .open()?;
729
730            // We create a copy of the original file here to make sure
731            // that the underlying mapped file will not be changed outside
732            // of the node. Otherwise it will randomly fail with exit code 7/BUS.
733            let mut temp_file = tempfile::tempfile_in(self.storage_dir.path())
734                .context("failed to create a temp file")?;
735
736            // Underlying implementation will call something like `copy_file_range`,
737            // and we hope that it will be just COW pages.
738            // TODO: Find a way to cancel this operation.
739            std::io::copy(&mut file, &mut temp_file).context("failed to copy a temp file")?;
740            temp_file.flush()?;
741            temp_file.seek(std::io::SeekFrom::Start(0))?;
742
743            MappedFile::from_existing_file(temp_file).context("failed to map a temp file")
744        };
745
746        let file =
747            load_mapped().with_context(|| format!("failed to cache {kind:?} for {block_id}"))?;
748
749        let new_state = Arc::new(CachedState { mc_seqno, file });
750
751        let prev_mc_seqno = match self.descriptor_cache.entry(key) {
752            Entry::Vacant(entry) => {
753                entry.insert(new_state);
754                None
755            }
756            Entry::Occupied(mut entry) => {
757                let prev_mc_seqno = entry.get().mc_seqno;
758                if mc_seqno <= prev_mc_seqno {
759                    // Cache only the most recent block (if changed)
760                    return Ok(());
761                }
762
763                entry.insert(new_state);
764                Some(prev_mc_seqno)
765            }
766        };
767
768        let mut index = self.mc_seqno_to_block_ids.lock();
769
770        // Remove previous entry if exists
771        if let Some(prev_mc_seqno) = prev_mc_seqno {
772            if let btree_map::Entry::Occupied(mut entry) = index.entry(prev_mc_seqno) {
773                entry.get_mut().remove(block_id);
774                if entry.get().is_empty() {
775                    entry.remove();
776                }
777            }
778        }
779
780        index.entry(mc_seqno).or_default().insert(*block_id);
781
782        Ok(())
783    }
784
785    fn clear_cache(&self, block_id: &BlockId) {
786        self.descriptor_cache.remove(&CacheKey {
787            block_id: *block_id,
788            kind: PersistentStateKind::Shard,
789        });
790        self.descriptor_cache.remove(&CacheKey {
791            block_id: *block_id,
792            kind: PersistentStateKind::Queue,
793        });
794    }
795}
796
797#[derive(Debug, Clone, Copy)]
798pub struct PersistentStateInfo {
799    pub size: NonZeroU64,
800    pub chunk_size: NonZeroU32,
801}
802
803struct CachedState {
804    mc_seqno: u32,
805    file: MappedFile,
806}
807
808#[derive(Default)]
809struct HandlesQueue {
810    handles: VecDeque<BlockHandle>,
811}
812
813impl HandlesQueue {
814    fn oldest_known(&self) -> Option<&BlockHandle> {
815        self.handles.back()
816    }
817
818    fn push(&mut self, new_handle: BlockHandle) -> bool {
819        // Allow only new blocks
820        if let Some(newest) = self.handles.front() {
821            if newest.id().seqno >= new_handle.id().seqno {
822                return false;
823            }
824        }
825
826        // Remove too old states
827        let now_utime = new_handle.gen_utime();
828        let mut has_suitable = false;
829        self.handles.retain(|old_handle| {
830            if !has_suitable {
831                has_suitable |= BlockStuff::can_use_for_boot(old_handle.gen_utime(), now_utime);
832                true
833            } else {
834                false
835            }
836        });
837
838        // Add the new one
839        self.handles.push_front(new_handle);
840        true
841    }
842}
843
844const STATE_CHUNK_SIZE: u64 = 1024 * 1024; // 1 MB