This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
generated from cruise-automation/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from alpe/alex/resource_caching_proposal
Proposal for caching admission resources decoding
- Loading branch information
Showing
22 changed files
with
284 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type contextKey int // local to the resource module | ||
|
||
const ( | ||
ctxKeyCache contextKey = iota | ||
) | ||
const ( | ||
cacheKeyPod = iota | ||
cacheKeyPodExec | ||
cacheKeyIngress | ||
) | ||
|
||
type cache struct { | ||
l sync.Mutex | ||
m map[int]interface{} | ||
} | ||
|
||
// getOrSet returns the cached value for the given key. When none exists the passed function is called once to create | ||
// the initial value. When cache is nil no caching happens and the create function is always called. | ||
// Calls are executed thread safe. | ||
func (c *cache) getOrSet(cacheKey int, f func() interface{}) interface{} { | ||
if c == nil { | ||
return f() | ||
} | ||
c.l.Lock() | ||
defer c.l.Unlock() | ||
if p, ok := c.m[cacheKey]; ok { | ||
return p | ||
} | ||
v := f() | ||
c.m[cacheKey] = v | ||
return v | ||
} | ||
|
||
// WithResourceCache adds a resource cache to the context returned. | ||
func WithResourceCache(ctx context.Context) context.Context { | ||
c := &cache{m: make(map[int]interface{}, 1)} | ||
return context.WithValue(ctx, ctxKeyCache, c) | ||
} | ||
|
||
// GetResourceCache returns the cache from the context. Result will return nil when none exists. | ||
func GetResourceCache(ctx context.Context) *cache { | ||
c := ctx.Value(ctxKeyCache) | ||
if c == nil { | ||
return nil | ||
} | ||
return c.(*cache) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestResourceCacheContext(t *testing.T) { | ||
const testRunCount = 2 | ||
const testCacheKey = 999 | ||
specs := map[string]struct { | ||
srcCtx func() context.Context | ||
srcKey int | ||
srcFactoryFunc *creatorMock | ||
expResp interface{} | ||
expCalls int | ||
}{ | ||
"with cache in ctx ": { | ||
srcCtx: func() context.Context { | ||
return WithResourceCache(context.TODO()) | ||
}, | ||
srcKey: testCacheKey, | ||
srcFactoryFunc: &creatorMock{respValue: "myValue"}, | ||
expCalls: 1, | ||
expResp: "myValue", | ||
}, | ||
"with cache filled": { | ||
srcCtx: func() context.Context { | ||
ctx := WithResourceCache(context.TODO()) | ||
GetResourceCache(ctx).getOrSet(testCacheKey, func() interface{} { | ||
return "myValue" | ||
}) | ||
return ctx | ||
}, | ||
srcKey: testCacheKey, | ||
srcFactoryFunc: &creatorMock{respValue: "otherValue"}, | ||
expCalls: 0, | ||
expResp: "myValue", | ||
}, | ||
"with empty ctx": { | ||
srcCtx: context.TODO, | ||
srcKey: testCacheKey, | ||
srcFactoryFunc: &creatorMock{respValue: "foo"}, | ||
expCalls: testRunCount, | ||
expResp: "foo", | ||
}, | ||
} | ||
for msg, spec := range specs { | ||
t.Run(msg, func(t *testing.T) { | ||
ctx := spec.srcCtx() | ||
mock := spec.srcFactoryFunc | ||
for i := 0; i < testRunCount; i++ { | ||
resp := GetResourceCache(ctx).getOrSet(spec.srcKey, mock.CountCall) | ||
if exp, got := spec.expResp, resp; !reflect.DeepEqual(exp, got) { | ||
t.Errorf("expected %v but got %v", exp, got) | ||
} | ||
} | ||
if exp, got := spec.expCalls, mock.called; exp != got { | ||
t.Errorf("expected %d but got %d", exp, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCacheWithConcurrentAccess(t *testing.T) { | ||
const testCacheKey = 999 | ||
const actorCount = 10 | ||
|
||
var awaitStart sync.WaitGroup | ||
awaitStart.Add(actorCount) | ||
var awaitCompleted sync.WaitGroup | ||
awaitCompleted.Add(actorCount) | ||
|
||
actors := make([]*creatorMock, actorCount) | ||
rsp := make(chan interface{}, actorCount) | ||
|
||
c := &cache{m: make(map[int]interface{}, 1)} | ||
for i := 0; i < actorCount; i++ { | ||
actors[i] = &creatorMock{respValue: i} | ||
go func(i int) { | ||
awaitStart.Done() | ||
awaitStart.Wait() // wait for all actors to start sync | ||
rsp <- c.getOrSet(testCacheKey, actors[i].CountCall) | ||
awaitCompleted.Done() | ||
}(i) | ||
} | ||
awaitCompleted.Wait() | ||
|
||
// then only 1 create function should be called | ||
var active *creatorMock | ||
var expResult int | ||
for i, a := range actors { | ||
if a.called != 0 { | ||
if active != nil { | ||
t.Fatal("more than 1 create function called") | ||
} | ||
active = a | ||
expResult = i | ||
} | ||
} | ||
// and all should see the same result | ||
for i := 0; i < actorCount; i++ { | ||
select { | ||
case r := <-rsp: | ||
if exp, got := expResult, r; exp != got { | ||
t.Errorf("expected %v but got %v", exp, got) | ||
} | ||
case <-time.After(time.Millisecond): | ||
t.Fatal("test timeout") | ||
} | ||
} | ||
} | ||
|
||
type creatorMock struct { | ||
l sync.Mutex | ||
called int | ||
respValue interface{} | ||
} | ||
|
||
func (m *creatorMock) CountCall() interface{} { | ||
m.l.Lock() | ||
m.called++ | ||
m.l.Unlock() | ||
return m.respValue | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.