-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathAbstractStreamingSyncImplementation.ts
More file actions
1191 lines (1062 loc) · 41.9 KB
/
Copy pathAbstractStreamingSyncImplementation.ts
File metadata and controls
1191 lines (1062 loc) · 41.9 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Logger, { ILogger } from 'js-logger';
import { DataStream } from '../../../utils/DataStream.js';
import { SyncStatus, SyncStatusOptions } from '../../../db/crud/SyncStatus.js';
import { FULL_SYNC_PRIORITY, InternalProgressInformation } from '../../../db/crud/SyncProgress.js';
import * as sync_status from '../../../db/crud/SyncStatus.js';
import { AbortOperation } from '../../../utils/AbortOperation.js';
import { BaseListener, BaseObserver, Disposable } from '../../../utils/BaseObserver.js';
import { throttleLeadingTrailing } from '../../../utils/async.js';
import {
BucketChecksum,
BucketDescription,
BucketStorageAdapter,
Checkpoint,
PowerSyncControlCommand
} from '../bucket/BucketStorageAdapter.js';
import { CrudEntry } from '../bucket/CrudEntry.js';
import { SyncDataBucket } from '../bucket/SyncDataBucket.js';
import { AbstractRemote, FetchStrategy, SyncStreamOptions } from './AbstractRemote.js';
import {
BucketRequest,
CrudUploadNotification,
StreamingSyncLine,
StreamingSyncLineOrCrudUploadComplete,
StreamingSyncRequestParameterType,
isStreamingKeepalive,
isStreamingSyncCheckpoint,
isStreamingSyncCheckpointComplete,
isStreamingSyncCheckpointDiff,
isStreamingSyncCheckpointPartiallyComplete,
isStreamingSyncData
} from './streaming-sync-types.js';
import { EstablishSyncStream, Instruction, SyncPriorityStatus } from './core-instruction.js';
export enum LockType {
CRUD = 'crud',
SYNC = 'sync'
}
export enum SyncStreamConnectionMethod {
HTTP = 'http',
WEB_SOCKET = 'web-socket'
}
export enum SyncClientImplementation {
/**
* Decodes and handles sync lines received from the sync service in JavaScript.
*
* This is the default option.
*
* @deprecated Don't use {@link SyncClientImplementation.JAVASCRIPT} directly. Instead, use
* {@link DEFAULT_SYNC_CLIENT_IMPLEMENTATION} or omit the option. The explicit choice to use
* the JavaScript-based sync implementation will be removed from a future version of the SDK.
*/
JAVASCRIPT = 'js',
/**
* This implementation offloads the sync line decoding and handling into the PowerSync
* core extension.
*
* @experimental
* While this implementation is more performant than {@link SyncClientImplementation.JAVASCRIPT},
* it has seen less real-world testing and is marked as __experimental__ at the moment.
*
* ## Compatibility warning
*
* The Rust sync client stores sync data in a format that is slightly different than the one used
* by the old {@link JAVASCRIPT} implementation. When adopting the {@link RUST} client on existing
* databases, the PowerSync SDK will migrate the format automatically.
* Further, the {@link JAVASCRIPT} client in recent versions of the PowerSync JS SDK (starting from
* the version introducing {@link RUST} as an option) also supports the new format, so you can switch
* back to {@link JAVASCRIPT} later.
*
* __However__: Upgrading the SDK version, then adopting {@link RUST} as a sync client and later
* downgrading the SDK to an older version (necessarily using the JavaScript-based implementation then)
* can lead to sync issues.
*/
RUST = 'rust'
}
/**
* The default {@link SyncClientImplementation} to use.
*
* Please use this field instead of {@link SyncClientImplementation.JAVASCRIPT} directly. A future version
* of the PowerSync SDK will enable {@link SyncClientImplementation.RUST} by default and remove the JavaScript
* option.
*/
export const DEFAULT_SYNC_CLIENT_IMPLEMENTATION = SyncClientImplementation.JAVASCRIPT;
/**
* Abstract Lock to be implemented by various JS environments
*/
export interface LockOptions<T> {
callback: () => Promise<T>;
type: LockType;
signal?: AbortSignal;
}
export interface AbstractStreamingSyncImplementationOptions extends AdditionalConnectionOptions {
adapter: BucketStorageAdapter;
uploadCrud: () => Promise<void>;
/**
* An identifier for which PowerSync DB this sync implementation is
* linked to. Most commonly DB name, but not restricted to DB name.
*/
identifier?: string;
logger?: ILogger;
remote: AbstractRemote;
}
export interface StreamingSyncImplementationListener extends BaseListener {
/**
* Triggered whenever a status update has been attempted to be made or
* refreshed.
*/
statusUpdated?: ((statusUpdate: SyncStatusOptions) => void) | undefined;
/**
* Triggers whenever the status' members have changed in value
*/
statusChanged?: ((status: SyncStatus) => void) | undefined;
}
/**
* Configurable options to be used when connecting to the PowerSync
* backend instance.
*/
export type PowerSyncConnectionOptions = Omit<InternalConnectionOptions, 'serializedSchema'>;
export interface InternalConnectionOptions extends BaseConnectionOptions, AdditionalConnectionOptions {}
/** @internal */
export interface BaseConnectionOptions {
/**
* Whether to use a JavaScript implementation to handle received sync lines from the sync
* service, or whether this work should be offloaded to the PowerSync core extension.
*
* This defaults to the JavaScript implementation ({@link SyncClientImplementation.JAVASCRIPT})
* since the ({@link SyncClientImplementation.RUST}) implementation is experimental at the moment.
*/
clientImplementation?: SyncClientImplementation;
/**
* The connection method to use when streaming updates from
* the PowerSync backend instance.
* Defaults to a HTTP streaming connection.
*/
connectionMethod?: SyncStreamConnectionMethod;
/**
* The fetch strategy to use when streaming updates from the PowerSync backend instance.
*/
fetchStrategy?: FetchStrategy;
/**
* These parameters are passed to the sync rules, and will be available under the`user_parameters` object.
*/
params?: Record<string, StreamingSyncRequestParameterType>;
/**
* The serialized schema - mainly used to forward information about raw tables to the sync client.
*/
serializedSchema?: any;
}
/** @internal */
export interface AdditionalConnectionOptions {
/**
* Delay for retrying sync streaming operations
* from the PowerSync backend after an error occurs.
*/
retryDelayMs?: number;
/**
* Backend Connector CRUD operations are throttled
* to occur at most every `crudUploadThrottleMs`
* milliseconds.
*/
crudUploadThrottleMs?: number;
}
/** @internal */
export type RequiredAdditionalConnectionOptions = Required<AdditionalConnectionOptions>;
export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener>, Disposable {
/**
* Connects to the sync service
*/
connect(options?: InternalConnectionOptions): Promise<void>;
/**
* Disconnects from the sync services.
* @throws if not connected or if abort is not controlled internally
*/
disconnect(): Promise<void>;
getWriteCheckpoint: () => Promise<string>;
hasCompletedSync: () => Promise<boolean>;
isConnected: boolean;
lastSyncedAt?: Date;
syncStatus: SyncStatus;
triggerCrudUpload: () => void;
waitForReady(): Promise<void>;
waitForStatus(status: SyncStatusOptions): Promise<void>;
waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
}
export const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000;
export const DEFAULT_RETRY_DELAY_MS = 5000;
export const DEFAULT_STREAMING_SYNC_OPTIONS = {
retryDelayMs: DEFAULT_RETRY_DELAY_MS,
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
};
export type RequiredPowerSyncConnectionOptions = Required<BaseConnectionOptions>;
export const DEFAULT_STREAM_CONNECTION_OPTIONS: RequiredPowerSyncConnectionOptions = {
connectionMethod: SyncStreamConnectionMethod.WEB_SOCKET,
clientImplementation: DEFAULT_SYNC_CLIENT_IMPLEMENTATION,
fetchStrategy: FetchStrategy.Buffered,
params: {},
serializedSchema: undefined
};
// The priority we assume when we receive checkpoint lines where no priority is set.
// This is the default priority used by the sync service, but can be set to an arbitrary
// value since sync services without priorities also won't send partial sync completion
// messages.
const FALLBACK_PRIORITY = 3;
export abstract class AbstractStreamingSyncImplementation
extends BaseObserver<StreamingSyncImplementationListener>
implements StreamingSyncImplementation
{
protected _lastSyncedAt: Date | null;
protected options: AbstractStreamingSyncImplementationOptions;
protected abortController: AbortController | null;
protected crudUpdateListener?: () => void;
protected streamingSyncPromise?: Promise<void>;
protected logger: ILogger;
private isUploadingCrud: boolean = false;
private notifyCompletedUploads?: () => void;
syncStatus: SyncStatus;
triggerCrudUpload: () => void;
constructor(options: AbstractStreamingSyncImplementationOptions) {
super();
this.options = { ...DEFAULT_STREAMING_SYNC_OPTIONS, ...options };
this.logger = options.logger ?? Logger.get('PowerSyncStream');
this.syncStatus = new SyncStatus({
connected: false,
connecting: false,
lastSyncedAt: undefined,
dataFlow: {
uploading: false,
downloading: false
}
});
this.abortController = null;
this.triggerCrudUpload = throttleLeadingTrailing(() => {
if (!this.syncStatus.connected || this.isUploadingCrud) {
return;
}
this.isUploadingCrud = true;
this._uploadAllCrud().finally(() => {
this.notifyCompletedUploads?.();
this.isUploadingCrud = false;
});
}, this.options.crudUploadThrottleMs!);
}
async waitForReady() {}
waitForStatus(status: SyncStatusOptions): Promise<void> {
return this.waitUntilStatusMatches((currentStatus) => {
/**
* Match only the partial status options provided in the
* matching status
*/
const matchPartialObject = (compA: object, compB: object) => {
return Object.entries(compA).every(([key, value]) => {
const comparisonBValue = compB[key];
if (typeof value == 'object' && typeof comparisonBValue == 'object') {
return matchPartialObject(value, comparisonBValue);
}
return value == comparisonBValue;
});
};
return matchPartialObject(status, currentStatus);
});
}
waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void> {
return new Promise((resolve) => {
if (predicate(this.syncStatus)) {
resolve();
return;
}
const l = this.registerListener({
statusChanged: (updatedStatus) => {
if (predicate(updatedStatus)) {
resolve();
l?.();
}
}
});
});
}
get lastSyncedAt() {
const lastSynced = this.syncStatus.lastSyncedAt;
return lastSynced && new Date(lastSynced);
}
get isConnected() {
return this.syncStatus.connected;
}
async dispose() {
this.crudUpdateListener?.();
this.crudUpdateListener = undefined;
}
abstract obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
async hasCompletedSync() {
return this.options.adapter.hasCompletedSync();
}
async getWriteCheckpoint(): Promise<string> {
const clientId = await this.options.adapter.getClientId();
let path = `/write-checkpoint2.json?client_id=${clientId}`;
const response = await this.options.remote.get(path);
const checkpoint = response['data']['write_checkpoint'] as string;
this.logger.debug(`Created write checkpoint: ${checkpoint}`);
return checkpoint;
}
protected async _uploadAllCrud(): Promise<void> {
return this.obtainLock({
type: LockType.CRUD,
callback: async () => {
/**
* Keep track of the first item in the CRUD queue for the last `uploadCrud` iteration.
*/
let checkedCrudItem: CrudEntry | undefined;
while (true) {
try {
/**
* This is the first item in the FIFO CRUD queue.
*/
const nextCrudItem = await this.options.adapter.nextCrudItem();
if (nextCrudItem) {
this.updateSyncStatus({
dataFlow: {
uploading: true
}
});
if (nextCrudItem.clientId == checkedCrudItem?.clientId) {
// This will force a higher log level than exceptions which are caught here.
this.logger.warn(`Potentially previously uploaded CRUD entries are still present in the upload queue.
Make sure to handle uploads and complete CRUD transactions or batches by calling and awaiting their [.complete()] method.
The next upload iteration will be delayed.`);
throw new Error('Delaying due to previously encountered CRUD item.');
}
checkedCrudItem = nextCrudItem;
await this.options.uploadCrud();
this.updateSyncStatus({
dataFlow: {
uploadError: undefined
}
});
} else {
// Uploading is completed
const neededUpdate = await this.options.adapter.updateLocalTarget(() => this.getWriteCheckpoint());
if (neededUpdate == false && checkedCrudItem != null) {
// Only log this if there was something to upload
this.logger.debug('Upload complete, no write checkpoint needed.');
}
break;
}
} catch (ex) {
checkedCrudItem = undefined;
this.updateSyncStatus({
dataFlow: {
uploading: false,
uploadError: ex
}
});
await this.delayRetry();
if (!this.isConnected) {
// Exit the upload loop if the sync stream is no longer connected
break;
}
this.logger.debug(
`Caught exception when uploading. Upload will retry after a delay. Exception: ${ex.message}`
);
} finally {
this.updateSyncStatus({
dataFlow: {
uploading: false
}
});
}
}
}
});
}
async connect(options?: PowerSyncConnectionOptions) {
if (this.abortController) {
await this.disconnect();
}
const controller = new AbortController();
this.abortController = controller;
this.streamingSyncPromise = this.streamingSync(this.abortController.signal, options);
// Return a promise that resolves when the connection status is updated to indicate that we're connected.
return new Promise<void>((resolve) => {
const disposer = this.registerListener({
statusChanged: (status) => {
if (status.dataFlowStatus.downloadError != null) {
this.logger.warn('Initial connect attempt did not successfully connect to server');
} else if (status.connecting) {
// Still connecting.
return;
}
disposer();
resolve();
}
});
});
}
async disconnect(): Promise<void> {
if (!this.abortController) {
return;
}
// This might be called multiple times
if (!this.abortController.signal.aborted) {
this.abortController.abort(new AbortOperation('Disconnect has been requested'));
}
// Await any pending operations before completing the disconnect operation
try {
await this.streamingSyncPromise;
} catch (ex) {
// The operation might have failed, all we care about is if it has completed
this.logger.warn(ex);
}
this.streamingSyncPromise = undefined;
this.abortController = null;
this.updateSyncStatus({ connected: false, connecting: false });
}
/**
* @deprecated use [connect instead]
*/
async streamingSync(signal?: AbortSignal, options?: PowerSyncConnectionOptions): Promise<void> {
if (!signal) {
this.abortController = new AbortController();
signal = this.abortController.signal;
}
/**
* Listen for CRUD updates and trigger upstream uploads
*/
this.crudUpdateListener = this.options.adapter.registerListener({
crudUpdate: () => this.triggerCrudUpload()
});
/**
* Create a new abort controller which aborts items downstream.
* This is needed to close any previous connections on exception.
*/
let nestedAbortController = new AbortController();
signal.addEventListener('abort', () => {
/**
* A request for disconnect was received upstream. Relay the request
* to the nested abort controller.
*/
nestedAbortController.abort(signal?.reason ?? new AbortOperation('Received command to disconnect from upstream'));
this.crudUpdateListener?.();
this.crudUpdateListener = undefined;
this.updateSyncStatus({
connected: false,
connecting: false,
dataFlow: {
downloading: false,
downloadProgress: null
}
});
});
/**
* This loops runs until [retry] is false or the abort signal is set to aborted.
* Aborting the nestedAbortController will:
* - Abort any pending fetch requests
* - Close any sync stream ReadableStreams (which will also close any established network requests)
*/
while (true) {
this.updateSyncStatus({ connecting: true });
let shouldDelayRetry = true;
try {
if (signal?.aborted) {
break;
}
await this.streamingSyncIteration(nestedAbortController.signal, options);
// Continue immediately, streamingSyncIteration will wait before completing if necessary.
} catch (ex) {
/**
* Either:
* - A network request failed with a failed connection or not OKAY response code.
* - There was a sync processing error.
* - The connection was aborted.
* This loop will retry after a delay if the connection was not aborted.
* The nested abort controller will cleanup any open network requests and streams.
* The WebRemote should only abort pending fetch requests or close active Readable streams.
*/
if (ex instanceof AbortOperation) {
this.logger.warn(ex);
shouldDelayRetry = false;
// A disconnect was requested, we should not delay since there is no explicit retry
} else {
this.logger.error(ex);
}
this.updateSyncStatus({
dataFlow: {
downloadError: ex
}
});
} finally {
this.notifyCompletedUploads = undefined;
if (!signal.aborted) {
nestedAbortController.abort(new AbortOperation('Closing sync stream network requests before retry.'));
nestedAbortController = new AbortController();
}
this.updateSyncStatus({
connected: false,
connecting: true // May be unnecessary
});
// On error, wait a little before retrying
if (shouldDelayRetry) {
await this.delayRetry(nestedAbortController.signal);
}
}
}
// Mark as disconnected if here
this.updateSyncStatus({ connected: false, connecting: false });
}
private async collectLocalBucketState(): Promise<[BucketRequest[], Map<string, BucketDescription | null>]> {
const bucketEntries = await this.options.adapter.getBucketStates();
const req: BucketRequest[] = bucketEntries.map((entry) => ({
name: entry.bucket,
after: entry.op_id
}));
const localDescriptions = new Map<string, BucketDescription | null>();
for (const entry of bucketEntries) {
localDescriptions.set(entry.bucket, null);
}
return [req, localDescriptions];
}
/**
* Older versions of the JS SDK used to encode subkeys as JSON in {@link OplogEntry.toJSON}.
* Because subkeys are always strings, this leads to quotes being added around them in `ps_oplog`.
* While this is not a problem as long as it's done consistently, it causes issues when a database
* created by the JS SDK is used with other SDKs, or (more likely) when the new Rust sync client
* is enabled.
*
* So, we add a migration from the old key format (with quotes) to the new one (no quotes). The
* migration is only triggered when necessary (for now). The function returns whether the new format
* should be used, so that the JS SDK is able to write to updated databases.
*
* @param requireFixedKeyFormat Whether we require the new format or also support the old one.
* The Rust client requires the new subkey format.
* @returns Whether the database is now using the new, fixed subkey format.
*/
private async requireKeyFormat(requireFixedKeyFormat: boolean): Promise<boolean> {
const hasMigrated = await this.options.adapter.hasMigratedSubkeys();
if (requireFixedKeyFormat && !hasMigrated) {
await this.options.adapter.migrateToFixedSubkeys();
return true;
} else {
return hasMigrated;
}
}
protected async streamingSyncIteration(signal: AbortSignal, options?: PowerSyncConnectionOptions): Promise<void> {
await this.obtainLock({
type: LockType.SYNC,
signal,
callback: async () => {
const resolvedOptions: RequiredPowerSyncConnectionOptions = {
...DEFAULT_STREAM_CONNECTION_OPTIONS,
...(options ?? {})
};
if (resolvedOptions.clientImplementation == SyncClientImplementation.JAVASCRIPT) {
await this.legacyStreamingSyncIteration(signal, resolvedOptions);
} else {
await this.requireKeyFormat(true);
await this.rustSyncIteration(signal, resolvedOptions);
}
}
});
}
private async legacyStreamingSyncIteration(signal: AbortSignal, resolvedOptions: RequiredPowerSyncConnectionOptions) {
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
if (rawTables != null && rawTables.length) {
this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
}
this.logger.debug('Streaming sync iteration started');
this.options.adapter.startSession();
let [req, bucketMap] = await this.collectLocalBucketState();
let targetCheckpoint: Checkpoint | null = null;
// A checkpoint that has been validated but not applied (e.g. due to pending local writes)
let pendingValidatedCheckpoint: Checkpoint | null = null;
const clientId = await this.options.adapter.getClientId();
const usingFixedKeyFormat = await this.requireKeyFormat(false);
this.logger.debug('Requesting stream from server');
const syncOptions: SyncStreamOptions = {
path: '/sync/stream',
abortSignal: signal,
data: {
buckets: req,
include_checksum: true,
raw_data: true,
parameters: resolvedOptions.params,
client_id: clientId
}
};
let stream: DataStream<StreamingSyncLineOrCrudUploadComplete>;
if (resolvedOptions?.connectionMethod == SyncStreamConnectionMethod.HTTP) {
stream = await this.options.remote.postStreamRaw(syncOptions, (line: string | CrudUploadNotification) => {
if (typeof line == 'string') {
return JSON.parse(line) as StreamingSyncLine;
} else {
// Directly enqueued by us
return line;
}
});
} else {
const bson = await this.options.remote.getBSON();
stream = await this.options.remote.socketStreamRaw(
{
...syncOptions,
...{ fetchStrategy: resolvedOptions.fetchStrategy }
},
(payload: Uint8Array | CrudUploadNotification) => {
if (payload instanceof Uint8Array) {
return bson.deserialize(payload) as StreamingSyncLine;
} else {
// Directly enqueued by us
return payload;
}
},
bson
);
}
this.logger.debug('Stream established. Processing events');
this.notifyCompletedUploads = () => {
if (!stream.closed) {
stream.enqueueData({ crud_upload_completed: null });
}
};
while (!stream.closed) {
const line = await stream.read();
if (!line) {
// The stream has closed while waiting
return;
}
if ('crud_upload_completed' in line) {
if (pendingValidatedCheckpoint != null) {
const { applied, endIteration } = await this.applyCheckpoint(pendingValidatedCheckpoint);
if (applied) {
pendingValidatedCheckpoint = null;
} else if (endIteration) {
break;
}
}
continue;
}
// A connection is active and messages are being received
if (!this.syncStatus.connected) {
// There is a connection now
Promise.resolve().then(() => this.triggerCrudUpload());
this.updateSyncStatus({
connected: true
});
}
if (isStreamingSyncCheckpoint(line)) {
targetCheckpoint = line.checkpoint;
// New checkpoint - existing validated checkpoint is no longer valid
pendingValidatedCheckpoint = null;
const bucketsToDelete = new Set<string>(bucketMap.keys());
const newBuckets = new Map<string, BucketDescription>();
for (const checksum of line.checkpoint.buckets) {
newBuckets.set(checksum.bucket, {
name: checksum.bucket,
priority: checksum.priority ?? FALLBACK_PRIORITY
});
bucketsToDelete.delete(checksum.bucket);
}
if (bucketsToDelete.size > 0) {
this.logger.debug('Removing buckets', [...bucketsToDelete]);
}
bucketMap = newBuckets;
await this.options.adapter.removeBuckets([...bucketsToDelete]);
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
} else if (isStreamingSyncCheckpointComplete(line)) {
const result = await this.applyCheckpoint(targetCheckpoint!);
if (result.endIteration) {
return;
} else if (!result.applied) {
// "Could not apply checkpoint due to local data". We need to retry after
// finishing uploads.
pendingValidatedCheckpoint = targetCheckpoint;
} else {
// Nothing to retry later. This would likely already be null from the last
// checksum or checksum_diff operation, but we make sure.
pendingValidatedCheckpoint = null;
}
} else if (isStreamingSyncCheckpointPartiallyComplete(line)) {
const priority = line.partial_checkpoint_complete.priority;
this.logger.debug('Partial checkpoint complete', priority);
const result = await this.options.adapter.syncLocalDatabase(targetCheckpoint!, priority);
if (!result.checkpointValid) {
// This means checksums failed. Start again with a new checkpoint.
// TODO: better back-off
await new Promise((resolve) => setTimeout(resolve, 50));
return;
} else if (!result.ready) {
// If we have pending uploads, we can't complete new checkpoints outside of priority 0.
// We'll resolve this for a complete checkpoint.
} else {
// We'll keep on downloading, but can report that this priority is synced now.
this.logger.debug('partial checkpoint validation succeeded');
// All states with a higher priority can be deleted since this partial sync includes them.
const priorityStates = this.syncStatus.priorityStatusEntries.filter((s) => s.priority <= priority);
priorityStates.push({
priority,
lastSyncedAt: new Date(),
hasSynced: true
});
this.updateSyncStatus({
connected: true,
priorityStatusEntries: priorityStates
});
}
} else if (isStreamingSyncCheckpointDiff(line)) {
// TODO: It may be faster to just keep track of the diff, instead of the entire checkpoint
if (targetCheckpoint == null) {
throw new Error('Checkpoint diff without previous checkpoint');
}
// New checkpoint - existing validated checkpoint is no longer valid
pendingValidatedCheckpoint = null;
const diff = line.checkpoint_diff;
const newBuckets = new Map<string, BucketChecksum>();
for (const checksum of targetCheckpoint.buckets) {
newBuckets.set(checksum.bucket, checksum);
}
for (const checksum of diff.updated_buckets) {
newBuckets.set(checksum.bucket, checksum);
}
for (const bucket of diff.removed_buckets) {
newBuckets.delete(bucket);
}
const newCheckpoint: Checkpoint = {
last_op_id: diff.last_op_id,
buckets: [...newBuckets.values()],
write_checkpoint: diff.write_checkpoint
};
targetCheckpoint = newCheckpoint;
await this.updateSyncStatusForStartingCheckpoint(targetCheckpoint);
bucketMap = new Map();
newBuckets.forEach((checksum, name) =>
bucketMap.set(name, {
name: checksum.bucket,
priority: checksum.priority ?? FALLBACK_PRIORITY
})
);
const bucketsToDelete = diff.removed_buckets;
if (bucketsToDelete.length > 0) {
this.logger.debug('Remove buckets', bucketsToDelete);
}
await this.options.adapter.removeBuckets(bucketsToDelete);
await this.options.adapter.setTargetCheckpoint(targetCheckpoint);
} else if (isStreamingSyncData(line)) {
const { data } = line;
const previousProgress = this.syncStatus.dataFlowStatus.downloadProgress;
let updatedProgress: InternalProgressInformation | null = null;
if (previousProgress) {
updatedProgress = { ...previousProgress };
const progressForBucket = updatedProgress[data.bucket];
if (progressForBucket) {
updatedProgress[data.bucket] = {
...progressForBucket,
since_last: progressForBucket.since_last + data.data.length
};
}
}
this.updateSyncStatus({
dataFlow: {
downloading: true,
downloadProgress: updatedProgress
}
});
await this.options.adapter.saveSyncData({ buckets: [SyncDataBucket.fromRow(data)] }, usingFixedKeyFormat);
} else if (isStreamingKeepalive(line)) {
const remaining_seconds = line.token_expires_in;
if (remaining_seconds == 0) {
// Connection would be closed automatically right after this
this.logger.debug('Token expiring; reconnect');
/**
* For a rare case where the backend connector does not update the token
* (uses the same one), this should have some delay.
*/
await this.delayRetry();
return;
} else if (remaining_seconds < 30) {
this.logger.debug('Token will expire soon; reconnect');
// Pre-emptively refresh the token
this.options.remote.invalidateCredentials();
return;
}
this.triggerCrudUpload();
} else {
this.logger.debug('Received unknown sync line', line);
}
}
this.logger.debug('Stream input empty');
// Connection closed. Likely due to auth issue.
return;
}
private async rustSyncIteration(signal: AbortSignal, resolvedOptions: RequiredPowerSyncConnectionOptions) {
const syncImplementation = this;
const adapter = this.options.adapter;
const remote = this.options.remote;
let receivingLines: Promise<void> | null = null;
let hadSyncLine = false;
const abortController = new AbortController();
signal.addEventListener('abort', () => abortController.abort());
// Pending sync lines received from the service, as well as local events that trigger a powersync_control
// invocation (local events include refreshed tokens and completed uploads).
// This is a single data stream so that we can handle all control calls from a single place.
let controlInvocations: DataStream<EnqueuedCommand, Uint8Array | EnqueuedCommand> | null = null;
async function connect(instr: EstablishSyncStream) {
const syncOptions: SyncStreamOptions = {
path: '/sync/stream',
abortSignal: abortController.signal,
data: instr.request
};
if (resolvedOptions.connectionMethod == SyncStreamConnectionMethod.HTTP) {
controlInvocations = await remote.postStreamRaw(syncOptions, (line: string | EnqueuedCommand) => {
if (typeof line == 'string') {
return {
command: PowerSyncControlCommand.PROCESS_TEXT_LINE,
payload: line
};
} else {
// Directly enqueued by us
return line;
}
});
} else {
controlInvocations = await remote.socketStreamRaw(
{
...syncOptions,
fetchStrategy: resolvedOptions.fetchStrategy
},
(payload: Uint8Array | EnqueuedCommand) => {
if (payload instanceof Uint8Array) {
return {
command: PowerSyncControlCommand.PROCESS_BSON_LINE,
payload: payload
};
} else {
// Directly enqueued by us
return payload;
}
}
);
}
// The rust client will set connected: true after the first sync line because that's when it gets invoked, but
// we're already connected here and can report that.
syncImplementation.updateSyncStatus({ connected: true });
try {
while (!controlInvocations.closed) {
const line = await controlInvocations.read();
if (line == null) {
return;
}
await control(line.command, line.payload);
if (!hadSyncLine) {
syncImplementation.triggerCrudUpload();
hadSyncLine = true;
}
}
} finally {
const activeInstructions = controlInvocations;
// We concurrently add events to the active data stream when e.g. a CRUD upload is completed or a token is
// refreshed. That would throw after closing (and we can't handle those events either way), so set this back
// to null.
controlInvocations = null;
await activeInstructions.close();
}
}
async function stop() {
await control(PowerSyncControlCommand.STOP);
}
async function control(op: PowerSyncControlCommand, payload?: Uint8Array | string) {
const rawResponse = await adapter.control(op, payload ?? null);
await handleInstructions(JSON.parse(rawResponse));
}
async function handleInstruction(instruction: Instruction) {
if ('LogLine' in instruction) {
switch (instruction.LogLine.severity) {
case 'DEBUG':
syncImplementation.logger.debug(instruction.LogLine.line);
break;
case 'INFO':
syncImplementation.logger.info(instruction.LogLine.line);
break;
case 'WARNING':
syncImplementation.logger.warn(instruction.LogLine.line);
break;
}
} else if ('UpdateSyncStatus' in instruction) {
function coreStatusToJs(status: SyncPriorityStatus): sync_status.SyncPriorityStatus {
return {
priority: status.priority,
hasSynced: status.has_synced ?? undefined,
lastSyncedAt: status?.last_synced_at != null ? new Date(status!.last_synced_at! * 1000) : undefined
};
}
const info = instruction.UpdateSyncStatus.status;
const coreCompleteSync = info.priority_status.find((s) => s.priority == FULL_SYNC_PRIORITY);
const completeSync = coreCompleteSync != null ? coreStatusToJs(coreCompleteSync) : null;
syncImplementation.updateSyncStatus({
connected: info.connected,
connecting: info.connecting,
dataFlow: {
downloading: info.downloading != null,
downloadProgress: info.downloading?.buckets
},