Skip to content

Commit

Permalink
Create nodes dict when initiating valkeyClusterContext
Browse files Browse the repository at this point in the history
Signed-off-by: Björn Svensson <[email protected]>
  • Loading branch information
bjosv committed Oct 22, 2024
1 parent 0985c62 commit 66576b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
38 changes: 9 additions & 29 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -2584,10 +2577,6 @@ static int valkeyClusterClearAll(valkeyClusterContext *cc) {

valkeyClusterClearError(cc);

if (cc->nodes == NULL) {
return VALKEY_ERR;
}

dictIterator di;
dictInitIterator(&di, cc->nodes);

Expand Down Expand Up @@ -2674,7 +2663,7 @@ void valkeyClusterReset(valkeyClusterContext *cc) {
int status;
void *reply;

if (cc == NULL || cc->nodes == NULL) {
if (cc == NULL) {
return;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 8 additions & 6 deletions tests/ct_out_of_memory_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 66576b1

Please sign in to comment.