-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMongoSyncBucketStorage.ts
More file actions
628 lines (535 loc) · 20.5 KB
/
Copy pathMongoSyncBucketStorage.ts
File metadata and controls
628 lines (535 loc) · 20.5 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
import * as lib_mongo from '@powersync/lib-service-mongodb';
import { mongo } from '@powersync/lib-service-mongodb';
import {
BaseObserver,
DO_NOT_LOG,
Logger,
ReplicationAbortedError,
ServiceAssertionError
} from '@powersync/lib-services-framework';
import {
BroadcastIterable,
CHECKPOINT_INVALIDATE_ALL,
CheckpointChanges,
GetCheckpointChangesOptions,
InternalOpId,
mergeAsyncIterables,
PopulateChecksumCacheOptions,
PopulateChecksumCacheResults,
ReplicationCheckpoint,
storage,
utils,
WatchWriteCheckpointOptions
} from '@powersync/service-core';
import { HydratedSyncConfig, ParameterLookupRows, ScopedParameterLookup } from '@powersync/service-sync-rules';
import * as bson from 'bson';
import { LRUCache } from 'lru-cache';
import * as timers from 'timers/promises';
import { retryOnMongoMaxTimeMSExpired } from '../../utils/util.js';
import { MongoBucketStorage } from '../MongoBucketStorage.js';
import { BucketDefinitionMapping } from './BucketDefinitionMapping.js';
import { MongoSyncBucketStorageContext } from './common/MongoSyncBucketStorageContext.js';
import type { VersionedPowerSyncMongo } from './db.js';
import { StorageConfig } from './models.js';
import { MongoBucketBatchOptions } from './MongoBucketBatch.js';
import { MongoChecksumOptions, MongoChecksums } from './MongoChecksums.js';
import { MongoCompactOptions, MongoCompactor } from './MongoCompactor.js';
import { MongoParameterCompactor } from './MongoParameterCompactor.js';
import { MongoPersistedReplicationStream } from './MongoPersistedReplicationStream.js';
import { MongoWriteCheckpointAPI } from './MongoWriteCheckpointAPI.js';
export interface MongoSyncBucketStorageOptions {
checksumOptions?: Omit<MongoChecksumOptions, 'storageConfig' | 'mapping'>;
storageConfig: StorageConfig;
}
interface InternalCheckpointChanges extends CheckpointChanges {
updatedWriteCheckpoints: Map<string, bigint>;
invalidateWriteCheckpoints: boolean;
}
export interface WriterSyncState {
lastCheckpointLsn: string | null;
resumeFromLsn: string | null;
keepaliveOp: InternalOpId | null;
syncConfigIds?: bson.ObjectId[];
}
/**
* Only keep checkpoints around for a minute, before fetching a fresh one.
*
* The reason is that we keep a MongoDB snapshot reference (clusterTime) with the checkpoint,
* and they expire after 5 minutes by default. This is an issue if the checkpoint stream is idle,
* but new clients connect and use an outdated checkpoint snapshot for parameter queries.
*
* These will be filtered out for existing clients, so should not create significant overhead.
*/
const CHECKPOINT_TIMEOUT_MS = 60_000;
export abstract class MongoSyncBucketStorage
extends BaseObserver<storage.SyncRulesBucketStorageListener>
implements storage.SyncRulesBucketStorage
{
readonly db: VersionedPowerSyncMongo;
[DO_NOT_LOG] = true;
readonly checksums: MongoChecksums;
private parsedSyncConfigCache: { parsed: HydratedSyncConfig; options: storage.ParseSyncConfigOptions } | undefined;
private writeCheckpointAPI: MongoWriteCheckpointAPI;
public readonly logger: Logger;
public readonly storageConfig: StorageConfig;
#storageInitialized = false;
constructor(
public readonly factory: MongoBucketStorage,
public readonly replicationStreamId: number,
public readonly replicationStream: MongoPersistedReplicationStream,
public readonly replicationStreamName: string,
writeCheckpointMode: storage.WriteCheckpointMode | undefined,
options: MongoSyncBucketStorageOptions
) {
super();
this.storageConfig = options.storageConfig;
this.db = factory.db.versioned(this.storageConfig);
this.checksums = this.createMongoChecksums(options);
this.writeCheckpointAPI = new MongoWriteCheckpointAPI({
db: this.db,
mode: writeCheckpointMode ?? storage.WriteCheckpointMode.MANAGED,
sync_rules_id: replicationStreamId
});
this.logger = replicationStream.logger;
}
/**
* Not for external use - public here for tests only.
*
* @internal
*/
abstract createMongoCompactor(options: MongoCompactOptions): MongoCompactor;
protected abstract createMongoChecksums(options: MongoSyncBucketStorageOptions): MongoChecksums;
protected abstract createMongoParameterCompactor(
checkpoint: InternalOpId,
options: storage.CompactOptions
): MongoParameterCompactor;
get writeCheckpointMode() {
return this.writeCheckpointAPI.writeCheckpointMode;
}
get mapping() {
return this.replicationStream.storageContent.mapping;
}
protected get versionContext(): MongoSyncBucketStorageContext {
return {
db: this.db,
group_id: this.replicationStreamId,
mapping: this.mapping
};
}
setWriteCheckpointMode(mode: storage.WriteCheckpointMode): void {
this.writeCheckpointAPI.setWriteCheckpointMode(mode);
}
createManagedWriteCheckpoint(checkpoint: storage.ManagedWriteCheckpointOptions): Promise<bigint> {
return this.writeCheckpointAPI.createManagedWriteCheckpoint(checkpoint);
}
lastWriteCheckpoint(filters: storage.SyncStorageLastWriteCheckpointFilters): Promise<bigint | null> {
return this.writeCheckpointAPI.lastWriteCheckpoint({
...filters,
sync_rules_id: this.replicationStreamId
});
}
getParsedSyncRules(options: storage.ParseSyncConfigOptions): HydratedSyncConfig {
const { parsed, options: cachedOptions } = this.parsedSyncConfigCache ?? {};
if (!parsed || options.defaultSchema != cachedOptions?.defaultSchema) {
this.parsedSyncConfigCache = { parsed: this.replicationStream.parsed(options).hydratedSyncConfig(), options };
}
return this.parsedSyncConfigCache!.parsed;
}
async getCheckpoint(): Promise<storage.ReplicationCheckpoint> {
return (await this.getCheckpointInternal()) ?? new EmptyReplicationCheckpoint();
}
protected abstract fetchCheckpointState(
session: mongo.ClientSession
): Promise<{ checkpoint: bigint; lsn: string | null } | null>;
async getCheckpointInternal(): Promise<storage.ReplicationCheckpoint | null> {
return await this.db.client.withSession({ snapshot: true }, async (session) => {
const state = await this.fetchCheckpointState(session);
if (state == null) {
return null;
}
const snapshotTime = (session as any).snapshotTime as bson.Timestamp | undefined;
if (snapshotTime == null) {
throw new ServiceAssertionError('Missing snapshotTime in getCheckpoint()');
}
return new MongoReplicationCheckpoint(this, state.checkpoint, state.lsn, snapshotTime);
});
}
protected abstract initializeVersionStorage(): Promise<void>;
private async initializeStorage() {
if (this.#storageInitialized) {
return;
}
await this.db.initializeStreamStorage(this.replicationStreamId);
await this.initializeVersionStorage();
this.#storageInitialized = true;
}
protected abstract createWriterImpl(batchOptions: MongoBucketBatchOptions): storage.BucketStorageBatch;
protected abstract getWriterSyncState(): Promise<WriterSyncState>;
async createWriter(options: storage.CreateWriterOptions): Promise<storage.BucketStorageBatch> {
await this.initializeStorage();
const state = await this.getWriterSyncState();
const parsed = this.replicationStream.parsed(options) as storage.ParsedSyncConfigSet & {
mapping?: BucketDefinitionMapping;
};
const batchOptions: MongoBucketBatchOptions = {
logger: options.logger ?? this.logger,
db: this.db,
syncRules: parsed.hydratedSyncConfig(),
mapping: parsed.mapping ?? this.replicationStream.storageContent.mapping,
replicationStreamId: this.replicationStreamId,
replicationStreamName: this.replicationStreamName,
lastCheckpointLsn: state.lastCheckpointLsn,
resumeFromLsn: state.resumeFromLsn,
keepaliveOp: state.keepaliveOp,
storeCurrentData: options.storeCurrentData,
skipExistingRows: options.skipExistingRows ?? false,
markRecordUnavailable: options.markRecordUnavailable,
hooks: options.hooks,
syncConfigIds: state.syncConfigIds,
tracer: options.tracer
};
const writer = this.createWriterImpl(batchOptions);
this.iterateListeners((cb) => cb.batchStarted?.(writer));
return writer;
}
async startBatch(
options: storage.CreateWriterOptions,
callback: (batch: storage.BucketStorageBatch) => Promise<void>
): Promise<storage.FlushedResult | null> {
await using writer = await this.createWriter(options);
await callback(writer);
await writer.flush();
return writer.last_flushed_op != null ? { flushed_op: writer.last_flushed_op } : null;
}
protected abstract getParameterSetsImpl(
checkpoint: MongoReplicationCheckpoint,
lookups: ScopedParameterLookup[],
limit: number
): Promise<ParameterLookupRows[]>;
async getParameterSets(
checkpoint: MongoReplicationCheckpoint,
lookups: ScopedParameterLookup[],
limit: number
): Promise<ParameterLookupRows[]> {
return this.getParameterSetsImpl(checkpoint, lookups, limit);
}
protected abstract getBucketDataBatchImpl(
checkpoint: utils.InternalOpId,
dataBuckets: storage.BucketDataRequest[],
options?: storage.BucketDataBatchOptions
): AsyncIterable<storage.SyncBucketDataChunk>;
async *getBucketDataBatch(
checkpoint: utils.InternalOpId,
dataBuckets: storage.BucketDataRequest[],
options?: storage.BucketDataBatchOptions
): AsyncIterable<storage.SyncBucketDataChunk> {
yield* this.getBucketDataBatchImpl(checkpoint, dataBuckets, options);
}
async getChecksums(
checkpoint: utils.InternalOpId,
buckets: storage.BucketChecksumRequest[]
): Promise<utils.ChecksumMap> {
return this.checksums.getChecksums(checkpoint, buckets);
}
clearChecksumCache() {
this.checksums.clearCache();
}
protected abstract terminateSyncRuleState(): Promise<void>;
async terminate(options?: storage.TerminateOptions) {
if (!options || options?.clearStorage) {
await this.clear(options);
}
await this.terminateSyncRuleState();
await this.db.notifyCheckpoint();
}
protected abstract getStatusImpl(): Promise<storage.SyncRuleStatus>;
async getStatus(): Promise<storage.SyncRuleStatus> {
return this.getStatusImpl();
}
protected abstract clearBucketData(signal?: AbortSignal): Promise<void>;
protected abstract clearParameterIndexes(signal?: AbortSignal): Promise<void>;
protected abstract clearSourceRecords(signal?: AbortSignal): Promise<void>;
protected abstract clearBucketState(signal?: AbortSignal): Promise<void>;
protected abstract clearSourceTables(signal?: AbortSignal): Promise<void>;
protected abstract clearSyncRuleState(): Promise<void>;
async clear(options?: storage.ClearStorageOptions): Promise<void> {
const signal = options?.signal;
if (signal?.aborted) {
throw new ReplicationAbortedError('Aborted clearing data', signal.reason);
}
await this.clearSyncRuleState();
await this.clearBucketData(signal);
await this.clearParameterIndexes(signal);
await this.clearSourceRecords(signal);
await this.clearBucketState(signal);
await this.clearSourceTables(signal);
this.#storageInitialized = false;
}
protected async clearDeleteMany(
label: string,
operation: () => Promise<mongo.DeleteResult>,
signal?: AbortSignal
): Promise<void> {
await retryOnMongoMaxTimeMSExpired(operation, {
signal,
abortMessage: 'Aborted clearing data',
retryDelayMs: lib_mongo.db.MONGO_CLEAR_OPERATION_TIMEOUT_MS / 5,
onRetry: () => {
this.logger.info(
`Cleared batch of ${label} in ${lib_mongo.db.MONGO_CLEAR_OPERATION_TIMEOUT_MS}ms, continuing...`
);
}
});
}
async reportError(e: any): Promise<void> {
const message = String(e.message ?? 'Replication failure');
await this.db.sync_rules.updateOne(
{
_id: this.replicationStreamId
},
{
$set: {
last_fatal_error: message,
last_fatal_error_ts: new Date()
}
}
);
}
async compact(options?: storage.CompactOptions) {
let maxOpId = options?.maxOpId;
if (maxOpId == null) {
const checkpoint = await this.getCheckpointInternal();
maxOpId = checkpoint?.checkpoint ?? undefined;
}
await this.createMongoCompactor({ ...options, maxOpId, logger: this.logger }).compact();
if (maxOpId != null && options?.compactParameterData) {
await this.createMongoParameterCompactor(maxOpId, options).compact();
}
}
async populatePersistentChecksumCache(options: PopulateChecksumCacheOptions): Promise<PopulateChecksumCacheResults> {
this.logger.info(`Populating persistent checksum cache...`);
const start = Date.now();
const compactor = this.createMongoCompactor({
...options,
memoryLimitMB: 0,
logger: this.logger
});
const result = await compactor.populateChecksums({
minBucketChanges: options.minBucketChanges ?? 10
});
const duration = Date.now() - start;
this.logger.info(`Populated persistent checksum cache in ${(duration / 1000).toFixed(1)}s`);
return result;
}
private async *watchActiveCheckpoint(signal: AbortSignal): AsyncIterable<ReplicationCheckpoint> {
if (signal.aborted) {
return;
}
const stream = mergeAsyncIterables(
[this.checkpointChangesStream(signal), this.checkpointTimeoutStream(signal)],
signal
);
for await (const _ of stream) {
if (signal.aborted) {
break;
}
const op = await this.getCheckpointInternal();
if (op == null) {
break;
}
yield op;
}
}
private readonly sharedIter = new BroadcastIterable((signal) => {
return this.watchActiveCheckpoint(signal);
});
async *watchCheckpointChanges(options: WatchWriteCheckpointOptions): AsyncIterable<storage.StorageCheckpointUpdate> {
let lastCheckpoint: ReplicationCheckpoint | null = null;
const iter = this.sharedIter[Symbol.asyncIterator](options.signal);
let writeCheckpoint: bigint | null = null;
let queriedInitialWriteCheckpoint = false;
for await (const nextCheckpoint of iter) {
if (nextCheckpoint.lsn != null && !queriedInitialWriteCheckpoint) {
writeCheckpoint = await this.writeCheckpointAPI.lastWriteCheckpoint({
sync_rules_id: this.replicationStreamId,
user_id: options.user_id,
heads: {
'1': nextCheckpoint.lsn
}
});
queriedInitialWriteCheckpoint = true;
}
if (
lastCheckpoint != null &&
lastCheckpoint.checkpoint == nextCheckpoint.checkpoint &&
lastCheckpoint.lsn == nextCheckpoint.lsn
) {
await timers.setTimeout(20 + 10 * Math.random());
continue;
}
if (lastCheckpoint == null) {
yield {
base: nextCheckpoint,
writeCheckpoint,
update: CHECKPOINT_INVALIDATE_ALL
};
} else {
const updates = await this.getCheckpointChanges({
lastCheckpoint,
nextCheckpoint
});
let updatedWriteCheckpoint = updates.updatedWriteCheckpoints.get(options.user_id) ?? null;
if (updates.invalidateWriteCheckpoints) {
updatedWriteCheckpoint = await this.writeCheckpointAPI.lastWriteCheckpoint({
sync_rules_id: this.replicationStreamId,
user_id: options.user_id,
heads: {
'1': nextCheckpoint.lsn!
}
});
}
if (updatedWriteCheckpoint != null && (writeCheckpoint == null || updatedWriteCheckpoint > writeCheckpoint)) {
writeCheckpoint = updatedWriteCheckpoint;
queriedInitialWriteCheckpoint = true;
}
yield {
base: nextCheckpoint,
writeCheckpoint,
update: {
updatedDataBuckets: updates.updatedDataBuckets,
invalidateDataBuckets: updates.invalidateDataBuckets,
updatedParameterLookups: updates.updatedParameterLookups,
invalidateParameterBuckets: updates.invalidateParameterBuckets
}
};
}
lastCheckpoint = nextCheckpoint;
}
}
private async *checkpointChangesStream(signal: AbortSignal): AsyncGenerator<void> {
if (signal.aborted) {
return;
}
const query = () => {
return this.db.checkpoint_events.find(
{},
{ tailable: true, awaitData: true, maxAwaitTimeMS: 10_000, batchSize: 1000 }
);
};
let cursor = query();
signal.addEventListener('abort', () => {
cursor.close().catch(() => {});
});
yield;
try {
while (!signal.aborted) {
const doc = await cursor.tryNext().catch((e) => {
if (lib_mongo.isMongoServerError(e) && e.codeName === 'CappedPositionLost') {
cursor = query();
return {};
} else {
return Promise.reject(e);
}
});
if (cursor.closed) {
return;
}
cursor.readBufferedDocuments();
if (doc != null) {
yield;
}
}
} catch (e) {
if (signal.aborted) {
return;
}
throw e;
} finally {
await cursor.close();
}
}
private async *checkpointTimeoutStream(signal: AbortSignal): AsyncGenerator<void> {
while (!signal.aborted) {
try {
await timers.setTimeout(CHECKPOINT_TIMEOUT_MS, undefined, { signal });
} catch (e) {
if (e.name == 'AbortError') {
return;
}
throw e;
}
if (!signal.aborted) {
yield;
}
}
}
protected abstract getDataBucketChangesImpl(
options: GetCheckpointChangesOptions
): Promise<Pick<CheckpointChanges, 'updatedDataBuckets' | 'invalidateDataBuckets'>>;
private async getDataBucketChanges(
options: GetCheckpointChangesOptions
): Promise<Pick<CheckpointChanges, 'updatedDataBuckets' | 'invalidateDataBuckets'>> {
return this.getDataBucketChangesImpl(options);
}
protected abstract getParameterBucketChangesImpl(
options: GetCheckpointChangesOptions
): Promise<Pick<CheckpointChanges, 'updatedParameterLookups' | 'invalidateParameterBuckets'>>;
private async getParameterBucketChanges(
options: GetCheckpointChangesOptions
): Promise<Pick<CheckpointChanges, 'updatedParameterLookups' | 'invalidateParameterBuckets'>> {
return this.getParameterBucketChangesImpl(options);
}
private checkpointChangesCache = new LRUCache<
string,
InternalCheckpointChanges,
{ options: GetCheckpointChangesOptions }
>({
max: 50,
maxSize: 12 * 1024 * 1024,
sizeCalculation: (value: InternalCheckpointChanges) => {
const paramSize = [...value.updatedParameterLookups].reduce<number>((a, b) => a + b.length, 0);
const bucketSize = [...value.updatedDataBuckets].reduce<number>((a, b) => a + b.length, 0);
const writeCheckpointSize = value.updatedWriteCheckpoints.size * 30;
return 100 + paramSize + bucketSize + writeCheckpointSize;
},
fetchMethod: async (_key, _staleValue, options) => {
return this.getCheckpointChangesInternal(options.context.options);
}
});
async getCheckpointChanges(options: GetCheckpointChangesOptions): Promise<InternalCheckpointChanges> {
const key = `${options.lastCheckpoint.checkpoint}_${options.lastCheckpoint.lsn}__${options.nextCheckpoint.checkpoint}_${options.nextCheckpoint.lsn}`;
const result = await this.checkpointChangesCache.fetch(key, { context: { options } });
return result!;
}
private async getCheckpointChangesInternal(options: GetCheckpointChangesOptions): Promise<InternalCheckpointChanges> {
const dataUpdates = await this.getDataBucketChanges(options);
const parameterUpdates = await this.getParameterBucketChanges(options);
const writeCheckpointUpdates = await this.writeCheckpointAPI.getWriteCheckpointChanges(options);
return {
...dataUpdates,
...parameterUpdates,
...writeCheckpointUpdates
};
}
}
class MongoReplicationCheckpoint implements ReplicationCheckpoint {
#storage: MongoSyncBucketStorage;
constructor(
storage: MongoSyncBucketStorage,
public readonly checkpoint: InternalOpId,
public readonly lsn: string | null,
public snapshotTime: mongo.Timestamp
) {
this.#storage = storage;
}
async getParameterSets(lookups: ScopedParameterLookup[], limit: number): Promise<ParameterLookupRows[]> {
return this.#storage.getParameterSets(this, lookups, limit);
}
}
class EmptyReplicationCheckpoint implements ReplicationCheckpoint {
readonly checkpoint: InternalOpId = 0n;
readonly lsn: string | null = null;
async getParameterSets(_lookups: ScopedParameterLookup[], _limit: number): Promise<ParameterLookupRows[]> {
return [];
}
}