Repository navigation
Conversation
df698c7 to
7c46e69
Compare
|
Sorry for the low-effort review, I just lack the time to get enough into the project to have a strong opinion. But this is what Bob said about it. I trust your judgment whether these make sense or not: Review of #1615: filter by keys to avoid expensive shared dict readSummary: the change preserves behavior for the target service, but it makes an existing I read the diff and the surrounding code, and checked What the code change doesThe PR touches only In
Everything else is only re-indented. Before, every key in the Does it preserve behavior?For normal cases, yes.
Small differences:
A hidden problem: the 1024-key limit
Once the dict holds more than 1024 live keys, The benchmark seems to show this. With 0 other services (150 keys), Performance impact
Recommendations
|
|
I was familiar with this file already so I took a look as well - the PRs change is very minimal and a performance improvement. The LRU dict updates from @akostadinov comment are a real hidden change that is reproducible (see below). This could be an impossible edge case that might require re-configuration, or timer tasks crashing for some reason. OTOH the effect is bad enough we might want to address this - @tkan145 is the best person to decide if it's something to worry about. Besides this just the changelog entry needs a correction. To reproduce I've run the slightly modified benchmark with
Conditions for the edge case:
Scriptlocal root = require('pl.path').currentdir()
package.path = root .. '/?.lua;' ..
root .. '/gateway/src/?.lua;' ..
root .. '/gateway/src/apicast/policy/3scale_batcher/?.lua;' ..
package.path
local monotonic_msec = require("resty.core.time").monotonic_msec
local update_time = ngx.update_time
local print = print
local Usage = require('apicast.usage')
local s, e
---------------------------------------------------------------------------
-- Benchmark: reports_batcher add/get_all, scaled by number of services
-- sharing the 'batched_reports' shdict.
--
-- This isolates the Lua-level cost of ReportsBatcher:get_all() from
-- end-to-end request latency (dominated by the backend auth round trip,
-- which swamps anything measurable here). It shows how get_all()'s cost for
-- ONE service grows as OTHER services accumulate keys in the same shared
-- dict, since storage:get_keys() has no way to filter by prefix and returns
-- every key in the dict regardless of which service is being flushed.
---------------------------------------------------------------------------
print('\n--- reports_batcher add/get_all (shared dict scan cost) ---')
local ReportsBatcher = require('apicast.policy.3scale_batcher.reports_batcher')
local Transaction = require('apicast.policy.3scale_batcher.transaction')
local GET_ALL_ITERATIONS = 1000
local CREDS_PER_SERVICE = 50
local METRICS_PER_CRED = 3
local function add_reports_for(reports_batcher, service_id, creds_per_service, metrics_per_cred)
for c = 1, creds_per_service do
local creds = { user_key = 'user_' .. service_id .. '_' .. c }
local usage = Usage.new()
for m = 1, metrics_per_cred do
usage:add('metric_' .. m, 1)
end
reports_batcher:add(Transaction.new(service_id, creds, usage))
end
end
local function print_histogram(histogram)
local returned_counts = {}
for returned in pairs(histogram) do
returned_counts[#returned_counts + 1] = returned
end
table.sort(returned_counts)
print(string.format(' reports returned per call (expected %d):',
CREDS_PER_SERVICE * METRICS_PER_CRED))
for _, returned in ipairs(returned_counts) do
print(string.format(' %4d reports: %4d calls', returned, histogram[returned]))
end
end
local function bench_get_all(num_other_services)
ngx.shared.batched_reports:flush_all()
ngx.shared.batched_reports:flush_expired()
local reports_batcher = ReportsBatcher.new(
ngx.shared.batched_reports, 'batched_reports_locks')
local target_service = 'svc_target'
-- Other services' pending reports that get_all(target_service) has to
-- scan past and discard on every call.
for s = 1, num_other_services do
add_reports_for(reports_batcher, 'svc_other_' .. s, CREDS_PER_SERVICE, METRICS_PER_CRED)
end
-- histogram[number of reports returned by get_all] = number of calls
local histogram = {}
update_time()
s = monotonic_msec()
for _ = 1, GET_ALL_ITERATIONS do
-- get_all() deletes the target service's keys as it collects them, so
-- repopulate before each call to keep the scan size constant.
add_reports_for(reports_batcher, target_service, CREDS_PER_SERVICE, METRICS_PER_CRED)
local returned = #reports_batcher:get_all(target_service)
histogram[returned] = (histogram[returned] or 0) + 1
end
update_time()
e = monotonic_msec()
print(string.format(
' get_all (own creds=%d, other services=%d, total keys~=%d): ',
CREDS_PER_SERVICE, num_other_services,
CREDS_PER_SERVICE * METRICS_PER_CRED * (num_other_services + 1)),
e - s, ' ms for ', GET_ALL_ITERATIONS, ' calls')
print_histogram(histogram)
end
---------------------------------------------------------------------------
-- Window rotation: does get_all(target) ever reach the target's keys when
-- other services' keys fill the get_keys() window?
--
-- Unlike bench_get_all, every key is added ONCE and never touched by the
-- benchmark again, so the only thing that can change the LRU order between
-- calls is get_all() itself (the per-key storage:get() moves the key to the
-- head of the LRU). The other services have no timer here, so nothing
-- collects their keys.
--
-- get_keys() returns at most GET_KEYS_WINDOW keys, so a get_all() that moves
-- the keys it scans to the LRU head needs about foreign_keys / window calls
-- to reach the target. The loop stops at the first call that returns
-- reports, or after twice that many calls (plus a margin) if none does.
---------------------------------------------------------------------------
local GET_KEYS_WINDOW = 1024
local function bench_rotation(num_other_services)
ngx.shared.batched_reports:flush_all()
ngx.shared.batched_reports:flush_expired()
local reports_batcher = ReportsBatcher.new(
ngx.shared.batched_reports, 'batched_reports_locks')
local target_service = 'svc_target'
for o = 1, num_other_services do
add_reports_for(reports_batcher, 'svc_other_' .. o, CREDS_PER_SERVICE, METRICS_PER_CRED)
end
-- Added last, so the target's keys start at the head of the LRU (the last
-- ones get_keys() returns).
add_reports_for(reports_batcher, target_service, CREDS_PER_SERVICE, METRICS_PER_CRED)
local foreign_keys = CREDS_PER_SERVICE * METRICS_PER_CRED * num_other_services
local max_calls = 2 * math.ceil(foreign_keys / GET_KEYS_WINDOW) + 5
local first_hit
for call = 1, max_calls do
if #reports_batcher:get_all(target_service) > 0 then
first_hit = call
break
end
end
print(string.format(
' rotation (other services=%d, total keys~=%d): target %s',
num_other_services,
CREDS_PER_SERVICE * METRICS_PER_CRED * (num_other_services + 1),
first_hit and ('first collected on call ' .. first_hit)
or ('NOT collected in ' .. max_calls .. ' calls')))
end
bench_get_all(0)
bench_get_all(10)
bench_get_all(50)
bench_get_all(200)
print('\n--- window rotation (keys added once, get_all called repeatedly) ---')
bench_rotation(10)
bench_rotation(50)
bench_rotation(200)
masterthis PR |
What
Previously, we need to fetches all keys from the shared dict, then loop through each key and retrieve each key from the shared_dict and discard all keys that does not match service_id. This PR will filter by keys to avoid expensive shared dict read.
Benchmark
Benchmark script
Details
Run with
Results
Before
After