aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/modules/hellocluster.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/src/modules/hellocluster.c')
-rw-r--r--examples/redis-unstable/src/modules/hellocluster.c98
1 files changed, 0 insertions, 98 deletions
diff --git a/examples/redis-unstable/src/modules/hellocluster.c b/examples/redis-unstable/src/modules/hellocluster.c
deleted file mode 100644
index 38d7e1f..0000000
--- a/examples/redis-unstable/src/modules/hellocluster.c
+++ /dev/null
@@ -1,98 +0,0 @@
1/* Helloworld cluster -- A ping/pong cluster API example.
2 *
3 * -----------------------------------------------------------------------------
4 *
5 * Copyright (c) 2018-Present, Redis Ltd.
6 * All rights reserved.
7 *
8 * Licensed under your choice of (a) the Redis Source Available License 2.0
9 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
10 * GNU Affero General Public License v3 (AGPLv3).
11 */
12
13#include "../redismodule.h"
14#include <stdio.h>
15#include <stdlib.h>
16#include <ctype.h>
17#include <string.h>
18
19#define MSGTYPE_PING 1
20#define MSGTYPE_PONG 2
21
22/* HELLOCLUSTER.PINGALL */
23int PingallCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
24 REDISMODULE_NOT_USED(argv);
25 REDISMODULE_NOT_USED(argc);
26
27 RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PING,"Hey",3);
28 return RedisModule_ReplyWithSimpleString(ctx, "OK");
29}
30
31/* HELLOCLUSTER.LIST */
32int ListCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
33 REDISMODULE_NOT_USED(argv);
34 REDISMODULE_NOT_USED(argc);
35
36 size_t numnodes;
37 char **ids = RedisModule_GetClusterNodesList(ctx,&numnodes);
38 if (ids == NULL) {
39 return RedisModule_ReplyWithError(ctx,"Cluster not enabled");
40 }
41
42 RedisModule_ReplyWithArray(ctx,numnodes);
43 for (size_t j = 0; j < numnodes; j++) {
44 int port;
45 RedisModule_GetClusterNodeInfo(ctx,ids[j],NULL,NULL,&port,NULL);
46 RedisModule_ReplyWithArray(ctx,2);
47 RedisModule_ReplyWithStringBuffer(ctx,ids[j],REDISMODULE_NODE_ID_LEN);
48 RedisModule_ReplyWithLongLong(ctx,port);
49 }
50 RedisModule_FreeClusterNodesList(ids);
51 return REDISMODULE_OK;
52}
53
54/* Callback for message MSGTYPE_PING */
55void PingReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
56 RedisModule_Log(ctx,"notice","PING (type %d) RECEIVED from %.*s: '%.*s'",
57 type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
58 RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PONG,"Ohi!",4);
59 RedisModuleCallReply *reply = RedisModule_Call(ctx, "INCR", "c", "pings_received");
60 RedisModule_FreeCallReply(reply);
61}
62
63/* Callback for message MSGTYPE_PONG. */
64void PongReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
65 RedisModule_Log(ctx,"notice","PONG (type %d) RECEIVED from %.*s: '%.*s'",
66 type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
67}
68
69/* This function must be present on each Redis module. It is used in order to
70 * register the commands into the Redis server. */
71int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
72 REDISMODULE_NOT_USED(argv);
73 REDISMODULE_NOT_USED(argc);
74
75 if (RedisModule_Init(ctx,"hellocluster",1,REDISMODULE_APIVER_1)
76 == REDISMODULE_ERR) return REDISMODULE_ERR;
77
78 if (RedisModule_CreateCommand(ctx,"hellocluster.pingall",
79 PingallCommand_RedisCommand,"readonly",0,0,0) == REDISMODULE_ERR)
80 return REDISMODULE_ERR;
81
82 if (RedisModule_CreateCommand(ctx,"hellocluster.list",
83 ListCommand_RedisCommand,"readonly",0,0,0) == REDISMODULE_ERR)
84 return REDISMODULE_ERR;
85
86 /* Disable Redis Cluster sharding and redirections. This way every node
87 * will be able to access every possible key, regardless of the hash slot.
88 * This way the PING message handler will be able to increment a specific
89 * variable. Normally you do that in order for the distributed system
90 * you create as a module to have total freedom in the keyspace
91 * manipulation. */
92 RedisModule_SetClusterFlags(ctx,REDISMODULE_CLUSTER_FLAG_NO_REDIRECTION);
93
94 /* Register our handlers for different message types. */
95 RedisModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PING,PingReceiver);
96 RedisModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PONG,PongReceiver);
97 return REDISMODULE_OK;
98}