Skip to content

Commit

Permalink
[Fix] Another fix for Redis sentinel
Browse files Browse the repository at this point in the history
Issue: #2796
  • Loading branch information
vstakhov committed Apr 1, 2019
1 parent 302da75 commit 7dfecb9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 30 deletions.
33 changes: 25 additions & 8 deletions lualib/lua_redis.lua
Expand Up @@ -313,8 +313,10 @@ local function enrich_defaults(rspamd_config, module, redis_params)
local opts = rspamd_config:get_all_opt('redis')

if opts then
if opts[module] then
process_redis_opts(opts[module], redis_params)
if module then
if opts[module] then
process_redis_opts(opts[module], redis_params)
end
end

process_redis_opts(opts, redis_params)
Expand Down Expand Up @@ -344,7 +346,7 @@ end
-- @module lua_redis
-- This module contains helper functions for working with Redis
--]]
local function try_load_redis_servers(options, rspamd_config, result)
local function process_redis_options(options, rspamd_config, result)
local default_port = 6379
local upstream_list = require "rspamd_upstream_list"
local read_only = true
Expand Down Expand Up @@ -427,7 +429,22 @@ local function try_load_redis_servers(options, rspamd_config, result)
return false
end
exports.try_load_redis_servers = try_load_redis_servers
--[[[
@function try_load_redis_servers(options, rspamd_config, no_fallback)
Tries to load redis servers from the specified `options` object.
Returns `redis_params` table or nil in case of failure
--]]
exports.try_load_redis_servers = function(options, rspamd_config, no_fallback, module_name)
local result = {}
if process_redis_options(options, rspamd_config, result) then
if not no_fallback then
enrich_defaults(rspamd_config, module_name, result)
end
return maybe_return_cached(result)
end
end
-- This function parses redis server definition using either
-- specific server string for this module or global
Expand All @@ -448,7 +465,7 @@ local function rspamd_parse_redis_server(module_name, module_opts, no_fallback)
local ret
if opts.redis then
ret = try_load_redis_servers(opts.redis, rspamd_config, result)
ret = process_redis_options(opts.redis, rspamd_config, result)
if ret then
if not no_fallback then
Expand All @@ -458,7 +475,7 @@ local function rspamd_parse_redis_server(module_name, module_opts, no_fallback)
end
end
ret = try_load_redis_servers(opts, rspamd_config, result)
ret = process_redis_options(opts, rspamd_config, result)
if ret then
if not no_fallback then
Expand All @@ -482,13 +499,13 @@ local function rspamd_parse_redis_server(module_name, module_opts, no_fallback)
local ret
if opts[module_name] then
ret = try_load_redis_servers(opts[module_name], rspamd_config, result)
ret = process_redis_options(opts[module_name], rspamd_config, result)
if ret then
return maybe_return_cached(result)
end
else
ret = try_load_redis_servers(opts, rspamd_config, result)
ret = process_redis_options(opts, rspamd_config, result)
-- Exclude disabled
if opts['disabled_modules'] then
Expand Down
12 changes: 7 additions & 5 deletions lualib/rspamadm/configwizard.lua
Expand Up @@ -449,9 +449,11 @@ local function check_redis_classifier(cls, changes)
return
end

local parsed_redis = {}
if not lua_redis.try_load_redis_servers(cls, nil, parsed_redis) then
if not lua_redis.try_load_redis_servers(redis_params, nil, parsed_redis) then
local parsed_redis = lua_redis.try_load_redis_servers(cls, nil)

if not parsed_redis then
parsed_redis = lua_redis.try_load_redis_servers(redis_params, nil)
if not parsed_redis then
printf("Cannot parse Redis params")
return
end
Expand Down Expand Up @@ -583,8 +585,8 @@ local function setup_statistic(cfg, changes)
return false
end

local parsed_redis = {}
if lua_redis.try_load_redis_servers(redis_params, nil, parsed_redis) then
local parsed_redis = lua_redis.try_load_redis_servers(redis_params, nil)
if parsed_redis then
printf('You have %d sqlite classifiers', #sqlite_configs)
local expire = readline_expire()

Expand Down
4 changes: 2 additions & 2 deletions lualib/rspamadm/stat_convert.lua
Expand Up @@ -5,11 +5,11 @@ local logger = require "rspamd_logger"
local lua_util = require "lua_util"

return function (_, res)
local redis_params = {}
local redis_params = lua_redis.try_load_redis_servers(res.redis, nil)
if res.expire then
res.expire = lua_util.parse_time_interval(res.expire)
end
if not lua_redis.try_load_redis_servers(res.redis, nil, redis_params) then
if not redis_params then
logger.errx('cannot load redis server definition')

return false
Expand Down
2 changes: 1 addition & 1 deletion src/libserver/cfg_rcl.c
Expand Up @@ -3554,7 +3554,7 @@ rspamd_rcl_jinja_handler (struct ucl_parser *parser,
GString *tb;

tb = lua_touserdata (L, -1);
msg_err_config ("cannot call lua try_load_redis_servers script: %s", tb->str);
msg_err_config ("cannot call lua jinja_template script: %s", tb->str);
g_string_free (tb, TRUE);
lua_settop (L, err_idx - 1);

Expand Down
14 changes: 6 additions & 8 deletions src/lua/lua_common.c
Expand Up @@ -2500,12 +2500,9 @@ gboolean
rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj,
struct rspamd_config *cfg, gint *ref_id)
{
gint res_pos, err_idx;
gint err_idx;
struct rspamd_config **pcfg;

/* Create results table */
lua_createtable (L, 0, 0);
res_pos = lua_gettop (L);
lua_pushcfunction (L, &rspamd_lua_traceback);
err_idx = lua_gettop (L);

Expand All @@ -2522,7 +2519,7 @@ rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj,
pcfg = lua_newuserdata (L, sizeof (*pcfg));
rspamd_lua_setclass (L, "rspamd{config}", -1);
*pcfg = cfg;
lua_pushvalue (L, res_pos);
lua_pushboolean (L, false); /* no_fallback */

if (lua_pcall (L, 3, 1, err_idx) != 0) {
GString *tb;
Expand All @@ -2535,16 +2532,17 @@ rspamd_lua_try_load_redis (lua_State *L, const ucl_object_t *obj,
return FALSE;
}

if (lua_toboolean (L, -1)) {
if (lua_istable (L, -1)) {
if (ref_id) {
/* Ref table */
lua_pushvalue (L, res_pos);
lua_pushvalue (L, -1);
*ref_id = luaL_ref (L, LUA_REGISTRYINDEX);
lua_settop (L, 0);
}
else {
/* Leave it on the stack */
lua_settop (L, res_pos);
lua_insert (L, err_idx);
lua_settop (L, err_idx);
}

return TRUE;
Expand Down
11 changes: 7 additions & 4 deletions src/plugins/lua/bayes_expiry.lua
Expand Up @@ -97,10 +97,13 @@ local function check_redis_classifier(cls, cfg)
end
-- Now try to load redis_params if needed

local redis_params = {}
if not lredis.try_load_redis_servers(cls, rspamd_config, redis_params) then
if not lredis.try_load_redis_servers(cfg[N] or E, rspamd_config, redis_params) then
if not lredis.try_load_redis_servers(cfg['redis'] or E, rspamd_config, redis_params) then
local redis_params
redis_params = lredis.try_load_redis_servers(cls, rspamd_config, false, 'bayes')
if not redis_params then
redis_params = lredis.try_load_redis_servers(cfg[N] or E, rspamd_config, false, 'bayes')
if not redis_params then
redis_params = lredis.try_load_redis_servers(cfg[N] or E, rspamd_config, true)
if not redis_params then
return false
end
end
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/lua/reputation.lua
Expand Up @@ -897,8 +897,9 @@ end
local function reputation_redis_init(rule, cfg, ev_base, worker)
local our_redis_params = {}
if not lua_redis.try_load_redis_servers(rule.backend.config,
rspamd_config, our_redis_params) then
our_redis_params = lua_redis.try_load_redis_servers(rule.backend.config, rspamd_config,
true)
if not our_redis_params then
our_redis_params = redis_params
end
if not our_redis_params then
Expand Down

0 comments on commit 7dfecb9

Please sign in to comment.