Skip to content

Commit 77f9719

Browse files
feature: support mongodb collection sharding
1 parent ed1d6c1 commit 77f9719

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

lib/storage/metadata/MetadataWrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class MetadataWrapper {
9090
replicationGroupId: params.replicationGroupId,
9191
path: params.mongodb.path,
9292
authCredentials: params.mongodb.authCredentials,
93+
shardCollections: params.mongodb.shardCollections,
9394
config: params.config,
9495
logger,
9596
});

lib/storage/metadata/mongoclient/MongoClientInterface.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,23 @@ class MongoClientInterface {
9292
constructor(params) {
9393
const { replicaSetHosts, writeConcern, replicaSet, readPreference, path,
9494
database, logger, replicationGroupId, authCredentials,
95-
isLocationTransient } = params;
95+
isLocationTransient, shardCollections } = params;
9696
const cred = MongoUtils.credPrefix(authCredentials);
9797
this.mongoUrl = `mongodb://${cred}${replicaSetHosts}/` +
98-
`?w=${writeConcern}&replicaSet=${replicaSet}` +
99-
`&readPreference=${readPreference}`;
98+
`?w=${writeConcern}&readPreference=${readPreference}`;
99+
100+
if (!shardCollections) {
101+
this.mongoUrl += `&replicaSet=${replicaSet}`;
102+
}
103+
100104
this.logger = logger;
101105
this.client = null;
102106
this.db = null;
103107
this.path = path;
104108
this.replicationGroupId = replicationGroupId;
105109
this.database = database;
106110
this.isLocationTransient = isLocationTransient;
111+
this.shardCollections = shardCollections;
107112

108113
this.concurrentCursors = (process.env.CONCURRENT_CURSORS &&
109114
!Number.isNaN(process.env.CONCURRENT_CURSORS))
@@ -142,6 +147,7 @@ class MongoClientInterface {
142147
this.db = client.db(this.database, {
143148
ignoreUndefined: true,
144149
});
150+
this.adminDb = client.db('admin');
145151
return this.usersBucketHack(cb);
146152
});
147153
}
@@ -216,9 +222,24 @@ class MongoClientInterface {
216222
if (err) {
217223
log.error(
218224
'createBucket: error creating bucket',
219-
{ error: err.message });
225+
{ error: err });
220226
return cb(errors.InternalError);
221227
}
228+
if (this.shardCollections) {
229+
const cmd = {
230+
shardCollection: `${this.database}.${bucketName}`,
231+
key: { _id: 1 },
232+
};
233+
return this.adminDb.command(cmd, {}, err => {
234+
if (err) {
235+
log.error(
236+
'createBucket: enabling sharding',
237+
{ error: err });
238+
return cb(errors.InternalError);
239+
}
240+
return cb();
241+
});
242+
}
222243
return cb();
223244
});
224245
}
@@ -1706,4 +1727,3 @@ class MongoClientInterface {
17061727
}
17071728

17081729
module.exports = MongoClientInterface;
1709-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"engines": {
44
"node": ">=8"
55
},
6-
"version": "8.1.10",
6+
"version": "8.1.11",
77
"description": "Common utilities for the S3 project components",
88
"main": "index.js",
99
"repository": {

0 commit comments

Comments
 (0)