risingwave_hummock_trace/
opts.rs

1// Copyright 2025 RisingWave Labs
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
15use bincode::{Decode, Encode};
16use foyer::Hint;
17use risingwave_common::bitmap::Bitmap;
18use risingwave_common::cache::CachePriority;
19use risingwave_common::catalog::{TableId, TableOption};
20use risingwave_common::util::epoch::EpochPair;
21use risingwave_hummock_sdk::{HummockReadEpoch, HummockVersionId};
22use risingwave_pb::common::PbBuffer;
23
24use crate::TracedBytes;
25
26#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
27pub struct TracedPrefetchOptions {
28    pub prefetch: bool,
29    pub for_large_query: bool,
30}
31
32#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
33pub enum TracedCachePolicy {
34    Disable,
35    Fill(TracedCachePriority),
36    NotFill,
37}
38
39#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
40pub enum TracedCachePriority {
41    High,
42    Low,
43}
44
45impl From<CachePriority> for TracedCachePriority {
46    fn from(value: CachePriority) -> Self {
47        match value {
48            CachePriority::High => Self::High,
49            CachePriority::Low => Self::Low,
50        }
51    }
52}
53
54impl From<TracedCachePriority> for CachePriority {
55    fn from(value: TracedCachePriority) -> Self {
56        match value {
57            TracedCachePriority::High => Self::High,
58            TracedCachePriority::Low => Self::Low,
59        }
60    }
61}
62
63impl From<Hint> for TracedCachePriority {
64    fn from(value: Hint) -> Self {
65        match value {
66            Hint::Normal => Self::High,
67            Hint::Low => Self::Low,
68        }
69    }
70}
71
72impl From<TracedCachePriority> for Hint {
73    fn from(value: TracedCachePriority) -> Self {
74        match value {
75            TracedCachePriority::High => Self::Normal,
76            TracedCachePriority::Low => Self::Low,
77        }
78    }
79}
80
81#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
82pub struct TracedTableId {
83    pub table_id: u32,
84}
85
86impl From<TableId> for TracedTableId {
87    fn from(value: TableId) -> Self {
88        Self {
89            table_id: value.table_id,
90        }
91    }
92}
93
94impl From<TracedTableId> for TableId {
95    fn from(value: TracedTableId) -> Self {
96        Self {
97            table_id: value.table_id,
98        }
99    }
100}
101
102#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
103pub struct TracedReadOptions {
104    pub prefix_hint: Option<TracedBytes>,
105    pub prefetch_options: TracedPrefetchOptions,
106    pub cache_policy: TracedCachePolicy,
107
108    pub retention_seconds: Option<u32>,
109    pub table_id: TracedTableId,
110    pub read_version_from_backup: bool,
111    pub read_committed: bool,
112}
113
114impl TracedReadOptions {
115    pub fn for_test(table_id: u32) -> Self {
116        Self {
117            prefix_hint: Some(TracedBytes::from(vec![0])),
118            prefetch_options: TracedPrefetchOptions {
119                prefetch: true,
120                for_large_query: true,
121            },
122            cache_policy: TracedCachePolicy::Disable,
123            retention_seconds: None,
124            table_id: TracedTableId { table_id },
125            read_version_from_backup: false,
126            read_committed: false,
127        }
128    }
129}
130
131#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
132pub struct TracedTableOption {
133    pub retention_seconds: Option<u32>,
134}
135
136impl From<TableOption> for TracedTableOption {
137    fn from(value: TableOption) -> Self {
138        Self {
139            retention_seconds: value.retention_seconds,
140        }
141    }
142}
143
144impl From<TracedTableOption> for TableOption {
145    fn from(value: TracedTableOption) -> Self {
146        Self {
147            retention_seconds: value.retention_seconds,
148        }
149    }
150}
151
152#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
153pub enum TracedOpConsistencyLevel {
154    Inconsistent,
155    ConsistentOldValue,
156}
157
158#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
159pub struct TracedNewLocalOptions {
160    pub table_id: TracedTableId,
161    pub op_consistency_level: TracedOpConsistencyLevel,
162    pub table_option: TracedTableOption,
163    pub is_replicated: bool,
164    pub vnodes: TracedBitmap,
165}
166
167#[derive(Encode, Decode, PartialEq, Debug, Clone)]
168pub struct TracedTryWaitEpochOptions {
169    pub table_id: TracedTableId,
170}
171
172#[cfg(test)]
173impl TracedNewLocalOptions {
174    pub(crate) fn for_test(table_id: u32) -> Self {
175        use risingwave_common::hash::VirtualNode;
176
177        Self {
178            table_id: TracedTableId { table_id },
179            op_consistency_level: TracedOpConsistencyLevel::Inconsistent,
180            table_option: TracedTableOption {
181                retention_seconds: None,
182            },
183            is_replicated: false,
184            vnodes: TracedBitmap::from(Bitmap::ones(VirtualNode::COUNT_FOR_TEST)),
185        }
186    }
187}
188
189pub type TracedHummockEpoch = u64;
190
191#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
192pub enum TracedHummockReadEpoch {
193    Committed(TracedHummockEpoch),
194    BatchQueryReadCommitted(TracedHummockEpoch, u64),
195    NoWait(TracedHummockEpoch),
196    Backup(TracedHummockEpoch),
197    TimeTravel(TracedHummockEpoch),
198}
199
200impl From<HummockReadEpoch> for TracedHummockReadEpoch {
201    fn from(value: HummockReadEpoch) -> Self {
202        match value {
203            HummockReadEpoch::Committed(epoch) => Self::Committed(epoch),
204            HummockReadEpoch::BatchQueryCommitted(epoch, version_id) => {
205                Self::BatchQueryReadCommitted(epoch, version_id.to_u64())
206            }
207            HummockReadEpoch::NoWait(epoch) => Self::NoWait(epoch),
208            HummockReadEpoch::Backup(epoch) => Self::Backup(epoch),
209            HummockReadEpoch::TimeTravel(epoch) => Self::TimeTravel(epoch),
210        }
211    }
212}
213
214impl From<TracedHummockReadEpoch> for HummockReadEpoch {
215    fn from(value: TracedHummockReadEpoch) -> Self {
216        match value {
217            TracedHummockReadEpoch::Committed(epoch) => Self::Committed(epoch),
218            TracedHummockReadEpoch::BatchQueryReadCommitted(epoch, version_id) => {
219                Self::BatchQueryCommitted(epoch, HummockVersionId::new(version_id))
220            }
221            TracedHummockReadEpoch::NoWait(epoch) => Self::NoWait(epoch),
222            TracedHummockReadEpoch::Backup(epoch) => Self::Backup(epoch),
223            TracedHummockReadEpoch::TimeTravel(epoch) => Self::TimeTravel(epoch),
224        }
225    }
226}
227
228#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
229pub struct TracedEpochPair {
230    pub curr: TracedHummockEpoch,
231    pub prev: TracedHummockEpoch,
232}
233
234impl From<EpochPair> for TracedEpochPair {
235    fn from(value: EpochPair) -> Self {
236        TracedEpochPair {
237            curr: value.curr,
238            prev: value.prev,
239        }
240    }
241}
242
243impl From<TracedEpochPair> for EpochPair {
244    fn from(value: TracedEpochPair) -> Self {
245        EpochPair {
246            curr: value.curr,
247            prev: value.prev,
248        }
249    }
250}
251
252#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
253pub struct TracedInitOptions {
254    pub epoch: TracedEpochPair,
255}
256
257#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
258pub struct TracedSealCurrentEpochOptions {
259    // The watermark is serialized into protobuf
260    pub table_watermarks: Option<(bool, Vec<Vec<u8>>, bool)>,
261    pub switch_op_consistency_level: Option<bool>,
262}
263
264#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
265pub struct TracedBitmap {
266    pub compression: i32,
267    pub body: Vec<u8>,
268}
269
270impl From<Bitmap> for TracedBitmap {
271    fn from(value: Bitmap) -> Self {
272        let pb = value.to_protobuf();
273        Self {
274            compression: pb.compression,
275            body: pb.body,
276        }
277    }
278}
279
280impl From<TracedBitmap> for Bitmap {
281    fn from(value: TracedBitmap) -> Self {
282        let pb = PbBuffer {
283            compression: value.compression,
284            body: value.body,
285        };
286        Bitmap::from(&pb)
287    }
288}