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

Function clusterManagerFixOpenSlot

src/redis-cli.c:4139–4474  ·  view source on GitHub ↗

Slot 'slot' was found to be in importing or migrating state in one or * more nodes. This function fixes this condition by migrating keys where * it seems more sensible. */

Source from the content-addressed store, hash-verified

4137 * more nodes. This function fixes this condition by migrating keys where
4138 * it seems more sensible. */
4139int clusterManagerFixOpenSlot(int slot) {
4140 int force_fix = config.cluster_manager_command.flags &
4141 CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS;
4142
4143 if (cluster_manager.unreachable_masters > 0 && !force_fix) {
4144 clusterManagerLogWarn("*** Fixing open slots with %d unreachable masters is dangerous: keydb-cli will assume that slots about masters that are not reachable are not covered, and will try to reassign them to the reachable nodes. This can cause data loss and is rarely what you want to do. If you really want to proceed use the --cluster-fix-with-unreachable-masters option.\n", cluster_manager.unreachable_masters);
4145 exit(1);
4146 }
4147
4148 clusterManagerLogInfo(">>> Fixing open slot %d\n", slot);
4149 /* Try to obtain the current slot owner, according to the current
4150 * nodes configuration. */
4151 int success = 1;
4152 list *owners = listCreate(); /* List of nodes claiming some ownership.
4153 it could be stating in the configuration
4154 to have the node ownership, or just
4155 holding keys for such slot. */
4156 list *migrating = listCreate();
4157 list *importing = listCreate();
4158 sds migrating_str = sdsempty();
4159 sds importing_str = sdsempty();
4160 clusterManagerNode *owner = NULL; /* The obvious slot owner if any. */
4161
4162 /* Iterate all the nodes, looking for potential owners of this slot. */
4163 listIter li;
4164 listNode *ln;
4165 listRewind(cluster_manager.nodes, &li);
4166 while ((ln = listNext(&li)) != NULL) {
4167 clusterManagerNode *n = ln->value;
4168 if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
4169 if (n->slots[slot]) {
4170 listAddNodeTail(owners, n);
4171 } else {
4172 redisReply *r = CLUSTER_MANAGER_COMMAND(n,
4173 "CLUSTER COUNTKEYSINSLOT %d", slot);
4174 success = clusterManagerCheckRedisReply(n, r, NULL);
4175 if (success && r->integer > 0) {
4176 clusterManagerLogWarn("*** Found keys about slot %d "
4177 "in non-owner node %s:%d!\n", slot,
4178 n->ip, n->port);
4179 listAddNodeTail(owners, n);
4180 }
4181 if (r) freeReplyObject(r);
4182 if (!success) goto cleanup;
4183 }
4184 }
4185
4186 /* If we have only a single potential owner for this slot,
4187 * set it as "owner". */
4188 if (listLength(owners) == 1) owner = listFirst(owners)->value;
4189
4190 /* Scan the list of nodes again, in order to populate the
4191 * list of nodes in importing or migrating state for
4192 * this slot. */
4193 listRewind(cluster_manager.nodes, &li);
4194 while ((ln = listNext(&li)) != NULL) {
4195 clusterManagerNode *n = ln->value;
4196 if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;

Callers 1

Calls 15

listCreateFunction · 0.85
sdsemptyFunction · 0.85
listRewindFunction · 0.85
listNextFunction · 0.85
listAddNodeTailFunction · 0.85
freeReplyObjectFunction · 0.85
sdscatfmtFunction · 0.85
sdslenFunction · 0.85

Tested by

no test coverage detected