From 77f971957b025ee06ebafeeba5c96683d2a587f0 Mon Sep 17 00:00:00 2001 From: Rached Ben Mustapha Date: Fri, 22 Oct 2021 16:12:23 +0000 Subject: [PATCH] feature: support mongodb collection sharding --- lib/storage/metadata/MetadataWrapper.js | 1 + .../mongoclient/MongoClientInterface.js | 30 +++++++++++++++---- package.json | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/storage/metadata/MetadataWrapper.js b/lib/storage/metadata/MetadataWrapper.js index c869dc723..7763b9b2b 100644 --- a/lib/storage/metadata/MetadataWrapper.js +++ b/lib/storage/metadata/MetadataWrapper.js @@ -90,6 +90,7 @@ class MetadataWrapper { replicationGroupId: params.replicationGroupId, path: params.mongodb.path, authCredentials: params.mongodb.authCredentials, + shardCollections: params.mongodb.shardCollections, config: params.config, logger, }); diff --git a/lib/storage/metadata/mongoclient/MongoClientInterface.js b/lib/storage/metadata/mongoclient/MongoClientInterface.js index 1592bed50..c0fe80f64 100644 --- a/lib/storage/metadata/mongoclient/MongoClientInterface.js +++ b/lib/storage/metadata/mongoclient/MongoClientInterface.js @@ -92,11 +92,15 @@ class MongoClientInterface { constructor(params) { const { replicaSetHosts, writeConcern, replicaSet, readPreference, path, database, logger, replicationGroupId, authCredentials, - isLocationTransient } = params; + isLocationTransient, shardCollections } = params; const cred = MongoUtils.credPrefix(authCredentials); this.mongoUrl = `mongodb://${cred}${replicaSetHosts}/` + - `?w=${writeConcern}&replicaSet=${replicaSet}` + - `&readPreference=${readPreference}`; + `?w=${writeConcern}&readPreference=${readPreference}`; + + if (!shardCollections) { + this.mongoUrl += `&replicaSet=${replicaSet}`; + } + this.logger = logger; this.client = null; this.db = null; @@ -104,6 +108,7 @@ class MongoClientInterface { this.replicationGroupId = replicationGroupId; this.database = database; this.isLocationTransient = isLocationTransient; + this.shardCollections = shardCollections; this.concurrentCursors = (process.env.CONCURRENT_CURSORS && !Number.isNaN(process.env.CONCURRENT_CURSORS)) @@ -142,6 +147,7 @@ class MongoClientInterface { this.db = client.db(this.database, { ignoreUndefined: true, }); + this.adminDb = client.db('admin'); return this.usersBucketHack(cb); }); } @@ -216,9 +222,24 @@ class MongoClientInterface { if (err) { log.error( 'createBucket: error creating bucket', - { error: err.message }); + { error: err }); return cb(errors.InternalError); } + if (this.shardCollections) { + const cmd = { + shardCollection: `${this.database}.${bucketName}`, + key: { _id: 1 }, + }; + return this.adminDb.command(cmd, {}, err => { + if (err) { + log.error( + 'createBucket: enabling sharding', + { error: err }); + return cb(errors.InternalError); + } + return cb(); + }); + } return cb(); }); } @@ -1706,4 +1727,3 @@ class MongoClientInterface { } module.exports = MongoClientInterface; - diff --git a/package.json b/package.json index 88b0e923d..937137cbf 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "engines": { "node": ">=8" }, - "version": "8.1.10", + "version": "8.1.11", "description": "Common utilities for the S3 project components", "main": "index.js", "repository": {