From f5e09b16e6796c5899c43bf74875a5c40102a41e Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 12 Feb 2019 17:51:23 -0200 Subject: [PATCH] fix(balancer) avoid crash when failing to obtain list of upstreams Avoid a stacktrace when the database fails to obtain the list of upstreams (as seen in an error log provided by @hutchic): instead report a CRIT-level message and fallback to an empty list. --- kong/runloop/balancer.lua | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/kong/runloop/balancer.lua b/kong/runloop/balancer.lua index a14c8d93ca1d..7fe04d0c015a 100644 --- a/kong/runloop/balancer.lua +++ b/kong/runloop/balancer.lua @@ -466,15 +466,15 @@ end local get_all_upstreams do - ------------------------------------------------------------------------------ - -- Implements a simple dictionary with all upstream-ids indexed - -- by their name. - -- @return The upstreams dictionary, a map with upstream names as string keys - -- and upstream entity tables as values, or nil+error local function load_upstreams_dict_into_memory() local upstreams_dict = {} - for up in singletons.db.upstreams:each(1000) do -- build a dictionary, indexed by the upstream name + for up, err in singletons.db.upstreams:each(1000) do + if err then + log(CRIT, "could not obtain list of upstreams: ", err) + return nil + end + upstreams_dict[up.name] = up.id end return upstreams_dict @@ -482,19 +482,22 @@ do _load_upstreams_dict_into_memory = load_upstreams_dict_into_memory + local opts = { neg_ttl = 10 } + + ------------------------------------------------------------------------------ - -- Finds and returns an upstream entity. This function covers - -- caching, invalidation, db access, et al. - -- @param upstream_name string. - -- @return upstream table, or `false` if not found, or nil+error + -- Implements a simple dictionary with all upstream-ids indexed + -- by their name. + -- @return The upstreams dictionary (a map with upstream names as string keys + -- and upstream entity tables as values), or nil+error get_all_upstreams = function() - local upstreams_dict, err = singletons.cache:get("balancer:upstreams", nil, + local upstreams_dict, err = singletons.cache:get("balancer:upstreams", opts, load_upstreams_dict_into_memory) if err then return nil, err end - return upstreams_dict + return upstreams_dict or {} end end @@ -718,7 +721,7 @@ local function init() local upstreams, err = get_all_upstreams() if not upstreams then - log(CRIT, "failed loading initial list of upstreams: " .. err) + log(CRIT, "failed loading initial list of upstreams: ", err) return end