Skip to content

Commit 0cdee24

Browse files
committed
ft: ZENKO-2968 enable sharding on bucket creation
1 parent d27c057 commit 0cdee24

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

lib/storage/metadata/MetadataWrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class MetadataWrapper {
8585
replicaSetHosts: params.mongodb.replicaSetHosts,
8686
writeConcern: params.mongodb.writeConcern,
8787
replicaSet: params.mongodb.replicaSet,
88+
enableSharding: params.mongodb.enableSharding,
8889
readPreference: params.mongodb.readPreference,
8990
database: params.mongodb.database,
9091
replicationGroupId: params.replicationGroupId,

lib/storage/metadata/mongoclient/LogConsumer.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ class LogConsumer {
1717
* @param {string} logger - logger
1818
*/
1919
constructor(mongoConfig, logger) {
20-
const { authCredentials, replicaSetHosts, replicaSet, database } = mongoConfig;
20+
const { authCredentials, replicaSetHosts, replicaSet, database,
21+
enableSharding } = mongoConfig;
2122
const cred = MongoUtils.credPrefix(authCredentials);
2223
this._mongoUrl = `mongodb://${cred}${replicaSetHosts}/`;
2324
this._replicaSet = replicaSet;
25+
this._enableSharding =
26+
enableSharding !== undefined ? enableSharding : false;
2427
this._logger = logger;
2528
this._oplogNsRegExp = new RegExp(`^${database}\\.`);
2629
// oplog collection
@@ -36,10 +39,15 @@ class LogConsumer {
3639
* @return {undefined}
3740
*/
3841
connectMongo(done) {
39-
MongoClient.connect(this._mongoUrl, {
40-
replicaSet: this._replicaSet,
42+
const options = {
4143
useNewUrlParser: true,
42-
},
44+
};
45+
if (!this._enableSharding) {
46+
// XXX real fix is with change streams
47+
options.replicaSet = this._replicaSet;
48+
}
49+
MongoClient.connect(this._mongoUrl,
50+
options,
4351
(err, client) => {
4452
if (err) {
4553
this._logger.error('Unable to connect to MongoDB',

lib/storage/metadata/mongoclient/MongoClientInterface.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ function generatePHDVersion(versionId) {
9191
class MongoClientInterface {
9292
constructor(params) {
9393
const { replicaSetHosts, writeConcern, replicaSet, readPreference, path,
94-
database, logger, replicationGroupId, authCredentials,
94+
database, enableSharding, logger,
95+
replicationGroupId, authCredentials,
9596
isLocationTransient } = params;
9697
const cred = MongoUtils.credPrefix(authCredentials);
98+
this._enableSharding =
99+
enableSharding !== undefined ? enableSharding : false;
97100
this.mongoUrl = `mongodb://${cred}${replicaSetHosts}/` +
98-
`?w=${writeConcern}&replicaSet=${replicaSet}` +
101+
`?w=${writeConcern}` +
102+
`${!this._enableSharding && !!replicaSet ? `&replicaSet=${replicaSet}` : ''}` +
99103
`&readPreference=${readPreference}`;
100104
this.logger = logger;
101105
this.client = null;
@@ -142,6 +146,7 @@ class MongoClientInterface {
142146
this.db = client.db(this.database, {
143147
ignoreUndefined: true,
144148
});
149+
this.adminDb = client.db('admin');
145150
return this.usersBucketHack(cb);
146151
});
147152
}
@@ -219,6 +224,21 @@ class MongoClientInterface {
219224
{ error: err.message });
220225
return cb(errors.InternalError);
221226
}
227+
if (this._enableSharding) {
228+
const cmd = {
229+
shardCollection: `${this.database}.${bucketName}`,
230+
key: { _id: 1 },
231+
};
232+
return this.adminDb.command(cmd, {}, err => {
233+
if (err) {
234+
log.error(
235+
'createBucket: enabling sharding',
236+
{ error: err.message });
237+
return cb(errors.InternalError);
238+
}
239+
return cb();
240+
});
241+
}
222242
return cb();
223243
});
224244
}

0 commit comments

Comments
 (0)