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

Make activations cache blacklist TTL configurable #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions virtual/activations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import (
)

const (
// TODO: This should be configurable.
activationBlacklistCacheTTL = time.Minute
defaultActivationBlacklistCacheTTL = time.Minute
)

type activations struct {
Expand Down Expand Up @@ -55,12 +54,18 @@ type activations struct {
}

// Dependencies.
registry registry.Registry
moduleStore registry.ModuleStore
environment Environment
goModules map[types.NamespacedIDNoType]Module
customHostFns map[string]func([]byte) ([]byte, error)
gcActorsAfter time.Duration
registry registry.Registry
moduleStore registry.ModuleStore
environment Environment
goModules map[types.NamespacedIDNoType]Module
customHostFns map[string]func([]byte) ([]byte, error)
gcActorsAfter time.Duration
activationBlacklistCacheTTL time.Duration
}

type activationsOptions struct {
gcActorsAfter time.Duration
activationBlacklistCacheTTL time.Duration
}

func newActivations(
Expand All @@ -69,11 +74,17 @@ func newActivations(
moduleStore registry.ModuleStore,
environment Environment,
customHostFns map[string]func([]byte) ([]byte, error),
gcActorsAfter time.Duration,
opts activationsOptions,
) *activations {
gcActorsAfter := opts.gcActorsAfter
activationBlacklistCacheTTL := opts.activationBlacklistCacheTTL

if gcActorsAfter < 0 {
panic(fmt.Sprintf("[invariant violated] illegal value for gcActorsAfter: %d", gcActorsAfter))
}
if activationBlacklistCacheTTL <= 0 {
activationBlacklistCacheTTL = defaultActivationBlacklistCacheTTL
}

blacklist, err := ristretto.NewCache(&ristretto.Config{
NumCounters: maxNumActivationsToCache * 10, // * 10 per the docs.
Expand All @@ -93,13 +104,14 @@ func newActivations(
_blacklist: blacklist,
_actorResourceTracker: newActorResourceTracker(),

log: log.With(slog.String("module", "activations")),
registry: registry,
moduleStore: moduleStore,
environment: environment,
goModules: make(map[types.NamespacedIDNoType]Module),
customHostFns: customHostFns,
gcActorsAfter: gcActorsAfter,
log: log.With(slog.String("module", "activations")),
registry: registry,
moduleStore: moduleStore,
environment: environment,
goModules: make(map[types.NamespacedIDNoType]Module),
customHostFns: customHostFns,
gcActorsAfter: gcActorsAfter,
activationBlacklistCacheTTL: activationBlacklistCacheTTL,
}
a._moduleState.modules = make(map[types.NamespacedID]Module)
return a
Expand Down Expand Up @@ -538,7 +550,7 @@ func (a *activations) shedMemUsage(numBytes int) {
for _, v := range toShed {
key := formatActorCacheKey(nil, v.id.Namespace, v.id.Module, v.id.ID)
if _, ok := a._blacklist.Get(key); !ok {
a._blacklist.SetWithTTL(key, nil, 1, activationBlacklistCacheTTL)
a._blacklist.SetWithTTL(key, nil, 1, a.activationBlacklistCacheTTL)
a.log.Info(
"shedding actor to reduce memory usage",
slog.String("actor_id", v.id.String()))
Expand Down
7 changes: 6 additions & 1 deletion virtual/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ const (
type EnvironmentOptions struct {
// ActivationCacheTTL is the TTL of the activation cache.
ActivationCacheTTL time.Duration
// ActivationBlacklistCacheTTL is the TTL of the activations blacklist cache.
ActivationBlacklistCacheTTL time.Duration
// DisableActivationCache disables the activation cache.
DisableActivationCache bool
// Discovery contains the discovery options.
Expand Down Expand Up @@ -281,7 +283,10 @@ func NewEnvironment(
}
env.randState.rng = rand.New(rand.NewSource(time.Now().UnixNano()))
activations := newActivations(
opts.Logger, reg, moduleStore, env, env.opts.CustomHostFns, opts.GCActorsAfterDurationWithNoInvocations)
opts.Logger, reg, moduleStore, env, env.opts.CustomHostFns, activationsOptions{
gcActorsAfter: opts.GCActorsAfterDurationWithNoInvocations,
activationBlacklistCacheTTL: opts.ActivationBlacklistCacheTTL,
})
env.activations = activations

// Skip confusing log if dnsregistry is being used since it doesn't use the registry-based
Expand Down