-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(cache) soft errors would not be handled properly by the cache #3150
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely tested
for _ = 1, 2 do | ||
local value, err = cache:get(key, nil, load_into_memory, SOFT_ERROR) | ||
assert.is_nil(value) | ||
assert(err:find(SOFT_ERROR.err, 1, true), "expected `" .. tostring(err) .. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably better to use the native assert.matches(expected, subj, nil, true)
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally missed that assertion, I actually thought about writing one some day. Glad I didn't do that just yet 🙈
for _ = 1, 2 do | ||
local value, err = cache:get(key, nil, load_into_memory, HARD_ERROR) | ||
assert.is_nil(value) | ||
assert(err:find(HARD_ERROR.err, 1, true), "expected `" .. tostring(err) .. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here as well
cache = kong_cache.new { | ||
cluster_events = cluster_events, | ||
worker_events = worker_events, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: the indentation seems to be completely off in this chunk? :(
kong/cache.lua
Outdated
-- Temporary fix to convert soft callback errors into hard ones. | ||
-- FIXME: use upstream mlcache lib instead of local copy | ||
local soft_to_hard = function(cb) | ||
return function(...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perf: it would probably be worth it to maintain a cache of such closures to lower the GC overhead and ensure we JIT compile in such hot code paths served by the caching library (performance being the whole point here). This change as it is would be quite hurtful to performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last couple of changes imho - ultimately, it'd be nice if we could run some benchmarks to ensure this isn't impacting the cache in any unexpected way.
kong/cache.lua
Outdated
local function create_wrapper(cb) | ||
s2h_cache[cb] = function(...) | ||
local result, err = cb(...) | ||
if result == nil and err then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is instructed to users of this API that they should never rely on the truthiness/falseness of the first returned value to infer errors (since the library caches misses as well). As such, as a developer I would easily expect that when I write a callback, I can return whichever value I want when the second one is an error (non-nil). While this isn't the typical idiom we try to follow in this codebase (we prefer nil
+ err
), I believe this comes down to usability/robustness: we should only check for non-nil errors here to infer one (disregarding the first return value).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thibaultcha all comments addressed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Let's wait for the tests to double check.
kong/cache.lua
Outdated
@@ -31,6 +31,28 @@ local function log(lvl, ...) | |||
return ngx_log(lvl, "[DB cache] ", ...) | |||
end | |||
|
|||
-- Temporary fix to convert soft callback errors into hard ones. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: 2 line jumps
kong/cache.lua
Outdated
return s2h_cache[cb] | ||
end | ||
|
||
soft_to_hard = function(cb) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: ditto
Merged! |
The callback run inside a
pcall
and the error checking will only deal with thepcall
failing, not with apcall
success and returningnil + err
.This is temporary until the mlcache dependency is updated (upstream lib instead of a copy)