Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB Sharded Cluster Support #213

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/weak-lions-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@powersync/service-module-mongodb': minor
'@powersync/service-core': minor
'@powersync/service-image': minor
---

Added support for sharded MongoDB replication connections.
18 changes: 10 additions & 8 deletions modules/module-mongodb/src/replication/ChangeStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,23 @@ export class ChangeStream {

// We need to get the snapshot time before taking the initial snapshot.
const hello = await this.defaultDb.command({ hello: 1 });
const snapshotTime = hello.lastWrite?.majorityOpTime?.ts as mongo.Timestamp;
if (hello.msg == 'isdbgrid') {
throw new ServiceError(
ErrorCode.PSYNC_S1341,
'Sharded MongoDB Clusters are not supported yet (including MongoDB Serverless instances).'
);
} else if (hello.setName == null) {
// Use the clusterTime for sharded clusters
const snapshotTime: mongo.Timestamp = hello.lastWrite?.majorityOpTime?.ts ?? hello.$clusterTime?.clusterTime;

// Sharded cluster don't provide a setName, but we do support them.
// We don't support standalone instances
if (hello.msg != 'isdbgrid' && hello.setName == null) {
throw new ServiceError(
ErrorCode.PSYNC_S1342,
'Standalone MongoDB instances are not supported - use a replicaset.'
);
} else if (snapshotTime == null) {
}

if (snapshotTime == null) {
// Not known where this would happen apart from the above cases
throw new ReplicationAssertionError('MongoDB lastWrite timestamp not found.');
}

// We previously used {snapshot: true} for the snapshot session.
// While it gives nice consistency guarantees, it fails when the
// snapshot takes longer than 5 minutes, due to minSnapshotHistoryWindowInSeconds
Expand Down
11 changes: 4 additions & 7 deletions modules/module-mongodb/src/replication/replication-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCode, ServiceError } from '@powersync/lib-services-framework';
import { MongoManager } from './MongoManager.js';
import { PostImagesOption } from '../types/types.js';
import { MongoManager } from './MongoManager.js';

export const CHECKPOINTS_COLLECTION = '_powersync_checkpoints';

Expand All @@ -10,12 +10,9 @@ export async function checkSourceConfiguration(connectionManager: MongoManager):
const db = connectionManager.db;

const hello = await db.command({ hello: 1 });
if (hello.msg == 'isdbgrid') {
throw new ServiceError(
ErrorCode.PSYNC_S1341,
'Sharded MongoDB Clusters are not supported yet (including MongoDB Serverless instances).'
);
} else if (hello.setName == null) {
// Sharded cluster don't provide a setName, but we do support them.
// We don't support standalone instances
if (hello.msg != 'isdbgrid' && hello.setName == null) {
throw new ServiceError(ErrorCode.PSYNC_S1342, 'Standalone MongoDB instances are not supported - use a replicaset.');
}

Expand Down