Skip to content

Commit

Permalink
cache.clear: clearing root clears everything, not only the root zone
Browse files Browse the repository at this point in the history
Problem was caused by our lookup format where only the root zone starts
with \0 and all other zones start differently. This caused
cache_match('.') to match only data from root zone.
  • Loading branch information
pspacek authored and vcunat committed Aug 17, 2018
1 parent 083ea00 commit 7ac4a45
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Expand Up @@ -79,3 +79,4 @@ files['daemon/lua/kres-gen.lua'].ignore = {'631'} -- Allow overly long lines
files['scripts'].ignore = {'111', '112', '113'}
files['tests'].ignore = {'111', '112', '113'}
files['modules/**/*.test.lua'].ignore = {'111', '112', '113', '121', '122'}
files['daemon/**/*.test.lua'].ignore = {'111', '112', '113', '121', '122'}
26 changes: 17 additions & 9 deletions daemon/cache.test/clear.test.lua
Expand Up @@ -26,9 +26,9 @@ assert(net.interfaces() ~= nil)
-- Self-checks on loaded stuff
assert(#modules.list() > 0)
-- Self-check timers
ev = event.recurrent(1 * sec, function (ev) return 1 end)
ev = event.recurrent(1 * sec, function () return 1 end)
event.cancel(ev)
ev = event.after(0, function (ev) return 1 end)
ev = event.after(0, function () return 1 end)


-- import fake root zone
Expand Down Expand Up @@ -61,8 +61,8 @@ local function import_zone()
worker.sleep(0.2) -- zimport is delayed by 100 ms from function call
-- sanity checks - cache must be filled in
ok(cache.count() > 0, 'cache is not empty after import')
check_answer('root apex is cache',
'.', kres.type.NS, kres.rcode.NOERROR)
check_answer('root apex is in cache',
'.', kres.type.NS, kres.rcode.NOERROR)
check_answer('deep subdomain is in cache',
'a.b.subtree1.', kres.type.AAAA, kres.rcode.NOERROR)

Expand Down Expand Up @@ -120,23 +120,22 @@ end

local function test_callback()
local test_name = '20r.subtree2.'
local test_maxcount = 1
local test_exactname = true
local test_rrtype = nil
local test_maxcount = 1
local test_chunksize = 1
local test_prev_state = { works = true }
local function check_callback(name, exact_name, rr_type, chunk_size, callback, prev_state, errors)
is(errors.count, 1, 'callback received correct # of removed records')
is(test_name, name, 'callback received subtree name')
is(test_exactname, exact_name, 'callback received exact_name')
is(test_rrtype, rrtype, 'callback received rr_type')
is(test_chunksize, chunksize, 'callback received maxcount')
is(test_rrtype, rr_type, 'callback received rr_type')
is(test_chunksize, chunk_size, 'callback received chunk_size')
is(check_callback, callback, 'callback received reference to itself')
is(type(errors), 'table', 'callback received table of errors')
same(test_prev_state, prev_state, 'callback received previous state')
return 666
end
same(cache.clear(test_name, test_exactname, test_rrtype, test_maxcount, check_callback, test_prev_state),
same(cache.clear(test_name, test_exactname, test_rrtype, test_chunksize, check_callback, test_prev_state),
666, 'first callback return value is passed to cache.clear() caller')
local cnt_before_wait = cache.count()
worker.sleep(0.2)
Expand Down Expand Up @@ -173,6 +172,13 @@ local function test_apex()
'a.b.subtree1.', kres.type.NULL, kres.rcode.SERVFAIL)
end

local function test_root()
local prev_count = cache.count()
res = cache.clear('.')
is(res.count, prev_count, 'full clear reports correct number of entries')
is(cache.count(), 0, 'clearing root clears everything')
end

local function test_complete_flush()
local prev_count = cache.count()
res = cache.clear()
Expand All @@ -190,5 +196,7 @@ return {
test_subtree_limit,
test_apex,
import_zone,
test_root,
import_zone,
test_complete_flush,
}
3 changes: 2 additions & 1 deletion daemon/lua/sandbox.lua
Expand Up @@ -159,7 +159,8 @@ setmetatable(modules, {


cache.clear = function (name, exact_name, rr_type, chunk_size, callback, prev_state)
if name == nil then -- keep same output format as for 'standard' clear
if name == nil or (name == '.' and not exact_name) then
-- keep same output format as for 'standard' clear
local total_count = cache.count()
if not cache.clear_everything() then
error('unable to clear everything')
Expand Down

0 comments on commit 7ac4a45

Please sign in to comment.