From 725aeb1e19dd3aa63095dc62d1663bd253ab30fc Mon Sep 17 00:00:00 2001 From: Renato Riccio Date: Fri, 8 Nov 2024 01:20:57 +0100 Subject: [PATCH 1/3] add --embeddedcrs option --- mtools/mlaunch/mlaunch.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/mtools/mlaunch/mlaunch.py b/mtools/mlaunch/mlaunch.py index 8473ac6a..7423fa01 100755 --- a/mtools/mlaunch/mlaunch.py +++ b/mtools/mlaunch/mlaunch.py @@ -294,6 +294,9 @@ def run(self, arguments=None): type=int, metavar='NUM', help=('adds NUM config servers to sharded ' 'setup (requires --sharded, default=1)')) + + init_parser.add_argument('--embeddedcsrs', default=False, action='store_true', + help=('use embedded CSRS, default=False')) # As of MongoDB 3.6, all config servers must be CSRS init_parser.add_argument('--csrs', default=True, action='store_true', @@ -668,6 +671,10 @@ def init(self): version.parse(self.current_version) == version.parse("0.0.0")): self.args['csrs'] = True + if self.args['embeddedcsrs'] and version.parse(self.current_version) < version.parse("8.0.0"): + print("--embeddedcsrs can only be used with 8.0.0+ version") + sys.exit(1) + # construct startup strings self._construct_cmdlines() @@ -721,6 +728,8 @@ def init(self): "an additional '--port '\n") raise SystemExit(errmsg) + + if self.args['sharded']: shard_names = self._get_shard_names(self.args) @@ -737,18 +746,20 @@ def init(self): print('Initiating config server replica set.') members = sorted(self.get_tagged(["config"])) self._initiate_replset(members[0], "configRepl") - for shard in shard_names: - # initiate replica set on first member - if self.args['verbose']: - print('Initiating shard replica set %s.' % shard) - members = sorted(self.get_tagged([shard])) - self._initiate_replset(members[0], shard) + + if not (self.args['embeddedcsrs'] and int(self.args['sharded'][0]) == 1): + for shard in shard_names: + # initiate replica set on first member + if self.args['verbose']: + print('Initiating shard replica set %s.' % shard) + members = sorted(self.get_tagged([shard])) + self._initiate_replset(members[0], shard) # add mongos mongos = sorted(self.get_tagged(['mongos', 'down'])) self._start_on_ports(mongos, wait=True, override_auth=True) - if first_init: + if first_init and not (self.args['embeddedcsrs'] and int(self.args['sharded'][0]) == 1): # add shards mongos = sorted(self.get_tagged(['mongos'])) con = self.client('localhost:%i' % mongos[0]) @@ -791,6 +802,12 @@ def init(self): print('Shard addition failed: ' + res + ' - will retry') time.sleep(1) + + if self.args['embeddedcsrs']: + if self.args['verbose']: + print("Configuring embedded config servers") + con = self.client('localhost:%i' % mongos[0]) + con.admin.command({"transitionFromDedicatedConfigServer": 1}) elif self.args['single']: # just start node From fb4b2697b94944ee9977d4e202147c7b3c71855e Mon Sep 17 00:00:00 2001 From: Renato Riccio Date: Fri, 8 Nov 2024 09:55:29 +0100 Subject: [PATCH 2/3] consider config servers in the count number when using embedded csrs --- mtools/mlaunch/mlaunch.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mtools/mlaunch/mlaunch.py b/mtools/mlaunch/mlaunch.py index 7423fa01..f93d55cc 100755 --- a/mtools/mlaunch/mlaunch.py +++ b/mtools/mlaunch/mlaunch.py @@ -296,7 +296,7 @@ def run(self, arguments=None): 'setup (requires --sharded, default=1)')) init_parser.add_argument('--embeddedcsrs', default=False, action='store_true', - help=('use embedded CSRS, default=False')) + help=('use embedded CSRS (only for MongoDB 8.0.0+), default=False')) # As of MongoDB 3.6, all config servers must be CSRS init_parser.add_argument('--csrs', default=True, action='store_true', @@ -672,7 +672,7 @@ def init(self): self.args['csrs'] = True if self.args['embeddedcsrs'] and version.parse(self.current_version) < version.parse("8.0.0"): - print("--embeddedcsrs can only be used with 8.0.0+ version") + print("--embeddedcsrs can only be used with MongoDB 8.0.0+") sys.exit(1) # construct startup strings @@ -1757,6 +1757,10 @@ def _get_shard_names(self, args): # --sharded was a number, name shards shard01, shard02, # ... (only works with replica sets) n_shards = int(args['sharded'][0]) + + if args["embeddedcsrs"]: + n_shards -= 1 + shard_names = ['shard%.2i' % (i + 1) for i in range(n_shards)] except ValueError: From 89fd5d6a6c9649b6fcfd3618a0b19b1737f50b96 Mon Sep 17 00:00:00 2001 From: Renato Riccio Date: Tue, 12 Nov 2024 22:07:59 +0100 Subject: [PATCH 3/3] improve support for embedded csrs --- mtools/mlaunch/mlaunch.py | 78 +++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/mtools/mlaunch/mlaunch.py b/mtools/mlaunch/mlaunch.py index f93d55cc..544c6d08 100755 --- a/mtools/mlaunch/mlaunch.py +++ b/mtools/mlaunch/mlaunch.py @@ -290,7 +290,7 @@ def run(self, arguments=None): 'several singles or replica sets. ' 'Provide either list of shard names or ' 'number of shards.')) - init_parser.add_argument('--config', action='store', default=1, + init_parser.add_argument('--config', action='store', default=-1, type=int, metavar='NUM', help=('adds NUM config servers to sharded ' 'setup (requires --sharded, default=1)')) @@ -663,8 +663,13 @@ def init(self): sys.stderr.write('warning: server requires certificates but no' ' --tlsClientCertificateKeyFile provided\n') # number of default config servers - if self.args['config'] == -1: - self.args['config'] = 1 + if self.args['config'] == -1 and self.args["sharded"]: + if self.args['embeddedcsrs']: + print("Config members set to: " + str(self.args['nodes'])) + self.args['config'] = self.args['nodes'] + else: + print("Config members set to: 1") + self.args['config'] = 1 # add the 'csrs' parameter as default for MongoDB >= 3.3.0 if (version.parse(self.current_version) >= version.parse("3.3.0") or @@ -675,6 +680,11 @@ def init(self): print("--embeddedcsrs can only be used with MongoDB 8.0.0+") sys.exit(1) + # Check if embedded is used, if it's the case the number of shards should be decremented by 1 + if self.args['embeddedcsrs'] and 'sharded' in self.args and self.args['sharded']: + if len(self.args['sharded']) == 1: + self.args['sharded'][0] = str(int(self.args['sharded'][0]) - 1) + # construct startup strings self._construct_cmdlines() @@ -727,8 +737,6 @@ def init(self): errmsg += (" * You can also specify a different port range with " "an additional '--port '\n") raise SystemExit(errmsg) - - if self.args['sharded']: @@ -746,20 +754,18 @@ def init(self): print('Initiating config server replica set.') members = sorted(self.get_tagged(["config"])) self._initiate_replset(members[0], "configRepl") - - if not (self.args['embeddedcsrs'] and int(self.args['sharded'][0]) == 1): - for shard in shard_names: - # initiate replica set on first member - if self.args['verbose']: - print('Initiating shard replica set %s.' % shard) - members = sorted(self.get_tagged([shard])) - self._initiate_replset(members[0], shard) + for shard in shard_names: + # initiate replica set on first member + if self.args['verbose']: + print('Initiating shard replica set %s.' % shard) + members = sorted(self.get_tagged([shard])) + self._initiate_replset(members[0], shard) # add mongos mongos = sorted(self.get_tagged(['mongos', 'down'])) self._start_on_ports(mongos, wait=True, override_auth=True) - if first_init and not (self.args['embeddedcsrs'] and int(self.args['sharded'][0]) == 1): + if first_init: # add shards mongos = sorted(self.get_tagged(['mongos'])) con = self.client('localhost:%i' % mongos[0]) @@ -804,10 +810,9 @@ def init(self): time.sleep(1) if self.args['embeddedcsrs']: - if self.args['verbose']: - print("Configuring embedded config servers") + print("Configuring embedded config servers") con = self.client('localhost:%i' % mongos[0]) - con.admin.command({"transitionFromDedicatedConfigServer": 1}) + con.admin.command({'transitionFromDedicatedConfigServer': 1}) elif self.args['single']: # just start node @@ -1046,12 +1051,26 @@ def list(self): print_docs.append(None) # configs + # temporary list used to find the list of running nodes + tmp_config_list = [] + string_config = "config server" for node in sorted(self.get_tagged(['config'])): - doc = OrderedDict([('process', 'config server'), + if self.cluster_running[node]: + status = 'running' + if self._check_if_embeddedcsrs(): + string_config = "config shard" + else: + status = 'down' + + doc = OrderedDict([('process', string_config), ('port', node), - ('status', 'running' - if self.cluster_running[node] else 'down')]) - print_docs.append(doc) + ('status', status)]) + tmp_config_list.append(doc) + + # construct the final list with the right config type + for item in tmp_config_list: + item['process'] = string_config + print_docs.append(item) if len(self.get_tagged(['config'])) > 0: print_docs.append(None) @@ -1758,9 +1777,6 @@ def _get_shard_names(self, args): # ... (only works with replica sets) n_shards = int(args['sharded'][0]) - if args["embeddedcsrs"]: - n_shards -= 1 - shard_names = ['shard%.2i' % (i + 1) for i in range(n_shards)] except ValueError: @@ -2207,6 +2223,20 @@ def _read_key_file(self, keyfile=None): with open(keyfile, 'rb') as f: return ''.join(f.readlines()) + def _check_if_embeddedcsrs(self) -> bool: + """ + Returns True if embedded CSRS is used + """ + mongos = sorted(self.get_tagged(['mongos'])) + con = self.client('localhost:%i' % mongos[0], readPreference="primaryPreferred") + try: + if con['config']['shards'].find_one({ '_id': 'config' }): + return True + return False + except OperationFailure: + print("WARNING: Unable to check if config server is embedded") + return False + def main(): tool = MLaunchTool() tool.run()