From 7bfdced05660bd2314f9f1e0812ed7c6c629cbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Svensson?= Date: Tue, 22 Oct 2024 11:29:03 +0200 Subject: [PATCH] Create request list when initiating valkeyClusterContext MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Björn Svensson --- src/cluster.c | 36 ++++++++++++------------------- tests/ct_out_of_memory_handling.c | 16 +++++++------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index bd1de54f..a7449867 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -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; } @@ -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; @@ -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; @@ -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; @@ -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); diff --git a/tests/ct_out_of_memory_handling.c b/tests/ct_out_of_memory_handling.c index a49a527e..e5caca35 100644 --- a/tests/ct_out_of_memory_handling.c +++ b/tests/ct_out_of_memory_handling.c @@ -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); } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); }