client: clarify relationship of AfterIndex and waitIndex

This commit is contained in:
Brian Waldon 2015-01-29 14:27:07 -08:00 committed by Yicheng Qin
parent 09017af35e
commit cd85451971
4 changed files with 42 additions and 40 deletions

View File

@ -335,8 +335,10 @@ func (k *httpKeysAPI) Watcher(key string, opts *WatcherOptions) Watcher {
} }
if opts != nil { if opts != nil {
act.AfterIndex = opts.AfterIndex
act.Recursive = opts.Recursive act.Recursive = opts.Recursive
if opts.AfterIndex > 0 {
act.WaitIndex = opts.AfterIndex + 1
}
} }
return &httpWatcher{ return &httpWatcher{
@ -361,7 +363,7 @@ func (hw *httpWatcher) Next(ctx context.Context) (*Response, error) {
return nil, err return nil, err
} }
hw.nextWait.AfterIndex = resp.Node.ModifiedIndex + 1 hw.nextWait.WaitIndex = resp.Node.ModifiedIndex + 1
return resp, nil return resp, nil
} }
@ -397,7 +399,7 @@ func (g *getAction) HTTPRequest(ep url.URL) *http.Request {
type waitAction struct { type waitAction struct {
Prefix string Prefix string
Key string Key string
AfterIndex uint64 WaitIndex uint64
Recursive bool Recursive bool
} }
@ -406,7 +408,7 @@ func (w *waitAction) HTTPRequest(ep url.URL) *http.Request {
params := u.Query() params := u.Query()
params.Set("wait", "true") params.Set("wait", "true")
params.Set("waitIndex", strconv.FormatUint(w.AfterIndex, 10)) params.Set("waitIndex", strconv.FormatUint(w.WaitIndex, 10))
params.Set("recursive", strconv.FormatBool(w.Recursive)) params.Set("recursive", strconv.FormatBool(w.Recursive))
u.RawQuery = params.Encode() u.RawQuery = params.Encode()

View File

@ -154,23 +154,23 @@ func TestWaitAction(t *testing.T) {
wantHeader := http.Header{} wantHeader := http.Header{}
tests := []struct { tests := []struct {
afterIndex uint64 waitIndex uint64
recursive bool recursive bool
wantQuery string wantQuery string
}{ }{
{ {
recursive: false, recursive: false,
afterIndex: uint64(0), waitIndex: uint64(0),
wantQuery: "recursive=false&wait=true&waitIndex=0", wantQuery: "recursive=false&wait=true&waitIndex=0",
}, },
{ {
recursive: false, recursive: false,
afterIndex: uint64(12), waitIndex: uint64(12),
wantQuery: "recursive=false&wait=true&waitIndex=12", wantQuery: "recursive=false&wait=true&waitIndex=12",
}, },
{ {
recursive: true, recursive: true,
afterIndex: uint64(12), waitIndex: uint64(12),
wantQuery: "recursive=true&wait=true&waitIndex=12", wantQuery: "recursive=true&wait=true&waitIndex=12",
}, },
} }
@ -178,7 +178,7 @@ func TestWaitAction(t *testing.T) {
for i, tt := range tests { for i, tt := range tests {
f := waitAction{ f := waitAction{
Key: "/foo/bar", Key: "/foo/bar",
AfterIndex: tt.afterIndex, WaitIndex: tt.waitIndex,
Recursive: tt.recursive, Recursive: tt.recursive,
} }
got := *f.HTTPRequest(ep) got := *f.HTTPRequest(ep)
@ -188,7 +188,7 @@ func TestWaitAction(t *testing.T) {
err := assertRequest(got, "GET", wantURL, wantHeader, nil) err := assertRequest(got, "GET", wantURL, wantHeader, nil)
if err != nil { if err != nil {
t.Errorf("#%d: %v", i, err) t.Errorf("#%d: unexpected error: %#v", i, err)
} }
} }
} }
@ -631,7 +631,7 @@ func TestHTTPWatcherNextWaitAction(t *testing.T) {
Prefix: "/pants", Prefix: "/pants",
Key: "/foo/bar", Key: "/foo/bar",
Recursive: true, Recursive: true,
AfterIndex: 19, WaitIndex: 19,
} }
client := &actionAssertingHTTPClient{ client := &actionAssertingHTTPClient{
@ -655,7 +655,7 @@ func TestHTTPWatcherNextWaitAction(t *testing.T) {
Prefix: "/pants", Prefix: "/pants",
Key: "/foo/bar", Key: "/foo/bar",
Recursive: true, Recursive: true,
AfterIndex: 22, WaitIndex: 22,
} }
watcher := &httpWatcher{ watcher := &httpWatcher{
@ -705,7 +705,7 @@ func TestHTTPWatcherNextFail(t *testing.T) {
Prefix: "/pants", Prefix: "/pants",
Key: "/foo/bar", Key: "/foo/bar",
Recursive: true, Recursive: true,
AfterIndex: 19, WaitIndex: 19,
} }
watcher := &httpWatcher{ watcher := &httpWatcher{

View File

@ -190,7 +190,7 @@ func (d *discovery) createSelf(contents string) error {
} }
// ensure self appears on the server we connected to // ensure self appears on the server we connected to
w := d.c.Watcher(d.selfKey(), &client.WatcherOptions{AfterIndex: resp.Node.CreatedIndex}) w := d.c.Watcher(d.selfKey(), &client.WatcherOptions{AfterIndex: resp.Node.CreatedIndex - 1})
_, err = w.Next(context.Background()) _, err = w.Next(context.Background())
return err return err
} }
@ -279,7 +279,7 @@ func (d *discovery) waitNodes(nodes []*client.Node, size int, index uint64) ([]*
nodes = nodes[:size] nodes = nodes[:size]
} }
// watch from the next index // watch from the next index
w := d.c.Watcher(d.cluster, &client.WatcherOptions{AfterIndex: index + 1, Recursive: true}) w := d.c.Watcher(d.cluster, &client.WatcherOptions{AfterIndex: index, Recursive: true})
all := make([]*client.Node, len(nodes)) all := make([]*client.Node, len(nodes))
copy(all, nodes) copy(all, nodes)
for _, n := range all { for _, n := range all {

View File

@ -141,7 +141,7 @@ func TestForceNewCluster(t *testing.T) {
cancel() cancel()
// ensure create has been applied in this machine // ensure create has been applied in this machine
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout) ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
if _, err := kapi.Watcher("/foo", &client.WatcherOptions{AfterIndex: resp.Node.ModifiedIndex}).Next(ctx); err != nil { if _, err := kapi.Watcher("/foo", &client.WatcherOptions{AfterIndex: resp.Node.ModifiedIndex - 1}).Next(ctx); err != nil {
t.Fatalf("unexpected watch error: %v", err) t.Fatalf("unexpected watch error: %v", err)
} }
cancel() cancel()
@ -162,7 +162,7 @@ func TestForceNewCluster(t *testing.T) {
kapi = client.NewKeysAPI(cc) kapi = client.NewKeysAPI(cc)
// ensure force restart keep the old data, and new cluster can make progress // ensure force restart keep the old data, and new cluster can make progress
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout) ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
if _, err := kapi.Watcher("/foo", &client.WatcherOptions{AfterIndex: resp.Node.ModifiedIndex}).Next(ctx); err != nil { if _, err := kapi.Watcher("/foo", &client.WatcherOptions{AfterIndex: resp.Node.ModifiedIndex - 1}).Next(ctx); err != nil {
t.Fatalf("unexpected watch error: %v", err) t.Fatalf("unexpected watch error: %v", err)
} }
cancel() cancel()
@ -188,7 +188,7 @@ func clusterMustProgress(t *testing.T, membs []*member) {
mcc := mustNewHTTPClient(t, []string{u}) mcc := mustNewHTTPClient(t, []string{u})
mkapi := client.NewKeysAPI(mcc) mkapi := client.NewKeysAPI(mcc)
mctx, mcancel := context.WithTimeout(context.Background(), requestTimeout) mctx, mcancel := context.WithTimeout(context.Background(), requestTimeout)
if _, err := mkapi.Watcher(key, &client.WatcherOptions{AfterIndex: resp.Node.ModifiedIndex}).Next(mctx); err != nil { if _, err := mkapi.Watcher(key, &client.WatcherOptions{AfterIndex: resp.Node.ModifiedIndex - 1}).Next(mctx); err != nil {
t.Fatalf("#%d: watch on %s error: %v", i, u, err) t.Fatalf("#%d: watch on %s error: %v", i, u, err)
} }
mcancel() mcancel()