-
Notifications
You must be signed in to change notification settings - Fork 136
Ensure that ActiveContextCache only enters a given combination of (activeCtx, localCtx) once in its internal FIFO #44
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
Open
kacarstensen-shift
wants to merge
1
commit into
digitalbazaar:master
from
kacarstensen-shift:fix-active-context-cache
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
If we do this, then we'll try and fail to evict the same key combination multiple times, leading to KeyErrors.
Panaetius
commented
Nov 14, 2019
I'm surprised this PR has been open for this long.
I ran into the same issue, though I fixed it by changing set to:
def set(self, active_ctx, local_ctx, result):
if len(self.order) == self.size:
entry = self.order.popleft()
if sum(
e['activeCtx'] == entry['activeCtx'] and
e['localCtx'] == entry['localCtx'] for e in self.order
) == 0:
# only delete from cache if it doesn't exist in context deque
del self.cache[entry['activeCtx']][entry['localCtx']]
key1 = json.dumps(active_ctx)
key2 = json.dumps(local_ctx)
self.order.append({'activeCtx': key1, 'localCtx': key2})
self.cache.setdefault(key1, {})[key2] = json.loads(json.dumps(result))
So only remove an entry from cache if it's not anywhere in the deque, this way contexts don't get evicted from cache if they're still used somewhere else, but the deque itself is still limited to self.size number of entries.
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.
If
ActiveContextCache.setis called multiple times during processing with a particular combination ofactiveCtxandlocalCtx,ActiveContextCache.ordergets out of sync withActiveContextCache.cache(i.e., multiple copies of a particular key combination can exist inordereven though only one copy can be cached at any given time). This causes issues later in the cache's lifecycle when it begins to evict previously set elements. The first instance of a given (activeCtx,localCtx) found inActiveContextCache.orderwill be evicted fromActiveContextCache.cachesuccessfully, but subsequent copies will cause in aKeyError. To resolve this, I made inclusion inActiveContextCache.orderconditional on the (activeCtx,localCtx) combination not already existing inActiveContextCache.cache. This keeps the two data structures are in sync and prevents theKeyErrors.