Skip to content

Commit

Permalink
Create request list 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 23, 2024
1 parent 62954c3 commit c95220a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
36 changes: 14 additions & 22 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,13 @@ valkeyClusterContext *valkeyClusterContextInit(void) {
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 @@ -2349,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 @@ -2438,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 @@ -2609,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 @@ -2685,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
16 changes: 8 additions & 8 deletions tests/ct_out_of_memory_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ void test_alloc_failure_handling(void) {
// Context init
valkeyClusterContext *cc;
{
for (int i = 0; i < 2; ++i) {
for (int i = 0; i < 3; ++i) {
successfulAllocations = i;
cc = valkeyClusterContextInit();
assert(cc == NULL);
}

successfulAllocations = 2;
successfulAllocations = 3;
cc = valkeyClusterContextInit();
assert(cc);
}
Expand Down Expand Up @@ -231,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 @@ -243,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 @@ -275,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 @@ -287,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 @@ -487,12 +487,12 @@ void test_alloc_failure_handling_async(void) {
// Context init
valkeyClusterAsyncContext *acc;
{
for (int i = 0; i < 3; ++i) {
for (int i = 0; i < 4; ++i) {
successfulAllocations = 0;
acc = valkeyClusterAsyncContextInit();
assert(acc == NULL);
}
successfulAllocations = 3;
successfulAllocations = 4;
acc = valkeyClusterAsyncContextInit();
assert(acc);
}
Expand Down

0 comments on commit c95220a

Please sign in to comment.