syncular-runtime 0.1.0

Shared Rust runtime for Syncular SQLite-backed native and browser clients.
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::binary_snapshot::SnapshotChunkRows;
use crate::client::SubscriptionSpec;
use crate::error::{Result, SyncularError};
use crate::protocol::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[cfg(not(all(target_arch = "wasm32", feature = "web-transport")))]
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(not(all(target_arch = "wasm32", feature = "web-transport")))]
pub fn now_ms() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as i64
}

#[cfg(all(target_arch = "wasm32", feature = "web-transport"))]
pub fn now_ms() -> i64 {
    js_sys::Date::now() as i64
}

pub const MAX_SYNC_RETRIES: i32 = 5;
pub const SYNC_SENDING_TIMEOUT_MS: i64 = 30_000;
pub const MAX_BLOB_UPLOAD_RETRIES: i32 = 3;
pub const BLOB_UPLOAD_STALE_TIMEOUT_MS: i64 = 30_000;
pub const SQLITE_BUSY_TIMEOUT_MS: i32 = 5_000;
pub const APP_SCHEMA_ID: &str = "syncular-app";

const RETRY_BASE_DELAY_MS: i64 = 1_000;
const RETRY_MAX_DELAY_MS: i64 = 30_000;
const BLOB_UPLOAD_RETRY_BASE_DELAY_MS: i64 = 100;
const BLOB_UPLOAD_RETRY_MAX_DELAY_MS: i64 = 5_000;

pub fn retry_backoff_delay_ms(attempt_count: i32) -> i64 {
    let exponent = attempt_count.saturating_sub(1).min(12) as u32;
    RETRY_BASE_DELAY_MS
        .saturating_mul(2_i64.saturating_pow(exponent))
        .min(RETRY_MAX_DELAY_MS)
}

pub fn next_retry_at(now: i64, attempt_count: i32) -> i64 {
    now.saturating_add(retry_backoff_delay_ms(attempt_count))
}

pub fn blob_upload_retry_backoff_delay_ms(attempt_count: i32) -> i64 {
    let exponent = attempt_count.saturating_sub(1).min(12) as u32;
    BLOB_UPLOAD_RETRY_BASE_DELAY_MS
        .saturating_mul(2_i64.saturating_pow(exponent))
        .min(BLOB_UPLOAD_RETRY_MAX_DELAY_MS)
}

