Skip to content

Commit

Permalink
Persistence - Remove Unowned Keys
Browse files Browse the repository at this point in the history
For cluster, after startup loading, remove keys
that shouldn't be served by this server based on slot
assignment of a cluster.

Signed-off-by: Liang Tang <[email protected]>
  • Loading branch information
singku committed Aug 13, 2024
1 parent fa238dc commit b8a9d6b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ ConnectionType *connTypeOfCluster(void) {
return connectionTypeTcp();
}

/* delete unowned keys from database at server start up after loading persistence files. */
void delUnownedKeys(void) {
if (!server.cluster_enabled) {
return;
}
clusterNode *primary = clusterNodeGetPrimary(server.cluster->myself);
for (unsigned int i = 0; i < CLUSTER_SLOTS; i++) {
if (server.cluster->slots[i] != primary) {
unsigned int deleted = delKeysInSlot(i);
if (deleted > 0) {
serverLog(LL_VERBOSE, "Deleted %u keys from unowned slot: %d after loading RDB", deleted, i);
}
}
}
}

/* -----------------------------------------------------------------------------
* DUMP, RESTORE and MIGRATE commands
* -------------------------------------------------------------------------- */
Expand Down
1 change: 1 addition & 0 deletions src/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@ int isNodeAvailable(clusterNode *node);
long long getNodeReplicationOffset(clusterNode *node);
sds aggregateClientOutputBuffer(client *c);
void resetClusterStats(void);
unsigned int delKeysInSlot(unsigned int);
#endif /* __CLUSTER_H */
2 changes: 1 addition & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6930,7 +6930,7 @@ int main(int argc, char **argv) {
if (server.cluster_enabled) {
serverAssert(verifyClusterConfigWithData() == C_OK);
}

delUnownedKeys();
for (j = 0; j < CONN_TYPE_MAX; j++) {
connListener *listener = &server.listeners[j];
if (listener->ct == NULL) continue;
Expand Down
2 changes: 2 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3678,6 +3678,8 @@ unsigned long LFUDecrAndReturn(robj *o);
int performEvictions(void);
void startEvictionTimeProc(void);

void delUnownedKeys(void);

/* Keys hashing / comparison functions for dict.c hash tables. */
uint64_t dictSdsHash(const void *key);
uint64_t dictSdsCaseHash(const void *key);
Expand Down

0 comments on commit b8a9d6b

Please sign in to comment.