Repository: valkey-io/valkey
File: src/cluster_legacy.c (C)

Code before:
                        server.cluster->stat_cluster_links_buffer_limit_exceeded);

    return info;
}


void removeChannelsInSlot(unsigned int slot) {
    if (countChannelsInSlot(slot) == 0) return;

    pubsubShardUnsubscribeAllChannelsInSlot(slot);
}

/* Remove all the keys in the specified hash slot.
 * The number of removed items is returned. */
unsigned int delKeysInSlot(unsigned int hashslot) {
    if (!countKeysInSlot(hashslot)) return 0;

    /* We may lose a slot during the pause. We need to track this
     * state so that we don't assert in propagateNow(). */
    server.server_del_keys_in_slot = 1;
    unsigned int j = 0;

    for (int i = 0; i < server.dbnum; i++) {
        kvstoreHashtableIterator *kvs_di = NULL;
        void *next;
        serverDb *db = server.db[0];
        kvs_di = kvstoreGetHashtableIterator(db->keys, hashslot, HASHTABLE_ITER_SAFE);
        while (kvstoreHashtableIteratorNext(kvs_di, &next)) {
            robj *valkey = next;
            enterExecutionUnit(1, 0);
            sds sdskey = objectGetKey(valkey);
            robj *key = createStringObject(sdskey, sdslen(sdskey));
            dbDelete(db, key);
            propagateDeletion(db, key, server.lazyfree_lazy_server_del);
            signalModifiedKey(NULL, db, key);
            /* The keys are not actually logically deleted from the database, just moved to another node.
             * The modules needs to know that these keys are no longer available locally, so just send the
             * keyspace notification to the modules, but not to clients. */
            moduleNotifyKeyspaceEvent(NOTIFY_GENERIC, "del", key, db->id);
            exitExecutionUnit();
            postExecutionUnitOperations();
            decrRefCount(key);
            j++;
            server.dirty++;
        }
        kvstoreReleaseHashtableIterator(kvs_di);
    }
    server.server_del_keys_in_slot = 0;
    serverAssert(server.execution_nesting == 0);
    return j;
}

Review comment (suggestion):
Shouldn't this be `i`?

And should we skip uninitialized databases?

```suggestion
        serverDb *db = server.db[i];
        if (db == NULL) continue;
```

Code after:
        tot_msg_received += server.cluster->stats_bus_messages_received[i];
        info = sdscatprintf(info, "cluster_stats_messages_%s_received:%lld\r\n", clusterGetMessageTypeString(i),
                            server.cluster->stats_bus_messages_received[i]);
    }
    info = sdscatprintf(info, "cluster_stats_messages_received:%lld\r\n", tot_msg_received);

    info = sdscatprintf(info, "total_cluster_links_buffer_limit_exceeded:%llu\r\n",
                        server.cluster->stat_cluster_links_buffer_limit_exceeded);

    return info;
}


void removeChannelsInSlot(unsigned int slot) {
    if (countChannelsInSlot(slot) == 0) return;

    pubsubShardUnsubscribeAllChannelsInSlot(slot);
}

/* Remove all the keys in the specified hash slot.
 * The number of removed items is returned. */
unsigned int delKeysInSlot(unsigned int hashslot, int lazy, bool propagate_del, bool send_del_event) {
    if (!countKeysInSlot(hashslot)) return 0;

    /* We may lose a slot during the pause. We need to track this
     * state so that we don't assert in propagateNow(). */
    server.server_del_keys_in_slot = 1;
    unsigned int j = 0;
    int before_execution_nesting = server.execution_nesting;

    for (int i = 0; i < server.dbnum; i++) {
        kvstoreHashtableIterator *kvs_di = NULL;
        void *next;
        serverDb *db = server.db[i];
        if (db == NULL) continue;
        kvs_di = kvstoreGetHashtableIterator(db->keys, hashslot, HASHTABLE_ITER_SAFE);
        while (kvstoreHashtableIteratorNext(kvs_di, &next)) {
            robj *valkey = next;
            enterExecutionUnit(1, 0);
            sds sdskey = objectGetKey(valkey);
            robj *key = createStringObject(sdskey, sdslen(sdskey));
            if (lazy) {
                dbAsyncDelete(db, key);
            } else {
                dbSyncDelete(db, key);
            }
            // if is command, skip del propagate
            if (propagate_del) propagateDeletion(db, key, lazy);
            signalModifiedKey(NULL, db, key);
            if (send_del_event) {
                /* In the `cluster flushslot` scenario, the keys are actually deleted so notify everyone. */