pub fn next_blob_upload_retry_at(now: i64, attempt_count: i32) -> i64 {
    now.saturating_add(blob_upload_retry_backoff_delay_ms(attempt_count))
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg(feature = "demo-todo-fixture")]
pub struct Task {
    pub id: String,
    pub title: String,
    pub completed: i32,
    pub user_id: String,
    pub project_id: Option<String>,
    pub server_version: i64,
    pub image: Option<String>,
    pub title_yjs_state: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutboxCommit {
    pub id: String,
    pub client_commit_id: String,
    pub status: String,
    pub operations_json: String,
    pub last_response_json: Option<String>,
    pub error: Option<String>,
    pub created_at: i64,
    pub updated_at: i64,
    pub attempt_count: i32,
    pub acked_commit_seq: Option<i64>,
    pub schema_version: i32,
    pub next_attempt_at: i64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub auth_lease: Option<AuthLeaseProvenance>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionState {
    pub state_id: String,
    pub subscription_id: String,
    pub table: String,
    pub scopes_json: String,
    pub params_json: String,
    pub cursor: i64,
    pub bootstrap_state_json: Option<String>,
    pub status: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerifiedRoot {
    pub state_id: String,
    pub subscription_id: String,
    pub partition_id: String,
    pub commit_seq: i64,
    pub root: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppliedMigration {
    pub version: String,
    pub name: String,
    pub checksum: String,
    pub applied_at: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AppSchemaState {
    pub schema_id: String,
    pub schema_version: Option<i32>,
    pub current_schema_version: i32,
    pub updated_at: Option<i64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutboxSummary {
    pub client_commit_id: String,
    pub status: String,
    pub schema_version: i32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub auth_lease: Option<AuthLeaseProvenance>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthLeaseRecord {
    pub lease_id: String,
    pub kid: String,
    pub actor_id: String,
    pub issued_at_ms: i64,
    pub not_before_ms: i64,
    pub expires_at_ms: i64,
    pub schema_version: i32,
    pub payload_json: String,
    pub token: String,
    pub status: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_validation_error: Option<String>,
    pub created_at_ms: i64,
    pub updated_at_ms: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConflictSummary {
    pub id: String,
    pub client_commit_id: String,
    pub op_index: i32,
    pub result_status: String,
    pub message: String,
    pub code: Option<String>,
    pub server_version: Option<i64>,
    pub resolved_at: Option<i64>,
    pub resolution: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlobHealthSummary {
    pub cache_count: i64,
    pub cache_bytes: i64,
    pub upload_pending: i64,
    pub upload_uploading: i64,
    pub upload_failed: i64,
    pub checked_references: i64,
    pub invalid_references: i64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CrdtHealthSummary {
    pub document_count: i64,
    pub pending_updates: i64,
    pub flushed_updates: i64,
    pub acked_updates: i64,
    pub log_updates: i64,
    pub orphaned_documents: i64,
    pub orphaned_log_entries: i64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScopedRowsHealthSummary {
    pub checked_synced_rows: i64,
    pub orphaned_synced_rows: i64,
    pub tables: Vec<ScopedRowsTableHealth>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScopedRowsTableHealth {
    pub table: String,
    pub checked_synced_rows: i64,
    pub orphaned_synced_rows: i64,
}

pub trait SyncStore {
    type Tx<'a>: SyncStoreTx
    where
        Self: 'a;

    fn transaction<T>(&mut self, f: impl FnOnce(&mut Self::Tx<'_>) -> Result<T>) -> Result<T>;

    fn supports_sqlite_snapshot_artifacts(&self) -> bool {
        false
    }

    fn decode_sqlite_snapshot_artifact_rows(
        &self,
        _table: &str,
        _artifact_bytes: &[u8],
    ) -> Result<Vec<Value>> {
        Err(SyncularError::protocol_message(
            "snapshot artifacts are not supported by this store",
        ))
    }
}

pub trait SyncStoreTx {
    fn pending_outbox(&mut self, limit: i64) -> Result<Vec<OutboxCommit>>;
    fn requeue_stale_outbox(&mut self) -> Result<()>;
    fn mark_outbox_sending(&mut self, row_id: &str) -> Result<()>;
    fn mark_pushed_operation_server_versions(
        &mut self,
        _outbox: &OutboxCommit,
        _response: &PushCommitResponse,
    ) -> Result<()> {
        Ok(())
    }
    fn mark_outbox_acked(&mut self, row_id: &str, response: &PushCommitResponse) -> Result<()>;
    fn mark_outbox_failed(
        &mut self,
        row_id: &str,
        error: &str,
        response: &PushCommitResponse,
    ) -> Result<()>;
    fn mark_outbox_retry(
        &mut self,
        row_id: &str,
        error: &str,
        next_attempt_at: i64,
        failed: bool,
    ) -> Result<()>;
    fn insert_conflict(&mut self, outbox: &OutboxCommit, result: &OperationResult) -> Result<()>;

    fn upsert_auth_lease(&mut self, _lease: &AuthLeaseRecord) -> Result<()> {
        Err(SyncularError::storage(anyhow::anyhow!(
            "auth lease storage is not supported by this store"
        )))
    }

    fn auth_lease(&mut self, _lease_id: &str) -> Result<Option<AuthLeaseRecord>> {
        Err(SyncularError::storage(anyhow::anyhow!(
            "auth lease storage is not supported by this store"
        )))
    }

    fn active_auth_leases(
        &mut self,
        _actor_id: Option<&str>,
        _now_ms: i64,
    ) -> Result<Vec<AuthLeaseRecord>> {
        Err(SyncularError::storage(anyhow::anyhow!(
            "auth lease storage is not supported by this store"
        )))
    }

    fn set_outbox_auth_lease(
        &mut self,
        _client_commit_id: &str,
        _provenance: Option<&AuthLeaseProvenance>,
    ) -> Result<()> {
        Err(SyncularError::storage(anyhow::anyhow!(
            "outbox auth lease provenance is not supported by this store"
        )))
    }

    fn subscription_state(
        &mut self,
        state_id: &str,
        subscription_id: &str,
    ) -> Result<Option<SubscriptionState>>;
    fn subscription_states(&mut self, _state_id: &str) -> Result<Vec<SubscriptionState>> {
        Ok(Vec::new())
    }
    fn upsert_subscription_state(&mut self, state: &SubscriptionState) -> Result<()>;
    fn delete_subscription_state(&mut self, state_id: &str, subscription_id: &str) -> Result<()>;
    fn verified_root(
        &mut self,
        _state_id: &str,
        _subscription_id: &str,
    ) -> Result<Option<VerifiedRoot>> {
        Ok(None)
    }
    fn verified_roots(&mut self, _state_id: &str) -> Result<Vec<VerifiedRoot>> {
        Ok(Vec::new())
    }
    fn upsert_verified_root(&mut self, _root: &VerifiedRoot) -> Result<()> {
        Ok(())
    }
    fn delete_verified_root(&mut self, _state_id: &str, _subscription_id: &str) -> Result<()> {
        Ok(())
    }
    fn crdt_state_vector_hints(
        &mut self,
        _table: &str,
        _scopes: &ScopeValues,
        _limit: i64,
    ) -> Result<Vec<CrdtStateVectorHint>> {
        Ok(Vec::new())
    }

    fn clear_table_for_scopes(&mut self, table: &str, scopes: &ScopeValues) -> Result<()>;
    fn clear_synced_rows_for_scopes(&mut self, _table: &str, _scopes: &ScopeValues) -> Result<i64> {
        Err(SyncularError::storage(anyhow::anyhow!(
            "clearing synced rows is not supported by this store"
        )))
    }
    fn clear_table_for_scopes_preserving_local_crdt(
        &mut self,
        table: &str,
        scopes: &ScopeValues,
    ) -> Result<()> {
        self.clear_table_for_scopes(table, scopes)
    }
    fn current_row_json(&mut self, _table: &str, _row_id: &str) -> Result<Option<Value>> {
        Ok(None)
    }
    fn upsert_row(&mut self, table: &str, row: &Value, fallback_version: Option<i64>)
        -> Result<()>;
    fn upsert_rows(
        &mut self,
        table: &str,
        rows: &[Value],
        fallback_version: Option<i64>,
    ) -> Result<()> {
        for row in rows {
            self.upsert_row(table, row, fallback_version)?;
        }
        Ok(())
    }
    fn upsert_snapshot_chunk_rows(
        &mut self,
        table: &str,
        rows: &SnapshotChunkRows,
        fallback_version: Option<i64>,
    ) -> Result<()> {
        let rows = rows.clone().try_into_value_rows()?;
        self.upsert_rows(table, &rows, fallback_version)
    }
    fn apply_change(&mut self, change: &SyncChange) -> Result<()>;
}

pub trait SyncStateStore {
    fn applied_migrations(&mut self) -> Result<Vec<AppliedMigration>>;

    fn app_schema_state(&mut self, current_schema_version: i32) -> Result<AppSchemaState> {
        Ok(AppSchemaState {
            schema_id: APP_SCHEMA_ID.to_string(),
            schema_version: None,
            current_schema_version,
            updated_at: None,
        })
    }

    fn outbox_summaries(&mut self) -> Result<Vec<OutboxSummary>>;

    fn next_outbox_retry_at(&mut self) -> Result<Option<i64>> {
        Ok(None)
    }

    fn next_blob_upload_retry_at(&mut self) -> Result<Option<i64>> {
        Ok(None)
    }

    fn conflict_summaries(&mut self) -> Result<Vec<ConflictSummary>>;

    fn blob_health_summary(&mut self) -> Result<Option<BlobHealthSummary>> {
        Ok(None)
    }

    fn crdt_health_summary(&mut self) -> Result<Option<CrdtHealthSummary>> {
        Ok(None)
    }

    fn scoped_rows_health_summary(
        &mut self,
        _subscriptions: &[SubscriptionSpec],
    ) -> Result<Option<ScopedRowsHealthSummary>> {
        Ok(None)
    }

    fn clear_orphaned_synced_rows(
        &mut self,
        _subscriptions: &[SubscriptionSpec],
        _tables: &[String],
    ) -> Result<ScopedRowsHealthSummary> {
        Err(SyncularError::storage(anyhow::anyhow!(
            "clearing orphaned synced rows is not supported by this store"
        )))
    }

    fn resolve_conflict(&mut self, id: &str, resolution: &str) -> Result<()>;

    fn retry_conflict_keep_local(&mut self, id: &str) -> Result<String>;
}

#[cfg(feature = "demo-todo-fixture")]
pub trait DemoTaskStore {
    fn add_task(
        &mut self,
        actor_id: &str,
        project_id: Option<&str>,
        task_id: String,
        title_value: String,
    ) -> Result<()>;

    fn patch_task_title(
        &mut self,
        project_id: Option<&str>,
        task_id: String,
        title_value: String,
    ) -> Result<()>;

    fn list_tasks(&mut self) -> Result<Vec<Task>>;
}