[FW][FIX] registry: avoid re-signaling invalidated cache - #41500
Closed
fw-bot wants to merge 1 commit into
Closed
Conversation
In a multi-threaded environment, when the cache is being cleared through a call to `registry.clear_caches`, it's possible any number of other threads signal the database that the cache has been invalidated, causing extra, unnecessary invalidations, possibly in a looping fashion. A scenario where this can cause issues: 1) There are 2 threads busy handling a request each, [A] & [B]. 2) The database has its ´base_cache_signaling´ sequence incremented. 3) [A] calls `registry.check_signaling`, sees the cache should be invalidated and calls `registry.clear_caches`, setting `registry.cache_invalidated` to True for each model it clears the cache for. 4) [B] finished serving its request, calls `check_signaling`, sees `registry.clear_caches` is True and signals the database by increasing the `base_cache_signaling` sequence. 5) [A] finishes looping through the models being invalidated and attempts to clear the `registry.cache_invalidated`, but it's too late because [B] has already read it. Should 2 or more multi-threaded servers run in parallel on the same database (both handling many concurrent requests), it's possible this triggers alternatively on each server and loops indefinitely. To fix this problem, we avoid setting `registry.cache_invalidated` to True when clearing the cache. It's interesting to note that the cache invalidation is not thread-safe, and can lead to inconsistent cache states during its invalidation & clearing. Fixing this fully requires a bigger design change that may come in the future, but most likely not as a fix in a stable version. X-original-commit: 8a7fe5f
Contributor
Author
|
This PR targets saas-12.3 and is part of the forward-port chain. Further PRs will be created up to master. More info at https://github.com/odoo/odoo/wiki/Mergebot#forward-port |
robodoo
pushed a commit
that referenced
this pull request
Dec 9, 2019
In a multi-threaded environment, when the cache is being cleared through a call to `registry.clear_caches`, it's possible any number of other threads signal the database that the cache has been invalidated, causing extra, unnecessary invalidations, possibly in a looping fashion. A scenario where this can cause issues: 1) There are 2 threads busy handling a request each, [A] & [B]. 2) The database has its ´base_cache_signaling´ sequence incremented. 3) [A] calls `registry.check_signaling`, sees the cache should be invalidated and calls `registry.clear_caches`, setting `registry.cache_invalidated` to True for each model it clears the cache for. 4) [B] finished serving its request, calls `check_signaling`, sees `registry.clear_caches` is True and signals the database by increasing the `base_cache_signaling` sequence. 5) [A] finishes looping through the models being invalidated and attempts to clear the `registry.cache_invalidated`, but it's too late because [B] has already read it. Should 2 or more multi-threaded servers run in parallel on the same database (both handling many concurrent requests), it's possible this triggers alternatively on each server and loops indefinitely. To fix this problem, we avoid setting `registry.cache_invalidated` to True when clearing the cache. It's interesting to note that the cache invalidation is not thread-safe, and can lead to inconsistent cache states during its invalidation & clearing. Fixing this fully requires a bigger design change that may come in the future, but most likely not as a fix in a stable version. closes #41500 X-original-commit: 8a7fe5f Signed-off-by: Denis Vermylen <Icallhimtest@users.noreply.github.com>
fw-bot
deleted the
saas-12.3-11.0-multithreaded-cache-signaling-dve-dHSX-fw
branch
December 23, 2019 11:46
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.
In a multithreaded environment, when the cache is being cleared through
a call to
registry.clear_caches, it's possible any number of otherthreads signal the database that the cache has been invalidated.
One of multiple scenarios where this can cause issues:
check_signaling, sees it should beinvalidated and calls
registry.clear_caches, settingregistry.cache_invalidatedto True while it clears the cache foreach model.
check_signaling, seesregistry.clear_cachesis True and signals the database byincreasing the
base_cache_signalingsequence.Should 2 multi-threaded servers run in parallel on the same database
(both handling many requests), it's possible this triggers alternatively
on each worker and loops indefinitely.
To fix this problem, we avoid setting
registry.cache_invalidatedtoTrue when clearing the cache.
It's interesting to note that the cache invalidation is not thread-safe,
and can be in inconsistent states during its invalidation & clearing.
Fixing this fully requires a bigger design change that may come in the
future, but most likely not as a fix in a stable version.
Forward-Port-Of: #40983