Skip to content

ft: ZENKO-2968 enable sharding on bucket creation #1303

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

Open
wants to merge 1 commit into
base: development/8.1
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/storage/metadata/MetadataWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class MetadataWrapper {
replicaSetHosts: params.mongodb.replicaSetHosts,
writeConcern: params.mongodb.writeConcern,
replicaSet: params.mongodb.replicaSet,
enableSharding: params.mongodb.enableSharding,
readPreference: params.mongodb.readPreference,
database: params.mongodb.database,
replicationGroupId: params.replicationGroupId,
Expand Down
16 changes: 12 additions & 4 deletions lib/storage/metadata/mongoclient/LogConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ class LogConsumer {
* @param {string} logger - logger
*/
constructor(mongoConfig, logger) {
const { authCredentials, replicaSetHosts, replicaSet, database } = mongoConfig;
const { authCredentials, replicaSetHosts, replicaSet, database,
enableSharding } = mongoConfig;
const cred = MongoUtils.credPrefix(authCredentials);
this._mongoUrl = `mongodb://${cred}${replicaSetHosts}/`;
this._replicaSet = replicaSet;
this._enableSharding =
enableSharding !== undefined ? enableSharding : false;
this._logger = logger;
this._oplogNsRegExp = new RegExp(`^${database}\\.`);
// oplog collection
Expand All @@ -36,10 +39,15 @@ class LogConsumer {
* @return {undefined}
*/
connectMongo(done) {
MongoClient.connect(this._mongoUrl, {
replicaSet: this._replicaSet,
const options = {
useNewUrlParser: true,
},
};
if (!this._enableSharding) {
// XXX real fix is with change streams
options.replicaSet = this._replicaSet;
}
MongoClient.connect(this._mongoUrl,
options,
(err, client) => {
if (err) {
this._logger.error('Unable to connect to MongoDB',
Expand Down
24 changes: 22 additions & 2 deletions lib/storage/metadata/mongoclient/MongoClientInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ function generatePHDVersion(versionId) {
class MongoClientInterface {
constructor(params) {
const { replicaSetHosts, writeConcern, replicaSet, readPreference, path,
database, logger, replicationGroupId, authCredentials,
database, enableSharding, logger,
replicationGroupId, authCredentials,
isLocationTransient } = params;
const cred = MongoUtils.credPrefix(authCredentials);
this._enableSharding =
enableSharding !== undefined ? enableSharding : false;
this.mongoUrl = `mongodb://${cred}${replicaSetHosts}/` +
`?w=${writeConcern}&replicaSet=${replicaSet}` +
`?w=${writeConcern}` +
`${!this._enableSharding && !!replicaSet ? `&replicaSet=${replicaSet}` : ''}` +
`&readPreference=${readPreference}`;
this.logger = logger;
this.client = null;
Expand Down Expand Up @@ -142,6 +146,7 @@ class MongoClientInterface {
this.db = client.db(this.database, {
ignoreUndefined: true,
});
this.adminDb = client.db('admin');
return this.usersBucketHack(cb);
});
}
Expand Down Expand Up @@ -219,6 +224,21 @@ class MongoClientInterface {
{ error: err.message });
return cb(errors.InternalError);
}
if (this._enableSharding) {
const cmd = {
shardCollection: `${this.database}.${bucketName}`,
key: { _id: 1 },
};
return this.adminDb.command(cmd, {}, err => {
if (err) {
log.error(
'createBucket: enabling sharding',
{ error: err.message });
return cb(errors.InternalError);
}
return cb();
});
}
return cb();
});
}
Expand Down