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

Rebranding of code in libvalkeycluster #15

Merged
merged 7 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
124 changes: 62 additions & 62 deletions libvalkeycluster/examples/src/clientside_caching_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* messages are received via the registered push callback.
* The disconnect callback should also be used as an indication of invalidation.
*/
#include <hiredis_cluster/adapters/libevent.h>
#include <hiredis_cluster/hircluster.h>
#include <valkeycluster/adapters/libevent.h>
#include <valkeycluster/valkeycluster.h>

#include <assert.h>
#include <stdio.h>
Expand All @@ -15,108 +15,108 @@
#define CLUSTER_NODE "127.0.0.1:7000"
#define KEY "key:1"

void pushCallback(redisAsyncContext *ac, void *r);
void setCallback(redisClusterAsyncContext *acc, void *r, void *privdata);
void getCallback1(redisClusterAsyncContext *acc, void *r, void *privdata);
void getCallback2(redisClusterAsyncContext *acc, void *r, void *privdata);
void pushCallback(valkeyAsyncContext *ac, void *r);
void setCallback(valkeyClusterAsyncContext *acc, void *r, void *privdata);
void getCallback1(valkeyClusterAsyncContext *acc, void *r, void *privdata);
void getCallback2(valkeyClusterAsyncContext *acc, void *r, void *privdata);
void modifyKey(const char *key, const char *value);

