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