Debian Codegraph @

src/modules/ndb_redis/README

raw
1 NDB_REDIS Module
2
3 Daniel-Constantin Mierla
4
5 <[email protected]>
6
7 Edited by
8
9 Daniel-Constantin Mierla
10
11 <[email protected]>
12
13 Vicente Hernando
14
15 <[email protected]>
16
17 Morten Isaksen
18
19 <[email protected]>
20
21 Carsten Bock
22
23 <[email protected]>
24
25 Joel Centelles Martin
26
27 <[email protected]>
28
29 Copyright © 2011 asipto.com
30
31 Copyright © 2012 www.systemonenoc.com
32
33 Copyright © 2017 ng-voice GmbH
34 __________________________________________________________________
35
36 Table of Contents
37
38 1. Admin Guide
39
40 1. Overview
41 2. Dependencies
42
43 2.1. Kamailio Modules
44 2.2. External Libraries or Applications
45
46 3. Parameters
47
48 3.1. server (str)
49 3.2. init_without_redis (integer)
50 3.3. connect_timeout (int)
51 3.4. cmd_timeout (int)
52 3.5. cluster (integer)
53 3.6. allowed_timeouts (integer)
54 3.7. disable_time (integer)
55 3.8. flush_on_reconnect (integer)
56 3.9. allow_dynamic_nodes (integer)
57 3.10. debug (integer)
58 3.11. ca_path (string)
59
60 4. Functions
61
62 4.1. redis_cmd(srvname, command, ..., replyid)
63 4.2. redis_pipe_cmd(srvname, command, ..., replyid)
64 4.3. redis_execute(srvname)
65 4.4. redis_free(replyid)
66
67 List of Examples
68
69 1.1. Set server parameter
70 1.2. Set init_without_redis parameter
71 1.3. Set connect_timeout parameter
72 1.4. Set cmd_timeout parameter
73 1.5. Set cluster parameter
74 1.6. Set allowed_timeouts parameter
75 1.7. Set disable_time parameter
76 1.8. Set flush_on_reconnect parameter
77 1.9. Set allow_dynamic_nodes parameter
78 1.10. Set debug parameter
79 1.11. Setting CA path
80 1.12. redis_cmd usage
81 1.13. redis_execute usage
82 1.14. redis_free usage
83
84 Chapter 1. Admin Guide
85
86 Table of Contents
87
88 1. Overview
89 2. Dependencies
90
91 2.1. Kamailio Modules
92 2.2. External Libraries or Applications
93
94 3. Parameters
95
96 3.1. server (str)
97 3.2. init_without_redis (integer)
98 3.3. connect_timeout (int)
99 3.4. cmd_timeout (int)
100 3.5. cluster (integer)
101 3.6. allowed_timeouts (integer)
102 3.7. disable_time (integer)
103 3.8. flush_on_reconnect (integer)
104 3.9. allow_dynamic_nodes (integer)
105 3.10. debug (integer)
106 3.11. ca_path (string)
107
108 4. Functions
109
110 4.1. redis_cmd(srvname, command, ..., replyid)
111 4.2. redis_pipe_cmd(srvname, command, ..., replyid)
112 4.3. redis_execute(srvname)
113 4.4. redis_free(replyid)
114
115 1. Overview
116
117 This module provides a connector to interact with REDIS NoSQL Database
118 from configuration file. You can read more about REDIS at
119 http://redis.io.
120
121 It can connect to many REDIS servers and store the results in different
122 containers.
123
124 2. Dependencies
125
126 2.1. Kamailio Modules
127 2.2. External Libraries or Applications
128
129 2.1. Kamailio Modules
130
131 The following modules must be loaded before this module:
132 * none.
133
134 2.2. External Libraries or Applications
135
136 The following libraries or applications must be installed before
137 running Kamailio with this module loaded:
138 * hiredis - available at https://github.com/redis/hiredis.
139
140 3. Parameters
141
142 3.1. server (str)
143 3.2. init_without_redis (integer)
144 3.3. connect_timeout (int)
145 3.4. cmd_timeout (int)
146 3.5. cluster (integer)
147 3.6. allowed_timeouts (integer)
148 3.7. disable_time (integer)
149 3.8. flush_on_reconnect (integer)
150 3.9. allow_dynamic_nodes (integer)
151 3.10. debug (integer)
152 3.11. ca_path (string)
153
154 3.1. server (str)
155
156 Specify the details to connect to REDIS server. It takes a list of
157 attribute=value separated by semicolon, the attributes can be:
158 * name - is a generic identifier to be used with module functions.
159 * unix - is the path to the unix domain socket to connect to REDIS
160 server.
161 * addr - is the IP address to connect to REDIS server.
162 * port - is the port to connect to REDIS server.
163 * db - is the DB number to use (defaults to 0 if not specified).
164 * pass - is the server password.
165 * tls - is to enable TLS connectivity.
166 * connect_timeout - is to set connect timeout to REDIS server.
167 * command_timeout - is to set command timeout to REDIS server.
168
169 Thr unix and (addr, port) are mutually exclusive, if both appear in
170 same server settings unix domain socket is configured.
171
172 You can set this parameter many times, in case you want to connect to
173 many REDIS servers, just give different attributes and use the specific
174 server name when querying the REDIS instance.
175
176 If tls is enabled, the module will validate the REDIS server
177 certificate against the ca_path. There is currently no way to connect
178 with a specified client certificate, the corresponding configuration to
179 check client certificates in the REDIS server must therefore be turned
180 off.
181
182 Default value is NULL.
183
184 Example 1.1. Set server parameter
185 ...
186 modparam("ndb_redis", "server", "name=srvN;addr=127.0.0.1;port=6379;db=1")
187 modparam("ndb_redis", "server", "name=srvX;addr=127.0.0.2;port=6379;db=4;"
188 "pass=mypassword")
189 modparam("ndb_redis", "server", "name=srvY;addr=127.0.0.3;port=6379;db=5;"
190 "pass=mypassword;tls=1")
191
192 # Unix domain socket
193 modparam("ndb_redis", "server", "name=srvY;unix=/tmp/redis.sock;db=3")
194
195 # sentinel (for a redis slave)
196 modparam("ndb_redis", "server", "name=srvZ;sentinel_group=group_name;"
197 "sentinel_master=0;sentinel=1.2.3.4:26379;sentinel=1.2.3.5:26379")
198
199 # sentinel (for a redis master)
200 modparam("ndb_redis", "server", "name=srvZ;sentinel_group=group_name;"
201 "sentinel_master=1;sentinel=1.2.3.4:26379;sentinel=1.2.3.5:26379")
202 ...
203
204 3.2. init_without_redis (integer)
205
206 If set to 1, the module will correctly initialize even if redis is not
207 available at start up.
208
209 Default value is “0”.
210
211 Example 1.2. Set init_without_redis parameter
212 ...
213 modparam("ndb_redis", "init_without_redis", 1)
214 ...
215
216 3.3. connect_timeout (int)
217
218 The timeout when connecting to the redis server
219
220 Default value is 1000 ms.
221
222 Example 1.3. Set connect_timeout parameter
223 ...
224 modparam("ndb_redis", "connect_timeout", 500)
225 ...
226
227 3.4. cmd_timeout (int)
228
229 The timeout for each query to the redis server. If the redis server
230 does not reply within the timeout value, the command will fail and
231 kamailio will continue executing the cfg file.
232
233 Default value is 1000 ms.
234
235 Example 1.4. Set cmd_timeout parameter
236 ...
237 modparam("ndb_redis", "cmd_timeout", 500)
238 ...
239
240 3.5. cluster (integer)
241
242 If set to 1, the module will connect to servers indicated in the
243 "MOVED" reply.
244
245 The module needs to know all existing REDIS-Nodes at startup. The nodes
246 are searched by the name "ip:port", e.g. if REDIS replies with "MOVED
247 127.0.0.1:4711", ndb_redis needs to know the databases
248 "127.0.0.1:4711".
249
250 Default value is “0” (disabled).
251
252 Example 1.5. Set cluster parameter
253 ...
254 modparam("ndb_redis", "server", "name=127.0.0.1:26001;addr=127.0.0.1;port=26001"
255 )
256 modparam("ndb_redis", "server", "name=127.0.0.1:26004;addr=127.0.0.1;port=26004"
257 )
258 modparam("ndb_redis", "server", "name=127.0.0.1:26008;addr=127.0.0.1;port=26008"
259 )
260 ...
261 modparam("ndb_redis", "cluster", 1)
262 ...
263
264 3.6. allowed_timeouts (integer)
265
266 If this is set to a non-negative value, it sets the number of
267 consecutive REDIS commands that can fail before temporarily disabling
268 the REDIS server. This is similar to rtpengine_disable_tout parameter
269 from the rtpengine module.
270
271 When communicating with a REDIS server, if redis_cmd or redis_execute
272 will fail for more than “allowed_timeouts” consecutive times, the
273 server will be temporary disabled for a number of seconds configured by
274 the “disable_time” parameter.
275
276 Disabling a server means that further redis_cmd and redis_execute
277 commands will not do anything and return a negative value “-2”.
278 Messages are also logged when disabling and re-enabling a server.
279
280 The number of consecutive fails are counted by each Kamailio process,
281 so when disabling a server this is done just for that process, not
282 globally.
283
284 Default value is “-1” (disabled).
285
286 Example 1.6. Set allowed_timeouts parameter
287 ...
288 modparam("ndb_redis", "allowed_timeouts", 3)
289 ...
290
291 3.7. disable_time (integer)
292
293 If allowed_timeouts is set to a non negative value this determines the
294 number of seconds the REDIS server will be disabled
295
296 Default value is “0”.
297
298 Example 1.7. Set disable_time parameter
299 ...
300 modparam("ndb_redis", "allowed_timeouts", 0)
301 modparam("ndb_redis", "disable_time", 30)
302 ...
303
304 3.8. flush_on_reconnect (integer)
305
306 If this is set to a non zero value, a "FLUSHALL" command is issued
307 after reconnecting to a REDIS server, to clear the entire database.
308
309 When a command to a REDIS server fails, a reconnection to that server
310 is made, so with this parameter each failed command will result in a
311 flush of the database.
312
313 This is useful in scenarios when a REDIS server does not respond to
314 commands, but the commands might have been issued, and the responses
315 lost. If this leaves the data in the db in an uncertain state, a flush
316 might fix any issues that may occur.
317
318 Default value is “0” (disabled).
319
320 Example 1.8. Set flush_on_reconnect parameter
321 ...
322 modparam("ndb_redis", "flush_on_reconnect", 1)
323 ...
324
325 3.9. allow_dynamic_nodes (integer)
326
327 If set to 1, the module will connect to servers indicated in the
328 "MOVED" reply, even if they are not specified at startup.
329
330 When “cluster” is enabled the module supports redirections ("MOVED")
331 replies. Set “allow_dynamic_nodes” to 1 to avoid listing all the nodes
332 at startup. You can only list one node, e.g. by using a DNS name for
333 the cluster instead of an IP address. The module will add dynamically
334 the new nodes as the MOVED replies are received. Only works if
335 “cluster” is set to 1.
336
337 Default value is “0” (disabled).
338
339 Example 1.9. Set allow_dynamic_nodes parameter
340 ...
341 modparam("ndb_redis", "server", "name=redis_cluster;addr=127.0.0.1;port=26008")
342 ...
343 modparam("ndb_redis", "cluster", 1)
344 modparam("ndb_redis", "allow_dynamic_nodes", 1)
345 ...
346
347 3.10. debug (integer)
348
349 Set the verbosity level for some of the log messages. It has to be a
350 log level value.
351
352 Default value is “3” (L_DBG).
353
354 Example 1.10. Set debug parameter
355 ...
356 modparam("ndb_redis", "debug", 1)
357 ...
358
359 3.11. ca_path (string)
360
361 Sets the path where Certificates Authorities certs for the REDIS server
362 certificate are stored.
363
364 Default value: "" (empty).
365
366 Example 1.11. Setting CA path
367 ...
368 modparam("db_redis", "ca_path", "/etc/ssl/certs")
369 ...
370
371 4. Functions
372
373 4.1. redis_cmd(srvname, command, ..., replyid)
374 4.2. redis_pipe_cmd(srvname, command, ..., replyid)
375 4.3. redis_execute(srvname)
376 4.4. redis_free(replyid)
377
378 4.1. redis_cmd(srvname, command, ..., replyid)
379
380 Send a command to REDIS server identified by srvname. The reply will be
381 stored in a local container identified by replyid. All the parameters
382 can be strings with pseudo-variables that are evaluated at runtime.
383
384 Minimum required arguments are srvname, command and replyid. Command
385 argument can be separated into several ones using %s token. (See
386 examples) Total number of arguments cannot exceed six.
387
388 The reply can be accessed via pseudo-variable $redis(key). The key can
389 be: type - type of the reply (as in hiredis.h); value - the value
390 returned by REDIS server; info - in case of error from REDIS, it will
391 contain an info message.
392
393 If reply type is an array (as in hiredis.h), there are other keys
394 available:
395 * size - returns number of elements in the array.
396 * type[n] - returns the type of the nth element in the array. type -
397 returns array type.
398 * value[n] - returns value of the nth element. value - returns null
399 for an array. You need to get each element by index.
400
401 In case one of the members of the array is also an array (for example
402 calling SMEMBERS in a MULTI/EXEC transaction), the members of the array
403 can be accessed by adding any of the above keys, after a value[n] key.
404 The first value[n] references the element in the first array, while the
405 next key references an element of the referenced array.
406
407 The result type can be compared with $redisd(key) variable to test its
408 value. The key can be: rpl_str, rpl_arr, rpl_int, rpl_err, rpl_sts,
409 rpl_nil.
410
411 Example 1.12. redis_cmd usage
412 ...
413 if(redis_cmd("srvN", "INCR cnt", "r")) {
414 # success - the incremented value is in $redis(r=>value)
415 xlog("===== $redis(r=>type) * $redis(r=>value)\n");
416 }
417
418 # set a value
419 redis_cmd("srvN", "SET foo bar", "r");
420
421 redis_cmd("srvN", "SET ruri $ru", "r");
422
423 # get a value
424 redis_cmd("srvN", "GET foo", "r");
425
426 # same command separated into two arguments:
427 redis_cmd("srvN", "GET %s", "foo", "r");
428
429 # if we have a key with spaces within it:
430 redis_cmd("srvN", "GET %s", "foo bar", "r");
431
432 # several values substitution:
433 redis_cmd("srvN", "HMGET %s %s %s", "key1", "field1", "field2", "r");
434
435
436 # array example
437 if(redis_cmd("srvN", "HMGET foo_key field1 field3", "r")) {
438 xlog("array size: $redis(r=>size)\n");
439 xlog("first values: $redis(r=>value[0]) , $redis(r=>value[1])\n");
440 }
441
442
443 # array as element of an array example
444 redis_cmd("srvN", "MULTI", "r1");
445 redis_cmd("srvN", "SMEMBERS foo", "r2");
446 if (redis_cmd("srvN", "EXEC", "r")) {
447 xlog("array size: $redis(r=>value[0]=>size)\n");
448 xlog("first member of the set:$redis(r=>value[0]=>value[0])\n");
449 xlog("type of the second member of the set:$redis(r=>value[0]=>type[1])\n");
450 }
451 ...
452
453 4.2. redis_pipe_cmd(srvname, command, ..., replyid)
454
455 Add a command to be sent to REDIS server identified by srvname. All the
456 commands will be stored in a buffer until a call to redis_execute is
457 made. When calling redis_execute the stored commands are sent using the
458 pipelining functionality of redis. The replies will be stored in local
459 containers identified by the replyid of each added command. All the
460 parameters can be strings with pseudo-variables that are evaluated at
461 runtime.
462
463 This command is similar in syntax with redis_cmd, the only difference
464 is that it does not send the command but instead appends it to a
465 buffer.
466
467 See examples from redis_execute.
468
469 Note: Pipelining feature is incompatible with the clustering feature.
470 If cluster parameter is set to 1, this function will log an error and
471 do nothing.
472
473 4.3. redis_execute(srvname)
474
475 Sends commands to REDIS server identified by srvname. Commands are
476 added with redis_pipe_cmd function, and will be stored for an existing
477 REDIS server. When this function is called all the commands will be
478 sent in a single message to the REDIS server.
479
480 When using redis_cmd together with redis_pipe_cmd it is recommended
481 that a call to redis_execute is made before calling redis_cmd in case
482 there are pipelined commands, otherwise when calling redis_cmd, if
483 pipelined messages exist, a call to redis_execute is made automatically
484 and a warning message is logged.
485
486 Note: Pipelining feature is incompatible with the clustering feature.
487 If cluster parameter is set to 1, this function will log an error and
488 do nothing.
489
490 Example 1.13. redis_execute usage
491 ...
492 After several redis command calls:
493 redis_pipe_cmd("srvA", "SET foo bar", "r1");
494
495 redis_pipe_cmd("srvB", "SET ruri $ru", "r2");
496
497 redis_pipe_cmd("srvB", "GET foo", "r3");
498
499 Send the data and retrieve the results:
500 redis_execute("srvA"); //send command to srvA and wait for reply. Store
501 the reply in r1
502
503 redis_execute("srvB"); //send command to srvA and wait for reply. Store
504 the replies in r2 (for SET ruri $ru) and r3 (for GET foo)
505
506 Using both redis_cmd and redis_pipe_cmd:
507 redis_pipe_cmd("srvA", "SET foo bar", "r1");
508
509 redis_pipe_cmd("srvA", "SET ruri $ru", "r2");
510
511 redis_execute("srvA"); //send commands to srvA and wait for reply. Store
512 replies in r1 and r2
513
514 redis_cmd("srvA", "GET foo", "r3"); //send command, wait for reply and s
515 tore it in r3
516
517
518 redis_pipe_cmd("srvA", "SET foo bar", "r1");
519
520 redis_pipe_cmd("srvA", "SET ruri $ru", "r2");
521
522 redis_cmd("srvA", "GET foo", "r3"); //first call redis execute (replies
523 are stored in r1 and r2), log warning and execute redis_cmd
524
525 redis_execute("srvA"); //this does nothing as there are no more pipeline
526 d commands. The call is not necessary
527 ...
528
529 4.4. redis_free(replyid)
530
531 Frees data in a previous reply from memory. After this function call,
532 accessing to a freed replyid returns null value.
533
534 It is not necessary to free a reply to use it again in a new redis_cmd
535 function. When ndb_redis module closes, all pending replies are freed
536 automatically.
537
538 Example 1.14. redis_free usage
539 ...
540 After a redis command call:
541 redis_cmd("srvN", "INCR cnt", "r");
542
543 free reply data:
544 redis_free("r");
545 ...