Skip to content

Commit

Permalink
Merge pull request kubernetes#897 from smarterclayton/watch_starting_…
Browse files Browse the repository at this point in the history
…too_early

Watch should start from next index when getting the initial state
  • Loading branch information
brendandburns committed Aug 15, 2014
2 parents 2d7a0c3 + 7c67cbf commit 39d2020
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
9 changes: 7 additions & 2 deletions pkg/tools/etcd_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ func etcdGetInitialWatchState(client EtcdGetSet, key string, recursive bool, inc
return
}
if index, ok := etcdErrorIndex(err); ok {
resourceVersion = index
resourceVersion = index + 1
}
return
}
resourceVersion = resp.EtcdIndex
resourceVersion = resp.EtcdIndex + 1
convertRecursiveResponse(resp.Node, resp, incoming)
return
}
Expand All @@ -442,6 +442,11 @@ func convertRecursiveResponse(node *etcd.Node, response *etcd.Response, incoming
return
}
copied := *response
if node.ModifiedIndex == node.CreatedIndex {
copied.Action = "create"
} else {
copied.Action = "set"
}
copied.Node = node
incoming <- &copied
}
Expand Down
45 changes: 16 additions & 29 deletions pkg/tools/etcd_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ func TestWatch(t *testing.T) {
}

fakeClient.WaitForWatchCompletion()
// when no get can be done AND the server doesn't provide an index, the Watch is 0 (from now)
if fakeClient.WatchIndex != 0 {
t.Errorf("Expected client to be at index %d, got %#v", 0, fakeClient)
// when server returns not found, the watch index starts at the next value (1)
if fakeClient.WatchIndex != 1 {
t.Errorf("Expected client to be at index %d, got %#v", 1, fakeClient)
}

// Test normal case
Expand Down Expand Up @@ -539,51 +539,35 @@ func TestWatchFromZeroIndex(t *testing.T) {
ExpectedVersion uint64
ExpectedType watch.EventType
}{
"last write was a modify": {
"get value created": {
EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: api.EncodeOrDie(pod),
CreatedIndex: 1,
ModifiedIndex: 1,
},
Action: "compareAndSwap",
Action: "get",
EtcdIndex: 2,
},
},
1,
watch.Modified,
},
"last write was a delete": {
EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: api.EncodeOrDie(pod),
ModifiedIndex: 2,
},
PrevNode: &etcd.Node{
Value: api.EncodeOrDie(pod),
ModifiedIndex: 1,
},
Action: "delete",
EtcdIndex: 3,
},
},
2,
watch.Deleted,
watch.Added,
},
"last write was a create": {
"get value modified": {
EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: api.EncodeOrDie(pod),
CreatedIndex: 1,
ModifiedIndex: 2,
},
Action: "create",
Action: "get",
EtcdIndex: 3,
},
},
2,
watch.Added,
watch.Modified,
},
}

Expand All @@ -598,6 +582,9 @@ func TestWatchFromZeroIndex(t *testing.T) {
}

fakeClient.WaitForWatchCompletion()
if e, a := testCase.Response.R.EtcdIndex+1, fakeClient.WatchIndex; e != a {
t.Errorf("%s: expected watch index to be %d, got %d", k, e, a)
}

// the existing node is detected and the index set
event := <-watching.ResultChan()
Expand Down Expand Up @@ -693,8 +680,8 @@ func TestWatchFromNotFound(t *testing.T) {
}

fakeClient.WaitForWatchCompletion()
if fakeClient.WatchIndex != 2 {
t.Errorf("Expected client to wait for %d, got %#v", 2, fakeClient)
if fakeClient.WatchIndex != 3 {
t.Errorf("Expected client to wait for %d, got %#v", 3, fakeClient)
}

watching.Stop()
Expand Down

0 comments on commit 39d2020

Please sign in to comment.