-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathREADME
More file actions
495 lines (365 loc) · 15.9 KB
/
Copy pathREADME
File metadata and controls
495 lines (365 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
SQL Cacher Module
__________________________________________________________
Table of Contents
1. Admin Guide
1.1. Overview
1.2. Dependencies
1.3. Exported Parameters
1.3.1. cache_table (string)
1.3.2. spec_delimiter (string)
1.3.3. pvar_delimiter (string)
1.3.4. columns_delimiter (string)
1.3.5. sql_fetch_nr_rows (integer)
1.3.6. full_caching_expire (integer)
1.3.7. reload_interval (integer)
1.3.8. bigint_to_str (integer)
1.4. Exported Functions
1.4.1. sql_cache_dump(caching_id, columns,
result_avps)
1.5. Exported MI Functions
1.5.1. sql_cacher_reload
1.6. Exported Pseudo-Variables
1.6.1. $sql_cached_value(id{sep}col{sep}key)
1.7. Usage Example
1.8. Exported Status/Report Identifiers
1.8.1. [cache_entry_id]
2. Contributors
2.1. By Commit Statistics
2.2. By Commit Activity
3. Documentation
3.1. Contributors
List of Tables
2.1. Top contributors by DevScore^(1), authored commits^(2) and
lines added/removed^(3)
2.2. Most recently active contributors^(1) to this module
List of Examples
1.1. cache_table parameter usage
1.2. spec_delimiter parameter usage
1.3. pvar_delimiter parameter usage
1.4. columns_delimiter parameter usage
1.5. sql_fetch_nr_rows parameter usage
1.6. full_caching_expire parameter usage
1.7. reload_interval parameter usage
1.8. bigint_to_str parameter usage
1.9. sql_cache_dump usage
1.10. sql_cacher_reload usage
1.11. sql_cached_value(id{sep}col{sep}key) pseudo-variable
usage
1.12. Example database content - carrierfailureroute table
1.13. Setting the cache_table parameter
1.14. Accessing cached values
Chapter 1. Admin Guide
1.1. Overview
The sql_cacher module introduces the possibility to cache data
from a SQL-based database (using different OpenSIPS modules
which implement the DB API) into a cache system implemented in
OpenSIPS through the CacheDB Interface. This is done by
specifying the databases URLs, SQL table to be used, desired
columns to be cached and other details in the OpenSIPS
configuration script.
The cached data is available in the script through the
read-only pseudovariable “$sql_cached_value” similar to a
Key-Value system. A specified column from the SQL table has the
role of “key” therefore the value of this column along with the
name of a required column are provided as "parameters" to the
pseudovariable returning the appropriate value of the column.
There are two types of caching available:
* full caching - the entire SQL table (all the rows) is
loaded into the cache at OpenSIPS startup;
* on demand - the rows of the SQL table are loaded at runtime
when appropriate keys are requested.
For on demand caching, the stored values have a configurable
expire period after which they are permanently removed unless
an MI reload function is called for a specific key. In the case
of full caching the data is automatically reloaded at a
configurable interval. Consequently if the data in the SQL
database changes and a MI reload function is called, the old
data remains in cache only until it expires.
1.2. Dependencies
The following modules must be loaded before this module:
* The OpenSIPS modules that offer actual database back-end
connection
1.3. Exported Parameters
1.3.1. cache_table (string)
This parameter can be set multiple times in order to cache
multiple SQL tables or even the same table but with a different
configuration. The module distinguishes those different entries
by an “id” string.
The caching entry is specified via this parameter that has it's
own subparameters. Each of those parameters are separated by a
delimiter configured by spec_delimiter and have the following
format:
param_name=param_value
The parameters are:
* id : cache entry id
* db_url : the URL of the SQL database
* cachedb_url : the URL of the CacheDB database
* table : SQL database table name
* key : SQL database column name of the “key” column
* key_type : data type for the SQL "key" column:
+ string
+ int
If not present, default value is “string”
* columns : names of the columns to be cached from the SQL
database, separated by a delimiter configured by
columns_delimiter.
If not present, all the columns from the table will be
cached
* on_demand : specifies the type of caching:
+ 0 : full caching
+ 1 : on demand
If not present, default value is “0”
* expire : expire period for the values stored in the cache
for the on demand caching type in seconds
If not present, default value is “1 hour”
* full_caching_lock_scope : whether to lock reading from the cache
when loading the table into the cache on a row or table basis
with it set to “table” large tables writing into the cache can
block reads when reading an sql_cached_value from that table
stalling the SIP processing but ensures full table consistency,
“row” allows reads during writing on rows not currently being written to.
+ row
+ table
If not present, default value is “row”
The parameters must be given in the exact order specified
above.
Overall, the parameter does not have a default value, it must
be set at least once in order to cache any table.
Example 1.1. cache_table parameter usage
modparam("sql_cacher", "cache_table",
"id=caching_name
db_url=mysql://root:opensips@localhost/opensips_2_2
cachedb_url=mongodb:mycluster://127.0.0.1:27017/db.col
table=table_name
key=column_name_0
columns=column_name_1 column_name_2 column_name_3
on_demand=0
full_caching_lock_scope=row")
1.3.2. spec_delimiter (string)
The delimiter to be used in the caching entry specification
provided in the cache_table parameter to separate the
subparameters. It must be a single character.
The default value is newline.
Example 1.2. spec_delimiter parameter usage
modparam("sql_cacher", "spec_delimiter", "\n")
1.3.3. pvar_delimiter (string)
The delimiter to be used in the “$sql_cached_value”
pseudovariable to separate the caching id, the desired column
name and the value of the key. It must be a single character.
The default value is “:”.
Example 1.3. pvar_delimiter parameter usage
modparam("sql_cacher", "pvar_delimiter", " ")
1.3.4. columns_delimiter (string)
The delimiter to be used in the columns subparameter of the
caching entry specification provided in the cache_table
parameter to separate the desired columns names. It must be a
single character.
The default value is “ ”(space).
Example 1.4. columns_delimiter parameter usage
modparam("sql_cacher", "columns_delimiter", ",")
1.3.5. sql_fetch_nr_rows (integer)
The number of rows to be fetched into OpenSIPS private memory
in one chunk from the SQL database driver. When querying large
tables, adjust this parameter accordingly to avoid the filling
of OpenSIPS private memory.
The default value is “100”.
Example 1.5. sql_fetch_nr_rows parameter usage
modparam("sql_cacher", "sql_fetch_nr_rows", 1000)
1.3.6. full_caching_expire (integer)
Expire period for the values stored in cache for the full
caching type in seconds. This is the longest time that deleted
or modified data remains in cache.
The default value is “24 hours”.
Example 1.6. full_caching_expire parameter usage
modparam("sql_cacher", "full_caching_expire", 3600)
1.3.7. reload_interval (integer)
This parameter represents how many seconds before the data
expires (for full caching) the automatic reloading is
triggered.
The default value is “60 s”.
Example 1.7. reload_interval parameter usage
modparam("sql_cacher", "reload_interval", 5)
1.3.8. bigint_to_str (integer)
Controls bigint conversion. By default bigint values are
returned as int. If the value stored in bigint is out of the
int range, by enabling bigint to string conversion, the bigint
value will be returned as string.
The default value is “0” (disabled).
Example 1.8. bigint_to_str parameter usage
modparam("sql_cacher", "bigint_to_str", 1)
1.4. Exported Functions
1.4.1. sql_cache_dump(caching_id, columns, result_avps)
Dump all columns cached within the given caching_id, and write
them to their respective result_avps.
Parameters:
* caching_id (string) - Identifier for the SQL cache
* columns (string) - the desired SQL columns to be dumped,
specified as comma-separated values
* result_avps (string) - comma-separated list of AVPs where
the results will be written to
Return Codes:
* -1 - Internal Error
* -2 - Zero Results Returned
* 1, 2, 3, ... - Number of results returned into each output
AVP
This function can be used from any route.
Example 1.9. sql_cache_dump usage
...
# Example of pulling all cached CNAM records
$var(n) = sql_cache_dump("cnam", "caller,callee,calling_name,fraud_score
",
"$avp(caller),$avp(callee),$avp(cnam),$avp(fraud)");
$var(i) = 0;
while ($var(i) < $var(n)) {
xlog("Caller $(avp(caller)[$var(i)]) has CNAM $(avp(cnam)[$var(i
)])\n");
$var(i) += 1;
}
...
1.5. Exported MI Functions
1.5.1. sql_cacher_reload
Reloads the entire SQL table in cache or the single key (if key
provided) in full caching mode.
Reloads the given key or invalidates all the keys in cache in
on demand mode.
Parameters:
* id - the caching entry's id
* key (optional) - the specific key to be reloaded.
Example 1.10. sql_cacher_reload usage
...
$ opensips-cli -x mi sql_cacher_reload subs_caching
...
$ opensips-cli -x mi sql_cacher_reload subs_caching alice@domain.com
...
1.6. Exported Pseudo-Variables
1.6.1. $sql_cached_value(id{sep}col{sep}key)
The cached data is available through this read-only PV.The
format is the following:
* sep : separator configured by pvar_delimiter
* id : cache entry id
* col : name of the required column
* key : value of the “key” column
Example 1.11. sql_cached_value(id{sep}col{sep}key)
pseudo-variable usage
...
$avp(a) = $sql_cached_value(caching_name:column_name_1:key1);
...
1.7. Usage Example
This section provides an usage example for the caching of an
SQL table.
Suppose one in interested in caching the columns: “host_name”,
“reply_code”, “flags” and “next_domain” from the
“carrierfailureroute” table of the OpenSIPS database.
Example 1.12. Example database content - carrierfailureroute
table
...
+----+---------+-----------+------------+--------+-----+-------------+
| id | domain | host_name | reply_code | flags | mask | next_domain |
+----+---------+-----------+------------+-------+------+-------------+
| 1 | 99 | | 408 | 16 | 16 | |
| 2 | 99 | gw1 | 404 | 0 | 0 | 100 |
| 3 | 99 | gw2 | 50. | 0 | 0 | 100 |
| 4 | 99 | | 404 | 2048 | 2112 | asterisk-1 |
+----+---------+-----------+------------+-------+------+-------------+
...
In the first place, the details of the caching must be provided
by setting the module parameter “cache_table” in the OpenSIPS
configuration script.
Example 1.13. Setting the cache_table parameter
modparam("sql_cacher", "cache_table",
"id=carrier_fr_caching
db_url=mysql://root:opensips@localhost/opensips
cachedb_url=mongodb:mycluster://127.0.0.1:27017/my_db.col
table=carrierfailureroute
key=id
columns=host_name reply_code flags next_domain")
Next, the values of the cached columns ca be accessed through
the “$sql_cached_value” PV.
Example 1.14. Accessing cached values
...
$avp(rc1) = $sql_cached_value(carrier_fr_caching:reply_code:1);
$avp(rc2) = $sql_cached_value(carrier_fr_caching:reply_code:2);
...
var(some_id)=4;
$avp(nd) = $sql_cached_value(carrier_fr_caching:next_domain:$var(some_id
));
...
xlog("host name is: $sql_cached_value(carrier_fr_caching:host_name:2)");
...
1.8. Exported Status/Report Identifiers
The module provides the "sql_cacher" Status/Report group, where
each full cache is defined as a separate SR identifier. NOTE
that there are no identifiers created for the on-demand caches.
1.8.1. [cache_entry_id]
The status of these identifiers reflects the readiness/status
of the cached data (if available or not when being loaded from
DB):
* -2 - no data at all (initial status)
* -1 - no data, initial loading in progress
* 1 - data loaded, partition ready
* 2 - data available, a reload in progress
In terms of reports/logs, the following events will be
reported:
* starting DB data loading
* DB data loading failed, discarding
* DB data loading successfully completed
* N records loaded)
For how to access and use the Status/Report information, please
see
https://www.opensips.org/Documentation/Interface-StatusReport-3
-3.
Chapter 2. Contributors
2.1. By Commit Statistics
Table 2.1. Top contributors by DevScore^(1), authored
commits^(2) and lines added/removed^(3)
Name DevScore Commits Lines ++ Lines --
1. Vlad Patrascu (@rvlad-patrascu) 94 44 3640 1114
2. Liviu Chircu (@liviuchircu) 30 22 498 150
3. Razvan Crainea (@razvancrainea) 16 14 33 15
4. Bogdan-Andrei Iancu (@bogdan-iancu) 13 10 128 31
5. Ovidiu Sas (@ovidiusas) 7 5 83 7
6. Maksym Sobolyev (@sobomax) 7 5 8 9
7. Bence Szigeti 4 2 3 2
8. Ionel Cerghit (@ionel-cerghit) 4 1 50 92
9. Dan Pascu (@danpascu) 3 1 1 1
10. Peter Lemenkov (@lemenkov) 3 1 1 1
All remaining contributors: Walter Doekes (@wdoekes), Gang
Zhuo.
(1) DevScore = author_commits + author_lines_added /
(project_lines_added / project_commits) + author_lines_deleted
/ (project_lines_deleted / project_commits)
(2) including any documentation-related commits, excluding
merge commits. Regarding imported patches/code, we do our best
to count the work on behalf of the proper owner, as per the
"fix_authors" and "mod_renames" arrays in
opensips/doc/build-contrib.sh. If you identify any
patches/commits which do not get properly attributed to you,
please submit a pull request which extends "fix_authors" and/or
"mod_renames".
(3) ignoring whitespace edits, renamed files and auto-generated
files
2.2. By Commit Activity
Table 2.2. Most recently active contributors^(1) to this module
Name Commit Activity
1. Liviu Chircu (@liviuchircu) Mar 2016 - May 2025
2. Razvan Crainea (@razvancrainea) Feb 2016 - Jul 2024
3. Bogdan-Andrei Iancu (@bogdan-iancu) May 2017 - Apr 2024
4. Ovidiu Sas (@ovidiusas) Mar 2017 - Apr 2024
5. Bence Szigeti Jan 2024 - Jan 2024
6. Maksym Sobolyev (@sobomax) Oct 2020 - Nov 2023
7. Vlad Patrascu (@rvlad-patrascu) Aug 2015 - Jul 2022
8. Gang Zhuo Nov 2021 - Nov 2021
9. Walter Doekes (@wdoekes) Apr 2021 - Apr 2021
10. Dan Pascu (@danpascu) May 2019 - May 2019
All remaining contributors: Peter Lemenkov (@lemenkov), Ionel
Cerghit (@ionel-cerghit).
(1) including any documentation-related commits, excluding
merge commits
Chapter 3. Documentation
3.1. Contributors
Last edited by: Liviu Chircu (@liviuchircu), Bogdan-Andrei
Iancu (@bogdan-iancu), Ovidiu Sas (@ovidiusas), Vlad Patrascu
(@rvlad-patrascu), Razvan Crainea (@razvancrainea), Peter
Lemenkov (@lemenkov).
Documentation Copyrights:
Copyright © 2015 www.opensips-solutions.com