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

Add tracing to listing in Cacher #34925

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/storage/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
return err
}

trace := util.NewTrace(fmt.Sprintf("cacher %v: List", c.objectType.String()))
defer trace.LogIfLong(250 * time.Millisecond)

c.ready.wait()
trace.Step("Ready")

// List elements from cache, with at least 'listRV'.
listPtr, err := meta.GetItemsPtr(listObj)
Expand All @@ -380,10 +384,11 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
}
filter := filterFunction(key, c.keyFunc, pred)

objs, readResourceVersion, err := c.watchCache.WaitUntilFreshAndList(listRV)
objs, readResourceVersion, err := c.watchCache.WaitUntilFreshAndList(listRV, trace)
if err != nil {
return fmt.Errorf("failed to wait for fresh list: %v", err)
}
trace.Step(fmt.Sprintf("Listed %d items from cache", len(objs)))
for _, obj := range objs {
object, ok := obj.(runtime.Object)
if !ok {
Expand All @@ -393,6 +398,7 @@ func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, p
listVal.Set(reflect.Append(listVal, reflect.ValueOf(object).Elem()))
}
}
trace.Step(fmt.Sprintf("Filtered %d items", listVal.Len()))
if c.versioner != nil {
if err := c.versioner.UpdateList(listObj, readResourceVersion); err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion pkg/storage/watch_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/clock"
"k8s.io/kubernetes/pkg/watch"
)
Expand Down Expand Up @@ -206,7 +207,7 @@ func (w *watchCache) List() []interface{} {
return w.store.List()
}

func (w *watchCache) WaitUntilFreshAndList(resourceVersion uint64) ([]interface{}, uint64, error) {
func (w *watchCache) WaitUntilFreshAndList(resourceVersion uint64, trace *util.Trace) ([]interface{}, uint64, error) {
startTime := w.clock.Now()
go func() {
// Wake us up when the time limit has expired. The docs
Expand All @@ -228,6 +229,9 @@ func (w *watchCache) WaitUntilFreshAndList(resourceVersion uint64) ([]interface{
}
w.cond.Wait()
}
if trace != nil {
trace.Step("Cache is fresh enough")
}
return w.store.List(), w.resourceVersion, nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/storage/watch_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func TestWaitUntilFreshAndList(t *testing.T) {
store.Add(makeTestPod("bar", 5))
}()

list, resourceVersion, err := store.WaitUntilFreshAndList(5)
list, resourceVersion, err := store.WaitUntilFreshAndList(5, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func TestWaitUntilFreshAndListTimeout(t *testing.T) {
store.Add(makeTestPod("bar", 5))
}()

_, _, err := store.WaitUntilFreshAndList(5)
_, _, err := store.WaitUntilFreshAndList(5, nil)
if err == nil {
t.Fatalf("unexpected lack of timeout error")
}
Expand All @@ -300,7 +300,7 @@ func TestReflectorForWatchCache(t *testing.T) {
store := newTestWatchCache(5)

{
_, version, err := store.WaitUntilFreshAndList(0)
_, version, err := store.WaitUntilFreshAndList(0, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -323,7 +323,7 @@ func TestReflectorForWatchCache(t *testing.T) {
r.ListAndWatch(wait.NeverStop)

{
_, version, err := store.WaitUntilFreshAndList(10)
_, version, err := store.WaitUntilFreshAndList(10, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down