/* The connect callback enables RESP3 and client tracking.
The non-const connect callback is used since we want to
set the push callback in the hiredis context. */
void connectCallbackNC(redisAsyncContext *ac, int status) {
assert(status == REDIS_OK);
redisAsyncSetPushCallback(ac, pushCallback);
redisAsyncCommand(ac, NULL, NULL, "HELLO 3");
redisAsyncCommand(ac, NULL, NULL, "CLIENT TRACKING ON");
set the push callback in the libvalkey context. */
void connectCallbackNC(valkeyAsyncContext *ac, int status) {
assert(status == VALKEY_OK);
valkeyAsyncSetPushCallback(ac, pushCallback);
valkeyAsyncCommand(ac, NULL, NULL, "HELLO 3");
valkeyAsyncCommand(ac, NULL, NULL, "CLIENT TRACKING ON");
printf("Connected to %s:%d\n", ac->c.tcp.host, ac->c.tcp.port);
}

/* The event callback issues a 'SET' command when the client is ready to accept
commands. A reply is expected via a call to 'setCallback()' */
void eventCallback(const redisClusterContext *cc, int event, void *privdata) {
void eventCallback(const valkeyClusterContext *cc, int event, void *privdata) {
(void)cc;
redisClusterAsyncContext *acc = (redisClusterAsyncContext *)privdata;
valkeyClusterAsyncContext *acc = (valkeyClusterAsyncContext *)privdata;

/* We send our commands when the client is ready to accept commands. */
if (event == HIRCLUSTER_EVENT_READY) {
if (event == VALKEYCLUSTER_EVENT_READY) {
printf("Client is ready to accept commands\n");

int status =
redisClusterAsyncCommand(acc, setCallback, NULL, "SET %s 1", KEY);
assert(status == REDIS_OK);
valkeyClusterAsyncCommand(acc, setCallback, NULL, "SET %s 1", KEY);
assert(status == VALKEY_OK);
}
}

/* Message callback for 'SET' commands. Issues a 'GET' command and a reply is
expected as a call to 'getCallback1()' */
void setCallback(redisClusterAsyncContext *acc, void *r, void *privdata) {
void setCallback(valkeyClusterAsyncContext *acc, void *r, void *privdata) {
(void)privdata;
redisReply *reply = (redisReply *)r;
valkeyReply *reply = (valkeyReply *)r;
assert(reply != NULL);
printf("Callback for 'SET', reply: %s\n", reply->str);

int status =
redisClusterAsyncCommand(acc, getCallback1, NULL, "GET %s", KEY);
assert(status == REDIS_OK);
valkeyClusterAsyncCommand(acc, getCallback1, NULL, "GET %s", KEY);
assert(status == VALKEY_OK);
}

/* Message callback for the first 'GET' command. Modifies the key to
trigger Redis to send a key invalidation message and then sends another
trigger Valkey to send a key invalidation message and then sends another
'GET' command. The invalidation message is received via the registered
push callback. */
void getCallback1(redisClusterAsyncContext *acc, void *r, void *privdata) {
void getCallback1(valkeyClusterAsyncContext *acc, void *r, void *privdata) {
(void)privdata;
redisReply *reply = (redisReply *)r;
valkeyReply *reply = (valkeyReply *)r;
assert(reply != NULL);

printf("Callback for first 'GET', reply: %s\n", reply->str);

/* Modify the key from another client which will invalidate a cached value.
Redis will send an invalidation message via a push message. */
Valkey will send an invalidation message via a push message. */
modifyKey(KEY, "99");

int status =
redisClusterAsyncCommand(acc, getCallback2, NULL, "GET %s", KEY);
assert(status == REDIS_OK);
valkeyClusterAsyncCommand(acc, getCallback2, NULL, "GET %s", KEY);
assert(status == VALKEY_OK);
}

/* Push message callback handling invalidation messages. */
void pushCallback(redisAsyncContext *ac, void *r) {
redisReply *reply = r;
if (!(reply->type == REDIS_REPLY_PUSH && reply->elements == 2 &&
reply->element[0]->type == REDIS_REPLY_STRING &&
void pushCallback(valkeyAsyncContext *ac, void *r) {
valkeyReply *reply = r;
if (!(reply->type == VALKEY_REPLY_PUSH && reply->elements == 2 &&
reply->element[0]->type == VALKEY_REPLY_STRING &&
!strncmp(reply->element[0]->str, "invalidate", 10) &&
reply->element[1]->type == REDIS_REPLY_ARRAY)) {
reply->element[1]->type == VALKEY_REPLY_ARRAY)) {
/* Not an 'invalidate' message. Ignore. */
return;
}
redisReply *payload = reply->element[1];
valkeyReply *payload = reply->element[1];
size_t i;
for (i = 0; i < payload->elements; i++) {
redisReply *key = payload->element[i];
if (key->type == REDIS_REPLY_STRING)
valkeyReply *key = payload->element[i];
if (key->type == VALKEY_REPLY_STRING)
printf("Invalidate key '%.*s'\n", (int)key->len, key->str);
else if (key->type == REDIS_REPLY_NIL)
else if (key->type == VALKEY_REPLY_NIL)
printf("Invalidate all\n");
}
}

/* Message callback for 'GET' commands. Exits program. */
void getCallback2(redisClusterAsyncContext *acc, void *r, void *privdata) {
void getCallback2(valkeyClusterAsyncContext *acc, void *r, void *privdata) {
(void)privdata;
redisReply *reply = (redisReply *)r;
valkeyReply *reply = (valkeyReply *)r;
assert(reply != NULL);

printf("Callback for second 'GET', reply: %s\n", reply->str);

/* Exit the eventloop after a couple of sent commands. */
redisClusterAsyncDisconnect(acc);
valkeyClusterAsyncDisconnect(acc);
}

/* A disconnect callback should invalidate all cached keys. */
void disconnectCallback(const redisAsyncContext *ac, int status) {
assert(status == REDIS_OK);
void disconnectCallback(const valkeyAsyncContext *ac, int status) {
assert(status == VALKEY_OK);
printf("Disconnected from %s:%d\n", ac->c.tcp.host, ac->c.tcp.port);

printf("Invalidate all\n");
Expand All @@ -125,43 +125,43 @@ void disconnectCallback(const redisAsyncContext *ac, int status) {
/* Helper to modify keys using a separate client. */
void modifyKey(const char *key, const char *value) {
printf("Modify key: '%s'\n", key);
redisClusterContext *cc = redisClusterContextInit();
int status = redisClusterSetOptionAddNodes(cc, CLUSTER_NODE);
assert(status == REDIS_OK);
status = redisClusterConnect2(cc);
assert(status == REDIS_OK);
valkeyClusterContext *cc = valkeyClusterContextInit();
int status = valkeyClusterSetOptionAddNodes(cc, CLUSTER_NODE);
assert(status == VALKEY_OK);
status = valkeyClusterConnect2(cc);
assert(status == VALKEY_OK);

redisReply *reply = redisClusterCommand(cc, "SET %s %s", key, value);
valkeyReply *reply = valkeyClusterCommand(cc, "SET %s %s", key, value);
assert(reply != NULL);
freeReplyObject(reply);

redisClusterFree(cc);
valkeyClusterFree(cc);
}

int main(int argc, char **argv) {
redisClusterAsyncContext *acc = redisClusterAsyncContextInit();
valkeyClusterAsyncContext *acc = valkeyClusterAsyncContextInit();
assert(acc);

int status;
status = redisClusterAsyncSetConnectCallbackNC(acc, connectCallbackNC);
assert(status == REDIS_OK);
status = redisClusterAsyncSetDisconnectCallback(acc, disconnectCallback);
assert(status == REDIS_OK);
status = redisClusterSetEventCallback(acc->cc, eventCallback, acc);
assert(status == REDIS_OK);
status = redisClusterSetOptionAddNodes(acc->cc, CLUSTER_NODE);
assert(status == REDIS_OK);
status = valkeyClusterAsyncSetConnectCallbackNC(acc, connectCallbackNC);
assert(status == VALKEY_OK);
status = valkeyClusterAsyncSetDisconnectCallback(acc, disconnectCallback);
assert(status == VALKEY_OK);
status = valkeyClusterSetEventCallback(acc->cc, eventCallback, acc);
assert(status == VALKEY_OK);
status = valkeyClusterSetOptionAddNodes(acc->cc, CLUSTER_NODE);
assert(status == VALKEY_OK);

struct event_base *base = event_base_new();
status = redisClusterLibeventAttach(acc, base);
assert(status == REDIS_OK);
status = valkeyClusterLibeventAttach(acc, base);
assert(status == VALKEY_OK);

status = redisClusterAsyncConnect2(acc);
assert(status == REDIS_OK);
status = valkeyClusterAsyncConnect2(acc);
assert(status == VALKEY_OK);

event_base_dispatch(base);

redisClusterAsyncFree(acc);
valkeyClusterAsyncFree(acc);
event_base_free(base);
return 0;
}
21 changes: 11 additions & 10 deletions libvalkeycluster/examples/src/example.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
#include <hiredis_cluster/hircluster.h>
#include <stdio.h>
#include <stdlib.h>
#include <valkeycluster/valkeycluster.h>

int main(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);
struct timeval timeout = {1, 500000}; // 1.5s

redisClusterContext *cc = redisClusterContextInit();
redisClusterSetOptionAddNodes(cc, "127.0.0.1:7000");
redisClusterSetOptionConnectTimeout(cc, timeout);
redisClusterSetOptionRouteUseSlots(cc);
redisClusterConnect2(cc);
valkeyClusterContext *cc = valkeyClusterContextInit();
valkeyClusterSetOptionAddNodes(cc, "127.0.0.1:7000");
valkeyClusterSetOptionConnectTimeout(cc, timeout);
valkeyClusterSetOptionRouteUseSlots(cc);
valkeyClusterConnect2(cc);
if (cc && cc->err) {
printf("Error: %s\n", cc->errstr);
// handle error
exit(-1);
}

redisReply *reply =
(redisReply *)redisClusterCommand(cc, "SET %s %s", "key", "value");
valkeyReply *reply =
(valkeyReply *)valkeyClusterCommand(cc, "SET %s %s", "key", "value");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);

redisReply *reply2 = (redisReply *)redisClusterCommand(cc, "GET %s", "key");
valkeyReply *reply2 =
(valkeyReply *)valkeyClusterCommand(cc, "GET %s", "key");
printf("GET: %s\n", reply2->str);
freeReplyObject(reply2);

redisClusterFree(cc);
valkeyClusterFree(cc);
return 0;
}
58 changes: 29 additions & 29 deletions libvalkeycluster/examples/src/example_async.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <hiredis_cluster/adapters/libevent.h>
#include <hiredis_cluster/hircluster.h>
#include <stdio.h>
#include <stdlib.h>
#include <valkeycluster/adapters/libevent.h>
#include <valkeycluster/valkeycluster.h>

void getCallback(redisClusterAsyncContext *cc, void *r, void *privdata) {
redisReply *reply = (redisReply *)r;
void getCallback(valkeyClusterAsyncContext *cc, void *r, void *privdata) {
valkeyReply *reply = (valkeyReply *)r;
if (reply == NULL) {
if (cc->errstr) {
printf("errstr: %s\n", cc->errstr);
Expand All @@ -14,11 +14,11 @@ void getCallback(redisClusterAsyncContext *cc, void *r, void *privdata) {
printf("privdata: %s reply: %s\n", (char *)privdata, reply->str);

/* Disconnect after receiving the reply to GET */
redisClusterAsyncDisconnect(cc);
valkeyClusterAsyncDisconnect(cc);
}

void setCallback(redisClusterAsyncContext *cc, void *r, void *privdata) {
redisReply *reply = (redisReply *)r;
void setCallback(valkeyClusterAsyncContext *cc, void *r, void *privdata) {
valkeyReply *reply = (valkeyReply *)r;
if (reply == NULL) {
if (cc->errstr) {
printf("errstr: %s\n", cc->errstr);
Expand All @@ -28,16 +28,16 @@ void setCallback(redisClusterAsyncContext *cc, void *r, void *privdata) {
printf("privdata: %s reply: %s\n", (char *)privdata, reply->str);
}

void connectCallback(const redisAsyncContext *ac, int status) {
if (status != REDIS_OK) {
void connectCallback(const valkeyAsyncContext *ac, int status) {
if (status != VALKEY_OK) {
printf("Error: %s\n", ac->errstr);
return;
}
printf("Connected to %s:%d\n", ac->c.tcp.host, ac->c.tcp.port);
}

void disconnectCallback(const redisAsyncContext *ac, int status) {
if (status != REDIS_OK) {
void disconnectCallback(const valkeyAsyncContext *ac, int status) {
if (status != VALKEY_OK) {
printf("Error: %s\n", ac->errstr);
return;
}
Expand All @@ -46,48 +46,48 @@ void disconnectCallback(const redisAsyncContext *ac, int status) {

int main(int argc, char **argv) {
printf("Connecting...\n");
redisClusterAsyncContext *cc =
redisClusterAsyncConnect("127.0.0.1:7000", HIRCLUSTER_FLAG_NULL);
valkeyClusterAsyncContext *cc =
valkeyClusterAsyncConnect("127.0.0.1:7000", VALKEYCLUSTER_FLAG_NULL);
if (cc && cc->err) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just 0? Can we rename this to VALKEYCLUSTER_FLAG_NONE or just use 0? "Null" sticks out to me.

Copy link
Collaborator Author

@bjosv bjosv Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to replace the flags used in the Cluster API with a new valkeyClusterOptions,
like its done in standalone:

valkeyAsyncContext *valkeyAsyncConnectWithOptions(const valkeyOptions *options);

Then we could set any option with that API, and possibly keep the simpler one for easy usage.

valkeyClusterAsyncContext *valkeyClusterAsyncConnectWithOptions(const valkeyClusterOptions *options);
valkeyClusterAsyncContext *valkeyClusterAsyncConnect(const char *addrs);

With this change the cluster api would look the same as the standalone.

printf("Error: %s\n", cc->errstr);
return 1;
}

struct event_base *base = event_base_new();
redisClusterLibeventAttach(cc, base);
redisClusterAsyncSetConnectCallback(cc, connectCallback);
redisClusterAsyncSetDisconnectCallback(cc, disconnectCallback);
valkeyClusterLibeventAttach(cc, base);
valkeyClusterAsyncSetConnectCallback(cc, connectCallback);
valkeyClusterAsyncSetDisconnectCallback(cc, disconnectCallback);

int status;
status = redisClusterAsyncCommand(cc, setCallback, (char *)"THE_ID",
"SET %s %s", "key", "value");
if (status != REDIS_OK) {
status = valkeyClusterAsyncCommand(cc, setCallback, (char *)"THE_ID",
"SET %s %s", "key", "value");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
}

status = redisClusterAsyncCommand(cc, getCallback, (char *)"THE_ID",
"GET %s", "key");
if (status != REDIS_OK) {
status = valkeyClusterAsyncCommand(cc, getCallback, (char *)"THE_ID",
"GET %s", "key");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
}

status = redisClusterAsyncCommand(cc, setCallback, (char *)"THE_ID",
"SET %s %s", "key2", "value2");
if (status != REDIS_OK) {
status = valkeyClusterAsyncCommand(cc, setCallback, (char *)"THE_ID",
"SET %s %s", "key2", "value2");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
}

status = redisClusterAsyncCommand(cc, getCallback, (char *)"THE_ID",
"GET %s", "key2");
if (status != REDIS_OK) {
status = valkeyClusterAsyncCommand(cc, getCallback, (char *)"THE_ID",
"GET %s", "key2");
if (status != VALKEY_OK) {
printf("error: err=%d errstr=%s\n", cc->err, cc->errstr);
}

printf("Dispatch..\n");
event_base_dispatch(base);

printf("Done..\n");
redisClusterAsyncFree(cc);
valkeyClusterAsyncFree(cc);
event_base_free(base);
return 0;
}
Loading
Loading