Skip to content
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

Fix LRU cache eviction behavior #2353

Merged
merged 1 commit into from
Jan 7, 2022
Merged

Fix LRU cache eviction behavior #2353

merged 1 commit into from
Jan 7, 2022

Conversation

wxing1292
Copy link
Contributor

What changed?

  • Before this PR, LRU cache will only attempt to evict the last access cache item, if this item is pinned, cache is considered full.
  • After this PR, LRU cache will attempt to evict the last access & evictable cache item, even if this item is in the middle.
  • Update unit test

Why?
Fix cache eviction behavior, so cache can be better utilized.

How did you test it?
New tests

Potential risks
N/A

Is hotfix candidate?
Yes

@wxing1292 wxing1292 requested review from dynajoe, samarabbas, meiliang86, yiminc and a team January 7, 2022 00:31
* Before this PR, LRU cache will only attempt to evict the last access cache item, if this item is pinned, cache is considered full.
* After this PR, LRU cache will attempt to evict the last access & evictable cache item, even if this item is in the middle.
* Update unit test
return
}

// entry.refCount > 0
Copy link
Member

@dynajoe dynajoe Jan 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed there's no guard in Release to prevent decrementing refCount to less than zero: https://github.com/temporalio/temporal/pull/2353/files#diff-2d61d82d9a8acd1031ca31021b40eb5f8766840ac54b4113dba6b50e8e033afeR241

albeit unlikely - a bug could cause the refCount to be less than zero. In which case this assertion would not be correct (refCount > 0).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ref count must be maintained correctly, by the caller (meaning if double release ever happen, the behavior is undefined)

the reference count > 0 means whether the cache item is being used, e.g. cache cannot evict this item if ref count > 0

the reason to use a refcount here is to guarantee all mutation to a mutable state must be serialized

  • caller fist get the mutable state cache entry
  • caller then use the mutable state (from the cache entry) by first locking it

about double release, there are few things that can be done

  • if ref count < 0, panic (this should be long term solution, not suitable for patch)
  • do <= 0 check instead of == 0 check, this will prevent some weird behavior, but when refcount < 0, system is already broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

synced offline, this PR is ok for patching

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! Looks good to me.

@wxing1292 wxing1292 merged commit 9ed152f into temporalio:master Jan 7, 2022
@wxing1292 wxing1292 deleted the cache-eviction branch January 7, 2022 01:29
yiminc pushed a commit that referenced this pull request Jan 7, 2022
* Before this PR, LRU cache will only attempt to evict the last access cache item, if this item is pinned, cache is considered full.
* After this PR, LRU cache will attempt to evict the last access & evictable cache item, even if this item is in the middle.
* Update unit test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants