Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A fresh worker's
KeyIndexstarts withlast = 0, so its firstadd()(inPrometheus.init, forthe error metric) and its first timer
sync()both runsync_range(0, N): onedict:getper storedkey. On
nginx -s reload, every new worker does this at the same moment, all on the same dictionarylock. New workers don't accept connections until
init_workerreturns, and old workers stall on thelock in their counter flushes.
This is #107. With 96 workers and ~35k keys we saw request stalls over 5 s (TTFB) and upstream
timeouts during every reload. With this change, max TTFB during reload is under 1 s.
#178 serialized the first sync with a lock. That avoids the contention, but each worker still scans
the whole index at startup. This PR removes the scan.
Change
<prefix>rkey_<key> -> slot.add()of a key that is alreadystored costs two
dict:gets (the reverse entry, plus the slot to check it), no matter how manykeys are stored.
dict:add. If twoworkers add the same key at once, the loser drops its slot and uses the winner's.
list()dropsduplicates as a safety net.
sync_deletes(), which is O(1) unlessdelete_countchanged.add()doesn't need a synced index any more; only deletes have to reach
delete_callback.list()(collection) still syncs as before.
deletedset to the dictionary's currentdelete_count, since it hasno lookup tables to invalidate for earlier deletes.
no reverse entries.
Prometheus.initcallsbuild_reverse(). One worker at a time (a lock witha TTL) adds the missing reverse entries, scanning only keys added since the previous call
(
rkey_upto). Workers that skip it may create a duplicate slot for an old key, whichlist()hides.key_countis lower than an occupied slot (e.g. afterflush_all()while aworker was adding a key, see prometheuslib.init is blocked in the init_worker_by_lua_block phase. #162), the insert loop retried the same slot forever. It now steps
past occupied slots and only ever raises
key_count.Cost: one extra dictionary entry per key.
Tests
SimpleDict:addnow returnsfalse, "exists"for an existing key, likengx.shared.DICT:add.The existing tests pass with that.
TestKeyIndextests:add()on a fresh index doesn't scan the index (countsdict:getcalls);build_reverse(), including the incremental scan and skipping while another worker holds the lock;list()dropping duplicates;key_countlower than occupied slots (this test hangs on the current code);sync_deletes().Tested under load on a 96-worker node with ~35k stored keys, by reloading nginx.