def process_array_with_master_node_fields(node_properties_as_array): host, port = util.split_address(node_properties_as_array[1]) if is_ip(host): start_slot = 0 end_slot = 0 if len(node_properties_as_array) is 9: start_slot, end_slot = process_start_end_slots( node_properties_as_array[8]) master_node = MasterNode(start_slot, end_slot, host, int(port), node_properties_as_array[0]) logger.debug(master_node) return master_node return None
def extract_masters_without_slots(all_nodes): master_nodes_to_return = [] i = 0 while i < len(all_nodes): node = all_nodes[i] if not ('slave' in node or 'noaddr' in node): node_as_array = re.compile(' ').split(node) if 8 >= len(node_as_array) > 1: host, port = util.split_address(node_as_array[1]) if is_ip(host): master_node_to_add = MasterNode(0, 0, host, int(port), node_as_array[0]) logger.debug(master_node_to_add) master_nodes_to_return.append(master_node_to_add) i += 1 return master_nodes_to_return
def reshard(source): logger.info("Started resharding") host, port = util.split_address(source) cluster_masters_with_slots = get_slot_distribution(host, port) logger.debug('Found %s master(s) in the cluster with slots', len(cluster_masters_with_slots)) cluster_masters_without_slots = get_master_without_slots(host, port) logger.debug('Found %s master(s) in the cluster without slots', len(cluster_masters_without_slots)) if len(cluster_masters_without_slots) == 0: logger.error( 'Cannot start resharding, since there are no masters where slots could be placed' ) return logger.info('Performing resharding...') perform_resharding(cluster_masters_with_slots, cluster_masters_without_slots, source) logger.info('[√] Done resharding')
def is_valid_redis_node(node_address): logging.debug('Validating connection to the %s', node_address) host, port = util.split_address(node_address) cmd_args = ['-c', '-h', host, '-p', port, 'ping'] result = util.run_redis_cli_cmd(cmd_args, True) parsed_cmd_result = result.stdout.decode("utf-8").rstrip() if parsed_cmd_result == "PONG": cmd_args = ['-c', '-h', host, '-p', port, 'cluster', 'info'] result = util.run_redis_cli_cmd(cmd_args, True) parsed_cmd_result = result.stdout.decode("utf-8").rstrip() if parsed_cmd_result.find("cluster_size:0") >= 0: logging.error( 'Managed to establish a connection with the %s node. But node is a part of an empty redis cluster', node_address) return False logging.debug('Successfully established connection to %s', node_address) return True logging.error( 'Could not establish connection with the %s node. Returned code: %s', node_address, result.returncode) return False
def add_node_to_cluster(source, target, target_role): for redis_node_address in target: host, port = split_address(redis_node_address) if is_ip(host): is_valid_redis_node(redis_node_address) logging.info('Adding %s with the %s role to the cluster ', target, target_role) full_node_address = host + ":" + str(port) cmd = ['--cluster', 'add-node', full_node_address, source] if target_role == "slave": cmd.append("--cluster-slave") result = run_redis_cli_cmd(cmd, True) if result.returncode == 0: logging.info('[V] Node %s was added to the cluster with role %s', full_node_address, target_role) else: logging.error( '[X] Node %s was NOT added to the cluster. ' 'Check if the given node is in clustered mode, ' 'if is it empty or if this node is already part of the cluster', full_node_address) else: logging.error( '[X] %s node does not have a valid address', target)