[kamailio/kamailio] db_redis: add sentinel support (PR #4373)
- added with_sentinels, sentinels_config, redis_master_name params - when with_sentinels param is set, sentinels configuration in db_redis module is used to retrieve host and port of redis master, obsoleting the host and port part in usrloc's db_url - modparam sentinels_config: "password=password;sentinels=host1:port1|host2:port2|host3:port3" <!-- Kamailio Pull Request Template --> <!-- IMPORTANT: - for detailed contributing guidelines, read: https://github.com/kamailio/kamailio/blob/master/.github/CONTRIBUTING.md - pull requests must be done to master branch, unless they are backports of fixes from master branch to a stable branch - backports to stable branches must be done with 'git cherry-pick -x ...' - code is contributed under BSD for core and main components (tm, sl, auth, tls) - code is contributed GPLv2 or a compatible license for the other components - GPL code is contributed with OpenSSL licensing exception --> #### Pre-Submission Checklist <!-- Go over all points below, and after creating the PR, tick all the checkboxes that apply --> <!-- All points should be verified, otherwise, read the CONTRIBUTING guidelines from above--> <!-- If you're unsure about any of these, don't hesitate to ask on sr-dev mailing list --> - [x] Commit message has the format required by CONTRIBUTING guide - [x] Commits are split per component (core, individual modules, libs, utils, ...) - [x] Each component has a single commit (if not, squash them into one commit) - [x] No commits to README files for modules (changes must be done to docbook files in `doc/` subfolder, the README file is autogenerated) #### Type Of Change - [ ] Small bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds new functionality) - [ ] Breaking change (fix or feature that would change existing functionality) #### Checklist: <!-- Go over all points below, and after creating the PR, tick the checkboxes that apply --> - [ ] PR should be backported to stable branches - [x] Tested changes locally - [ ] Related to issue #XXXX (replace XXXX with an open issue number) #### Description <!-- Describe your changes in detail --> You can view, comment on, or merge this pull request online at: https://github.com/kamailio/kamailio/pull/4373 -- Commit Summary -- * db_redis: add sentinel support -- File Changes -- M src/modules/db_redis/db_redis_mod.c (17) M src/modules/db_redis/doc/db_redis_admin.xml (60) M src/modules/db_redis/redis_connection.c (128) M src/modules/db_redis/redis_dbase.c (117) M src/modules/db_redis/redis_dbase.h (29) -- Patch Links -- https://github.com/kamailio/kamailio/pull/4373.patch https://github.com/kamailio/kamailio/pull/4373.diff -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Hi, Does anyone have any comments about this? Thank you -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3204315034 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3204315034@github.com>
miconda left a comment (kamailio/kamailio#4373) I guess it can be merged, nobody having comments. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3269991017 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3269991017@github.com>
@henningw commented on this pull request.
I was not in the office for some time, so just had a quick look. Could you quickly check the memory allocation noted in the diff, just to be sure that its correct? Thank you.
> + }
+
+ if(db_redis_authenticate(sentinel_con, sc.password) != 0) {
+ LM_ERR("Authentication error\n");
+ goto err;
+ }
+ reply = redisCommand(sentinel_con,
+ "SENTINEL get-master-addr-by-name %s", db_redis_master_name);
+ if(!reply) {
+ LM_ERR("Failed to run SENTINEL get-master-addr-by-name.\n");
+ goto err;
+ }
+ // The reply should be an array with two elements: the master IP and port.
+ if(reply->type == REDIS_REPLY_ARRAY && reply->elements == 2) {
+ if(con->id->host)
+ pkg_free(con->id->host);
Is this actually allocated as as dedicated segment?
> @@ -374,6 +456,8 @@ int db_redis_connect(km_redis_con_t *con)
redisFree(con->con);
con->con = NULL;
}
+ if(sentinel_con)
If its correct to allocate new memory for con->id->host, we should probably also release it here in error cases.
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4373#pullrequestreview-3200856611
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/pull/4373/review/3200856611@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Thank you for the comments. Adding the label work-in-progress on this as we are actually extending this feature atm. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3274888226 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3274888226@github.com>
@lbalaceanu pushed 2 commits. cb97bd00cc53460ceea81d0938e4a60f29c83a4d db_redis: add sentinel support 29e731f7da3cd8fe425a0324c1d2e3f80c815435 db_redis: add replicas failover to master in sentinel and use_replicas scenarios -- View it on GitHub: https://github.com/kamailio/kamailio/pull/4373/files/34b4fea83fb2451b3066cce... You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/before/34b4fea83fb2451b3066cce2ac51e5c439f49d9c/after/29e731f7da3cd8fe425a0324c1d2e3f80c815435@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Hi everyone, I finally updated the code for this redis sentinels. It works for us. Please review and comment. I have one question though. In tests, on Kamailio startup with fork=no I see the db_do_init() being called 3 times for a process that on the long run becomes the stand-alone receiver (kamctl ps). I see these are: 1. fix_rls -> save_fixup -> domain_fixup 2. main_loop -> init_child(PROC_SIPINIT) -> db_redis_init 3. main_loop -> init_child(PROC_POSTCHILDINIT) -> db_redis_init What is this PROC_POSTCHILDINIT? How many connections to db_redis should be created in this case for the said process? 1 or 3? I am asking because at some point in set_con_details() I am actually changing con->id->host/ con->id->port details. Thank you -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3381903932 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3381903932@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) I have one question though. In tests, on Kamailio startup with fork=no I see the db_do_init() being called 3 times for a process that on the long run becomes the stand-alone receiver (kamctl ps). I see these are: 1. fix_rls -> save_fixup -> domain_fixup 2. main_loop -> init_child(PROC_SIPINIT) -> db_redis_init 3. main_loop -> init_child(PROC_POSTCHILDINIT) -> db_redis_init What is this PROC_POSTCHILDINIT? How many connections to db_redis should be created in this case for the said process? 1 or 3? I am asking because at some point in set_con_details() I am actually changing con->id->host/ con->id->port details. Thank you -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3381912477 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3381912477@github.com>
henningw left a comment (kamailio/kamailio#4373) The PROC_POSTCHILDINIT will get you called after forking of all child processes. There is a special registration function (e.g. compare to ndb_redis) for that. If you don't set it, I think you should not see it. Maybe there is an issue in the core? You can find the introduction of this function in usrloc module here, for example 4780d46976a7 Usually you want to skip database initialization for non worker processes, this is what some of the other DB modules do. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3397865503 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3397865503@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Hi @henningw, I appreciate you pointing me to the commit. While I don't understand why it was added, given that Daniel specifically added that, I think it is safe to consider that the connection is really needed there. Thank you -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3406084497 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3406084497@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Hi, Does anybody have any comments? Thanks -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3414826443 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3414826443@github.com>
@henningw commented on this pull request.
Thank you for the update! I had a look to the usual topics, and added some remarks about smaller things I noticed.
> + strcpy(elem->host, sentinel);
+ elem->port = port;
+ elem->next = NULL;
+
+ if(sc.sentinel_list == NULL) {
+ sc.sentinel_list = elem;
+ sc.sentinel_list_tail = elem;
+ } else {
+ sc.sentinel_list_tail->next = elem;
+ sc.sentinel_list_tail = elem;
+ }
+
+ sentinel = strtok(NULL, "|");
+ }
+
+ LM_INFO("Adding usrloc dburl sentinels:\n");
This log seems a bit odd, not sure what the relationship to usrloc is. The actual log output is also LM_DBG, maybe it make sense to also make this one LM_DBG?
> @@ -141,6 +170,30 @@ static int mod_init(void)
db_redis_hash_expires_str.len = snprintf(
db_redis_hash_expires_str.s, 20, "%d", db_redis_hash_expires);
}
+
+ shared_time = shm_malloc(sizeof(time_t));
The malloc can fail, consider adding a check for NULL and SHM_MEM_ERROR or similar. Its also not freed in error cases, but arguably it don't matter much as the server will not start anyway. But it would be cleaner.
Actually, do we need a shm_malloc for this? Its an integer in the end.
> int mod_register(char *path, int *dlflags, void *p1, void *p2)
{
if(db_api_init() < 0)
return -1;
return 0;
}
+static void db_redis_timer(unsigned int ticks, void *param)
+{
+ if(shared_time) {
I am wondering if we need to protect the access to this shared variable with a Kamailio lock. The timer will run indepently and then the individual process might access when its changed.
> @@ -537,11 +604,22 @@ void *db_redis_command_argv(km_redis_con_t *con, redis_key_t *query)
}
LM_DBG("query has %d args\n", argc);
- redisReply *reply =
- redisCommandArgv(con->con, argc, (const char **)argv, NULL);
- if(con->con->err != REDIS_OK) {
- LM_DBG("redis connection is gone, try reconnect. (%d:%s)\n",
- con->con->err, con->con->errstr);
+ current_time = *shared_time;
See previous comment about the locking
>
int db_redis_hash_expires = 0;
str db_redis_hash_expires_str = str_init("");
char db_redis_hash_expires_buf[20] = {0};
+time_t *shared_time = NULL;
I think we should add a db_redis prefix to this variables, as there are pretty generic and there is a risk that they overlapping with another module.
> + * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+
+#include "redis_sentinels.h"
+#include "db_redis_mod.h"
+
+sentinel_config_t sc;
db_redis prefix - as above
>
if(!con || !con->con) {
LM_ERR("Internal error passing null connection\n");
return -1;
}
- *reply = NULL;
- ret = redisGetReply(con->con, reply);
- if(con->con->err != REDIS_OK) {
- LM_DBG("redis connection is gone, try reconnect. (%d:%s)\n",
- con->con->err, con->con->errstr);
+ current_time = *shared_time;
locking as above
> +
+// Redis role enum
+typedef enum
+{
+ REDIS_ROLE_MASTER,
+ REDIS_ROLE_REPLICA
+} redis_role_t;
+
+typedef struct redis_sentinel
+{
+ char *host;
+ unsigned int port;
+ struct redis_sentinel *next;
+} redis_sentinel_t;
+
+typedef struct
Maybe I missed it, but I failed to find the actual usage of the "user" and "attr" (besides helping with the parsing) fields? It is of course ok to keep it for future use, if this was the intention.
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4373#pullrequestreview-3349693621
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/pull/4373/review/3349693621@github.com>
@lbalaceanu commented on this pull request.
> + strcpy(elem->host, sentinel);
+ elem->port = port;
+ elem->next = NULL;
+
+ if(sc.sentinel_list == NULL) {
+ sc.sentinel_list = elem;
+ sc.sentinel_list_tail = elem;
+ } else {
+ sc.sentinel_list_tail->next = elem;
+ sc.sentinel_list_tail = elem;
+ }
+
+ sentinel = strtok(NULL, "|");
+ }
+
+ LM_INFO("Adding usrloc dburl sentinels:\n");
Hi, you are right about this log mentioning usrloc and needing to be LM_DBG.
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4373#discussion_r2444081239
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/pull/4373/review/3355357993@github.com>
@@ -141,6 +170,30 @@ static int mod_init(void) db_redis_hash_expires_str.len = snprintf( db_redis_hash_expires_str.s, 20, "%d", db_redis_hash_expires); }
@lbalaceanu commented on this pull request. + + shared_time = shm_malloc(sizeof(time_t)); I consider we should use a shared mem value there because this is a value used by all Kamailio processes. Here is the background: The timer is there for the "failover" cases when _use_replicas=1_ : when no Redis replica is reachable for some reason, for some process, Kamailio should try to connect, even for reading, to the Redis master (maybe this one is working). After some time, Kamailio should try to recheck the replica availability. The thing is that we have multiple Redis connections and to different Redis replicas (each Kamailio receiver child holds a Redis connection of its own) I implemented the recheck for replicas by using a "central timer" for ALL Kamailio processes and then only the processes interested (_using_master_read_only=1_) would look into this value. Possibly I should have implemented with a timer per proc - please advise or point me to some code example. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#discussion_r2444184528 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/review/3355482069@github.com>
@lbalaceanu commented on this pull request.
int mod_register(char *path, int *dlflags, void *p1, void *p2) { if(db_api_init() < 0) return -1; return 0; }
+static void db_redis_timer(unsigned int ticks, void *param) +{ + if(shared_time) { Since the shared_time is read by many processes and written to by only one, possibly we can get away with no lock ?! -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#discussion_r2444193752 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/review/3355492531@github.com>
@lbalaceanu commented on this pull request.
> +
+// Redis role enum
+typedef enum
+{
+ REDIS_ROLE_MASTER,
+ REDIS_ROLE_REPLICA
+} redis_role_t;
+
+typedef struct redis_sentinel
+{
+ char *host;
+ unsigned int port;
+ struct redis_sentinel *next;
+} redis_sentinel_t;
+
+typedef struct
Nice catch. You are right, attr is for parsing and user authentication is somehow for future use. At least as far as I know, user authentication is not yet supported for Redis.
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4373#discussion_r2444654580
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/pull/4373/review/3356022559@github.com>
@lbalaceanu pushed 1 commit. cbde68c2c1a9d606234b42f9f11f8cc6ffbfbe75 db_redis: add replicas failover to master in sentinel and use_replicas scenarios -- View it on GitHub: https://github.com/kamailio/kamailio/pull/4373/files/29e731f7da3cd8fe425a032... You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/before/29e731f7da3cd8fe425a0324c1d2e3f80c815435/after/cbde68c2c1a9d606234b42f9f11f8cc6ffbfbe75@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Hi, I just pushed a change for LM_INFO -> LM_DBG, usrloc mention removal and db_redis prefix add. Thank you -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3421567734 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3421567734@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Hi @miconda, Since there are no more comments on this issue, can I merge the PR? Thank you -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3450192369 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3450192369@github.com>
@henningw commented on this pull request.
int mod_register(char *path, int *dlflags, void *p1, void *p2) { if(db_api_init() < 0) return -1; return 0; }
+static void db_redis_timer(unsigned int ticks, void *param) +{ + if(shared_time) { It is an integer value, and you are also doing an assigment and not an increment. So its probably fine without a lock. Even if some process access it during the assignment, it should just receive a stale value. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#discussion_r2465577442 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/review/3383367479@github.com>
@@ -141,6 +170,30 @@ static int mod_init(void) db_redis_hash_expires_str.len = snprintf( db_redis_hash_expires_str.s, 20, "%d", db_redis_hash_expires); }
@henningw commented on this pull request. + + shared_time = shm_malloc(sizeof(time_t)); A shared timer is probably fine, in the end you will be not execute it multiple times per second. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#discussion_r2465583480 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/review/3383375537@github.com>
Merged #4373 into master. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#event-20524891931 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/issue_event/20524891931@github.com>
henningw left a comment (kamailio/kamailio#4373) Thanks for addressing the discussed topics, merged it. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3451190738 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3451190738@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Thank you! -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-3451511864 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c3451511864@github.com>
xkaraman left a comment (kamailio/kamailio#4373) @lbalaceanu Hey there, i was taking a look at #4773 and it seems that this won't compile when `hiredis_cluster` is also present in the OS. Did you had the same issue at all? or you only tested for `hiredis` installation? Thanks! -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-5105652606 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c5105652606@github.com>
lbalaceanu left a comment (kamailio/kamailio#4373) Hi @xkaraman, Sorry, I didn't see this earlier, indeed I just tested only for hiredis installation. Also looking on the hiredis_cluster issue now. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4373#issuecomment-5277935471 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4373/c5277935471@github.com>
participants (4)
-
Daniel-Constantin Mierla -
Henning Westerholt -
Lucian Balaceanu -
Xenofon Karamanos