From bf38f151ba08e16788603d4a54c7f8769acbba75 Mon Sep 17 00:00:00 2001 From: Frederic-Philippe Metz Date: Wed, 13 Jan 2016 14:21:01 +0100 Subject: [PATCH] Fix for restoring notified call from own redis db --- daemon/call.h | 1 + daemon/main.c | 6 +++--- daemon/redis.c | 32 ++++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/daemon/call.h b/daemon/call.h index dcb1034a3..bc647dfa3 100644 --- a/daemon/call.h +++ b/daemon/call.h @@ -427,6 +427,7 @@ struct callmaster_config { struct redis *redis; struct redis *redis_read; struct redis *redis_write; + struct redis *redis_read_notify; struct event_base *redis_notify_event_base; struct redisAsyncContext *redis_notify_async_context; char *b2b_url; diff --git a/daemon/main.c b/daemon/main.c index a5a19637e..84d129f2c 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -559,19 +559,19 @@ no_kernel: } if (!is_addr_unspecified(&redis_ep.address)) { - mc.redis = redis_new(&redis_ep, redis_db, MASTER_REDIS_ROLE); + mc.redis = mc.redis_read_notify = redis_new(&redis_ep, redis_db, MASTER_REDIS_ROLE); if (!mc.redis) die("Cannot start up without Redis database"); } if (!is_addr_unspecified(&redis_read_ep.address)) { - mc.redis_read = redis_new(&redis_read_ep, redis_read_db, ANY_REDIS_ROLE); + mc.redis_read = mc.redis_read_notify = redis_new(&redis_read_ep, redis_read_db, ANY_REDIS_ROLE); if (!mc.redis_read) die("Cannot start up without Redis read database"); } if (!is_addr_unspecified(&redis_write_ep.address)) { - mc.redis_write = redis_new(&redis_write_ep, redis_write_db, ANY_REDIS_ROLE); + mc.redis_write = mc.redis_read_notify = redis_new(&redis_write_ep, redis_write_db, ANY_REDIS_ROLE); if (!mc.redis_write) die("Cannot start up without Redis write database"); } diff --git a/daemon/redis.c b/daemon/redis.c index 1df5a846b..5ca4774d7 100644 --- a/daemon/redis.c +++ b/daemon/redis.c @@ -226,15 +226,22 @@ void onRedisNotification(redisAsyncContext *actx, void *reply, void *privdata) { unsigned char* p = 0; int dbno; - if (cm->conf.redis_read) { - r = cm->conf.redis_read; - } else if (cm->conf.redis) { - r = cm->conf.redis; - } else { + if (!(cm->conf.redis_read) && !(cm->conf.redis)) { rlog(LOG_ERROR, "A redis notification has been there but role was not 'master' or 'read'"); return; } + r = cm->conf.redis_read_notify; + +// if (cm->conf.redis_read) { +// r = cm->conf.redis_read_notify; +// } else if (cm->conf.redis) { +// r = cm->conf.redis_read_notify; +// } else { +// rlog(LOG_ERROR, "A redis notification has been there but role was not 'master' or 'read'"); +// return; +// } + redisReply *rr = (redisReply*)reply; if (reply == NULL || rr->type != REDIS_REPLY_ARRAY) @@ -265,7 +272,16 @@ void onRedisNotification(redisAsyncContext *actx, void *reply, void *privdata) { return; } } - dbno = atoi(db_str); + dbno = r->db = atoi(db_str); + + // select the right db for restoring the call + if (redisCommandNR(r->ctx, "SELECT %i", r->db)) { + if (r->ctx->err) + rlog(LOG_ERR, "Redis error: %s", r->ctx->errstr); + redisFree(r->ctx); + r->ctx = NULL; + return; + } pch += strlen("notifier-"); str_cut(rr->element[2]->str,0,pch-rr->element[2]->str); @@ -302,14 +318,14 @@ void redis_notify_event_base_loopbreak(struct callmaster *cm) { } void redis_notify_subscribe_keyspace(struct callmaster *cm, int keyspace) { - char* main_db_str[256]; + char* main_db_str[256]; memset(&main_db_str,0,256); sprintf(main_db_str,"psubscribe __keyspace@%i*:notifier-*", keyspace); redisAsyncCommand(cm->conf.redis_notify_async_context, onRedisNotification, (void*)cm, main_db_str); } void redis_notify_unsubscribe_keyspace(struct callmaster *cm, int keyspace) { - char* main_db_str[256]; + char* main_db_str[256]; memset(&main_db_str,0,256); sprintf(main_db_str,"punsubscribe __keyspace@%i*:notifier-*", keyspace); redisAsyncCommand(cm->conf.redis_notify_async_context, onRedisNotification, (void*)cm, main_db_str);