Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create cluster nodes dict and request list when initiating valkeyClusterContext #123

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 23 additions & 51 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,18 @@ valkeyClusterContext *valkeyClusterContextInit(void) {
if (cc == NULL)
return NULL;

cc->nodes = dictCreate(&clusterNodesDictType, NULL);
if (cc->nodes == NULL) {
valkeyClusterFree(cc);
return NULL;
}
cc->requests = listCreate();
if (cc->requests == NULL) {
valkeyClusterFree(cc);
return NULL;
}
cc->requests->free = listCommandFree;

cc->max_retry_count = CLUSTER_DEFAULT_MAX_RETRY_COUNT;
return cc;
}
Expand Down Expand Up @@ -1384,13 +1391,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 +1631,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 +1688,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 @@ -2356,14 +2356,6 @@ int valkeyClusterAppendFormattedCommand(valkeyClusterContext *cc, char *cmd,
int len) {
struct cmd *command = NULL;

if (cc->requests == NULL) {
cc->requests = listCreate();
if (cc->requests == NULL) {
goto oom;
}
cc->requests->free = listCommandFree;
}

command = command_get();
if (command == NULL) {
goto oom;
Expand Down Expand Up @@ -2445,14 +2437,6 @@ int valkeyClustervAppendCommandToNode(valkeyClusterContext *cc,
char *cmd = NULL;
int len;

if (cc->requests == NULL) {
cc->requests = listCreate();
if (cc->requests == NULL)
goto oom;

cc->requests->free = listCommandFree;
}

c = valkeyClusterGetValkeyContext(cc, node);
if (c == NULL) {
return VALKEY_ERR;
Expand Down Expand Up @@ -2543,7 +2527,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 +2568,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 @@ -2620,12 +2600,9 @@ int valkeyClusterGetReply(valkeyClusterContext *cc, void **reply) {
valkeyClusterClearError(cc);
*reply = NULL;

if (cc->requests == NULL)
return VALKEY_ERR; // No queued requests

list_command = listFirst(cc->requests);

// no more reply
/* No queued requests. */
if (list_command == NULL) {
*reply = NULL;
return VALKEY_OK;
Expand Down Expand Up @@ -2674,7 +2651,7 @@ void valkeyClusterReset(valkeyClusterContext *cc) {
int status;
void *reply;

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

Expand All @@ -2696,8 +2673,12 @@ void valkeyClusterReset(valkeyClusterContext *cc) {
} while (reply != NULL);
}

listRelease(cc->requests);
cc->requests = NULL;
listIter li;
listRewind(cc->requests, &li);
listNode *ln;
while ((ln = listNext(&li))) {
listDelNode(cc->requests, ln);
}

if (cc->need_update_route) {
status = valkeyClusterUpdateSlotmap(cc);
Expand Down Expand Up @@ -3051,11 +3032,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 +3496,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
22 changes: 12 additions & 10 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 < 3; ++i) {
successfulAllocations = i;
cc = valkeyClusterContextInit();
assert(cc == NULL);
}

successfulAllocations = 1;
successfulAllocations = 3;
cc = valkeyClusterContextInit();
assert(cc);
}
Expand Down Expand Up @@ -229,7 +231,7 @@ void test_alloc_failure_handling(void) {
valkeyReply *reply;
const char *cmd = "SET foo one";

for (int i = 0; i < 34; ++i) {
for (int i = 0; i < 33; ++i) {
prepare_allocation_test(cc, i);
result = valkeyClusterAppendCommand(cc, cmd);
assert(result == VALKEY_ERR);
Expand All @@ -241,7 +243,7 @@ void test_alloc_failure_handling(void) {
for (int i = 0; i < 4; ++i) {
// Appended command lost when receiving error from valkey
// during a GetReply, needs a new append for each test loop
prepare_allocation_test(cc, 34);
prepare_allocation_test(cc, 33);
result = valkeyClusterAppendCommand(cc, cmd);
assert(result == VALKEY_OK);

Expand Down Expand Up @@ -273,7 +275,7 @@ void test_alloc_failure_handling(void) {
assert(node);

// OOM failing appends
for (int i = 0; i < 35; ++i) {
for (int i = 0; i < 34; ++i) {
prepare_allocation_test(cc, i);
result = valkeyClusterAppendCommandToNode(cc, node, cmd);
assert(result == VALKEY_ERR);
Expand All @@ -285,7 +287,7 @@ void test_alloc_failure_handling(void) {
// OOM failing GetResults
for (int i = 0; i < 4; ++i) {
// First a successful append
prepare_allocation_test(cc, 35);
prepare_allocation_test(cc, 34);
result = valkeyClusterAppendCommandToNode(cc, node, cmd);
assert(result == VALKEY_OK);

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 < 4; ++i) {
successfulAllocations = 0;
acc = valkeyClusterAsyncContextInit();
assert(acc == NULL);
}
successfulAllocations = 2;
successfulAllocations = 4;
acc = valkeyClusterAsyncContextInit();
assert(acc);
}
Expand Down
Loading