@powersync/service-module-mongodb 0.0.0-dev-20261007110800 → 0.0.0-dev-20261015132441
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -11
- package/benchmarks/env.ts +11 -0
- package/benchmarks/replication_branch_throughput.benchmark.ts +240 -0
- package/dist/api/MongoRouteAPIAdapter.d.ts +5 -0
- package/dist/api/MongoRouteAPIAdapter.js +11 -4
- package/dist/api/MongoRouteAPIAdapter.js.map +1 -1
- package/dist/module/MongoModule.js +1 -0
- package/dist/module/MongoModule.js.map +1 -1
- package/dist/replication/ChangeStream.d.ts +2 -1
- package/dist/replication/ChangeStream.js +164 -150
- package/dist/replication/ChangeStream.js.map +1 -1
- package/dist/replication/ChangeStreamReplicationJob.js +17 -9
- package/dist/replication/ChangeStreamReplicationJob.js.map +1 -1
- package/dist/replication/MongoReplicationQueryProvider.d.ts +9 -11
- package/dist/replication/MongoReplicationQueryProvider.js.map +1 -1
- package/dist/replication/MongoReplicationStream.d.ts +37 -48
- package/dist/replication/MongoReplicationStream.js +157 -30
- package/dist/replication/MongoReplicationStream.js.map +1 -1
- package/dist/replication/MongoReplicationStreamTypes.d.ts +157 -0
- package/dist/replication/MongoReplicationStreamTypes.js +19 -0
- package/dist/replication/MongoReplicationStreamTypes.js.map +1 -0
- package/dist/replication/MongoSnapshotter.js +59 -30
- package/dist/replication/MongoSnapshotter.js.map +1 -1
- package/dist/replication/estimateFilteredSnapshotCount.d.ts +12 -0
- package/dist/replication/estimateFilteredSnapshotCount.js +79 -0
- package/dist/replication/estimateFilteredSnapshotCount.js.map +1 -0
- package/dist/replication/replication-index.d.ts +1 -0
- package/dist/replication/replication-index.js +1 -0
- package/dist/replication/replication-index.js.map +1 -1
- package/package.json +11 -10
- package/src/api/MongoRouteAPIAdapter.ts +12 -5
- package/src/module/MongoModule.ts +1 -0
- package/src/replication/ChangeStream.ts +183 -173
- package/src/replication/ChangeStreamReplicationJob.ts +16 -9
- package/src/replication/MongoReplicationQueryProvider.ts +9 -11
- package/src/replication/MongoReplicationStream.ts +196 -83
- package/src/replication/MongoReplicationStreamTypes.ts +169 -0
- package/src/replication/MongoSnapshotter.ts +64 -33
- package/src/replication/estimateFilteredSnapshotCount.ts +93 -0
- package/src/replication/replication-index.ts +1 -0
- package/test/src/change_event_memory.test.ts +33 -0
- package/test/src/change_stream_abort.test.ts +110 -0
- package/test/src/filtering_example.test.ts +65 -0
- package/test/src/filtering_lifecycle.test.ts +101 -0
- package/test/src/query_provider_availability.test.ts +1 -0
- package/test/src/query_provider_diagnostics.test.ts +55 -0
- package/test/src/replication_stream.test.ts +411 -34
- package/test/src/snapshot_count.test.ts +146 -0
- package/test/src/snapshot_count_integration.test.ts +70 -0
- package/test/src/snapshot_query_provider.test.ts +21 -2
- package/test/src/stream_progress.test.ts +62 -44
- package/test/tsconfig.json +2 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/vitest.benchmark.config.ts +15 -0
|
@@ -32,14 +32,15 @@ import {
|
|
|
32
32
|
MongoReplicationQueryProvider,
|
|
33
33
|
MongoReplicationQueryProviderFactory
|
|
34
34
|
} from './MongoReplicationQueryProvider.js';
|
|
35
|
-
import {
|
|
35
|
+
import { openMongoReplicationStream } from './MongoReplicationStream.js';
|
|
36
|
+
import type { MongoReplicationStreamBatch } from './MongoReplicationStreamTypes.js';
|
|
36
37
|
import { MongoSnapshotter, MongoSnapshotterHooks } from './MongoSnapshotter.js';
|
|
37
38
|
import { ChangeStreamBatch, ProjectedChangeStreamDocument } from './RawChangeStream.js';
|
|
38
39
|
import { CHECKPOINTS_COLLECTION, detectDocumentDb, timestampToDate } from './replication-utils.js';
|
|
39
40
|
import { DirectSourceRowConverter, SourceRowConverter } from './SourceRowConverter.js';
|
|
40
41
|
export interface ChangeStreamOptions {
|
|
41
|
-
connections: MongoManager;
|
|
42
42
|
storage: storage.SyncRulesBucketStorage;
|
|
43
|
+
connections: MongoManager;
|
|
43
44
|
metrics: MetricsEngine;
|
|
44
45
|
abort_signal: AbortSignal;
|
|
45
46
|
/**
|
|
@@ -137,7 +138,7 @@ export class ChangeStream {
|
|
|
137
138
|
private lastPersistedResumeTimestamp = 0;
|
|
138
139
|
private lastBatchCheckpoint = 0;
|
|
139
140
|
|
|
140
|
-
constructor(options: ChangeStreamOptions) {
|
|
141
|
+
constructor(private readonly options: ChangeStreamOptions) {
|
|
141
142
|
this.storage = options.storage;
|
|
142
143
|
this.metrics = options.metrics;
|
|
143
144
|
this.group_id = options.storage.replicationStreamId;
|
|
@@ -557,7 +558,7 @@ export class ChangeStream {
|
|
|
557
558
|
signal?: AbortSignal;
|
|
558
559
|
tracer?: PerformanceTracer<'changestream'>;
|
|
559
560
|
onBatch?: (batch: ChangeStreamBatch) => Disposable | void;
|
|
560
|
-
}): AsyncIterableIterator<
|
|
561
|
+
}): AsyncIterableIterator<MongoReplicationStreamBatch> {
|
|
561
562
|
return openMongoReplicationStream({
|
|
562
563
|
db: this.defaultDb,
|
|
563
564
|
queryProvider: this.queryProvider,
|
|
@@ -624,6 +625,7 @@ export class ChangeStream {
|
|
|
624
625
|
let processingSpan: ReturnType<typeof tracer.span> | undefined;
|
|
625
626
|
let receivedBytes = 0;
|
|
626
627
|
let changesSinceProgress = 0;
|
|
628
|
+
let filteredSinceProgress: number | null = null;
|
|
627
629
|
const stream = this.openChangeStream({
|
|
628
630
|
lsn: resumeFromLsn,
|
|
629
631
|
filters,
|
|
@@ -647,13 +649,180 @@ export class ChangeStream {
|
|
|
647
649
|
let lastKeepalive = performance.now();
|
|
648
650
|
let lastTxnKey: string | null = null;
|
|
649
651
|
|
|
650
|
-
for await (const
|
|
652
|
+
streamLoop: for await (const sourceBatch of stream) {
|
|
653
|
+
if (this.abortSignal.aborted) break;
|
|
654
|
+
for (const { event: changeDocument, hasBufferedChanges } of sourceBatch.events) {
|
|
655
|
+
if (this.abortSignal.aborted) break streamLoop;
|
|
656
|
+
this.touch();
|
|
657
|
+
changesSinceProgress++;
|
|
658
|
+
const ns = 'ns' in changeDocument && 'coll' in changeDocument.ns ? changeDocument.ns : undefined;
|
|
659
|
+
|
|
660
|
+
if (ns?.coll == CHECKPOINTS_COLLECTION) {
|
|
661
|
+
/**
|
|
662
|
+
* Dropping the database does not provide an `invalidate` event.
|
|
663
|
+
* We typically would receive `drop` events for the collection which we
|
|
664
|
+
* would process below.
|
|
665
|
+
*
|
|
666
|
+
* However we don't commit the LSN after collections are dropped.
|
|
667
|
+
* This prevents the `startAfter` or `resumeToken` from advancing past the drop events.
|
|
668
|
+
* The stream also closes after the drop events.
|
|
669
|
+
* This causes an infinite loop of processing the collection drop events.
|
|
670
|
+
*
|
|
671
|
+
* This check here invalidates the change stream if our `_powersync_checkpoints` collection
|
|
672
|
+
* is dropped. This allows for detecting when the DB is dropped.
|
|
673
|
+
*/
|
|
674
|
+
if (changeDocument.operationType == 'drop') {
|
|
675
|
+
throw new ChangeStreamInvalidatedError(
|
|
676
|
+
'Internal collections have been dropped',
|
|
677
|
+
new Error('_powersync_checkpoints collection was dropped')
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
if (
|
|
682
|
+
!(
|
|
683
|
+
changeDocument.operationType == 'insert' ||
|
|
684
|
+
changeDocument.operationType == 'update' ||
|
|
685
|
+
changeDocument.operationType == 'replace'
|
|
686
|
+
)
|
|
687
|
+
) {
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// We handle two types of checkpoint events:
|
|
692
|
+
// 1. "Standalone" checkpoints, typically write checkpoints. We want to process these
|
|
693
|
+
// immediately, regardless of where they were created.
|
|
694
|
+
// 2. "Batch" checkpoints for the current stream. This is used as a form of dynamic rate
|
|
695
|
+
// limiting of commits, so we specifically want to exclude checkpoints from other streams.
|
|
696
|
+
//
|
|
697
|
+
// It may be useful to also throttle commits due to standalone checkpoints in the future.
|
|
698
|
+
// However, these typically have a much lower rate than batch checkpoints, so we don't do that for now.
|
|
699
|
+
|
|
700
|
+
const kind = this.checkpointImplementation.event.observe(changeDocument);
|
|
701
|
+
|
|
702
|
+
if (kind == 'foreign') {
|
|
703
|
+
// Another stream's barrier - ignore.
|
|
704
|
+
continue;
|
|
705
|
+
} else if (kind == 'standalone') {
|
|
706
|
+
// Standalone / write checkpoint received.
|
|
707
|
+
// When we are caught up, commit immediately to keep write checkpoint latency low.
|
|
708
|
+
// Once there is already a batch checkpoint pending, or the driver has buffered more
|
|
709
|
+
// change stream events, collapse standalone checkpoints into the normal batch
|
|
710
|
+
// checkpoint flow to avoid commit churn under sustained load.
|
|
711
|
+
if (hasBufferedChanges && waitForCheckpointLsn == null) {
|
|
712
|
+
// Buffered changes - create a new batch checkpoint to rate limit commits
|
|
713
|
+
using _ = tracer.span('source_checkpoint');
|
|
714
|
+
waitForCheckpointLsn = await this.createBatchCheckpoint();
|
|
715
|
+
continue;
|
|
716
|
+
} else if (waitForCheckpointLsn != null) {
|
|
717
|
+
// Skip this checkpoint - wait for the batch checkpoint.
|
|
718
|
+
continue;
|
|
719
|
+
} else {
|
|
720
|
+
// No buffered changes, and no batch checkpoint pending - commit immediately.
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
// kind == 'own-barrier' falls through to commit.
|
|
724
|
+
|
|
725
|
+
const lsn = this.checkpointImplementation.event.lsn(changeDocument);
|
|
726
|
+
|
|
727
|
+
if (
|
|
728
|
+
waitForCheckpointLsn != null &&
|
|
729
|
+
this.checkpointImplementation.event.resolvesBarrier(waitForCheckpointLsn, changeDocument)
|
|
730
|
+
) {
|
|
731
|
+
waitForCheckpointLsn = null;
|
|
732
|
+
}
|
|
733
|
+
const { checkpointBlocked, checkpointCreated } = await batch.commit(lsn, {
|
|
734
|
+
oldestUncommittedChange: this.replicationLag.oldestUncommittedChange
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
if (!checkpointBlocked || checkpointCreated) {
|
|
738
|
+
this.replicationLag.markCommitted();
|
|
739
|
+
}
|
|
740
|
+
} else if (
|
|
741
|
+
changeDocument.operationType == 'insert' ||
|
|
742
|
+
changeDocument.operationType == 'update' ||
|
|
743
|
+
changeDocument.operationType == 'replace' ||
|
|
744
|
+
changeDocument.operationType == 'delete'
|
|
745
|
+
) {
|
|
746
|
+
if (waitForCheckpointLsn == null) {
|
|
747
|
+
using _ = tracer.span('source_checkpoint');
|
|
748
|
+
waitForCheckpointLsn = await this.createBatchCheckpoint();
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
const rel = getMongoRelation(changeDocument.ns, this.connections.connectionTag);
|
|
752
|
+
const tables = await this.getRelations(batch, rel, {
|
|
753
|
+
// In most cases, we should not need to snapshot this. But if this is the first time we see the collection
|
|
754
|
+
// for whatever reason, then we do need to snapshot it.
|
|
755
|
+
// This may result in some duplicate operations when a collection is created for the first time after
|
|
756
|
+
// sync config was deployed.
|
|
757
|
+
snapshot: true
|
|
758
|
+
});
|
|
759
|
+
const tablesToReplicate = tables.filter((table) => table.syncAny);
|
|
760
|
+
if (tablesToReplicate.length > 0) {
|
|
761
|
+
this.replicationLag.trackUncommittedChange(
|
|
762
|
+
// Standard MongoDB uses clusterTime, unchanged. DocumentDB has no
|
|
763
|
+
// clusterTime, so fall back to wallTime there for the lag metric.
|
|
764
|
+
changeDocument.clusterTime != null
|
|
765
|
+
? timestampToDate(changeDocument.clusterTime)
|
|
766
|
+
: ((changeDocument as any).wallTime ?? null)
|
|
767
|
+
);
|
|
768
|
+
|
|
769
|
+
const transactionKeyValue = transactionKey(changeDocument);
|
|
770
|
+
|
|
771
|
+
if (transactionKeyValue == null || lastTxnKey != transactionKeyValue) {
|
|
772
|
+
// Very crude metric for counting transactions replicated.
|
|
773
|
+
// We ignore operations other than basic CRUD, and ignore changes to _powersync_checkpoints.
|
|
774
|
+
// Individual writes may not have a txnNumber, in which case we count them as separate transactions.
|
|
775
|
+
lastTxnKey = transactionKeyValue;
|
|
776
|
+
transactionsReplicatedMetric.add(1);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
for (const table of tablesToReplicate) {
|
|
780
|
+
await this.writeChange(batch, table, changeDocument);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
} else if (changeDocument.operationType == 'drop') {
|
|
784
|
+
const rel = getMongoRelation(changeDocument.ns, this.connections.connectionTag);
|
|
785
|
+
const tables = await this.getRelations(batch, rel, {
|
|
786
|
+
// We're "dropping" this collection, so never snapshot it.
|
|
787
|
+
snapshot: false
|
|
788
|
+
});
|
|
789
|
+
const tablesToDrop = tables.filter((table) => table.syncAny);
|
|
790
|
+
if (tablesToDrop.length > 0) {
|
|
791
|
+
await batch.drop(tablesToDrop);
|
|
792
|
+
}
|
|
793
|
+
this.relationCache.delete(rel);
|
|
794
|
+
} else if (changeDocument.operationType == 'rename') {
|
|
795
|
+
const relFrom = getMongoRelation(changeDocument.ns, this.connections.connectionTag);
|
|
796
|
+
const relTo = getMongoRelation(changeDocument.to, this.connections.connectionTag);
|
|
797
|
+
const tablesFrom = await this.getRelations(batch, relFrom, {
|
|
798
|
+
// We're "dropping" this collection, so never snapshot it.
|
|
799
|
+
snapshot: false
|
|
800
|
+
});
|
|
801
|
+
const tablesToDrop = tablesFrom.filter((table) => table.syncAny);
|
|
802
|
+
if (tablesToDrop.length > 0) {
|
|
803
|
+
await batch.drop(tablesToDrop);
|
|
804
|
+
}
|
|
805
|
+
this.relationCache.delete(relFrom);
|
|
806
|
+
// Here we do need to snapshot the new table
|
|
807
|
+
const collection = await this.getCollectionInfo(relTo.schema, relTo.name);
|
|
808
|
+
await this.handleRelation(batch, relTo, {
|
|
809
|
+
// This is a new (renamed) collection, so always snapshot it.
|
|
810
|
+
snapshot: true,
|
|
811
|
+
collectionInfo: collection
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
}
|
|
651
815
|
if (this.abortSignal.aborted) break;
|
|
652
816
|
this.touch();
|
|
653
|
-
|
|
654
|
-
|
|
817
|
+
const batchFilteredCount = sourceBatch.metadata.filteredCount;
|
|
818
|
+
if (batchFilteredCount != null) {
|
|
819
|
+
filteredSinceProgress = (filteredSinceProgress ?? 0) + batchFilteredCount;
|
|
820
|
+
}
|
|
821
|
+
const resumeToken = sourceBatch.getResumeToken();
|
|
822
|
+
if (resumeToken != null) {
|
|
823
|
+
const filteredCount = filteredSinceProgress;
|
|
655
824
|
if (changesSinceProgress == 0) {
|
|
656
|
-
// No retained changes since the previous progress
|
|
825
|
+
// No retained changes since the previous progress boundary: this is either idle or filtered-only traffic.
|
|
657
826
|
// Case 1: No pending barrier and the keepalive interval has elapsed. Advance checkpoints as below,
|
|
658
827
|
// then flush and save the resume token.
|
|
659
828
|
// Case 2: Idle traffic with a recent keepalive. Skip this resume update, whether or not a barrier
|
|
@@ -663,7 +832,7 @@ export class ChangeStream {
|
|
|
663
832
|
// progress still saves its token even during the keepalive interval, so excluded work is resumable.
|
|
664
833
|
const hadRecentKeepalive = performance.now() - lastKeepalive < this.keepaliveIntervalMs;
|
|
665
834
|
if (waitForCheckpointLsn == null && !hadRecentKeepalive) {
|
|
666
|
-
if (filteredCount > 0) {
|
|
835
|
+
if ((filteredCount ?? 0) > 0) {
|
|
667
836
|
// Case 1a: A transaction can span batches: this batch may contain only filtered changes,
|
|
668
837
|
// while retained changes from the same transaction are still unread. They share a timestamp,
|
|
669
838
|
// so publishing a checkpoint here could acknowledge the transaction before its data is saved.
|
|
@@ -679,7 +848,7 @@ export class ChangeStream {
|
|
|
679
848
|
}
|
|
680
849
|
this.touch();
|
|
681
850
|
lastKeepalive = performance.now();
|
|
682
|
-
} else if (filteredCount == 0 && hadRecentKeepalive) {
|
|
851
|
+
} else if ((filteredCount ?? 0) == 0 && hadRecentKeepalive) {
|
|
683
852
|
// Case 2: Only idle progress skips resume persistence. Cases 1 and 3 fall through below.
|
|
684
853
|
continue;
|
|
685
854
|
}
|
|
@@ -694,179 +863,20 @@ export class ChangeStream {
|
|
|
694
863
|
this.lastPersistedResumeTimestamp = timestamp?.getTime() ?? Date.now();
|
|
695
864
|
processingSpan?.end();
|
|
696
865
|
const durationsMicroseconds = outerSpan.end();
|
|
697
|
-
|
|
866
|
+
const filteringDescription = filteredCount == null ? '' : ` and ${filteredCount} filtered events`;
|
|
867
|
+
this.logger.info(`Processed ${changesSinceProgress} changes${filteringDescription}`, {
|
|
698
868
|
count: changesSinceProgress,
|
|
699
|
-
filteredCount,
|
|
869
|
+
...(filteredCount == null ? {} : { filteredCount }),
|
|
700
870
|
bytes: receivedBytes,
|
|
701
871
|
duration: processingSpan?.durationMillis,
|
|
702
872
|
t: durationsMicroseconds
|
|
703
873
|
});
|
|
704
874
|
changesSinceProgress = 0;
|
|
875
|
+
filteredSinceProgress = null;
|
|
705
876
|
receivedBytes = 0;
|
|
706
877
|
outerSpan = tracer.span('batch');
|
|
707
878
|
continue;
|
|
708
879
|
}
|
|
709
|
-
|
|
710
|
-
// The shared reader has already reassembled complete events and normalized their namespaces.
|
|
711
|
-
const changeDocument = item.event;
|
|
712
|
-
changesSinceProgress++;
|
|
713
|
-
const ns = 'ns' in changeDocument && 'coll' in changeDocument.ns ? changeDocument.ns : undefined;
|
|
714
|
-
|
|
715
|
-
if (ns?.coll == CHECKPOINTS_COLLECTION) {
|
|
716
|
-
/**
|
|
717
|
-
* Dropping the database does not provide an `invalidate` event.
|
|
718
|
-
* We typically would receive `drop` events for the collection which we
|
|
719
|
-
* would process below.
|
|
720
|
-
*
|
|
721
|
-
* However we don't commit the LSN after collections are dropped.
|
|
722
|
-
* This prevents the `startAfter` or `resumeToken` from advancing past the drop events.
|
|
723
|
-
* The stream also closes after the drop events.
|
|
724
|
-
* This causes an infinite loop of processing the collection drop events.
|
|
725
|
-
*
|
|
726
|
-
* This check here invalidates the change stream if our `_powersync_checkpoints` collection
|
|
727
|
-
* is dropped. This allows for detecting when the DB is dropped.
|
|
728
|
-
*/
|
|
729
|
-
if (changeDocument.operationType == 'drop') {
|
|
730
|
-
throw new ChangeStreamInvalidatedError(
|
|
731
|
-
'Internal collections have been dropped',
|
|
732
|
-
new Error('_powersync_checkpoints collection was dropped')
|
|
733
|
-
);
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
if (
|
|
737
|
-
!(
|
|
738
|
-
changeDocument.operationType == 'insert' ||
|
|
739
|
-
changeDocument.operationType == 'update' ||
|
|
740
|
-
changeDocument.operationType == 'replace'
|
|
741
|
-
)
|
|
742
|
-
) {
|
|
743
|
-
continue;
|
|
744
|
-
}
|
|
745
|
-
|
|
746
|
-
// We handle two types of checkpoint events:
|
|
747
|
-
// 1. "Standalone" checkpoints, typically write checkpoints. We want to process these
|
|
748
|
-
// immediately, regardless of where they were created.
|
|
749
|
-
// 2. "Batch" checkpoints for the current stream. This is used as a form of dynamic rate
|
|
750
|
-
// limiting of commits, so we specifically want to exclude checkpoints from other streams.
|
|
751
|
-
//
|
|
752
|
-
// It may be useful to also throttle commits due to standalone checkpoints in the future.
|
|
753
|
-
// However, these typically have a much lower rate than batch checkpoints, so we don't do that for now.
|
|
754
|
-
|
|
755
|
-
const kind = this.checkpointImplementation.event.observe(changeDocument);
|
|
756
|
-
|
|
757
|
-
if (kind == 'foreign') {
|
|
758
|
-
// Another stream's barrier - ignore.
|
|
759
|
-
continue;
|
|
760
|
-
} else if (kind == 'standalone') {
|
|
761
|
-
// Standalone / write checkpoint received.
|
|
762
|
-
// When we are caught up, commit immediately to keep write checkpoint latency low.
|
|
763
|
-
// Once there is already a batch checkpoint pending, or the driver has buffered more
|
|
764
|
-
// change stream events, collapse standalone checkpoints into the normal batch
|
|
765
|
-
// checkpoint flow to avoid commit churn under sustained load.
|
|
766
|
-
const hasBufferedChanges = item.hasBufferedChanges;
|
|
767
|
-
if (hasBufferedChanges && waitForCheckpointLsn == null) {
|
|
768
|
-
// Buffered changes - create a new batch checkpoint to rate limit commits
|
|
769
|
-
using _ = tracer.span('source_checkpoint');
|
|
770
|
-
waitForCheckpointLsn = await this.createBatchCheckpoint();
|
|
771
|
-
continue;
|
|
772
|
-
} else if (waitForCheckpointLsn != null) {
|
|
773
|
-
// Skip this checkpoint - wait for the batch checkpoint.
|
|
774
|
-
continue;
|
|
775
|
-
} else {
|
|
776
|
-
// No buffered changes, and no batch checkpoint pending - commit immediately.
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
// kind == 'own-barrier' falls through to commit.
|
|
780
|
-
|
|
781
|
-
const lsn = this.checkpointImplementation.event.lsn(changeDocument);
|
|
782
|
-
|
|
783
|
-
if (
|
|
784
|
-
waitForCheckpointLsn != null &&
|
|
785
|
-
this.checkpointImplementation.event.resolvesBarrier(waitForCheckpointLsn, changeDocument)
|
|
786
|
-
) {
|
|
787
|
-
waitForCheckpointLsn = null;
|
|
788
|
-
}
|
|
789
|
-
const { checkpointBlocked, checkpointCreated } = await batch.commit(lsn, {
|
|
790
|
-
oldestUncommittedChange: this.replicationLag.oldestUncommittedChange
|
|
791
|
-
});
|
|
792
|
-
|
|
793
|
-
if (!checkpointBlocked || checkpointCreated) {
|
|
794
|
-
this.replicationLag.markCommitted();
|
|
795
|
-
}
|
|
796
|
-
} else if (
|
|
797
|
-
changeDocument.operationType == 'insert' ||
|
|
798
|
-
changeDocument.operationType == 'update' ||
|
|
799
|
-
changeDocument.operationType == 'replace' ||
|
|
800
|
-
changeDocument.operationType == 'delete'
|
|
801
|
-
) {
|
|
802
|
-
if (waitForCheckpointLsn == null) {
|
|
803
|
-
using _ = tracer.span('source_checkpoint');
|
|
804
|
-
waitForCheckpointLsn = await this.createBatchCheckpoint();
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
const rel = getMongoRelation(changeDocument.ns, this.connections.connectionTag);
|
|
808
|
-
const tables = await this.getRelations(batch, rel, {
|
|
809
|
-
// In most cases, we should not need to snapshot this. But if this is the first time we see the collection
|
|
810
|
-
// for whatever reason, then we do need to snapshot it.
|
|
811
|
-
// This may result in some duplicate operations when a collection is created for the first time after
|
|
812
|
-
// sync config was deployed.
|
|
813
|
-
snapshot: true
|
|
814
|
-
});
|
|
815
|
-
const tablesToReplicate = tables.filter((table) => table.syncAny);
|
|
816
|
-
if (tablesToReplicate.length > 0) {
|
|
817
|
-
this.replicationLag.trackUncommittedChange(
|
|
818
|
-
// Standard MongoDB uses clusterTime, unchanged. DocumentDB has no
|
|
819
|
-
// clusterTime, so fall back to wallTime there for the lag metric.
|
|
820
|
-
changeDocument.clusterTime != null
|
|
821
|
-
? timestampToDate(changeDocument.clusterTime)
|
|
822
|
-
: ((changeDocument as any).wallTime ?? null)
|
|
823
|
-
);
|
|
824
|
-
|
|
825
|
-
const transactionKeyValue = transactionKey(changeDocument);
|
|
826
|
-
|
|
827
|
-
if (transactionKeyValue == null || lastTxnKey != transactionKeyValue) {
|
|
828
|
-
// Very crude metric for counting transactions replicated.
|
|
829
|
-
// We ignore operations other than basic CRUD, and ignore changes to _powersync_checkpoints.
|
|
830
|
-
// Individual writes may not have a txnNumber, in which case we count them as separate transactions.
|
|
831
|
-
lastTxnKey = transactionKeyValue;
|
|
832
|
-
transactionsReplicatedMetric.add(1);
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
for (const table of tablesToReplicate) {
|
|
836
|
-
await this.writeChange(batch, table, changeDocument);
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
} else if (changeDocument.operationType == 'drop') {
|
|
840
|
-
const rel = getMongoRelation(changeDocument.ns, this.connections.connectionTag);
|
|
841
|
-
const tables = await this.getRelations(batch, rel, {
|
|
842
|
-
// We're "dropping" this collection, so never snapshot it.
|
|
843
|
-
snapshot: false
|
|
844
|
-
});
|
|
845
|
-
const tablesToDrop = tables.filter((table) => table.syncAny);
|
|
846
|
-
if (tablesToDrop.length > 0) {
|
|
847
|
-
await batch.drop(tablesToDrop);
|
|
848
|
-
}
|
|
849
|
-
this.relationCache.delete(rel);
|
|
850
|
-
} else if (changeDocument.operationType == 'rename') {
|
|
851
|
-
const relFrom = getMongoRelation(changeDocument.ns, this.connections.connectionTag);
|
|
852
|
-
const relTo = getMongoRelation(changeDocument.to, this.connections.connectionTag);
|
|
853
|
-
const tablesFrom = await this.getRelations(batch, relFrom, {
|
|
854
|
-
// We're "dropping" this collection, so never snapshot it.
|
|
855
|
-
snapshot: false
|
|
856
|
-
});
|
|
857
|
-
const tablesToDrop = tablesFrom.filter((table) => table.syncAny);
|
|
858
|
-
if (tablesToDrop.length > 0) {
|
|
859
|
-
await batch.drop(tablesToDrop);
|
|
860
|
-
}
|
|
861
|
-
this.relationCache.delete(relFrom);
|
|
862
|
-
// Here we do need to snapshot the new table
|
|
863
|
-
const collection = await this.getCollectionInfo(relTo.schema, relTo.name);
|
|
864
|
-
await this.handleRelation(batch, relTo, {
|
|
865
|
-
// This is a new (renamed) collection, so always snapshot it.
|
|
866
|
-
snapshot: true,
|
|
867
|
-
collectionInfo: collection
|
|
868
|
-
});
|
|
869
|
-
}
|
|
870
880
|
}
|
|
871
881
|
}
|
|
872
882
|
);
|
|
@@ -70,15 +70,22 @@ export class ChangeStreamReplicationJob extends replication.AbstractReplicationJ
|
|
|
70
70
|
if (this.isStopped) {
|
|
71
71
|
return;
|
|
72
72
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
73
|
+
let stream: ChangeStream;
|
|
74
|
+
try {
|
|
75
|
+
stream = new ChangeStream({
|
|
76
|
+
abort_signal: this.abortController.signal,
|
|
77
|
+
storage: this.options.storage,
|
|
78
|
+
metrics: this.options.metrics,
|
|
79
|
+
connections: connectionManager,
|
|
80
|
+
keepaliveIntervalMs: connectionManager.options.heartbeat_interval_seconds * 1_000,
|
|
81
|
+
logger: this.logger,
|
|
82
|
+
createReplicationQueryProvider: this.createReplicationQueryProvider
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
// Construction can fail before ChangeStream.replicate() reports errors.
|
|
86
|
+
await this.storage.reportError(error);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
82
89
|
this.lastStream = stream;
|
|
83
90
|
await stream.replicate();
|
|
84
91
|
} finally {
|
|
@@ -2,7 +2,7 @@ import { mongo } from '@powersync/lib-service-mongodb';
|
|
|
2
2
|
import { SourceTable } from '@powersync/service-core';
|
|
3
3
|
import { HydratedSyncConfig } from '@powersync/service-sync-rules';
|
|
4
4
|
import { MongoManager } from './MongoManager.js';
|
|
5
|
-
import {
|
|
5
|
+
import type { MongoReplicationStreamBatch, MongoReplicationStreamContext } from './MongoReplicationStreamTypes.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Sync config and source namespace information used to construct a provider for a replication attempt.
|
|
@@ -35,17 +35,15 @@ export interface MongoReplicationQueryProvider {
|
|
|
35
35
|
validateSource?(options: { connectionManager: MongoManager; isDocumentDb: boolean }): Promise<void>;
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Call context.open() with any additional stages/pre-image options
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* Each call opens a separate iterator, also used for snapshot barriers and resume validation. Keep
|
|
46
|
-
* per-iterator state separate and close the underlying iterator when the wrapper exits.
|
|
38
|
+
* Call context.open() with any additional stages/pre-image options. For filtering, pass its result
|
|
39
|
+
* to filterMongoReplicationStream({ batches, filter }) with a MongoReplicationFilter callback.
|
|
40
|
+
* The helper preserves checkpoints, DDL, batch boundaries, buffered-change flags and safe progress,
|
|
41
|
+
* counts excluded events, and closes the source iterator on failure or early exit.
|
|
42
|
+
* The default provider returns the opener's batches directly.
|
|
43
|
+
* Each invocation is independent and also serves snapshot barriers and resume validation; keep
|
|
44
|
+
* filter state local to that invocation. Snapshot selection must agree with streaming decisions.
|
|
47
45
|
*/
|
|
48
|
-
openChangeStream(context: MongoReplicationStreamContext): AsyncIterableIterator<
|
|
46
|
+
openChangeStream(context: MongoReplicationStreamContext): AsyncIterableIterator<MongoReplicationStreamBatch>;
|
|
49
47
|
|
|
50
48
|
/**
|
|
51
49
|
* Returns an additional MongoDB find predicate for a physical source table.
|