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

Function clusterReset

src/cluster.cpp:590–646  ·  view source on GitHub ↗

Reset a node performing a soft or hard reset: * * 1) All other nodes are forgotten. * 2) All the assigned / open slots are released. * 3) If the node is a slave, it turns into a master. * 4) Only for hard reset: a new Node ID is generated. * 5) Only for hard reset: currentEpoch and configEpoch are set to 0. * 6) The new configuration is saved and the cluster state updated. * 7) If the node

Source from the content-addressed store, hash-verified

588 * 6) The new configuration is saved and the cluster state updated.
589 * 7) If the node was a slave, the whole data set is flushed away. */
590void clusterReset(int hard) {
591 dictIterator *di;
592 dictEntry *de;
593 int j;
594
595 /* Turn into master. */
596 if (nodeIsSlave(myself)) {
597 clusterSetNodeAsMaster(myself);
598 if (listLength(g_pserver->masters) > 0)
599 {
600 serverAssert(listLength(g_pserver->masters) == 1);
601 replicationUnsetMaster((redisMaster*)listFirst(g_pserver->masters)->value);
602 }
603 emptyDb(-1,EMPTYDB_NO_FLAGS,NULL);
604 }
605
606 /* Close slots, reset manual failover state. */
607 clusterCloseAllSlots();
608 resetManualFailover();
609
610 /* Unassign all the slots. */
611 for (j = 0; j < CLUSTER_SLOTS; j++) clusterDelSlot(j);
612
613 /* Forget all the nodes, but myself. */
614 di = dictGetSafeIterator(g_pserver->cluster->nodes);
615 while((de = dictNext(di)) != NULL) {
616 clusterNode *node = (clusterNode*)dictGetVal(de);
617
618 if (node == myself) continue;
619 clusterDelNode(node);
620 }
621 dictReleaseIterator(di);
622
623 /* Hard reset only: set epochs to 0, change node ID. */
624 if (hard) {
625 sds oldname;
626
627 g_pserver->cluster->currentEpoch = 0;
628 g_pserver->cluster->lastVoteEpoch = 0;
629 myself->configEpoch = 0;
630 serverLog(LL_WARNING, "configEpoch set to 0 via CLUSTER RESET HARD");
631
632 /* To change the Node ID we need to remove the old name from the
633 * nodes table, change the ID, and re-add back with new name. */
634 oldname = sdsnewlen(myself->name, CLUSTER_NAMELEN);
635 dictDelete(g_pserver->cluster->nodes,oldname);
636 sdsfree(oldname);
637 getRandomHexChars(myself->name, CLUSTER_NAMELEN);
638 clusterAddNode(myself);
639 serverLog(LL_NOTICE,"Node hard reset, now I'm %.40s", myself->name);
640 }
641
642 /* Make sure to persist the new config and update the state. */
643 clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|
644 CLUSTER_TODO_UPDATE_STATE|
645 CLUSTER_TODO_FSYNC_CONFIG);
646}
647

Callers 1

clusterCommandFunction · 0.85

Calls 15

clusterSetNodeAsMasterFunction · 0.85
replicationUnsetMasterFunction · 0.85
emptyDbFunction · 0.85
clusterCloseAllSlotsFunction · 0.85
resetManualFailoverFunction · 0.85
clusterDelSlotFunction · 0.85
dictGetSafeIteratorFunction · 0.85
clusterDelNodeFunction · 0.85
serverLogFunction · 0.85
sdsnewlenFunction · 0.85
sdsfreeFunction · 0.85
getRandomHexCharsFunction · 0.85

Tested by

no test coverage detected