-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathcheckpoint_record.rs
More file actions
302 lines (267 loc) · 9.51 KB
/
checkpoint_record.rs
File metadata and controls
302 lines (267 loc) · 9.51 KB
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
295
296
297
298
299
300
301
302
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
use diskann::ANNResult;
use serde::{Deserialize, Serialize};
use tracing::info;
use super::WorkStage;
/// Represents a checkpoint record in the index build process.
/// The checkpoint record can be marked as in-valid to indicate that the exising intermediate data should be discarded.
/// This can happen because of a crash or an unexpected shutdown during the in-memory index build.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CheckpointRecord {
/// The work type represents the current stage of the index build process.
stage: WorkStage,
/// Indicates if the checkpoint record is dirty.
is_valid: bool,
progress: usize,
}
impl Default for CheckpointRecord {
fn default() -> Self {
CheckpointRecord::new()
}
}
impl CheckpointRecord {
/// Create a new CheckpointRecord with the work type set to Start.
pub fn new() -> CheckpointRecord {
CheckpointRecord {
stage: WorkStage::Start,
is_valid: true,
progress: 0,
}
}
pub fn is_valid(&self) -> bool {
self.is_valid
}
pub fn get_resumption_point(&self, stage: WorkStage) -> Option<usize> {
if self.stage == stage {
info!(
"The resumption point is at {} for stage {:?}",
self.progress, stage
);
Some(if self.is_valid { self.progress } else { 0 })
} else {
info!(
"Failed to get resumption point for {:?} since the current stage is {:?}.",
stage, self.stage
);
None
}
}
// Advance the work type to the next stage in the index build process.
// This method is used in each individual step of the index build process
// ..t o update the checkpoint record.
pub fn advance_work_type(&self, next_stage: WorkStage) -> ANNResult<CheckpointRecord> {
info!(
"Advancing work type from {:?} to {:?}.",
self.stage, next_stage
);
Ok(CheckpointRecord {
stage: next_stage,
is_valid: true,
progress: 0,
})
}
// Mark the checkpoint record as invalid.
pub fn mark_as_invalid(&self) -> CheckpointRecord {
CheckpointRecord {
stage: self.stage,
is_valid: false,
progress: self.progress,
}
}
// Update the progress of the current work type.
pub fn update_progress(&self, progress: usize) -> CheckpointRecord {
info!("Updating progress to {:?}={}", self.stage, progress);
CheckpointRecord {
stage: self.stage,
is_valid: true,
progress,
}
}
#[allow(unused)]
// This function is used for testing purposes only.
pub(crate) fn get_work_stage(&self) -> WorkStage {
self.stage
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
enum LegacyWorkStage {
QuantizeFPV,
InMemIndexBuild,
WriteDiskLayout,
End,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
struct LegacyCheckpointRecord {
stage: LegacyWorkStage,
is_valid: bool,
progress: usize,
}
#[rstest]
#[case(LegacyWorkStage::QuantizeFPV, WorkStage::QuantizeFPV, true, 0)]
#[case(
LegacyWorkStage::InMemIndexBuild,
WorkStage::InMemIndexBuild,
false,
42
)]
#[case(
LegacyWorkStage::WriteDiskLayout,
WorkStage::WriteDiskLayout,
true,
100
)]
#[case(LegacyWorkStage::End, WorkStage::End, false, 0)]
fn test_backward_compatibility(
#[case] legacy_stage: LegacyWorkStage,
#[case] stage: WorkStage,
#[case] is_valid: bool,
#[case] progress: usize,
) {
// Test backward compatibility: Newer code (current) reading older data format (legacy)
let legacy_record = LegacyCheckpointRecord {
stage: legacy_stage,
is_valid,
progress,
};
let serialized = bincode::serialize(&legacy_record).unwrap();
let deserialized: CheckpointRecord = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized.stage, stage);
assert_eq!(deserialized.is_valid, is_valid);
assert_eq!(deserialized.progress, progress);
}
#[rstest]
#[case(WorkStage::QuantizeFPV, LegacyWorkStage::QuantizeFPV, true, 10)]
#[case(
WorkStage::InMemIndexBuild,
LegacyWorkStage::InMemIndexBuild,
false,
30
)]
#[case(WorkStage::WriteDiskLayout, LegacyWorkStage::WriteDiskLayout, true, 80)]
#[case(WorkStage::End, LegacyWorkStage::End, false, 0)]
fn test_forward_compatibility(
#[case] current_stage: WorkStage,
#[case] expected_legacy_stage: LegacyWorkStage,
#[case] is_valid: bool,
#[case] progress: usize,
) {
// Test forward compatibility: Older code (legacy) reading newer data format (current)
// This simulates rolling back to an older version after using a newer version
let current_record = CheckpointRecord {
stage: current_stage,
is_valid,
progress,
};
let serialized = bincode::serialize(¤t_record).unwrap();
// Legacy code should still be able to deserialize common enum variants
let deserialized: LegacyCheckpointRecord = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized.stage, expected_legacy_stage);
assert_eq!(deserialized.is_valid, is_valid);
assert_eq!(deserialized.progress, progress);
}
#[rstest]
#[case(WorkStage::PartitionData, true, 25)]
#[case(WorkStage::BuildIndicesOnShards(0), true, 75)]
#[case(WorkStage::BuildIndicesOnShards(10), true, 75)]
#[case(WorkStage::MergeIndices, false, 75)]
fn test_rolling_back_with_new_variants(
#[case] stage: WorkStage,
#[case] is_valid: bool,
#[case] progress: usize,
) {
// When rolling back to older versions, newer variants should fail to deserialize
// This is expected behavior and we should test for it
let current_record = CheckpointRecord {
stage,
is_valid,
progress,
};
let serialized = bincode::serialize(¤t_record).unwrap();
// Legacy code should fail to deserialize newer enum variants
// This is expected behavior - we're testing that it fails
let result: Result<LegacyCheckpointRecord, bincode::Error> =
bincode::deserialize(&serialized);
assert!(
result.is_err(),
"Legacy code should not be able to deserialize newer enum variants"
);
}
#[test]
fn test_checkpoint_record_default() {
let record = CheckpointRecord::default();
assert!(record.is_valid());
assert_eq!(record.get_work_stage(), WorkStage::Start);
}
#[test]
fn test_checkpoint_record_is_valid() {
let record = CheckpointRecord::new();
assert!(record.is_valid());
let invalid_record = record.mark_as_invalid();
assert!(!invalid_record.is_valid());
}
#[test]
fn test_get_resumption_point_with_matching_stage() {
let record = CheckpointRecord::new().update_progress(42);
let resumption = record.get_resumption_point(WorkStage::Start);
assert_eq!(resumption, Some(42));
}
#[test]
fn test_get_resumption_point_with_different_stage() {
let record = CheckpointRecord::new();
let resumption = record.get_resumption_point(WorkStage::QuantizeFPV);
assert_eq!(resumption, None);
}
#[test]
fn test_get_resumption_point_when_invalid() {
let record = CheckpointRecord::new()
.update_progress(100)
.mark_as_invalid();
let resumption = record.get_resumption_point(WorkStage::Start);
assert_eq!(resumption, Some(0)); // Should return 0 when invalid
}
#[test]
fn test_advance_work_type() {
let record = CheckpointRecord::new();
let advanced = record.advance_work_type(WorkStage::QuantizeFPV).unwrap();
assert_eq!(advanced.get_work_stage(), WorkStage::QuantizeFPV);
assert!(advanced.is_valid());
}
#[test]
fn test_advance_work_type_resets_progress() {
// Advancing to a new stage must reset progress to 0 regardless of prior progress.
let record = CheckpointRecord::new()
.update_progress(42)
.advance_work_type(WorkStage::QuantizeFPV)
.unwrap();
assert_eq!(
record.get_resumption_point(WorkStage::QuantizeFPV),
Some(0),
"progress should be reset to 0 after advancing"
);
}
#[test]
fn test_update_progress_on_invalid_record_revalidates() {
// update_progress always sets is_valid = true; calling it on an invalid record
// silently re-validates it, which is worth documenting explicitly.
let invalid_record = CheckpointRecord::new()
.update_progress(42)
.mark_as_invalid();
assert!(!invalid_record.is_valid());
let revalidated = invalid_record.update_progress(50);
assert!(
revalidated.is_valid(),
"update_progress should re-validate an invalid record"
);
assert_eq!(
revalidated.get_resumption_point(WorkStage::Start),
Some(50)
);
}
}