This function is called after the node startup in order to verify that data * loaded from disk is in agreement with the cluster configuration: * * 1) If we find keys about hash slots we have no responsibility for, the * following happens: * A) If no other node is in charge according to the current cluster * configuration, we add these slots to our node. * B) If according to o
| 4115 | * about desynchronizations between the data we have in memory and the |
| 4116 | * cluster configuration. */ |
| 4117 | int verifyClusterConfigWithData(void) { |
| 4118 | int j; |
| 4119 | int update_config = 0; |
| 4120 | |
| 4121 | /* Return ASAP if a module disabled cluster redirections. In that case |
| 4122 | * every master can store keys about every possible hash slot. */ |
| 4123 | if (g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_REDIRECTION) |
| 4124 | return C_OK; |
| 4125 | |
| 4126 | /* If this node is a slave, don't perform the check at all as we |
| 4127 | * completely depend on the replication stream. */ |
| 4128 | if (nodeIsSlave(myself)) return C_OK; |
| 4129 | |
| 4130 | /* Make sure we only have keys in DB0. */ |
| 4131 | for (j = 1; j < cserver.dbnum; j++) { |
| 4132 | if (g_pserver->db[j]->size()) return C_ERR; |
| 4133 | } |
| 4134 | |
| 4135 | /* Check that all the slots we see populated memory have a corresponding |
| 4136 | * entry in the cluster table. Otherwise fix the table. */ |
| 4137 | for (j = 0; j < CLUSTER_SLOTS; j++) { |
| 4138 | if (!countKeysInSlot(j)) continue; /* No keys in this slot. */ |
| 4139 | /* Check if we are assigned to this slot or if we are importing it. |
| 4140 | * In both cases check the next slot as the configuration makes |
| 4141 | * sense. */ |
| 4142 | if (g_pserver->cluster->slots[j] == myself || |
| 4143 | g_pserver->cluster->importing_slots_from[j] != NULL) continue; |
| 4144 | |
| 4145 | /* If we are here data and cluster config don't agree, and we have |
| 4146 | * slot 'j' populated even if we are not importing it, nor we are |
| 4147 | * assigned to this slot. Fix this condition. */ |
| 4148 | |
| 4149 | update_config++; |
| 4150 | /* Case A: slot is unassigned. Take responsibility for it. */ |
| 4151 | if (g_pserver->cluster->slots[j] == NULL) { |
| 4152 | serverLog(LL_WARNING, "I have keys for unassigned slot %d. " |
| 4153 | "Taking responsibility for it.",j); |
| 4154 | clusterAddSlot(myself,j); |
| 4155 | } else { |
| 4156 | serverLog(LL_WARNING, "I have keys for slot %d, but the slot is " |
| 4157 | "assigned to another node. " |
| 4158 | "Setting it to importing state.",j); |
| 4159 | g_pserver->cluster->importing_slots_from[j] = g_pserver->cluster->slots[j]; |
| 4160 | } |
| 4161 | } |
| 4162 | if (update_config) clusterSaveConfigOrDie(1); |
| 4163 | return C_OK; |
| 4164 | } |
| 4165 | |
| 4166 | /* ----------------------------------------------------------------------------- |
| 4167 | * SLAVE nodes handling |
no test coverage detected