Skip to main content

store_api/
region_engine.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Region Engine's definition
16
17use std::any::Any;
18use std::collections::HashMap;
19use std::fmt::{Debug, Display};
20use std::sync::{Arc, Mutex};
21use std::time::Instant;
22
23use api::greptime_proto::v1::meta::{GrantedRegion as PbGrantedRegion, RegionRole as PbRegionRole};
24use api::region::RegionResponse;
25use async_trait::async_trait;
26use common_error::ext::BoxedError;
27use common_recordbatch::adapter::RegionQueryStatCounters;
28use common_recordbatch::{EmptyRecordBatchStream, QueryMemoryTracker, SendableRecordBatchStream};
29use common_time::Timestamp;
30use datafusion_execution::memory_pool::{MemoryPool, UnboundedMemoryPool};
31use datafusion_physical_plan::metrics::ExecutionPlanMetricsSet;
32use datafusion_physical_plan::{DisplayAs, DisplayFormatType, PhysicalExpr};
33use datatypes::schema::SchemaRef;
34use futures::FutureExt;
35use futures::future::{BoxFuture, join_all};
36use serde::{Deserialize, Serialize};
37use tokio::sync::Semaphore;
38
39use crate::logstore::entry;
40use crate::metadata::RegionMetadataRef;
41use crate::region_request::{
42    BatchRegionDdlRequest, RegionCatchupRequest, RegionOpenRequest, RegionPutRequest, RegionRequest,
43};
44use crate::storage::{FileId, RegionId, ScanRequest, SequenceNumber};
45
46/// The settable region role state.
47#[derive(Debug, PartialEq, Eq, Clone, Copy)]
48pub enum SettableRegionRoleState {
49    Follower,
50    DowngradingLeader,
51    /// Exit staging mode and return to normal leader state. Only allowed from staging state.
52    Leader,
53    /// Enter staging mode. Region remains writable but disables checkpoint, compaction and flush. Only allowed from normal leader state.
54    StagingLeader,
55}
56
57impl Display for SettableRegionRoleState {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        match self {
60            SettableRegionRoleState::Follower => write!(f, "Follower"),
61            SettableRegionRoleState::DowngradingLeader => write!(f, "Leader(Downgrading)"),
62            SettableRegionRoleState::Leader => write!(f, "Leader"),
63            SettableRegionRoleState::StagingLeader => write!(f, "Leader(Staging)"),
64        }
65    }
66}
67
68impl From<SettableRegionRoleState> for RegionRole {
69    fn from(value: SettableRegionRoleState) -> Self {
70        match value {
71            SettableRegionRoleState::Follower => RegionRole::Follower,
72            SettableRegionRoleState::DowngradingLeader => RegionRole::DowngradingLeader,
73            SettableRegionRoleState::Leader => RegionRole::Leader,
74            SettableRegionRoleState::StagingLeader => RegionRole::StagingLeader,
75        }
76    }
77}
78
79/// The request to set region role state.
80#[derive(Debug, PartialEq, Eq)]
81pub struct SetRegionRoleStateRequest {
82    region_id: RegionId,
83    region_role_state: SettableRegionRoleState,
84}
85
86/// The success response of setting region role state.
87#[derive(Debug, PartialEq, Eq)]
88pub enum SetRegionRoleStateSuccess {
89    File,
90    Relational,
91    Mito {
92        last_entry_id: entry::Id,
93    },
94    Metric {
95        last_entry_id: entry::Id,
96        metadata_last_entry_id: entry::Id,
97    },
98}
99
100impl SetRegionRoleStateSuccess {
101    /// Returns a [SetRegionRoleStateSuccess::File].
102    pub fn file() -> Self {
103        Self::File
104    }
105
106    /// Returns a [SetRegionRoleStateSuccess::Relational].
107    pub fn relational() -> Self {
108        Self::Relational
109    }
110
111    /// Returns a [SetRegionRoleStateSuccess::Mito] with the `last_entry_id`.
112    pub fn mito(last_entry_id: entry::Id) -> Self {
113        SetRegionRoleStateSuccess::Mito { last_entry_id }
114    }
115
116    /// Returns a [SetRegionRoleStateSuccess::Metric] with the `last_entry_id` and `metadata_last_entry_id`.
117    pub fn metric(last_entry_id: entry::Id, metadata_last_entry_id: entry::Id) -> Self {
118        SetRegionRoleStateSuccess::Metric {
119            last_entry_id,
120            metadata_last_entry_id,
121        }
122    }
123}
124
125impl SetRegionRoleStateSuccess {
126    /// Returns the last entry id of the region.
127    pub fn last_entry_id(&self) -> Option<entry::Id> {
128        match self {
129            SetRegionRoleStateSuccess::File | SetRegionRoleStateSuccess::Relational => None,
130            SetRegionRoleStateSuccess::Mito { last_entry_id } => Some(*last_entry_id),
131            SetRegionRoleStateSuccess::Metric { last_entry_id, .. } => Some(*last_entry_id),
132        }
133    }
134
135    /// Returns the last entry id of the metadata of the region.
136    pub fn metadata_last_entry_id(&self) -> Option<entry::Id> {
137        match self {
138            SetRegionRoleStateSuccess::File | SetRegionRoleStateSuccess::Relational => None,
139            SetRegionRoleStateSuccess::Mito { .. } => None,
140            SetRegionRoleStateSuccess::Metric {
141                metadata_last_entry_id,
142                ..
143            } => Some(*metadata_last_entry_id),
144        }
145    }
146}
147
148/// The response of setting region role state.
149#[derive(Debug)]
150pub enum SetRegionRoleStateResponse {
151    Success(SetRegionRoleStateSuccess),
152    NotFound,
153    InvalidTransition(BoxedError),
154}
155
156impl SetRegionRoleStateResponse {
157    /// Returns a [SetRegionRoleStateResponse::Success] with the `File` success.
158    pub fn success(success: SetRegionRoleStateSuccess) -> Self {
159        Self::Success(success)
160    }
161
162    /// Returns a [SetRegionRoleStateResponse::InvalidTransition] with the error.
163    pub fn invalid_transition(error: BoxedError) -> Self {
164        Self::InvalidTransition(error)
165    }
166
167    /// Returns true if the response is a [SetRegionRoleStateResponse::NotFound].
168    pub fn is_not_found(&self) -> bool {
169        matches!(self, SetRegionRoleStateResponse::NotFound)
170    }
171
172    /// Returns true if the response is a [SetRegionRoleStateResponse::InvalidTransition].
173    pub fn is_invalid_transition(&self) -> bool {
174        matches!(self, SetRegionRoleStateResponse::InvalidTransition(_))
175    }
176}
177
178#[derive(Debug, Clone, PartialEq, Eq)]
179pub struct GrantedRegion {
180    pub region_id: RegionId,
181    pub region_role: RegionRole,
182    /// An opaque non-zero id the metadata plane attaches to some leases, `None` for a lease
183    /// without one. It reaches the engine through [`RegionEngine::set_region_role_with_lease`];
184    /// engines may keep it with the lease and must not interpret it.
185    pub candidate_lease_id: Option<u64>,
186    pub extensions: HashMap<String, Vec<u8>>,
187}
188
189impl GrantedRegion {
190    pub fn new(region_id: RegionId, region_role: RegionRole) -> Self {
191        Self {
192            region_id,
193            region_role,
194            candidate_lease_id: None,
195            extensions: HashMap::new(),
196        }
197    }
198}
199
200impl From<GrantedRegion> for PbGrantedRegion {
201    fn from(value: GrantedRegion) -> Self {
202        PbGrantedRegion {
203            region_id: value.region_id.as_u64(),
204            role: PbRegionRole::from(value.region_role).into(),
205            candidate_lease_id: value.candidate_lease_id.unwrap_or_default(),
206            extensions: value.extensions,
207        }
208    }
209}
210
211impl From<PbGrantedRegion> for GrantedRegion {
212    fn from(value: PbGrantedRegion) -> Self {
213        GrantedRegion {
214            region_id: RegionId::from_u64(value.region_id),
215            region_role: value.role().into(),
216            // 0 means the lease carries no candidate lease id.
217            candidate_lease_id: (value.candidate_lease_id != 0).then_some(value.candidate_lease_id),
218            extensions: value.extensions,
219        }
220    }
221}
222
223/// The role of the region.
224/// TODO(weny): rename it to `RegionRoleState`
225#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
226pub enum RegionRole {
227    // Readonly region(mito2)
228    Follower,
229    // Writable region(mito2), Readonly region(file).
230    Leader,
231    // Leader is in staging mode.
232    //
233    // This is leader-like and writable, but it follows the staging workflow
234    // semantics instead of a normal leader's steady state.
235    StagingLeader,
236    // Leader is downgrading to follower.
237    //
238    // This state is used to prevent new write requests.
239    DowngradingLeader,
240}
241
242impl Display for RegionRole {
243    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
244        match self {
245            RegionRole::Follower => write!(f, "Follower"),
246            RegionRole::Leader => write!(f, "Leader"),
247            RegionRole::StagingLeader => write!(f, "Leader(Staging)"),
248            RegionRole::DowngradingLeader => write!(f, "Leader(Downgrading)"),
249        }
250    }
251}
252
253impl RegionRole {
254    pub fn writable(&self) -> bool {
255        matches!(self, RegionRole::Leader | RegionRole::StagingLeader)
256    }
257}
258
259impl From<RegionRole> for PbRegionRole {
260    fn from(value: RegionRole) -> Self {
261        match value {
262            RegionRole::Follower => PbRegionRole::Follower,
263            RegionRole::Leader => PbRegionRole::Leader,
264            RegionRole::StagingLeader => PbRegionRole::StagingLeader,
265            RegionRole::DowngradingLeader => PbRegionRole::DowngradingLeader,
266        }
267    }
268}
269
270impl From<PbRegionRole> for RegionRole {
271    fn from(value: PbRegionRole) -> Self {
272        match value {
273            PbRegionRole::Leader => RegionRole::Leader,
274            PbRegionRole::StagingLeader => RegionRole::StagingLeader,
275            PbRegionRole::Follower => RegionRole::Follower,
276            PbRegionRole::DowngradingLeader => RegionRole::DowngradingLeader,
277        }
278    }
279}
280
281/// Output partition properties of the [RegionScanner].
282#[derive(Debug)]
283pub enum ScannerPartitioning {
284    /// Unknown partitioning scheme with a known number of partitions
285    Unknown(usize),
286}
287
288impl ScannerPartitioning {
289    /// Returns the number of partitions.
290    pub fn num_partitions(&self) -> usize {
291        match self {
292            ScannerPartitioning::Unknown(num_partitions) => *num_partitions,
293        }
294    }
295}
296
297/// Represents one data range within a partition
298#[derive(Debug, Clone, Copy, PartialEq, Eq)]
299pub struct PartitionRange {
300    /// Start time of time index column. Inclusive.
301    pub start: Timestamp,
302    /// End time of time index column. Exclusive.
303    pub end: Timestamp,
304    /// Number of rows in this range. Is used to balance ranges between partitions.
305    pub num_rows: usize,
306    /// Identifier to this range. Assigned by storage engine.
307    pub identifier: usize,
308}
309
310/// Properties of the [RegionScanner].
311#[derive(Debug, Default)]
312pub struct ScannerProperties {
313    /// A 2-dim partition ranges.
314    ///
315    /// The first dim vector's length represents the output partition number. The second
316    /// dim is ranges within one partition.
317    pub partitions: Vec<Vec<PartitionRange>>,
318
319    /// Whether scanner is in append-only mode.
320    append_mode: bool,
321
322    /// Total rows that **may** return by scanner. This field is only read iff
323    /// [ScannerProperties::append_mode] is true.
324    total_rows: usize,
325
326    /// Whether total_rows is exact before applying query predicates.
327    total_rows_is_exact: bool,
328
329    /// Whether to yield an empty batch to distinguish partition ranges.
330    pub distinguish_partition_range: bool,
331
332    /// The target partitions of the scanner. 0 indicates using the number of partitions as target partitions.
333    target_partitions: usize,
334
335    /// Whether the scanner reads one logical metric region, including a physical
336    /// metric region constrained by a table-ID filter.
337    logical_region: bool,
338
339    /// Region id that should receive query-load metrics for this scanner.
340    query_load_region_id: Option<RegionId>,
341    /// Counters that should receive query-load metrics for this scanner.
342    query_stat_counters: Option<RegionQueryStatCounters>,
343}
344
345impl ScannerProperties {
346    /// Sets append mode for scanner.
347    pub fn with_append_mode(mut self, append_mode: bool) -> Self {
348        self.append_mode = append_mode;
349        self
350    }
351
352    /// Sets total rows for scanner.
353    pub fn with_total_rows(mut self, total_rows: usize) -> Self {
354        self.total_rows = total_rows;
355        self
356    }
357
358    /// Sets whether the source row count is exact before query predicates.
359    pub fn with_total_rows_is_exact(mut self, exact: bool) -> Self {
360        self.total_rows_is_exact = exact;
361        self
362    }
363
364    /// Creates a new [`ScannerProperties`] with the given partitioning.
365    pub fn new(partitions: Vec<Vec<PartitionRange>>, append_mode: bool, total_rows: usize) -> Self {
366        Self {
367            partitions,
368            append_mode,
369            total_rows,
370            total_rows_is_exact: false,
371            distinguish_partition_range: false,
372            target_partitions: 0,
373            logical_region: false,
374            query_load_region_id: None,
375            query_stat_counters: None,
376        }
377    }
378
379    /// Updates the properties with the given [PrepareRequest].
380    pub fn prepare(&mut self, request: PrepareRequest) {
381        if let Some(ranges) = request.ranges {
382            self.partitions = ranges;
383        }
384        if let Some(distinguish_partition_range) = request.distinguish_partition_range {
385            self.distinguish_partition_range = distinguish_partition_range;
386        }
387        if let Some(target_partitions) = request.target_partitions {
388            self.target_partitions = target_partitions;
389        }
390    }
391
392    /// Returns the number of actual partitions.
393    pub fn num_partitions(&self) -> usize {
394        self.partitions.len()
395    }
396
397    pub fn append_mode(&self) -> bool {
398        self.append_mode
399    }
400
401    pub fn total_rows(&self) -> usize {
402        self.total_rows
403    }
404
405    /// Returns whether the source row count is exact before query predicates.
406    pub fn total_rows_is_exact(&self) -> bool {
407        self.total_rows_is_exact
408    }
409
410    /// Returns whether the scanner reads one logical metric region, including a
411    /// physical metric region constrained by a table-ID filter.
412    pub fn is_logical_region(&self) -> bool {
413        self.logical_region
414    }
415
416    /// Returns the target partitions of the scanner. If it is not set, returns the number of partitions.
417    pub fn target_partitions(&self) -> usize {
418        if self.target_partitions == 0 {
419            self.num_partitions()
420        } else {
421            self.target_partitions
422        }
423    }
424
425    /// Sets whether the scanner reads one logical metric region.
426    pub fn set_logical_region(&mut self, logical_region: bool) {
427        self.logical_region = logical_region;
428    }
429
430    /// Returns the region id that should receive query-load metrics.
431    pub fn query_load_region_id(&self) -> Option<RegionId> {
432        self.query_load_region_id
433    }
434
435    /// Returns the counters that should receive query-load metrics.
436    pub fn query_stat_counters(&self) -> Option<RegionQueryStatCounters> {
437        self.query_stat_counters.clone()
438    }
439
440    /// Sets the region id that should receive query-load metrics.
441    pub fn set_query_load_region_id(&mut self, region_id: RegionId) {
442        self.query_load_region_id = Some(region_id);
443    }
444
445    /// Sets the counters that should receive query-load metrics.
446    pub fn set_query_stat_counters(&mut self, counters: RegionQueryStatCounters) {
447        self.query_stat_counters = Some(counters);
448    }
449}
450
451/// Request to override the scanner properties.
452#[derive(Default)]
453pub struct PrepareRequest {
454    /// Assigned partition ranges.
455    pub ranges: Option<Vec<Vec<PartitionRange>>>,
456    /// Distringuishes partition range by empty batches.
457    pub distinguish_partition_range: Option<bool>,
458    /// The expected number of target partitions.
459    pub target_partitions: Option<usize>,
460}
461
462impl PrepareRequest {
463    /// Sets the ranges.
464    pub fn with_ranges(mut self, ranges: Vec<Vec<PartitionRange>>) -> Self {
465        self.ranges = Some(ranges);
466        self
467    }
468
469    /// Sets the distinguish partition range flag.
470    pub fn with_distinguish_partition_range(mut self, distinguish_partition_range: bool) -> Self {
471        self.distinguish_partition_range = Some(distinguish_partition_range);
472        self
473    }
474
475    /// Sets the target partitions.
476    pub fn with_target_partitions(mut self, target_partitions: usize) -> Self {
477        self.target_partitions = Some(target_partitions);
478        self
479    }
480}
481
482/// Necessary context of the query for the scanner.
483/// Direct callers should create one context per scan and reuse it across partitions.
484/// The default pool is unbounded; cloning the context shares its pool.
485#[derive(Clone)]
486pub struct QueryScanContext {
487    /// Whether the query is EXPLAIN ANALYZE VERBOSE.
488    pub explain_verbose: bool,
489    /// Executing node's query runtime pool, shared by all partitions of this scan.
490    pub memory_pool: Arc<dyn MemoryPool>,
491}
492
493impl Default for QueryScanContext {
494    fn default() -> Self {
495        Self {
496            explain_verbose: false,
497            memory_pool: Arc::new(UnboundedMemoryPool::default()),
498        }
499    }
500}
501
502/// A scanner that provides a way to scan the region concurrently.
503///
504/// The scanner splits the region into partitions so that each partition can be scanned concurrently.
505/// You can use this trait to implement an [`ExecutionPlan`](datafusion_physical_plan::ExecutionPlan).
506pub trait RegionScanner: Debug + DisplayAs + Send {
507    fn name(&self) -> &str;
508
509    /// Returns the properties of the scanner.
510    fn properties(&self) -> &ScannerProperties;
511
512    /// Returns the schema of the record batches.
513    fn schema(&self) -> SchemaRef;
514
515    /// Returns the metadata of the region.
516    fn metadata(&self) -> RegionMetadataRef;
517
518    /// Prepares the scanner with the given partition ranges.
519    ///
520    /// This method is for the planner to adjust the scanner's behavior based on the partition ranges.
521    fn prepare(&mut self, request: PrepareRequest) -> Result<(), BoxedError>;
522
523    /// Scans the partition and returns a stream of record batches.
524    ///
525    /// # Panics
526    /// Panics if the `partition` is out of bound.
527    fn scan_partition(
528        &self,
529        ctx: &QueryScanContext,
530        metrics_set: &ExecutionPlanMetricsSet,
531        partition: usize,
532    ) -> Result<SendableRecordBatchStream, BoxedError>;
533
534    /// Check if there is any predicate exclude region partition exprs that may be executed in this scanner.
535    fn has_predicate_without_region(&self) -> bool;
536
537    /// Add the given dynamic filter expressions to the predicate of the scanner.
538    /// Returns a vector of booleans indicating which filter expressions were applied.
539    /// true indicates the filter expression was applied(will be use by scanner to prune by stat for row group),
540    /// false otherwise.
541    fn add_dyn_filter_to_predicate(
542        &mut self,
543        filter_exprs: Vec<Arc<dyn PhysicalExpr>>,
544    ) -> Vec<bool>;
545
546    /// Resets execution state after streams from the previous execution have been dropped.
547    ///
548    /// Callers must not concurrently execute, reset, or install filters through scanner aliases.
549    /// Implementations retaining dynamic predicates must discard them and derived pruning state,
550    /// while preserving static predicates, the source snapshot, and prepared properties.
551    /// The default is a no-op and does not make inherently one-shot sources replayable.
552    fn reset_state(&mut self) {}
553
554    /// Sets whether the scanner reads one logical metric region, including a
555    /// physical metric region constrained by a table-ID filter.
556    fn set_logical_region(&mut self, logical_region: bool);
557
558    /// Sets the region id that should receive query-load metrics.
559    fn set_query_load_region_id(&mut self, region_id: RegionId);
560
561    fn snapshot_sequence(&self) -> Option<SequenceNumber> {
562        None
563    }
564}
565
566pub type RegionScannerRef = Box<dyn RegionScanner>;
567
568pub type BatchResponses = Vec<(RegionId, Result<RegionResponse, BoxedError>)>;
569
570/// A response of [`RegionEngine::handle_batch_put_requests`] and the region of each put it
571/// answers for.
572pub type BatchPutResponse = (
573    Vec<RegionId>,
574    BoxFuture<'static, Result<RegionResponse, BoxedError>>,
575);
576
577/// Represents the statistics of a region.
578#[derive(Debug, Deserialize, Serialize, Default)]
579pub struct RegionStatistic {
580    /// The number of rows stored in SST files owned by this region plus rows in memtables.
581    ///
582    /// Rows from SST files referenced from other regions, for example after repartition,
583    /// are not counted to avoid table-level double counting when summing region statistics.
584    #[serde(default)]
585    pub num_rows: u64,
586    /// The size of memtable in bytes.
587    pub memtable_size: u64,
588    /// The size of WAL in bytes.
589    pub wal_size: u64,
590    /// Stored bytes of the latest observed manifest checkpoint and subsequent deltas,
591    /// compressed when enabled. Excludes staging manifests, `_last_checkpoint`, and
592    /// superseded files still present on disk. This is a logical recovery-size estimate,
593    /// not the physical directory usage; observation failures can leave it incomplete.
594    pub manifest_size: u64,
595    /// The size of SST data files owned by this region in bytes.
596    ///
597    /// SST files referenced from other regions, for example after repartition, are not counted.
598    pub sst_size: u64,
599    /// The number of SST files owned by this region.
600    ///
601    /// SST files referenced from other regions, for example after repartition, are not counted.
602    pub sst_num: u64,
603    /// The size of SST index files owned by this region in bytes.
604    ///
605    /// SST index files referenced from other regions, for example after repartition, are not counted.
606    #[serde(default)]
607    pub index_size: u64,
608    /// The details of the region.
609    #[serde(default)]
610    pub manifest: RegionManifestInfo,
611    #[serde(default)]
612    /// The total bytes written of the region since region opened.
613    pub written_bytes: u64,
614    /// Total ingested Parquet bytes since the region opened.
615    /// Currently counts only committed flush output.
616    ///
617    /// Excludes index files, compaction and imported SSTs.
618    /// Resets when the region instance is recreated; manifest replay does not increment it.
619    /// Accepts the legacy `flushed_parquet_bytes` key; upgrade metasrv before datanodes.
620    #[serde(default, alias = "flushed_parquet_bytes")]
621    pub ingested_parquet_bytes: u64,
622    /// The total query CPU time of the region since region opened.
623    ///
624    /// Unit: nanoseconds.
625    #[serde(default)]
626    pub query_cpu_time: u64,
627    /// The total scanned bytes of the region since region opened.
628    #[serde(default)]
629    pub query_scanned_bytes: u64,
630    /// The earliest timestamp of the region's current data, or `None` if it holds
631    /// none. Unlike the size and row counters this covers files referenced from
632    /// other regions too, since a time range cannot double count.
633    #[serde(default)]
634    pub min_timestamp: Option<Timestamp>,
635    /// The latest timestamp of the region's current data. See [`Self::min_timestamp`].
636    #[serde(default)]
637    pub max_timestamp: Option<Timestamp>,
638    /// The latest entry id of the region's remote WAL since last flush.
639    /// For metric engine, there're two latest entry ids, one for data and one for metadata.
640    /// TODO(weny): remove this two fields and use single instead.
641    #[serde(default)]
642    pub data_topic_latest_entry_id: u64,
643    #[serde(default)]
644    pub metadata_topic_latest_entry_id: u64,
645}
646
647/// The manifest info of a region.
648#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
649pub enum RegionManifestInfo {
650    Mito {
651        manifest_version: u64,
652        flushed_entry_id: u64,
653        /// Number of files removed in the manifest's `removed_files` field.
654        file_removed_cnt: u64,
655    },
656    Metric {
657        data_manifest_version: u64,
658        data_flushed_entry_id: u64,
659        metadata_manifest_version: u64,
660        metadata_flushed_entry_id: u64,
661    },
662}
663
664impl RegionManifestInfo {
665    /// Creates a new [RegionManifestInfo] for mito2 engine.
666    pub fn mito(manifest_version: u64, flushed_entry_id: u64, file_removal_rate: u64) -> Self {
667        Self::Mito {
668            manifest_version,
669            flushed_entry_id,
670            file_removed_cnt: file_removal_rate,
671        }
672    }
673
674    /// Creates a new [RegionManifestInfo] for metric engine.
675    pub fn metric(
676        data_manifest_version: u64,
677        data_flushed_entry_id: u64,
678        metadata_manifest_version: u64,
679        metadata_flushed_entry_id: u64,
680    ) -> Self {
681        Self::Metric {
682            data_manifest_version,
683            data_flushed_entry_id,
684            metadata_manifest_version,
685            metadata_flushed_entry_id,
686        }
687    }
688
689    /// Returns true if the region is a mito2 region.
690    pub fn is_mito(&self) -> bool {
691        matches!(self, RegionManifestInfo::Mito { .. })
692    }
693
694    /// Returns true if the region is a metric region.
695    pub fn is_metric(&self) -> bool {
696        matches!(self, RegionManifestInfo::Metric { .. })
697    }
698
699    /// Returns the flushed entry id of the data region.
700    pub fn data_flushed_entry_id(&self) -> u64 {
701        match self {
702            RegionManifestInfo::Mito {
703                flushed_entry_id, ..
704            } => *flushed_entry_id,
705            RegionManifestInfo::Metric {
706                data_flushed_entry_id,
707                ..
708            } => *data_flushed_entry_id,
709        }
710    }
711
712    /// Returns the manifest version of the data region.
713    pub fn data_manifest_version(&self) -> u64 {
714        match self {
715            RegionManifestInfo::Mito {
716                manifest_version, ..
717            } => *manifest_version,
718            RegionManifestInfo::Metric {
719                data_manifest_version,
720                ..
721            } => *data_manifest_version,
722        }
723    }
724
725    /// Returns the manifest version of the metadata region.
726    pub fn metadata_manifest_version(&self) -> Option<u64> {
727        match self {
728            RegionManifestInfo::Mito { .. } => None,
729            RegionManifestInfo::Metric {
730                metadata_manifest_version,
731                ..
732            } => Some(*metadata_manifest_version),
733        }
734    }
735
736    /// Returns the flushed entry id of the metadata region.
737    pub fn metadata_flushed_entry_id(&self) -> Option<u64> {
738        match self {
739            RegionManifestInfo::Mito { .. } => None,
740            RegionManifestInfo::Metric {
741                metadata_flushed_entry_id,
742                ..
743            } => Some(*metadata_flushed_entry_id),
744        }
745    }
746
747    /// Encodes a list of ([RegionId], [RegionManifestInfo]) to a byte array.
748    pub fn encode_list(manifest_infos: &[(RegionId, Self)]) -> serde_json::Result<Vec<u8>> {
749        serde_json::to_vec(manifest_infos)
750    }
751
752    /// Decodes a list of ([RegionId], [RegionManifestInfo]) from a byte array.
753    pub fn decode_list(value: &[u8]) -> serde_json::Result<Vec<(RegionId, Self)>> {
754        serde_json::from_slice(value)
755    }
756}
757
758impl Default for RegionManifestInfo {
759    fn default() -> Self {
760        Self::Mito {
761            manifest_version: 0,
762            flushed_entry_id: 0,
763            file_removed_cnt: 0,
764        }
765    }
766}
767
768impl RegionStatistic {
769    /// Deserializes the region statistic to a byte array.
770    ///
771    /// Returns None if the deserialization fails.
772    pub fn deserialize_from_slice(value: &[u8]) -> Option<RegionStatistic> {
773        serde_json::from_slice(value).ok()
774    }
775
776    /// Serializes the region statistic to a byte array.
777    ///
778    /// Returns None if the serialization fails.
779    pub fn serialize_to_vec(&self) -> Option<Vec<u8>> {
780        serde_json::to_vec(self).ok()
781    }
782}
783
784impl RegionStatistic {
785    /// Returns the estimated disk size of the region.
786    pub fn estimated_disk_size(&self) -> u64 {
787        self.wal_size + self.sst_size + self.manifest_size + self.index_size
788    }
789}
790
791#[cfg(test)]
792mod tests {
793    use serde_json::json;
794
795    use super::*;
796
797    #[test]
798    fn test_legacy_granted_region_wire() {
799        use prost::Message;
800
801        let legacy_bytes: &[u8] = include_bytes!(
802            "../../../tests/compatibility/wire/candidate_lease_id/legacy_granted_region.pb"
803        );
804        let granted = GrantedRegion::from(PbGrantedRegion::decode(legacy_bytes).unwrap());
805        assert_eq!(
806            granted,
807            GrantedRegion {
808                candidate_lease_id: None,
809                extensions: HashMap::from([("plugin".to_string(), b"payload".to_vec())]),
810                ..GrantedRegion::new(RegionId::new(1024, 1), RegionRole::Follower)
811            }
812        );
813
814        assert_eq!(PbGrantedRegion::from(granted).encode_to_vec(), legacy_bytes);
815    }
816
817    #[test]
818    fn test_granted_region_candidate_lease_id_wire() {
819        use prost::Message;
820
821        let granted = GrantedRegion {
822            candidate_lease_id: Some(7),
823            ..GrantedRegion::new(RegionId::new(1024, 1), RegionRole::Follower)
824        };
825        // Region id (field 1), role Follower (field 2), then candidate_lease_id as a uint64
826        // varint in field 3.
827        let bytes: &[u8] = &[
828            0x08, 0x81, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01, 0x10, 0x01, 0x18, 0x07,
829        ];
830        assert_eq!(
831            PbGrantedRegion::from(granted.clone()).encode_to_vec(),
832            bytes
833        );
834        assert_eq!(
835            GrantedRegion::from(PbGrantedRegion::decode(bytes).unwrap()),
836            granted
837        );
838    }
839
840    #[test]
841    fn region_statistic_flush_bytes_compatibility() {
842        let legacy_bytes = br#"{
843            "num_rows": 1,
844            "memtable_size": 2,
845            "wal_size": 3,
846            "manifest_size": 4,
847            "sst_size": 5,
848            "sst_num": 6,
849            "index_size": 7,
850            "manifest": {
851                "Mito": {
852                    "manifest_version": 8,
853                    "flushed_entry_id": 9,
854                    "file_removed_cnt": 10
855                }
856            },
857            "written_bytes": 11
858        }"#;
859        let mut statistic = RegionStatistic::deserialize_from_slice(legacy_bytes).unwrap();
860        assert_eq!(statistic.ingested_parquet_bytes, 0);
861        statistic.ingested_parquet_bytes = 1234;
862        let encoded = statistic.serialize_to_vec().unwrap();
863        let decoded = RegionStatistic::deserialize_from_slice(&encoded).unwrap();
864        assert_eq!(decoded.ingested_parquet_bytes, 1234);
865        let legacy: serde_json::Value = serde_json::from_slice(legacy_bytes).unwrap();
866        let mut current: serde_json::Value = serde_json::from_slice(&encoded).unwrap();
867        assert_eq!(current["ingested_parquet_bytes"], 1234);
868        assert!(current.get("flushed_parquet_bytes").is_none());
869        let mut old_wire = current.clone();
870        let bytes = old_wire
871            .as_object_mut()
872            .unwrap()
873            .remove("ingested_parquet_bytes")
874            .unwrap();
875        old_wire["flushed_parquet_bytes"] = bytes;
876        assert_eq!(
877            serde_json::from_value::<RegionStatistic>(old_wire)
878                .unwrap()
879                .ingested_parquet_bytes,
880            1234
881        );
882        for (key, value) in legacy.as_object().unwrap() {
883            assert_eq!(&current[key], value);
884        }
885        current
886            .as_object_mut()
887            .unwrap()
888            .remove("ingested_parquet_bytes");
889        assert_eq!(
890            serde_json::from_value::<RegionStatistic>(current)
891                .unwrap()
892                .ingested_parquet_bytes,
893            0
894        );
895    }
896
897    #[test]
898    fn region_statistic_deserializes_without_query_stats() {
899        let statistic: RegionStatistic = serde_json::from_value(json!({
900            "num_rows": 1,
901            "memtable_size": 2,
902            "wal_size": 3,
903            "manifest_size": 4,
904            "sst_size": 5,
905            "sst_num": 6,
906            "index_size": 7,
907            "manifest": {
908                "Mito": {
909                    "manifest_version": 8,
910                    "flushed_entry_id": 9,
911                    "file_removed_cnt": 10
912                }
913            },
914            "written_bytes": 11
915        }))
916        .unwrap();
917
918        assert_eq!(statistic.query_cpu_time, 0);
919        assert_eq!(statistic.query_scanned_bytes, 0);
920    }
921}
922
923/// Request to sync the region from a manifest or a region.
924#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
925pub enum SyncRegionFromRequest {
926    /// Syncs the region using manifest information.
927    /// Used in leader-follower manifest sync scenarios.
928    FromManifest(RegionManifestInfo),
929    /// Syncs the region from another region.
930    ///
931    /// Used by the metric engine to sync logical regions from a source physical region
932    /// to a target physical region. This copies metadata region SST files and transforms
933    /// logical region entries to use the target's region number.
934    FromRegion {
935        /// The [`RegionId`] of the source region.
936        source_region_id: RegionId,
937        /// The parallelism of the sync operation.
938        parallelism: usize,
939    },
940}
941
942impl From<RegionManifestInfo> for SyncRegionFromRequest {
943    fn from(manifest_info: RegionManifestInfo) -> Self {
944        SyncRegionFromRequest::FromManifest(manifest_info)
945    }
946}
947
948impl SyncRegionFromRequest {
949    /// Creates a new request from a manifest info.
950    pub fn from_manifest(manifest_info: RegionManifestInfo) -> Self {
951        SyncRegionFromRequest::FromManifest(manifest_info)
952    }
953
954    /// Creates a new request from a region.
955    pub fn from_region(source_region_id: RegionId, parallelism: usize) -> Self {
956        SyncRegionFromRequest::FromRegion {
957            source_region_id,
958            parallelism,
959        }
960    }
961
962    /// Returns true if the request is from a manifest.
963    pub fn is_from_manifest(&self) -> bool {
964        matches!(self, SyncRegionFromRequest::FromManifest { .. })
965    }
966
967    /// Converts the request to a region manifest info.
968    ///
969    /// Returns None if the request is not from a manifest.
970    pub fn into_region_manifest_info(self) -> Option<RegionManifestInfo> {
971        match self {
972            SyncRegionFromRequest::FromManifest(manifest_info) => Some(manifest_info),
973            SyncRegionFromRequest::FromRegion { .. } => None,
974        }
975    }
976}
977
978/// The response of syncing the region.
979#[derive(Debug)]
980pub enum SyncRegionFromResponse {
981    NotSupported,
982    Mito {
983        /// Indicates if the data region was synced.
984        synced: bool,
985    },
986    Metric {
987        /// Indicates if the metadata region was synced.
988        metadata_synced: bool,
989        /// Indicates if the data region was synced.
990        data_synced: bool,
991        /// The logical regions that were newly opened during the sync operation.
992        /// This only occurs after the metadata region has been successfully synced.
993        new_opened_logical_region_ids: Vec<RegionId>,
994    },
995}
996
997impl SyncRegionFromResponse {
998    /// Returns true if data region is synced.
999    pub fn is_data_synced(&self) -> bool {
1000        match self {
1001            SyncRegionFromResponse::NotSupported => false,
1002            SyncRegionFromResponse::Mito { synced } => *synced,
1003            SyncRegionFromResponse::Metric { data_synced, .. } => *data_synced,
1004        }
1005    }
1006
1007    /// Returns true if the engine is a mito2 engine.
1008    pub fn is_mito(&self) -> bool {
1009        matches!(self, SyncRegionFromResponse::Mito { .. })
1010    }
1011
1012    /// Returns true if the engine is a metric engine.
1013    pub fn is_metric(&self) -> bool {
1014        matches!(self, SyncRegionFromResponse::Metric { .. })
1015    }
1016
1017    /// Returns the new opened logical region ids.
1018    pub fn new_opened_logical_region_ids(self) -> Option<Vec<RegionId>> {
1019        match self {
1020            SyncRegionFromResponse::Metric {
1021                new_opened_logical_region_ids,
1022                ..
1023            } => Some(new_opened_logical_region_ids),
1024            _ => None,
1025        }
1026    }
1027}
1028
1029/// Request to remap manifests from old regions to new regions.
1030#[derive(Debug, Clone)]
1031pub struct RemapManifestsRequest {
1032    /// The [`RegionId`] of a staging region used to obtain table directory and storage configuration for the remap operation.
1033    pub region_id: RegionId,
1034    /// Regions to remap manifests from.
1035    pub input_regions: Vec<RegionId>,
1036    /// For each old region, which new regions should receive its files
1037    pub region_mapping: HashMap<RegionId, Vec<RegionId>>,
1038    /// New partition expressions for the new regions.
1039    pub new_partition_exprs: HashMap<RegionId, String>,
1040}
1041
1042/// Response to remap manifests from old regions to new regions.
1043#[derive(Debug, Clone)]
1044pub struct RemapManifestsResponse {
1045    /// Maps region id to its staging manifest path.
1046    ///
1047    /// These paths are relative paths within the central region's staging blob storage,
1048    /// and should be passed to [`ApplyStagingManifestRequest`](RegionRequest::ApplyStagingManifest) to finalize the repartition.
1049    pub manifest_paths: HashMap<RegionId, String>,
1050}
1051
1052/// Request to copy files from a source region to a target region.
1053#[derive(Debug, Clone)]
1054pub struct MitoCopyRegionFromRequest {
1055    /// The [`RegionId`] of the source region.
1056    pub source_region_id: RegionId,
1057    /// The parallelism of the copy operation.
1058    pub parallelism: usize,
1059}
1060
1061#[derive(Debug, Clone)]
1062pub struct MitoCopyRegionFromResponse {
1063    /// Source SST IDs installed in the target by this copy. Shared references may
1064    /// be installed without copying their physical objects. If no new references
1065    /// need installation, returns the source IDs already present in the target.
1066    /// This is not a count or inventory of physical object transfers.
1067    pub copied_file_ids: Vec<FileId>,
1068}
1069
1070#[async_trait]
1071pub trait RegionEngine: Send + Sync {
1072    /// Name of this engine
1073    fn name(&self) -> &str;
1074
1075    /// Handles batch open region requests.
1076    async fn handle_batch_open_requests(
1077        &self,
1078        parallelism: usize,
1079        requests: Vec<(RegionId, RegionOpenRequest)>,
1080    ) -> Result<BatchResponses, BoxedError> {
1081        let semaphore = Arc::new(Semaphore::new(parallelism));
1082        let mut tasks = Vec::with_capacity(requests.len());
1083
1084        for (region_id, request) in requests {
1085            let semaphore_moved = semaphore.clone();
1086
1087            tasks.push(async move {
1088                // Safety: semaphore must exist
1089                let _permit = semaphore_moved.acquire().await.unwrap();
1090                let result = self
1091                    .handle_request(region_id, RegionRequest::Open(request))
1092                    .await;
1093                (region_id, result)
1094            });
1095        }
1096
1097        Ok(join_all(tasks).await)
1098    }
1099
1100    async fn handle_batch_catchup_requests(
1101        &self,
1102        parallelism: usize,
1103        requests: Vec<(RegionId, RegionCatchupRequest)>,
1104    ) -> Result<BatchResponses, BoxedError> {
1105        let semaphore = Arc::new(Semaphore::new(parallelism));
1106        let mut tasks = Vec::with_capacity(requests.len());
1107
1108        for (region_id, request) in requests {
1109            let semaphore_moved = semaphore.clone();
1110
1111            tasks.push(async move {
1112                // Safety: semaphore must exist
1113                let _permit = semaphore_moved.acquire().await.unwrap();
1114                let result = self
1115                    .handle_request(region_id, RegionRequest::Catchup(request))
1116                    .await;
1117                (region_id, result)
1118            });
1119        }
1120
1121        Ok(join_all(tasks).await)
1122    }
1123
1124    /// Handles the puts of one request into regions of this engine and returns the responses
1125    /// to await on their own, each with the region of every put it answers for: two puts to one
1126    /// region are two entries.
1127    ///
1128    /// The default handles each put with [`handle_request`](Self::handle_request) and answers
1129    /// each put on its own. An engine that writes several puts together may answer them with
1130    /// one response whose affected rows count all of them.
1131    fn handle_batch_put_requests(
1132        self: Arc<Self>,
1133        requests: Vec<(RegionId, RegionPutRequest)>,
1134    ) -> Vec<BatchPutResponse>
1135    where
1136        Self: 'static,
1137    {
1138        requests
1139            .into_iter()
1140            .map(|(region_id, request)| {
1141                let engine = self.clone();
1142                let response = async move {
1143                    engine
1144                        .handle_request(region_id, RegionRequest::Put(request))
1145                        .await
1146                }
1147                .boxed();
1148                (vec![region_id], response)
1149            })
1150            .collect()
1151    }
1152
1153    async fn handle_batch_ddl_requests(
1154        &self,
1155        request: BatchRegionDdlRequest,
1156    ) -> Result<RegionResponse, BoxedError> {
1157        let requests = request.into_region_requests();
1158
1159        let mut affected_rows = 0;
1160        let mut extensions = HashMap::new();
1161
1162        for (region_id, request) in requests {
1163            let result = self.handle_request(region_id, request).await?;
1164            affected_rows += result.affected_rows;
1165            extensions.extend(result.extensions);
1166        }
1167
1168        Ok(RegionResponse {
1169            affected_rows,
1170            extensions,
1171            metadata: Vec::new(),
1172        })
1173    }
1174
1175    /// Handles non-query request to the region. Returns the count of affected rows.
1176    async fn handle_request(
1177        &self,
1178        region_id: RegionId,
1179        request: RegionRequest,
1180    ) -> Result<RegionResponse, BoxedError>;
1181
1182    /// Returns the committed sequence (sequence of latest written data).
1183    async fn get_committed_sequence(
1184        &self,
1185        region_id: RegionId,
1186    ) -> Result<SequenceNumber, BoxedError>;
1187
1188    /// Handles query and return a scanner that can be used to scan the region concurrently.
1189    async fn handle_query(
1190        &self,
1191        region_id: RegionId,
1192        request: ScanRequest,
1193    ) -> Result<RegionScannerRef, BoxedError>;
1194
1195    /// Returns the query memory tracker for scan execution.
1196    fn query_memory_tracker(&self) -> Option<QueryMemoryTracker> {
1197        None
1198    }
1199
1200    /// Retrieves region's metadata.
1201    async fn get_metadata(&self, region_id: RegionId) -> Result<RegionMetadataRef, BoxedError>;
1202
1203    /// Retrieves region's statistic.
1204    fn region_statistic(&self, region_id: RegionId) -> Option<RegionStatistic>;
1205
1206    /// Stops the engine
1207    async fn stop(&self) -> Result<(), BoxedError>;
1208
1209    /// Sets [RegionRole] for a region.
1210    ///
1211    /// The engine checks whether the region is writable before writing to the region. Setting
1212    /// the region as readonly doesn't guarantee that write operations in progress will not
1213    /// take effect.
1214    fn set_region_role(&self, region_id: RegionId, role: RegionRole) -> Result<(), BoxedError>;
1215
1216    /// Sets [RegionRole] for a region from a lease the metasrv grants until `deadline`, in reply
1217    /// to a heartbeat sent at `sent_at`. Returns false if the engine ignores a Leader lease because
1218    /// the region was downgraded or (re)opened after `sent_at`.
1219    ///
1220    /// The region must not acknowledge a write whose WAL append completes at or after the
1221    /// deadline of its latest Leader or StagingLeader lease. Leases of other roles do not extend
1222    /// that deadline.
1223    ///
1224    /// `candidate_lease_id` is the lease's [GrantedRegion::candidate_lease_id].
1225    fn set_region_role_with_lease(
1226        &self,
1227        region_id: RegionId,
1228        role: RegionRole,
1229        sent_at: Instant,
1230        deadline: Instant,
1231        candidate_lease_id: Option<u64>,
1232    ) -> Result<bool, BoxedError>;
1233
1234    /// Syncs the region manifest to the given manifest version.
1235    async fn sync_region(
1236        &self,
1237        region_id: RegionId,
1238        request: SyncRegionFromRequest,
1239    ) -> Result<SyncRegionFromResponse, BoxedError>;
1240
1241    /// Remaps manifests from old regions to new regions.
1242    async fn remap_manifests(
1243        &self,
1244        request: RemapManifestsRequest,
1245    ) -> Result<RemapManifestsResponse, BoxedError>;
1246
1247    /// Sets region role state gracefully.
1248    ///
1249    /// After the call returns, the engine ensures no more write operations will succeed in the region.
1250    async fn set_region_role_state_gracefully(
1251        &self,
1252        region_id: RegionId,
1253        region_role_state: SettableRegionRoleState,
1254    ) -> Result<SetRegionRoleStateResponse, BoxedError>;
1255
1256    /// Indicates region role.
1257    ///
1258    /// Returns the `None` if the region is not found.
1259    fn role(&self, region_id: RegionId) -> Option<RegionRole>;
1260
1261    fn as_any(&self) -> &dyn Any;
1262}
1263
1264pub type RegionEngineRef = Arc<dyn RegionEngine>;
1265
1266/// A [RegionScanner] that only scans a single partition.
1267pub struct SinglePartitionScanner {
1268    stream: Mutex<Option<SendableRecordBatchStream>>,
1269    schema: SchemaRef,
1270    properties: ScannerProperties,
1271    metadata: RegionMetadataRef,
1272    snapshot_sequence: Option<SequenceNumber>,
1273}
1274
1275impl SinglePartitionScanner {
1276    /// Creates a new [SinglePartitionScanner] with the given stream and metadata.
1277    pub fn new(
1278        stream: SendableRecordBatchStream,
1279        append_mode: bool,
1280        metadata: RegionMetadataRef,
1281        snapshot_sequence: Option<SequenceNumber>,
1282    ) -> Self {
1283        let schema = stream.schema();
1284        Self {
1285            stream: Mutex::new(Some(stream)),
1286            schema,
1287            properties: ScannerProperties::default().with_append_mode(append_mode),
1288            metadata,
1289            snapshot_sequence,
1290        }
1291    }
1292}
1293
1294impl Debug for SinglePartitionScanner {
1295    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1296        write!(f, "SinglePartitionScanner: <SendableRecordBatchStream>")
1297    }
1298}
1299
1300impl RegionScanner for SinglePartitionScanner {
1301    fn name(&self) -> &str {
1302        "SinglePartition"
1303    }
1304
1305    fn properties(&self) -> &ScannerProperties {
1306        &self.properties
1307    }
1308
1309    fn schema(&self) -> SchemaRef {
1310        self.schema.clone()
1311    }
1312
1313    fn prepare(&mut self, request: PrepareRequest) -> Result<(), BoxedError> {
1314        self.properties.prepare(request);
1315        Ok(())
1316    }
1317
1318    fn scan_partition(
1319        &self,
1320        _ctx: &QueryScanContext,
1321        _metrics_set: &ExecutionPlanMetricsSet,
1322        _partition: usize,
1323    ) -> Result<SendableRecordBatchStream, BoxedError> {
1324        let mut stream = self.stream.lock().unwrap();
1325        let result = stream
1326            .take()
1327            .or_else(|| Some(Box::pin(EmptyRecordBatchStream::new(self.schema.clone()))));
1328        Ok(result.unwrap())
1329    }
1330
1331    fn has_predicate_without_region(&self) -> bool {
1332        false
1333    }
1334
1335    fn add_dyn_filter_to_predicate(
1336        &mut self,
1337        filter_exprs: Vec<Arc<dyn datafusion_physical_plan::PhysicalExpr>>,
1338    ) -> Vec<bool> {
1339        vec![false; filter_exprs.len()]
1340    }
1341
1342    fn metadata(&self) -> RegionMetadataRef {
1343        self.metadata.clone()
1344    }
1345
1346    fn set_logical_region(&mut self, logical_region: bool) {
1347        self.properties.set_logical_region(logical_region);
1348    }
1349
1350    fn set_query_load_region_id(&mut self, region_id: RegionId) {
1351        self.properties.set_query_load_region_id(region_id);
1352    }
1353
1354    fn snapshot_sequence(&self) -> Option<SequenceNumber> {
1355        self.snapshot_sequence
1356    }
1357}
1358
1359impl DisplayAs for SinglePartitionScanner {
1360    fn fmt_as(&self, _t: DisplayFormatType, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1361        write!(f, "{:?}", self)
1362    }
1363}