-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsync_status.rs
More file actions
246 lines (214 loc) · 7.93 KB
/
sync_status.rs
File metadata and controls
246 lines (214 loc) · 7.93 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
use alloc::{collections::btree_map::BTreeMap, rc::Rc, string::String, vec::Vec};
use core::{cell::RefCell, hash::BuildHasher};
use rustc_hash::FxBuildHasher;
use serde::Serialize;
use sqlite_nostd::ResultCode;
use crate::sync::storage_adapter::StorageAdapter;
use super::{
bucket_priority::BucketPriority, interface::Instruction, line::DataLine,
streaming_sync::OwnedCheckpoint,
};
/// Information about a progressing download.
#[derive(Serialize, Hash)]
pub struct DownloadSyncStatus {
/// Whether the socket to the sync service is currently open and connected.
///
/// This starts being true once we receive the first line, and is set to false as the iteration
/// ends.
pub connected: bool,
/// Whether we've requested the client SDK to connect to the socket while not receiving sync
/// lines yet.
pub connecting: bool,
/// Provides stats over which bucket priorities have already been synced (or when they've last
/// been changed).
///
/// Always sorted by descending [BucketPriority] in [SyncPriorityStatus] (or, in other words,
/// increasing priority numbers).
pub priority_status: Vec<SyncPriorityStatus>,
/// When a download is active (that is, a `checkpoint` or `checkpoint_diff` line has been
/// received), information about how far the download has progressed.
pub downloading: Option<SyncDownloadProgress>,
}
impl DownloadSyncStatus {
fn debug_assert_priority_status_is_sorted(&self) {
debug_assert!(self
.priority_status
.is_sorted_by(|a, b| a.priority >= b.priority))
}
pub fn disconnect(&mut self) {
self.connected = false;
self.connecting = false;
self.downloading = None;
}
pub fn start_connecting(&mut self) {
self.connected = false;
self.downloading = None;
self.connecting = true;
}
pub fn mark_connected(&mut self) {
self.connecting = false;
self.connected = true;
}
/// Transitions state after receiving a checkpoint line.
///
/// This sets the [downloading] state to include [progress].
pub fn start_tracking_checkpoint<'a>(&mut self, progress: SyncDownloadProgress) {
self.mark_connected();
self.downloading = Some(progress);
}
/// Increments [SyncDownloadProgress] progress for the given [DataLine].
pub fn track_line(&mut self, line: &DataLine) {
if let Some(ref mut downloading) = self.downloading {
downloading.increment_download_count(line);
}
}
pub fn partial_checkpoint_complete(&mut self, priority: BucketPriority, now: Timestamp) {
self.debug_assert_priority_status_is_sorted();
// We can delete entries with a higher priority because this partial sync includes them.
self.priority_status.retain(|i| i.priority < priority);
self.priority_status.insert(
0,
SyncPriorityStatus {
priority: priority,
last_synced_at: Some(now),
has_synced: Some(true),
},
);
self.debug_assert_priority_status_is_sorted();
}
pub fn applied_checkpoint(&mut self, now: Timestamp) {
self.downloading = None;
self.priority_status.clear();
self.priority_status.push(SyncPriorityStatus {
priority: BucketPriority::SENTINEL,
last_synced_at: Some(now),
has_synced: Some(true),
});
}
}
impl Default for DownloadSyncStatus {
fn default() -> Self {
Self {
connected: false,
connecting: false,
downloading: None,
priority_status: Vec::new(),
}
}
}
pub struct SyncStatusContainer {
status: Rc<RefCell<DownloadSyncStatus>>,
last_published_hash: u64,
}
impl SyncStatusContainer {
pub fn new() -> Self {
Self {
status: Rc::new(RefCell::new(Default::default())),
last_published_hash: 0,
}
}
/// Invokes a function to update the sync status, then emits an [Instruction::UpdateSyncStatus]
/// if the function did indeed change the status.
pub fn update<F: FnOnce(&mut DownloadSyncStatus) -> ()>(
&mut self,
apply: F,
instructions: &mut Vec<Instruction>,
) {
self.update_only(apply);
self.emit_changes(instructions);
}
/// Invokes a function to update the sync status without emitting a status event.
pub fn update_only<F: FnOnce(&mut DownloadSyncStatus) -> ()>(&self, apply: F) {
let mut status = self.status.borrow_mut();
apply(&mut *status);
}
/// If the status has been changed since the last time an [Instruction::UpdateSyncStatus] event
/// was emitted, emit such an event now.
pub fn emit_changes(&mut self, instructions: &mut Vec<Instruction>) {
let status = self.status.borrow();
let hash = FxBuildHasher.hash_one(&*status);
if hash != self.last_published_hash {
self.last_published_hash = hash;
instructions.push(Instruction::UpdateSyncStatus {
status: self.status.clone(),
});
}
}
}
#[repr(transparent)]
#[derive(Serialize, Hash, Clone, Copy)]
pub struct Timestamp(pub i64);
#[derive(Serialize, Hash)]
pub struct SyncPriorityStatus {
priority: BucketPriority,
last_synced_at: Option<Timestamp>,
has_synced: Option<bool>,
}
/// Per-bucket download progress information.
#[derive(Serialize, Hash)]
pub struct BucketProgress {
pub priority: BucketPriority,
pub at_last: i64,
pub since_last: i64,
pub target_count: i64,
}
#[derive(Serialize, Hash)]
pub struct SyncDownloadProgress {
buckets: BTreeMap<String, BucketProgress>,
}
pub struct SyncProgressFromCheckpoint {
pub progress: SyncDownloadProgress,
pub needs_counter_reset: bool,
}
impl SyncDownloadProgress {
pub fn for_checkpoint<'a>(
checkpoint: &OwnedCheckpoint,
adapter: &StorageAdapter,
) -> Result<SyncProgressFromCheckpoint, ResultCode> {
let mut buckets = BTreeMap::<String, BucketProgress>::new();
let mut needs_reset = false;
for bucket in checkpoint.buckets.values() {
buckets.insert(
bucket.bucket.clone(),
BucketProgress {
priority: bucket.priority,
target_count: bucket.count.unwrap_or(0),
// Will be filled out later by iterating local_progress
at_last: 0,
since_last: 0,
},
);
}
// Go through local bucket states to detect pending progress from previous sync iterations
// that may have been interrupted.
adapter.progress_stmt.reset()?;
while let Some(row) = adapter.step_progress()? {
let Some(progress) = buckets.get_mut(row.bucket) else {
continue;
};
progress.at_last = row.count_at_last;
progress.since_last = row.count_since_last;
if progress.target_count < row.count_at_last + row.count_since_last {
needs_reset = true;
// Either due to a defrag / sync rule deploy or a compactioon operation, the size
// of the bucket shrank so much that the local ops exceed the ops in the updated
// bucket. We can't possibly report progress in this case (it would overshoot 100%).
for (_, progress) in &mut buckets {
progress.at_last = 0;
progress.since_last = 0;
}
break;
}
}
adapter.progress_stmt.reset()?;
Ok(SyncProgressFromCheckpoint {
progress: Self { buckets },
needs_counter_reset: needs_reset,
})
}
pub fn increment_download_count(&mut self, line: &DataLine) {
if let Some(info) = self.buckets.get_mut(&*line.bucket) {
info.since_last += line.data.len() as i64
}
}
}