MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / clusterManagerCommandAddNode

Function clusterManagerCommandAddNode

src/redis-cli.c:4973–5068  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4971}
4972
4973static int clusterManagerCommandAddNode(int argc, char **argv) {
4974 int success = 1;
4975 redisReply *reply = NULL;
4976 char *ref_ip = NULL, *ip = NULL;
4977 int ref_port = 0, port = 0;
4978 if (!getClusterHostFromCmdArgs(argc - 1, argv + 1, &ref_ip, &ref_port))
4979 goto invalid_args;
4980 if (!getClusterHostFromCmdArgs(1, argv, &ip, &port))
4981 goto invalid_args;
4982 clusterManagerLogInfo(">>> Adding node %s:%d to cluster %s:%d\n", ip, port,
4983 ref_ip, ref_port);
4984 // Check the existing cluster
4985 clusterManagerNode *refnode = clusterManagerNewNode(ref_ip, ref_port);
4986 if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;
4987 if (!clusterManagerCheckCluster(0)) return 0;
4988
4989 /* If --cluster-master-id was specified, try to resolve it now so that we
4990 * abort before starting with the node configuration. */
4991 clusterManagerNode *master_node = NULL;
4992 if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_SLAVE) {
4993 char *master_id = config.cluster_manager_command.master_id;
4994 if (master_id != NULL) {
4995 master_node = clusterManagerNodeByName(master_id);
4996 if (master_node == NULL) {
4997 clusterManagerLogErr("[ERR] No such master ID %s\n", master_id);
4998 return 0;
4999 }
5000 } else {
5001 master_node = clusterManagerNodeWithLeastReplicas();
5002 assert(master_node != NULL);
5003 printf("Automatically selected master %s:%d\n", master_node->ip,
5004 master_node->port);
5005 }
5006 }
5007
5008 // Add the new node
5009 clusterManagerNode *new_node = clusterManagerNewNode(ip, port);
5010 int added = 0;
5011 if (!clusterManagerNodeConnect(new_node)) {
5012 clusterManagerLogErr("[ERR] Sorry, can't connect to node %s:%d\n",
5013 ip, port);
5014 success = 0;
5015 goto cleanup;
5016 }
5017 char *err = NULL;
5018 if (!(success = clusterManagerNodeIsCluster(new_node, &err))) {
5019 clusterManagerPrintNotClusterNodeError(new_node, err);
5020 if (err) zfree(err);
5021 goto cleanup;
5022 }
5023 if (!clusterManagerNodeLoadInfo(new_node, 0, &err)) {
5024 if (err) {
5025 CLUSTER_MANAGER_PRINT_REPLY_ERROR(new_node, err);
5026 zfree(err);
5027 }
5028 success = 0;
5029 goto cleanup;
5030 }

Callers

nothing calls this directly

Tested by

no test coverage detected