risingwave_hummock_trace/
opts.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use bincode::{Decode, Encode};
use foyer::CacheContext;
use risingwave_common::bitmap::Bitmap;
use risingwave_common::cache::CachePriority;
use risingwave_common::catalog::{TableId, TableOption};
use risingwave_common::util::epoch::EpochPair;
use risingwave_hummock_sdk::{HummockReadEpoch, HummockVersionId};
use risingwave_pb::common::PbBuffer;

use crate::TracedBytes;

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub struct TracedPrefetchOptions {
    pub prefetch: bool,
    pub for_large_query: bool,
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub enum TracedCachePolicy {
    Disable,
    Fill(TracedCachePriority),
    NotFill,
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub enum TracedCachePriority {
    High,
    Low,
}

impl From<CachePriority> for TracedCachePriority {
    fn from(value: CachePriority) -> Self {
        match value {
            CachePriority::High => Self::High,
            CachePriority::Low => Self::Low,
        }
    }
}

impl From<TracedCachePriority> for CachePriority {
    fn from(value: TracedCachePriority) -> Self {
        match value {
            TracedCachePriority::High => Self::High,
            TracedCachePriority::Low => Self::Low,
        }
    }
}

impl From<CacheContext> for TracedCachePriority {
    fn from(value: CacheContext) -> Self {
        match value {
            CacheContext::Default => Self::High,
            CacheContext::LowPriority => Self::Low,
        }
    }
}

impl From<TracedCachePriority> for CacheContext {
    fn from(value: TracedCachePriority) -> Self {
        match value {
            TracedCachePriority::High => Self::Default,
            TracedCachePriority::Low => Self::LowPriority,
        }
    }
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub struct TracedTableId {
    pub table_id: u32,
}

impl From<TableId> for TracedTableId {
    fn from(value: TableId) -> Self {
        Self {
            table_id: value.table_id,
        }
    }
}

impl From<TracedTableId> for TableId {
    fn from(value: TracedTableId) -> Self {
        Self {
            table_id: value.table_id,
        }
    }
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub struct TracedReadOptions {
    pub prefix_hint: Option<TracedBytes>,
    pub prefetch_options: TracedPrefetchOptions,
    pub cache_policy: TracedCachePolicy,

    pub retention_seconds: Option<u32>,
    pub table_id: TracedTableId,
    pub read_version_from_backup: bool,
    pub read_committed: bool,
}

impl TracedReadOptions {
    pub fn for_test(table_id: u32) -> Self {
        Self {
            prefix_hint: Some(TracedBytes::from(vec![0])),
            prefetch_options: TracedPrefetchOptions {
                prefetch: true,
                for_large_query: true,
            },
            cache_policy: TracedCachePolicy::Disable,
            retention_seconds: None,
            table_id: TracedTableId { table_id },
            read_version_from_backup: false,
            read_committed: false,
        }
    }
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub struct TracedWriteOptions {
    pub epoch: u64,
    pub table_id: TracedTableId,
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub struct TracedTableOption {
    pub retention_seconds: Option<u32>,
}

impl From<TableOption> for TracedTableOption {
    fn from(value: TableOption) -> Self {
        Self {
            retention_seconds: value.retention_seconds,
        }
    }
}

impl From<TracedTableOption> for TableOption {
    fn from(value: TracedTableOption) -> Self {
        Self {
            retention_seconds: value.retention_seconds,
        }
    }
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub enum TracedOpConsistencyLevel {
    Inconsistent,
    ConsistentOldValue,
}

#[derive(Encode, Decode, PartialEq, Eq, Debug, Clone)]
pub struct TracedNewLocalOptions {
    pub table_id: TracedTableId,
    pub op_consistency_level: TracedOpConsistencyLevel,
    pub table_option: TracedTableOption,
    pub is_replicated: bool,
    pub vnodes: TracedBitmap,
}

#[derive(Encode, Decode, PartialEq, Debug, Clone)]
pub struct TracedTryWaitEpochOptions {
    pub table_id: TracedTableId,
}

#[cfg(test)]
impl TracedNewLocalOptions {
    pub(crate) fn for_test(table_id: u32) -> Self {
        use risingwave_common::hash::VirtualNode;

        Self {
            table_id: TracedTableId { table_id },
            op_consistency_level: TracedOpConsistencyLevel::Inconsistent,
            table_option: TracedTableOption {
                retention_seconds: None,
            },
            is_replicated: false,
            vnodes: TracedBitmap::from(Bitmap::ones(VirtualNode::COUNT_FOR_TEST)),
        }
    }
}

pub type TracedHummockEpoch = u64;

#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
pub enum TracedHummockReadEpoch {
    Committed(TracedHummockEpoch),
    BatchQueryReadCommitted(TracedHummockEpoch, u64),
    NoWait(TracedHummockEpoch),
    Backup(TracedHummockEpoch),
    TimeTravel(TracedHummockEpoch),
}

impl From<HummockReadEpoch> for TracedHummockReadEpoch {
    fn from(value: HummockReadEpoch) -> Self {
        match value {
            HummockReadEpoch::Committed(epoch) => Self::Committed(epoch),
            HummockReadEpoch::BatchQueryCommitted(epoch, version_id) => {
                Self::BatchQueryReadCommitted(epoch, version_id.to_u64())
            }
            HummockReadEpoch::NoWait(epoch) => Self::NoWait(epoch),
            HummockReadEpoch::Backup(epoch) => Self::Backup(epoch),
            HummockReadEpoch::TimeTravel(epoch) => Self::TimeTravel(epoch),
        }
    }
}

impl From<TracedHummockReadEpoch> for HummockReadEpoch {
    fn from(value: TracedHummockReadEpoch) -> Self {
        match value {
            TracedHummockReadEpoch::Committed(epoch) => Self::Committed(epoch),
            TracedHummockReadEpoch::BatchQueryReadCommitted(epoch, version_id) => {
                Self::BatchQueryCommitted(epoch, HummockVersionId::new(version_id))
            }
            TracedHummockReadEpoch::NoWait(epoch) => Self::NoWait(epoch),
            TracedHummockReadEpoch::Backup(epoch) => Self::Backup(epoch),
            TracedHummockReadEpoch::TimeTravel(epoch) => Self::TimeTravel(epoch),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
pub struct TracedEpochPair {
    pub curr: TracedHummockEpoch,
    pub prev: TracedHummockEpoch,
}

impl From<EpochPair> for TracedEpochPair {
    fn from(value: EpochPair) -> Self {
        TracedEpochPair {
            curr: value.curr,
            prev: value.prev,
        }
    }
}

impl From<TracedEpochPair> for EpochPair {
    fn from(value: TracedEpochPair) -> Self {
        EpochPair {
            curr: value.curr,
            prev: value.prev,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
pub struct TracedInitOptions {
    pub epoch: TracedEpochPair,
}

#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
pub struct TracedSealCurrentEpochOptions {
    // The watermark is serialized into protobuf
    pub table_watermarks: Option<(bool, Vec<Vec<u8>>)>,
    pub switch_op_consistency_level: Option<bool>,
}

#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode)]
pub struct TracedBitmap {
    pub compression: i32,
    pub body: Vec<u8>,
}

impl From<Bitmap> for TracedBitmap {
    fn from(value: Bitmap) -> Self {
        let pb = value.to_protobuf();
        Self {
            compression: pb.compression,
            body: pb.body,
        }
    }
}

impl From<TracedBitmap> for Bitmap {
    fn from(value: TracedBitmap) -> Self {
        let pb = PbBuffer {
            compression: value.compression,
            body: value.body,
        };
        Bitmap::from(&pb)
    }
}