Skip to content
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

Merged
merged 3 commits into from
Jan 23, 2018

Conversation

Tieske
Copy link
Member

@Tieske Tieske commented Jan 16, 2018

The callback run inside a pcall and the error checking will only deal with the pcall failing, not with a pcall success and returning nil + err.

This is temporary until the mlcache dependency is updated (upstream lib instead of a copy)

Copy link
Member

@thibaultcha thibaultcha left a 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) ..
Copy link
Member

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

Copy link
Member Author

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) ..
Copy link
Member

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,
}
Copy link
Member

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(...)
Copy link
Member

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.

@thibaultcha thibaultcha added pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. and removed pr/status/please review labels Jan 20, 2018
@Tieske Tieske added pr/status/please review and removed pr/changes requested Changes were requested to this PR by a maintainer. Please address them and ping back once done. labels Jan 23, 2018
Copy link
Member

@thibaultcha thibaultcha left a 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
Copy link
Member

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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibaultcha all comments addressed

Copy link
Member

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.
Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: ditto

@thibaultcha thibaultcha merged commit de6390e into master Jan 23, 2018
@thibaultcha thibaultcha deleted the fix/cache branch January 23, 2018 23:46
@thibaultcha
Copy link
Member

Merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants