MCPcopy Create free account
hub / github.com/F-Stack/f-stack / clusterManagerCommandRebalance

Function clusterManagerCommandRebalance

app/redis-6.2.6/src/redis-cli.c:6268–6455  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6266}
6267
6268static int clusterManagerCommandRebalance(int argc, char **argv) {
6269 int port = 0;
6270 char *ip = NULL;
6271 clusterManagerNode **weightedNodes = NULL;
6272 list *involved = NULL;
6273 if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;
6274 clusterManagerNode *node = clusterManagerNewNode(ip, port);
6275 if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;
6276 int result = 1, i;
6277 if (config.cluster_manager_command.weight != NULL) {
6278 for (i = 0; i < config.cluster_manager_command.weight_argc; i++) {
6279 char *name = config.cluster_manager_command.weight[i];
6280 char *p = strchr(name, '=');
6281 if (p == NULL) {
6282 result = 0;
6283 goto cleanup;
6284 }
6285 *p = '\0';
6286 float w = atof(++p);
6287 clusterManagerNode *n = clusterManagerNodeByAbbreviatedName(name);
6288 if (n == NULL) {
6289 clusterManagerLogErr("*** No such master node %s\n", name);
6290 result = 0;
6291 goto cleanup;
6292 }
6293 n->weight = w;
6294 }
6295 }
6296 float total_weight = 0;
6297 int nodes_involved = 0;
6298 int use_empty = config.cluster_manager_command.flags &
6299 CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER;
6300 involved = listCreate();
6301 listIter li;
6302 listNode *ln;
6303 listRewind(cluster_manager.nodes, &li);
6304 /* Compute the total cluster weight. */
6305 while ((ln = listNext(&li)) != NULL) {
6306 clusterManagerNode *n = ln->value;
6307 if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)
6308 continue;
6309 if (!use_empty && n->slots_count == 0) {
6310 n->weight = 0;
6311 continue;
6312 }
6313 total_weight += n->weight;
6314 nodes_involved++;
6315 listAddNodeTail(involved, n);
6316 }
6317 weightedNodes = zmalloc(nodes_involved * sizeof(clusterManagerNode *));
6318 if (weightedNodes == NULL) goto cleanup;
6319 /* Check cluster, only proceed if it looks sane. */
6320 clusterManagerCheckCluster(1);
6321 if (cluster_manager.errors && listLength(cluster_manager.errors) > 0) {
6322 clusterManagerLogErr("*** Please fix your cluster problems "
6323 "before rebalancing\n");
6324 result = 0;
6325 goto cleanup;

Callers

nothing calls this directly

Calls 15

clusterManagerNewNodeFunction · 0.85
strchrFunction · 0.85
listCreateFunction · 0.85
listRewindFunction · 0.85
listNextFunction · 0.85
listAddNodeTailFunction · 0.85
zmallocFunction · 0.85
qsortFunction · 0.85

Tested by

no test coverage detected