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 watch cache treat resourceVersion consistent with uncached watch #24008

Merged
merged 1 commit into from
Apr 12, 2016
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
3 changes: 3 additions & 0 deletions pkg/storage/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/watch"

Expand Down Expand Up @@ -542,6 +543,8 @@ func (c *cacheWatcher) sendWatchCacheEvent(event watchCacheEvent) {
}

func (c *cacheWatcher) process(initEvents []watchCacheEvent) {
defer utilruntime.HandleCrash()

for _, event := range initEvents {
c.sendWatchCacheEvent(event)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/storage/cacher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package storage_test
import (
"fmt"
"reflect"
goruntime "runtime"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -159,15 +160,19 @@ func TestList(t *testing.T) {
}

func verifyWatchEvent(t *testing.T, w watch.Interface, eventType watch.EventType, eventObject runtime.Object) {
_, _, line, _ := goruntime.Caller(1)
select {
case event := <-w.ResultChan():
if e, a := eventType, event.Type; e != a {
t.Logf("(called from line %d)", line)
t.Errorf("Expected: %s, got: %s", eventType, event.Type)
}
if e, a := eventObject, event.Object; !api.Semantic.DeepDerivative(e, a) {
t.Logf("(called from line %d)", line)
t.Errorf("Expected (%s): %#v, got: %#v", eventType, e, a)
}
case <-time.After(wait.ForeverTestTimeout):
t.Logf("(called from line %d)", line)
t.Errorf("Timed out waiting for an event")
}
}
Expand Down Expand Up @@ -236,7 +241,6 @@ func TestWatch(t *testing.T) {
}
defer initialWatcher.Stop()

verifyWatchEvent(t, initialWatcher, watch.Added, podFoo)
verifyWatchEvent(t, initialWatcher, watch.Modified, podFooPrime)

// Now test watch from "now".
Expand Down Expand Up @@ -335,7 +339,6 @@ func TestFiltering(t *testing.T) {
}
defer watcher.Stop()

verifyWatchEvent(t, watcher, watch.Added, podFoo)
verifyWatchEvent(t, watcher, watch.Deleted, podFooFiltered)
verifyWatchEvent(t, watcher, watch.Added, podFoo)
verifyWatchEvent(t, watcher, watch.Modified, podFooPrime)
Expand Down
9 changes: 4 additions & 5 deletions pkg/storage/watch_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,13 @@ func (w *watchCache) GetAllEventsSinceThreadUnsafe(resourceVersion uint64) ([]wa
}
return result, nil
}
if resourceVersion < oldest {
return nil, errors.NewGone(fmt.Sprintf("too old resource version: %d (%d)", resourceVersion, oldest))
if resourceVersion < oldest-1 {
return nil, errors.NewGone(fmt.Sprintf("too old resource version: %d (%d)", resourceVersion, oldest-1))
}

// Binary search the smallest index at which resourceVersion is not smaller than
// the given one.
// Binary search the smallest index at which resourceVersion is greater than the given one.
f := func(i int) bool {
return w.cache[(w.startIndex+i)%w.capacity].resourceVersion >= resourceVersion
return w.cache[(w.startIndex+i)%w.capacity].resourceVersion > resourceVersion
}
first := sort.Search(size, f)
result := make([]watchCacheEvent, size-first)
Expand Down
22 changes: 11 additions & 11 deletions pkg/storage/watch_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestWatchCacheBasic(t *testing.T) {
func TestEvents(t *testing.T) {
store := newTestWatchCache(5)

store.Add(makeTestPod("pod", 2))
store.Add(makeTestPod("pod", 3))

// Test for Added event.
{
Expand All @@ -145,7 +145,7 @@ func TestEvents(t *testing.T) {
if result[0].Type != watch.Added {
t.Errorf("unexpected event type: %v", result[0].Type)
}
pod := makeTestPod("pod", uint64(2))
pod := makeTestPod("pod", uint64(3))
if !api.Semantic.DeepEqual(pod, result[0].Object) {
t.Errorf("unexpected item: %v, expected: %v", result[0].Object, pod)
}
Expand All @@ -154,8 +154,8 @@ func TestEvents(t *testing.T) {
}
}

store.Update(makeTestPod("pod", 3))
store.Update(makeTestPod("pod", 4))
store.Update(makeTestPod("pod", 5))

// Test with not full cache.
{
Expand All @@ -176,22 +176,22 @@ func TestEvents(t *testing.T) {
if result[i].Type != watch.Modified {
t.Errorf("unexpected event type: %v", result[i].Type)
}
pod := makeTestPod("pod", uint64(i+3))
pod := makeTestPod("pod", uint64(i+4))
if !api.Semantic.DeepEqual(pod, result[i].Object) {
t.Errorf("unexpected item: %v, expected: %v", result[i].Object, pod)
}
prevPod := makeTestPod("pod", uint64(i+2))
prevPod := makeTestPod("pod", uint64(i+3))
if !api.Semantic.DeepEqual(prevPod, result[i].PrevObject) {
t.Errorf("unexpected item: %v, expected: %v", result[i].PrevObject, prevPod)
}
}
}

for i := 5; i < 9; i++ {
for i := 6; i < 10; i++ {
store.Update(makeTestPod("pod", uint64(i)))
}

// Test with full cache - there should be elements from 4 to 8.
// Test with full cache - there should be elements from 5 to 9.
{
_, err := store.GetAllEventsSince(3)
if err == nil {
Expand All @@ -207,15 +207,15 @@ func TestEvents(t *testing.T) {
t.Fatalf("unexpected events: %v", result)
}
for i := 0; i < 5; i++ {
pod := makeTestPod("pod", uint64(i+4))
pod := makeTestPod("pod", uint64(i+5))
if !api.Semantic.DeepEqual(pod, result[i].Object) {
t.Errorf("unexpected item: %v, expected: %v", result[i].Object, pod)
}
}
}

// Test for delete event.
store.Delete(makeTestPod("pod", uint64(9)))
store.Delete(makeTestPod("pod", uint64(10)))

{
result, err := store.GetAllEventsSince(9)
Expand All @@ -228,11 +228,11 @@ func TestEvents(t *testing.T) {
if result[0].Type != watch.Deleted {
t.Errorf("unexpected event type: %v", result[0].Type)
}
pod := makeTestPod("pod", uint64(9))
pod := makeTestPod("pod", uint64(10))
if !api.Semantic.DeepEqual(pod, result[0].Object) {
t.Errorf("unexpected item: %v, expected: %v", result[0].Object, pod)
}
prevPod := makeTestPod("pod", uint64(8))
prevPod := makeTestPod("pod", uint64(9))
if !api.Semantic.DeepEqual(prevPod, result[0].PrevObject) {
t.Errorf("unexpected item: %v, expected: %v", result[0].PrevObject, prevPod)
}
Expand Down