Skip to content

Commit 916cd5a

Browse files
committed
Combining two options into single option "--rfr"
Refactoring with enum, readFromReplicas, and combining two options(rfa, rfro) int single option(rfr)
1 parent bd44040 commit 916cd5a

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/valkey-benchmark.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ struct benchmarkThread;
7777
struct clusterNode;
7878
struct serverConfig;
7979

80+
/* Read from replica options */
81+
typedef enum readFromReplica {
82+
FROM_PRIMARY_ONLY = 0, /* default option */
83+
FROM_REPLICA_ONLY,
84+
FROM_ALL
85+
} readFromReplica;
86+
8087
static struct config {
8188
aeEventLoop *el;
8289
cliConnInfo conn_info;
@@ -112,8 +119,7 @@ static struct config {
112119
int num_threads;
113120
struct benchmarkThread **threads;
114121
int cluster_mode;
115-
int read_from_all;
116-
int read_from_replicas_only;
122+
readFromReplica read_from_replica;
117123
int cluster_node_count;
118124
struct clusterNode **cluster_nodes;
119125
struct serverConfig *redis_config;
@@ -715,7 +721,7 @@ static client createClient(char *cmd, size_t len, client from, int thread_id) {
715721
c->prefix_pending++;
716722
}
717723

718-
if (config.cluster_mode && (config.read_from_replicas_only || config.read_from_all)) {
724+
if (config.cluster_mode && (config.read_from_replica == FROM_REPLICA_ONLY|| config.read_from_replica == FROM_ALL)) {
719725
char *buf = NULL;
720726
int len;
721727
len = redisFormatCommand(&buf, "READONLY");
@@ -850,9 +856,9 @@ static void showLatencyReport(void) {
850856
printf(" keep alive: %d\n", config.keepalive);
851857
if (config.cluster_mode) {
852858
const char *node_prefix = NULL;
853-
if (config.read_from_all) {
859+
if (config.read_from_replica == FROM_ALL) {
854860
node_prefix = "all";
855-
} else if (config.read_from_replicas_only) {
861+
} else if (config.read_from_replica == FROM_REPLICA_ONLY) {
856862
node_prefix = "replica";
857863
} else {
858864
node_prefix = "primary";
@@ -1076,7 +1082,6 @@ static int fetchClusterConfiguration(void) {
10761082
if (ctx == NULL) {
10771083
exit(1);
10781084
}
1079-
assert(!(config.read_from_all && config.read_from_replicas_only) && "--rfa and --rfro cannot be enabled simultaneously");
10801085

10811086
reply = redisCommand(ctx, "CLUSTER SLOTS");
10821087
if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
@@ -1094,8 +1099,8 @@ static int fetchClusterConfiguration(void) {
10941099
int to = r->element[1]->integer;
10951100
for (j = 2; j < r->elements; j++) {
10961101
int is_primary = (j == 2);
1097-
int is_cluster_option_only = (!config.read_from_all && !config.read_from_replicas_only);
1098-
if ((config.read_from_replicas_only && is_primary) || (is_cluster_option_only && !is_primary)) continue;
1102+
int is_cluster_option_only = (config.read_from_replica == FROM_PRIMARY_ONLY);
1103+
if ((config.read_from_replica == FROM_REPLICA_ONLY && is_primary) || (is_cluster_option_only && !is_primary)) continue;
10991104

11001105
redisReply *nr = r->element[j];
11011106
assert(nr->type == REDIS_REPLY_ARRAY && nr->elements >= 3);
@@ -1168,7 +1173,7 @@ static int fetchClusterSlotsConfiguration(client c) {
11681173
if (is_fetching_slots) return -1; // TODO: use other codes || errno ?
11691174
atomic_store_explicit(&config.is_fetching_slots, 1, memory_order_relaxed);
11701175
fprintf(stderr, "WARNING: Cluster slots configuration changed, fetching new one...\n");
1171-
fprintf(stderr, "If you are using the --rfa and --rfro option and sending write requests (set type commands),\nthe requests could not be processed properly.\n");
1176+
fprintf(stderr, "If you are using the --rfr option and sending write requests (set type commands),\nthe requests could not be processed properly.\n");
11721177

11731178
const char *errmsg = "Failed to update cluster slots configuration";
11741179

@@ -1208,10 +1213,10 @@ static int fetchClusterSlotsConfiguration(client c) {
12081213
from = r->element[0]->integer;
12091214
to = r->element[1]->integer;
12101215
size_t start, end;
1211-
if (config.read_from_all) {
1216+
if (config.read_from_replica == FROM_ALL) {
12121217
start = 2;
12131218
end = r->elements;
1214-
} else if (config.read_from_replicas_only) {
1219+
} else if (config.read_from_replica == FROM_REPLICA_ONLY) {
12151220
start = 3;
12161221
end = r->elements;
12171222
} else {
@@ -1402,10 +1407,11 @@ int parseOptions(int argc, char **argv) {
14021407
config.num_threads = 0;
14031408
} else if (!strcmp(argv[i], "--cluster")) {
14041409
config.cluster_mode = 1;
1405-
} else if (!strcmp(argv[i], "--rfa")) {
1406-
config.read_from_all = 1;
1407-
} else if (!strcmp(argv[i], "--rfro")) {
1408-
config.read_from_replicas_only = 1;
1410+
} else if (!strcmp(argv[i], "--rfr")) {
1411+
config.read_from_replica = FROM_REPLICA_ONLY;
1412+
if (argv[i + 1] && atoi(argv[++i]) == 2) {
1413+
config.read_from_replica = FROM_ALL;
1414+
}
14091415
} else if (!strcmp(argv[i], "--enable-tracking")) {
14101416
config.enable_tracking = 1;
14111417
} else if (!strcmp(argv[i], "--help")) {
@@ -1503,14 +1509,12 @@ int parseOptions(int argc, char **argv) {
15031509
" If the command is supplied on the command line in cluster\n"
15041510
" mode, the key must contain \"{tag}\". Otherwise, the\n"
15051511
" command will not be sent to the right cluster node.\n"
1506-
" --rfa Enable read from all nodes(primary and replica) in cluster mode.\n"
1507-
" This command must be used with the --cluster option.\n"
1508-
" When using this option, it is recommended to use only \n"
1509-
" the commands for read requests.\n"
1510-
" --rfro Enable read from replicas only in cluster mode.\n"
1512+
" --rfr <mode> Enable read from replicas in cluster mode.\n"
15111513
" This command must be used with the --cluster option.\n"
15121514
" When using this option, it is recommended to use only \n"
15131515
" the commands for read requests.\n"
1516+
" default=read from replica only\n"
1517+
" 2=read from all nodes(primary and replica)\n"
15141518
" --enable-tracking Send CLIENT TRACKING on before starting benchmark.\n"
15151519
" -k <boolean> 1=keep alive 0=reconnect (default 1)\n"
15161520
" -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD,\n"
@@ -1652,8 +1656,7 @@ int main(int argc, char **argv) {
16521656
config.num_threads = 0;
16531657
config.threads = NULL;
16541658
config.cluster_mode = 0;
1655-
config.read_from_all = 0;
1656-
config.read_from_replicas_only = 0;
1659+
config.read_from_replica = FROM_PRIMARY_ONLY;
16571660
config.cluster_node_count = 0;
16581661
config.cluster_nodes = NULL;
16591662
config.redis_config = NULL;
@@ -1699,9 +1702,9 @@ int main(int argc, char **argv) {
16991702
exit(1);
17001703
}
17011704
const char *node_prefix = NULL;
1702-
if (config.read_from_all) {
1705+
if (config.read_from_replica == FROM_ALL) {
17031706
node_prefix = "all";
1704-
} else if (config.read_from_replicas_only) {
1707+
} else if (config.read_from_replica == FROM_REPLICA_ONLY) {
17051708
node_prefix = "replica";
17061709
} else {
17071710
node_prefix = "primary";

0 commit comments

Comments
 (0)