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

fix: canceled should always be set to true when cancel a watch request #373

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions pkg/logstructured/logstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,17 +425,20 @@ func (l *LogStructured) Watch(ctx context.Context, prefix string, revision int64
}

result := make(chan []*server.Event, 100)
wr := server.WatchResult{Events: result}
errc := make(chan error, 1)
wr := server.WatchResult{Events: result, Errorc: errc}

rev, kvs, err := l.log.After(ctx, prefix, revision, 0)
if err != nil {
if !errors.Is(err, context.Canceled) {
logrus.Errorf("Failed to list %s for revision %d: %v", prefix, revision, err)
}
if err == server.ErrCompacted {
compact, _ := l.log.CompactRevision(ctx)
wr.CompactRevision = compact
wr.CurrentRevision = rev
if err == server.ErrCompacted {
compact, _ := l.log.CompactRevision(ctx)
wr.CompactRevision = compact
wr.CurrentRevision = rev
} else {
errc <- errors.New("failed to execute query in database")
Copy link
Member

@brandond brandond Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add ErrWatchCancelled = rpctypes.ErrGRPCWatchCanceled in pkg/server/types.go and just return that here as a generic watch failure message. We try to return the same errors as upstream whenever possible, even if the cause isn't necessarily the same.

If you can think of a better error from https://pkg.go.dev/go.etcd.io/etcd/api/v3/v3rpc/rpctypes to use please feel free.

Copy link
Contributor Author

@nic-6443 nic-6443 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, great, I think ErrGRPCUnhealthy is better to express the failure come from system level.

{"result":{"header":{},"watch_id":"1","created":true}}
{"result":{"header":{},"watch_id":"1","canceled":true,"cancel_reason":"rpc error: code = Unavailable desc = etcdserver: unhealthy cluster"}}

}
}
cancel()
}
Expand Down
1 change: 1 addition & 0 deletions pkg/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type WatchResult struct {
CurrentRevision int64
CompactRevision int64
Events <-chan []*Event
Errorc <-chan error
}

func unsupported(field string) error {
Expand Down
7 changes: 6 additions & 1 deletion pkg/server/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ func (w *watcher) Start(ctx context.Context, r *etcdserverpb.WatchCreateRequest)
}
}

w.Cancel(id, 0, 0, nil)
select {
case err := <-wr.Errorc:
w.Cancel(id, 0, 0, err)
default:
w.Cancel(id, 0, 0, nil)
}
logrus.Tracef("WATCH CLOSE id=%d, key=%s", id, key)
}()
}
Expand Down