diff --git a/src/cluster.c b/src/cluster.c index 7c7fef80..bd1de54f 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1264,11 +1264,6 @@ int valkeyClusterUpdateSlotmap(valkeyClusterContext *cc) { return VALKEY_ERR; } - if (cc->nodes == NULL) { - valkeyClusterSetError(cc, VALKEY_ERR_OTHER, "no server address"); - return VALKEY_ERR; - } - dictIterator di; dictInitIterator(&di, cc->nodes); @@ -1301,6 +1296,11 @@ valkeyClusterContext *valkeyClusterContextInit(void) { if (cc == NULL) return NULL; + cc->nodes = dictCreate(&clusterNodesDictType, NULL); + if (cc->nodes == NULL) { + valkeyClusterFree(cc); + return NULL; + } cc->max_retry_count = CLUSTER_DEFAULT_MAX_RETRY_COUNT; return cc; } @@ -1384,13 +1384,6 @@ static int valkeyClusterSetOptionAddNode(valkeyClusterContext *cc, const char *a return VALKEY_ERR; } - if (cc->nodes == NULL) { - cc->nodes = dictCreate(&clusterNodesDictType, NULL); - if (cc->nodes == NULL) { - goto oom; - } - } - sds addr_sds = sdsnew(addr); if (addr_sds == NULL) { goto oom; @@ -1631,7 +1624,7 @@ int valkeyClusterSetOptionTimeout(valkeyClusterContext *cc, memcpy(cc->command_timeout, &tv, sizeof(struct timeval)); /* Set timeout on already connected nodes */ - if (cc->nodes && dictSize(cc->nodes) > 0) { + if (dictSize(cc->nodes) > 0) { dictEntry *de; valkeyClusterNode *node; @@ -1688,7 +1681,7 @@ int valkeyClusterConnect2(valkeyClusterContext *cc) { return VALKEY_ERR; } - if (cc->nodes == NULL || dictSize(cc->nodes) == 0) { + if (dictSize(cc->nodes) == 0) { valkeyClusterSetError(cc, VALKEY_ERR_OTHER, "server address not configured"); return VALKEY_ERR; @@ -2543,7 +2536,7 @@ static int valkeyClusterSendAll(valkeyClusterContext *cc) { valkeyContext *c = NULL; int wdone = 0; - if (cc == NULL || cc->nodes == NULL) { + if (cc == NULL) { return VALKEY_ERR; } @@ -2584,10 +2577,6 @@ static int valkeyClusterClearAll(valkeyClusterContext *cc) { valkeyClusterClearError(cc); - if (cc->nodes == NULL) { - return VALKEY_ERR; - } - dictIterator di; dictInitIterator(&di, cc->nodes); @@ -2674,7 +2663,7 @@ void valkeyClusterReset(valkeyClusterContext *cc) { int status; void *reply; - if (cc == NULL || cc->nodes == NULL) { + if (cc == NULL) { return; } @@ -3051,11 +3040,6 @@ static int updateSlotMapAsync(valkeyClusterAsyncContext *acc, } if (ac == NULL) { - if (acc->cc->nodes == NULL) { - valkeyClusterAsyncSetError(acc, VALKEY_ERR_OTHER, "no nodes added"); - goto error; - } - valkeyClusterNode *node = selectNode(acc->cc->nodes); if (node == NULL) { goto error; @@ -3520,10 +3504,6 @@ void valkeyClusterAsyncDisconnect(valkeyClusterAsyncContext *acc) { cc = acc->cc; cc->flags |= VALKEYCLUSTER_FLAG_DISCONNECTING; - if (cc->nodes == NULL) { - return; - } - dictIterator di; dictInitIterator(&di, cc->nodes); diff --git a/tests/ct_out_of_memory_handling.c b/tests/ct_out_of_memory_handling.c index 0a56a4f8..a49a527e 100644 --- a/tests/ct_out_of_memory_handling.c +++ b/tests/ct_out_of_memory_handling.c @@ -117,11 +117,13 @@ void test_alloc_failure_handling(void) { // Context init valkeyClusterContext *cc; { - successfulAllocations = 0; - cc = valkeyClusterContextInit(); - assert(cc == NULL); + for (int i = 0; i < 2; ++i) { + successfulAllocations = i; + cc = valkeyClusterContextInit(); + assert(cc == NULL); + } - successfulAllocations = 1; + successfulAllocations = 2; cc = valkeyClusterContextInit(); assert(cc); } @@ -485,12 +487,12 @@ void test_alloc_failure_handling_async(void) { // Context init valkeyClusterAsyncContext *acc; { - for (int i = 0; i < 2; ++i) { + for (int i = 0; i < 3; ++i) { successfulAllocations = 0; acc = valkeyClusterAsyncContextInit(); assert(acc == NULL); } - successfulAllocations = 2; + successfulAllocations = 3; acc = valkeyClusterAsyncContextInit(); assert(acc); }