Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add hot-swap capability between count-based and size-based cache modes #6809

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

Merged
merged 11 commits into from
Apr 16, 2025

Conversation

timl3136
Copy link
Member

What changed?
This PR introduces a hot-swap capability between count-based and size-based cache modes in the LRU cache implementation. The key changes include:

  • Added shadow tracking of entry sizes even in count-based mode, allowing seamless transitions to size-based mode and vice versa
  • Enhanced cache eviction logic to handle both count and size-based constraints
  • Improved error handling for oversized entries and cache full scenarios

Why?
The current implementation requires a complete cache restart when switching between count-based and size-based modes, which:

  • Causes unnecessary cache evictions
  • Introduces latency spikes during mode transitions
  • Disrupts application performance during configuration changes
    This change enables smooth transitions between modes while maintaining cache contents, improving system stability and performance.

How did you test it?
I tested the logic with unit test and local testing.

Potential risks
Since it's the base cache, it could break every part that used this cache if there was a bug hidden. We need to rollback immediately if such things happens.

Release notes

Documentation Changes

common/cache/lru.go Show resolved Hide resolved
}
cache.sizeByKey = make(map[interface{}]uint64, opts.InitialCapacity)
if opts.IsSizeBased == nil {
cache.isSizeBased = dynamicproperties.GetBoolPropertyFn(false)
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to panic in this case. I'd like to have the behavior of cache to be completely managed by dynamic config. These implicit logic can make it hard to debug

Copy link
Member Author

Choose a reason for hiding this comment

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

I want to add the panic logic later once we add feature flags for all the cache usages. For now, existing usage will be default to count based.

Copy link
Member

Choose a reason for hiding this comment

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

I'd avoid panic'ing in library level and instead return an error for missing mandatory options

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I will add the error returning and make this into a mandatory flag once we add the feature flags for all other cache usage.

common/cache/lru.go Outdated Show resolved Hide resolved
common/cache/lru.go Outdated Show resolved Hide resolved
}
cache.sizeByKey = make(map[interface{}]uint64, opts.InitialCapacity)
if opts.IsSizeBased == nil {
cache.isSizeBased = dynamicproperties.GetBoolPropertyFn(false)
Copy link
Member

Choose a reason for hiding this comment

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

I'd avoid panic'ing in library level and instead return an error for missing mandatory options

common/cache/lru.go Outdated Show resolved Hide resolved
if c.isSizeBased() {
if valueSize > uint64(c.maxSize()) {
// value is too big to be cached, we also don't want to evict everyone else
return nil, ErrEntryTooBig
Copy link
Member

Choose a reason for hiding this comment

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

This is a good idea but we may need to update callers to work without caching for big workflow executions. Otherwise they will be stuck because caching the workflow execution is mandatory to process the workflow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it, this is similar to when the cache is full of pinned elements and can't evict or add new entries. One way I can think of is to avoid that is by setting a minimum cache size that fits any workflow execution—for example, 50MB if the limit is 30MB.
I'll add a comment to cover this edge case in the cache usage logic.

common/cache/lru.go Show resolved Hide resolved
common/cache/lru.go Outdated Show resolved Hide resolved
common/cache/lru.go Show resolved Hide resolved
common/cache/lru.go Show resolved Hide resolved
common/cache/lru.go Show resolved Hide resolved
common/cache/lru_test.go Outdated Show resolved Hide resolved
common/cache/lru_test.go Show resolved Hide resolved
common/cache/lru_test.go Show resolved Hide resolved
@timl3136 timl3136 enabled auto-merge (squash) April 16, 2025 21:29
@timl3136 timl3136 merged commit a653fa9 into cadence-workflow:master Apr 16, 2025
22 of 23 checks passed
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.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.