| 5353 | } |
| 5354 | |
| 5355 | static int clusterManagerCommandRebalance(int argc, char **argv) { |
| 5356 | int port = 0; |
| 5357 | char *ip = NULL; |
| 5358 | clusterManagerNode **weightedNodes = NULL; |
| 5359 | list *involved = NULL; |
| 5360 | if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args; |
| 5361 | clusterManagerNode *node = clusterManagerNewNode(ip, port); |
| 5362 | if (!clusterManagerLoadInfoFromNode(node, 0)) return 0; |
| 5363 | int result = 1, i; |
| 5364 | if (config.cluster_manager_command.weight != NULL) { |
| 5365 | for (i = 0; i < config.cluster_manager_command.weight_argc; i++) { |
| 5366 | char *name = config.cluster_manager_command.weight[i]; |
| 5367 | char *p = strchr(name, '='); |
| 5368 | if (p == NULL) { |
| 5369 | result = 0; |
| 5370 | goto cleanup; |
| 5371 | } |
| 5372 | *p = '\0'; |
| 5373 | float w = atof(++p); |
| 5374 | clusterManagerNode *n = clusterManagerNodeByAbbreviatedName(name); |
| 5375 | if (n == NULL) { |
| 5376 | clusterManagerLogErr("*** No such master node %s\n", name); |
| 5377 | result = 0; |
| 5378 | goto cleanup; |
| 5379 | } |
| 5380 | n->weight = w; |
| 5381 | } |
| 5382 | } |
| 5383 | float total_weight = 0; |
| 5384 | int nodes_involved = 0; |
| 5385 | int use_empty = config.cluster_manager_command.flags & |
| 5386 | CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER; |
| 5387 | involved = listCreate(); |
| 5388 | listIter li; |
| 5389 | listNode *ln; |
| 5390 | listRewind(cluster_manager.nodes, &li); |
| 5391 | /* Compute the total cluster weight. */ |
| 5392 | while ((ln = listNext(&li)) != NULL) { |
| 5393 | clusterManagerNode *n = ln->value; |
| 5394 | if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate) |
| 5395 | continue; |
| 5396 | if (!use_empty && n->slots_count == 0) { |
| 5397 | n->weight = 0; |
| 5398 | continue; |
| 5399 | } |
| 5400 | total_weight += n->weight; |
| 5401 | nodes_involved++; |
| 5402 | listAddNodeTail(involved, n); |
| 5403 | } |
| 5404 | weightedNodes = zmalloc(nodes_involved * sizeof(clusterManagerNode *), MALLOC_LOCAL); |
| 5405 | if (weightedNodes == NULL) goto cleanup; |
| 5406 | /* Check cluster, only proceed if it looks sane. */ |
| 5407 | clusterManagerCheckCluster(1); |
| 5408 | if (cluster_manager.errors && listLength(cluster_manager.errors) > 0 && !config.force_mode) { |
| 5409 | clusterManagerLogErr("*** Please fix your cluster problems " |
| 5410 | "before rebalancing\n"); |
| 5411 | result = 0; |
| 5412 | goto cleanup; |
nothing calls this directly
no test coverage detected