1 /* Helloworld cluster -- A ping/pong cluster API example.
2 *
3 * -----------------------------------------------------------------------------
4 *
5 * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Redis nor the names of its contributors may be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #define REDISMODULE_EXPERIMENTAL_API
34 #include "../redismodule.h"
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <string.h>
39
40 #define MSGTYPE_PING 1
41 #define MSGTYPE_PONG 2
42
43 /* HELLOCLUSTER.PINGALL */
PingallCommand_RedisCommand(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)44 int PingallCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
45 REDISMODULE_NOT_USED(argv);
46 REDISMODULE_NOT_USED(argc);
47
48 RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PING,(unsigned char*)"Hey",3);
49 return RedisModule_ReplyWithSimpleString(ctx, "OK");
50 }
51
52 /* HELLOCLUSTER.LIST */
ListCommand_RedisCommand(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)53 int ListCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
54 REDISMODULE_NOT_USED(argv);
55 REDISMODULE_NOT_USED(argc);
56
57 size_t numnodes;
58 char **ids = RedisModule_GetClusterNodesList(ctx,&numnodes);
59 if (ids == NULL) {
60 return RedisModule_ReplyWithError(ctx,"Cluster not enabled");
61 }
62
63 RedisModule_ReplyWithArray(ctx,numnodes);
64 for (size_t j = 0; j < numnodes; j++) {
65 int port;
66 RedisModule_GetClusterNodeInfo(ctx,ids[j],NULL,NULL,&port,NULL);
67 RedisModule_ReplyWithArray(ctx,2);
68 RedisModule_ReplyWithStringBuffer(ctx,ids[j],REDISMODULE_NODE_ID_LEN);
69 RedisModule_ReplyWithLongLong(ctx,port);
70 }
71 RedisModule_FreeClusterNodesList(ids);
72 return REDISMODULE_OK;
73 }
74
75 /* Callback for message MSGTYPE_PING */
PingReceiver(RedisModuleCtx * ctx,const char * sender_id,uint8_t type,const unsigned char * payload,uint32_t len)76 void PingReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
77 RedisModule_Log(ctx,"notice","PING (type %d) RECEIVED from %.*s: '%.*s'",
78 type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
79 RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PONG,(unsigned char*)"Ohi!",4);
80 RedisModule_Call(ctx, "INCR", "c", "pings_received");
81 }
82
83 /* Callback for message MSGTYPE_PONG. */
PongReceiver(RedisModuleCtx * ctx,const char * sender_id,uint8_t type,const unsigned char * payload,uint32_t len)84 void PongReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {
85 RedisModule_Log(ctx,"notice","PONG (type %d) RECEIVED from %.*s: '%.*s'",
86 type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);
87 }
88
89 /* This function must be present on each Redis module. It is used in order to
90 * register the commands into the Redis server. */
RedisModule_OnLoad(RedisModuleCtx * ctx,RedisModuleString ** argv,int argc)91 int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
92 REDISMODULE_NOT_USED(argv);
93 REDISMODULE_NOT_USED(argc);
94
95 if (RedisModule_Init(ctx,"hellocluster",1,REDISMODULE_APIVER_1)
96 == REDISMODULE_ERR) return REDISMODULE_ERR;
97
98 if (RedisModule_CreateCommand(ctx,"hellocluster.pingall",
99 PingallCommand_RedisCommand,"readonly",0,0,0) == REDISMODULE_ERR)
100 return REDISMODULE_ERR;
101
102 if (RedisModule_CreateCommand(ctx,"hellocluster.list",
103 ListCommand_RedisCommand,"readonly",0,0,0) == REDISMODULE_ERR)
104 return REDISMODULE_ERR;
105
106 /* Disable Redis Cluster sharding and redirections. This way every node
107 * will be able to access every possible key, regardless of the hash slot.
108 * This way the PING message handler will be able to increment a specific
109 * variable. Normally you do that in order for the distributed system
110 * you create as a module to have total freedom in the keyspace
111 * manipulation. */
112 RedisModule_SetClusterFlags(ctx,REDISMODULE_CLUSTER_FLAG_NO_REDIRECTION);
113
114 /* Register our handlers for different message types. */
115 RedisModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PING,PingReceiver);
116 RedisModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PONG,PongReceiver);
117 return REDISMODULE_OK;
118 }
119