Debian Codegraph @

src/modules/db_redis/README

raw
1 DB_REDIS Module
2
3 Andreas Granig
4
5 <[email protected]>
6
7 Edited by
8
9 Andreas Granig
10
11 <[email protected]>
12
13 Alex Balashov
14
15 <[email protected]>
16
17 Joel Centelles Martin
18
19 <[email protected]>
20
21 Donat Zenichev
22
23 <[email protected]>
24
25 Copyright © 2018 sipwise.com
26 __________________________________________________________________
27
28 Table of Contents
29
30 1. Admin Guide
31
32 1. Overview
33 2. Limitations
34 3. Dependencies
35
36 3.1. Kamailio Modules
37 3.2. External Libraries or Applications
38
39 4. Parameters
40
41 4.1. schema_path (string)
42 4.2. keys (string)
43 4.3. verbosity (int)
44 4.4. opt_tls (int)
45 4.5. max_key_length (int)
46 4.6. mapping_struct_type (int)
47 4.7. hash_value (string)
48 4.8. hash_expires (int)
49 4.9. db_pass (string)
50 4.10. ca_path (string)
51 4.11. with_sentinels (int)
52 4.12. sentinels_config (string)
53 4.13. redis_master_name (string)
54 4.14. use_replicas (int)
55 4.15. recheck_replicas_interval (int)
56
57 5. Usage
58 6. Module Specific Considerations
59
60 6.1. usrloc
61
62 List of Examples
63
64 1.1. Setting schema_path module parameter
65 1.2. Setting keys module parameter
66 1.3. Setting verbosity module parameter
67 1.4. Enabling TLS connection
68 1.5. Limiting the max key length
69 1.6. Enabling redis hashes
70 1.7. Setting hash_value
71 1.8. Setting hash expires
72 1.9. Setting a password
73 1.10. Setting CA path
74 1.11. Enabling sentinels
75 1.12. Setting sentinels
76 1.13. Setting Redis master name
77 1.14. Setting use_replicas
78 1.15. Setting use_replicas
79 1.16. Usage
80 1.17. Usage
81
82 Chapter 1. Admin Guide
83
84 Table of Contents
85
86 1. Overview
87 2. Limitations
88 3. Dependencies
89
90 3.1. Kamailio Modules
91 3.2. External Libraries or Applications
92
93 4. Parameters
94
95 4.1. schema_path (string)
96 4.2. keys (string)
97 4.3. verbosity (int)
98 4.4. opt_tls (int)
99 4.5. max_key_length (int)
100 4.6. mapping_struct_type (int)
101 4.7. hash_value (string)
102 4.8. hash_expires (int)
103 4.9. db_pass (string)
104 4.10. ca_path (string)
105 4.11. with_sentinels (int)
106 4.12. sentinels_config (string)
107 4.13. redis_master_name (string)
108 4.14. use_replicas (int)
109 4.15. recheck_replicas_interval (int)
110
111 5. Usage
112 6. Module Specific Considerations
113
114 6.1. usrloc
115
116 1. Overview
117
118 This module provides a DB APIv1 connector for the Redis server
119 (https://www.redis.io).
120
121 It can be used as a replacement for other database modules such as
122 db_mysql and db_postgres. Not all the specs of DB APIv1 are
123 implemented, thus the usage of this module might be restricted to
124 specific modules. Also, for proper performance, this module needs
125 particular configuration tailored to the modules that make use of it.
126
127 Since Redis does not provide a schema by itself, db_redis ships with
128 schema files. The path to these has to be defined using the module
129 parameter "schema_path". The schema definition is defined in one file
130 per table, such that the file name corresponds to the table name, and
131 each file is composed of a comma-separated list of column definitions
132 in the format <column-name>/<type>[,<column-name>/<type> ...] in one
133 line, followed by a line holding the table version.
134
135 Example definition for the "location" table (from the usrloc module):
136 username/string,domain/string,contact/string,received/string,path/string,expires
137 /timestamp,q/double,callid/string,cseq/int,last_modified/timestamp,flags/int,cfl
138 ags/int,user_agent/string,socket/string,methods/int,ruid/string,reg_id/int,insta
139 nce/string,server_id/int,connection_id/int,keepalive/int,partition/int
140 8
141
142 Because Redis is a key-value store, it requires unique keys. This means
143 that tables and rows from a relational SQL database, e.g. from MySQL,
144 can not be ported one a 1:1 basis to Redis.
145
146 For instance, usrloc relies on a key of "username@domain", but in order
147 to store multiple contacts per AoR, it cannot be constrained to
148 uniqueness. To work around this, db_redis supports mapping structures
149 (either sets or hashes). If sets are chosen, in the case of the usrloc
150 module for example, one would have a set with a key of
151 "username@domain" and its entries being unique keys per contact based
152 on the ruid of a contact. Thus, one contact in usrloc consists of a
153 unique key "location:entry::example-ruid-1" being a hash with the
154 columns like username, domain, contact, path etc. In addition, this
155 unique key is stored in a set
156 "location:usrdom::exampleuser:exampledomain.org". When usrloc does a
157 lookup based on "username@domain", db_redis figures out via the
158 keys/values the query constructed by usrloc to look for the final entry
159 key in the mapping set first. It then query the actual entries from
160 there, avoiding full table scans. For usrloc, the same holds true for
161 expired contacts, requiring a different kind of mapping. There is a
162 certain balance of read performance vs. write performance to consider,
163 because inserts and deletes also have to maintain the mappings, though
164 this yields much faster selects. The mappings can be freely defined, so
165 even though other kamailio modules don't require a specific mapping to
166 be in place for proper performance, mappings could be defined for
167 external applications to read faster (for instance letting the acc
168 module also write mappings besides the actual records for billing
169 systems to correlate start and stop records faster).
170
171 The key is always prefixed with 'tablename:entry::'. For example the
172 record in 'subscriber' table for user '[email protected]' has the key:
173 'subscriber:entry::alice:sip.com'. If all the records are just loaded
174 at startup (or all reloaded at runtime), the key can just be made
175 unique using whatever values added after 'tablename:entry::' prefix.
176 For example, keys for 'address' table records can be:
177 'address:entry::1', address:entry::2', address:entry::3', ... No 'keys'
178 modparam of 'db_redis' for 'address' table needs to be defined.
179
180 Important Note: at this moment the module requires at least one 'keys'
181 parameter, but it does not need to be related to the table loaded from
182 Redis server -- for example, if used only for permissions module with
183 'address' table, then the 'keys' parameter can be specified for
184 'version' table. However, if it used for a module that inserts or
185 updates the records in database table, the key for entry must be
186 defined for that table.
187
188 The mappings can be freely defined in the "keys" module parameter,
189 which is composed of a semi-colon separated list of definitions in the
190 format
191 <table-name>=<entry>:<column-name>[&<map-name>:<column-name>,<column-na
192 me>...]. Each table must at least have an "entry" key for db_redis to
193 be able to store data.
194
195 Example:
196 location=entry:ruid&usrdom:username,domain&timer:partition,keepalive;acc=entry:c
197 allid,time_hires&cid:callid
198
199 For readability purposes, definitions of keys per table can span
200 multiple Kamailio config lines by providing multiple "keys" modparams.
201
202 When the mapping structure is selected to be hash, one contact in
203 usrloc consists of the same unique key "location:entry::example-ruid-1"
204 as before, but now this unique key is also a key in the hash
205 "location:usrdom::exampleuser:exampledomain.org". The value associated
206 with this key is whatever module param hash_value is set to. When
207 usrloc does a lookup based on "username@domain", db_redis goes through
208 the keys in the hash "location:usrdom::exampleuser:exampledomain.org"
209 to retrieve the associated contact keys. All the other mapping
210 structures that used to be sets are now hashes as well (index::timer,
211 location:timer). This possibility of using hashes has only been tested
212 for usrloc and was introduced because from Redis v 7.4.0 onwards the
213 HEXPIRE command is available, which allows expiring individual keys
214 inside a hash. This makes the process of expiring contacts something
215 that Redis itself can handle, without the need for Kamailio to do it.
216
217 Example of structures inside Redis for the usrloc module, with mapping
218 structure type 1 (hashes):
219 HASH "location:entry::example-ruid-1" -> contact info
220 HASH "location:usrdom::exampleuser:exampledomain" -> con
221 tains (key:"location:entry::example-ruid-1" , value:"hash_value")
222 HASH "location::index::usrdom" -> contains (key:"locatio
223 n:usrdom::exampleuser:exampledomain", value:"hash_value")
224 HASH "location:timer::YYYY-MM-DD mm:ss:mm" -> contains (
225 key:"location:entry::example-ruid-1", value:"hash_value")
226 HASH "location::index::timer" -> contains (key:"location
227 :timer::YYYY-MM-DD mm:ss:mm", value:"hash_value")
228
229 2. Limitations
230
231 * This module has implemented equivalent underlying Redis operations
232 for INSERT, UPDATE, DELETE and SELECT. The ORDER BY clause for
233 SELECT is not implemented. Raw querying is not implemented inside
234 this module; for sending literal commands to the Redis server, use
235 ndb_redis.
236
237 3. Dependencies
238
239 3.1. Kamailio Modules
240 3.2. External Libraries or Applications
241
242 3.1. Kamailio Modules
243
244 The following modules must be loaded before this module:
245 * none.
246
247 3.2. External Libraries or Applications
248
249 The following libraries or applications must be installed before
250 running Kamailio with this module loaded:
251 * hiredis - available at https://github.com/redis/hiredis
252
253 The following library is an optional dependency to support redis
254 cluster protocol:
255 * hiredis-cluster - available at
256 https://github.com/Nordix/hiredis-cluster
257
258 4. Parameters
259
260 4.1. schema_path (string)
261 4.2. keys (string)
262 4.3. verbosity (int)
263 4.4. opt_tls (int)
264 4.5. max_key_length (int)
265 4.6. mapping_struct_type (int)
266 4.7. hash_value (string)
267 4.8. hash_expires (int)
268 4.9. db_pass (string)
269 4.10. ca_path (string)
270 4.11. with_sentinels (int)
271 4.12. sentinels_config (string)
272 4.13. redis_master_name (string)
273 4.14. use_replicas (int)
274 4.15. recheck_replicas_interval (int)
275
276 4.1. schema_path (string)
277
278 The path to the table schemas.
279
280 Default value: "/usr/share/kamailio/db_redis".
281
282 Example 1.1. Setting schema_path module parameter
283 ...
284 modparam("db_redis", "schema_path", "/usr/local/share/kamailio/db_redis/kamailio
285 ")
286 ...
287
288 4.2. keys (string)
289
290 The entry and mapping keys of tables.
291
292 Default value: "" (empty).
293
294 Example 1.2. Setting keys module parameter
295 ...
296 modparam("db_redis", "keys", "version=entry:table_name;location=entry:ruid&usrdo
297 m:username,domain&timer:partition,keepalive")
298 ...
299
300 4.3. verbosity (int)
301
302 Control the verbosity of debug messages printed by the module. If set
303 to 1, the module prints schema details for all tables on each connect
304 operation to Redis server.
305
306 Default value: 1.
307
308 Example 1.3. Setting verbosity module parameter
309 ...
310 modparam("db_redis", "verbosity", 0)
311 ...
312
313 4.4. opt_tls (int)
314
315 Controls TLS usage while connecting to a remote DB. If set to 1, TLS is
316 used to connect to the DB.
317
318 If TLS is enabled, the module will validate the Redis server
319 certificate against the ca_path. There is currently no way to connect
320 with a specified client certificate, the corresponding configuration to
321 check client certificates in the Redis server must therefore be turned
322 off.
323
324 Default value: 0.
325
326 Example 1.4. Enabling TLS connection
327 ...
328 modparam("db_redis", "opt_tls", 1)
329 ...
330
331 4.5. max_key_length (int)
332
333 Controls the max possible key length to be used when adding/prepending
334 strings into the redis key. If used will be always compared against the
335 given string key length.
336
337 Default value: 0. Means disabled.
338
339 Example 1.5. Limiting the max key length
340 ...
341 modparam("db_redis", "max_key_length", 4096)
342 ...
343
344 4.6. mapping_struct_type (int)
345
346 Controls the type of mapping structures to be used. Beforehand, only
347 sets were used. For the rationale behind mapping structures, see the
348 discussion about mapping structures in the overview section. Currently
349 supported values: 0 - sets (default), 1 - hashes.
350
351 Motivation of hashes is the implementation of HEXPIRE command in Redis,
352 available since Redis v 7.4.0 onwards, which allows expiring individual
353 keys inside hashes.
354
355 Default value: 0.
356
357 Example 1.6. Enabling redis hashes
358 ...
359 modparam("db_redis", "mapping_struct_type", 1)
360 ...
361
362 4.7. hash_value (string)
363
364 Only has sense if mapping_struct_type is set to hash (1). Defines the
365 value to be used for the hash entries in the hashes that now replace
366 mapping sets.
367
368 Default value: DUMMY.
369
370 Example 1.7. Setting hash_value
371 ...
372 modparam("db_redis", "hash_value", "DUMMY")
373 ...
374
375 4.8. hash_expires (int)
376
377 Set an expiration time in seconds for the keys in the hash data
378 structures in the database. This is useful for the usrloc module for
379 example, for automatic contact expiry. A value of 0 is interpreted as
380 no expiration. This is only supported with mapping_struct_type set to
381 hash (1).
382
383 Default value: 0.
384
385 Example 1.8. Setting hash expires
386 ...
387 modparam("db_redis", "hash_expires", 3600)
388 ...
389
390 4.9. db_pass (string)
391
392 Sets the password to connect to the DB.
393
394 Default value: "" (empty).
395
396 Example 1.9. Setting a password
397 ...
398 modparam("db_redis", "db_pass", "r3d1sPass")
399 ...
400
401 4.10. ca_path (string)
402
403 Sets the path where Certificates Authorities certs for the Redis server
404 certificate are stored.
405
406 Default value: "" (empty).
407
408 Example 1.10. Setting CA path
409 ...
410 modparam("db_redis", "ca_path", "/etc/ssl/certs")
411 ...
412
413 4.11. with_sentinels (int)
414
415 Enables the use of Redis sentinels for high availability. If set to 1,
416 the module will connect to the Redis server using the sentinels defined
417 in the "sentinels_config" modparam.
418
419 Default value: 0.
420
421 Example 1.11. Enabling sentinels
422 ...
423 modparam("db_redis", "with_sentinels", 1)
424 ...
425
426 4.12. sentinels_config (string)
427
428 Sets the sentinels to connect to the Redis server. The format is
429 semi-colon separated list of key-value pairs: password, sentinels.
430 Sentinels is a pipe separated list of host:port pairs. Password applies
431 to all sentinels.
432
433 Default value: "" (empty).
434
435 Example 1.12. Setting sentinels
436 ...
437 modparam("db_redis", "sentinels_config", "password=password;sentinels=host1:port
438 1|host2:port2|host3:port3")
439 ...
440
441 4.13. redis_master_name (string)
442
443 Sets the name of the Redis master. This is needed when building the
444 SENTINEL get-master-addr-by-name command. The name should match the
445 name used in the Redis Sentinel configuration.
446
447 Default value: "mymaster" (empty).
448
449 Example 1.13. Setting Redis master name
450 ...
451 modparam("db_redis", "redis_master_name", "mymaster")
452 ...
453
454 4.14. use_replicas (int)
455
456 Enables connecting to Redis replicas instead of the master when using
457 sentinels. Replicas are selected randomly. This helps distribute load
458 across the Redis setup (i.e.: read from replicas instead of master). If
459 for whatever reason no replicas are available, the module will fall
460 back to connecting to the master (this cannot be toggled off).
461
462 Default value: 0 (connect to master).
463
464 Example 1.14. Setting use_replicas
465 ...
466 modparam("db_redis", "use_replicas", 1)
467 ...
468
469 4.15. recheck_replicas_interval (int)
470
471 The parameter sets the interval in seconds to recheck for available
472 replicas so that we don't remain connected to the master. Because of
473 implementation details, the first check can exceptionally happen after
474 half of this interval. If set to 0, no rechecks will be performed and
475 the module will remain connected to the master until a new connection
476 is established (i.e.: Redis server goes down and comes back up).
477
478 Default value: 120 seconds.
479
480 Example 1.15. Setting use_replicas
481 ...
482 modparam("db_redis", "recheck_replicas_interval", 120)
483 ...
484
485 5. Usage
486
487 Load the module and set the "db_url" modparam for specific modules to:
488 'redis://[username]@host:port/database'. Username is optional. The
489 database portion must be a valid Redis database number.
490
491 For cluster support you need to set the "db_url" modparam with a comma
492 separated list of cluster hosts: 'redis://host1:port1,host2:port2/'.
493 The database portion is not supported in cluster mode.
494
495 If accessed DB requires TLS connections, you need to enable TLS support
496 setting the "opt_tls" parameter to 1. In case the DB requires a
497 password, that should be set using the "db_pass" parameter.
498
499 Example 1.16. Usage
500 ...
501 loadmodule "db_redis.so"
502 ...
503 #!define DBURL_USRLOC "redis://127.0.0.1:6379/5"
504 #!define DBURL_ACC "redis://127.0.0.1:6379/6"
505 #!define DBURL_AUTH "redis://127.0.0.1:6379/7"
506 #!define DBURL_PERM "redis://127.0.0.1:6379/8"
507 #!define DBURL_DLG "redis://127.0.0.1:6379/9"
508 ...
509 modparam("db_redis", "schema_path", "/usr/share/kamailio/db_redis/kamailio")
510 modparam("db_redis", "keys", "version=entry:table_name")
511 modparam("db_redis", "keys", "location=entry:ruid&usrdom:username,domain&timer:p
512 artition,keepalive")
513 modparam("db_redis", "keys", "acc=entry:callid,time_hires&cid:callid")
514 modparam("db_redis", "keys", "subscriber=entry:username,domain")
515 modparam("db_redis", "keys", "dialog=entry:hash_entry,hash_id&cid:callid")
516 modparam("db_redis", "keys", "dialog_vars=entry:hash_entry,hash_id,dialog_key&di
517 alog:hash_entry,hash_id")
518 ...
519 modparam("usrloc", "db_url", DBURL_USRLOC)
520 ...
521 modparam("acc_db", "db_url", DBURL_ACC)
522 ...
523 modparam("auth_db", "db_url", DBURL_AUTH)
524 ...
525 modparam("permissions", "db_url", DBURL_PERM)
526 ...
527 modparam("dialog", "db_url", DBURL_DLG)
528 ...
529
530 Samples adding records for address table using 'redis-cli':
531
532 Example 1.17. Usage
533 ...
534 SELECT 8
535 HMSET address:entry::1 id 1 grp 1 ip_addr "127.0.0.1" mask 32 port 0
536 HMSET address:entry::2 id 2 grp 1 ip_addr "127.0.0.2" mask 32 port 0
537 HMSET address:entry::3 id 3 grp 2 ip_addr "127.0.0.3" mask 32 port 0
538 HMSET address:entry::4 id 4 grp 2 ip_addr "127.0.0.4" mask 32 port 0 tag "test"
539 ...
540
541 Note that is some cases, the optional values in database tables can be
542 omitted. For 'address' table, the 'tag' value may be omitted. To avoid
543 any issues, set unused fields to their default values as defined by
544 database schema. When definition allows 'NULL', that field can be
545 unset.
546
547 6. Module Specific Considerations
548
549 6.1. usrloc
550
551 6.1. usrloc
552
553 If you set "expires_type" to "1" in order to use BIGINT instead of
554 DATETIME, make sure to update your location schema file and change the
555 type of "expires" and "last_modified" from "time" to "int".