Skip to content

Commit

Permalink
Add cache for workflow specific in memory data (#5594)
Browse files Browse the repository at this point in the history
What changed?
Adding a struct for storing in memory workflow specific data. It will first be used for storing workflow specific rate limiters, so we can rate limit too many requests to the same workflowID.

In the future it could store e.g. the current state of the workflow (running, terminated), query answers etc.

Why?
This is part of the general workflow specific rate limits project

How did you test it?
Unit tests

Potential risks
No risk, just adding the struct

Release notes

Documentation Changes
  • Loading branch information
jakobht authored Jan 18, 2024
1 parent 421a320 commit bed6774
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/quotas/dynamicratelimiterfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//go:generate mockgen -package=$GOPACKAGE -destination=limiterfactory_mock.go github.com/uber/cadence/common/quotas LimiterFactory

package quotas

import "github.com/uber/cadence/common/dynamicconfig"
Expand Down
2 changes: 2 additions & 0 deletions common/quotas/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//go:generate mockgen -package=$GOPACKAGE -destination=limiter_mock.go github.com/uber/cadence/common/quotas Limiter

package quotas

import (
Expand Down
100 changes: 100 additions & 0 deletions common/quotas/limiter_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions common/quotas/limiterfactory_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions service/history/workflowcache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// The MIT License (MIT)

// Copyright (c) 2017-2020 Uber Technologies Inc.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package workflowcache

import (
"time"

"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/quotas"
)

// WFCache is a per workflow cache used for workflow specific in memory data
type WFCache interface {
AllowExternal(domainID string, workflowID string) bool
AllowInternal(domainID string, workflowID string) bool
}

type wfCache struct {
lru cache.Cache
externalLimiterFactory quotas.LimiterFactory
internalLimiterFactory quotas.LimiterFactory
}

type cacheKey struct {
domainID string
workflowID string
}

type cacheValue struct {
externalRateLimiter quotas.Limiter
internalRateLimiter quotas.Limiter
}

// Params is the parameters for a new WFCache
type Params struct {
TTL time.Duration
MaxCount int
ExternalLimiterFactory quotas.LimiterFactory
InternalLimiterFactory quotas.LimiterFactory
}

// New creates a new WFCache
func New(params Params) WFCache {
return &wfCache{
lru: cache.New(&cache.Options{
TTL: params.TTL,
Pin: true,
MaxCount: params.MaxCount,
}),
externalLimiterFactory: params.ExternalLimiterFactory,
internalLimiterFactory: params.InternalLimiterFactory,
}
}

// AllowExternal returns true if the rate limiter for this domain/workflow allows an external request
func (c *wfCache) AllowExternal(domainID string, workflowID string) bool {
// Locking is not needed because both getCacheItem and the rate limiter are thread safe
value := c.getCacheItem(domainID, workflowID)
return value.externalRateLimiter.Allow()
}

// AllowInternal returns true if the rate limiter for this domain/workflow allows an internal request
func (c *wfCache) AllowInternal(domainID string, workflowID string) bool {
// Locking is not needed because both getCacheItem and the rate limiter are thread safe
value := c.getCacheItem(domainID, workflowID)
return value.internalRateLimiter.Allow()
}

func (c *wfCache) getCacheItem(domainID string, workflowID string) *cacheValue {
// The underlying lru cache is thread safe, so there is no need to lock
key := cacheKey{
domainID: domainID,
workflowID: workflowID,
}

value, ok := c.lru.Get(key).(*cacheValue)

if !ok {
value = &cacheValue{
externalRateLimiter: c.externalLimiterFactory.GetLimiter(domainID),
internalRateLimiter: c.internalLimiterFactory.GetLimiter(domainID),
}
c.lru.PutIfNotExist(key, value)
}

return value
}
Loading

0 comments on commit bed6774

Please sign in to comment.