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

mvcc: return -1 for wrong watcher range key >= end #6820

Merged
merged 3 commits into from
Nov 9, 2016

Conversation

gyuho
Copy link
Contributor

@gyuho gyuho commented Nov 8, 2016

Fix #6819.

@heyitsanthony @xiang90 Should we translate the error in etcdctl or clientv3 side? Currently clients will just see the watch create request is canceled.

@@ -108,6 +109,10 @@ func (ws *watchStream) Watch(key, end []byte, startRev int64, fcs ...FilterFunc)
id := ws.nextID
ws.nextID++

// prevent wrong range where key >= end lexicographically
Copy link
Contributor

Choose a reason for hiding this comment

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

check before acquiring the lock?

runWatchTest(t, testWatchWrongRange)
}

func testWatchWrongRange(t *testing.T, wctx *watchctx) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should this test be in /integration instead of /clientv3/integration?

@heyitsanthony
Copy link
Contributor

No strong opinion about it being translated in clientv3/etcdctl. We should probably teach etcdctl about watchresponse Err() regardless.

@gyuho
Copy link
Contributor Author

gyuho commented Nov 8, 2016

@heyitsanthony @xiang90

All fixed. Think error Error: watch is canceled by the server is good enough?
I instead added more clarification on range end docs for clientv3 and etcdctl.

PTAL.

Thanks.

@@ -329,7 +329,7 @@ put key2 "some extra key"

### WATCH [options] [key or prefix] [range_end]

Watch watches events stream on keys or prefixes, [key or prefix, range_end) if `range-end` is given. The watch command runs until it encounters an error or is terminated by the user.
Watch watches events stream on keys or prefixes, [key or prefix, range_end) if `range-end` is given. The watch command runs until it encounters an error or is terminated by the user. If range_end is given, it must be lexicographically greater than key.
Copy link
Contributor

@heyitsanthony heyitsanthony Nov 8, 2016

Choose a reason for hiding this comment

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

greater than the key or "\x00".? there's a special case for a string with only "\x00" (see WithFromKey)

@@ -36,7 +36,7 @@ var (
func NewWatchCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "watch [options] [key or prefix] [range_end]",
Short: "Watches events stream on keys or prefixes",
Short: "Watches events stream on keys or prefixes (range_end is optional or must be lexicographically greater than key)",
Copy link
Contributor

Choose a reason for hiding this comment

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

this is sort of wordy, probably can drop

@@ -99,6 +100,11 @@ type watchStream struct {
// Watch creates a new watcher in the stream and returns its WatchID.
// TODO: return error if ws is closed?
func (ws *watchStream) Watch(key, end []byte, startRev int64, fcs ...FilterFunc) WatchID {
// prevent wrong range where key >= end lexicographically
if len(end) != 0 && bytes.Compare(key, end) != -1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

does this break Watch(ctx, "abc", clientv3.WithFromKey())?

@gyuho
Copy link
Contributor Author

gyuho commented Nov 8, 2016

@heyitsanthony Added special case handling for []byte{0} with test. PTAL. Thanks!

@@ -99,6 +100,12 @@ type watchStream struct {
// Watch creates a new watcher in the stream and returns its WatchID.
// TODO: return error if ws is closed?
func (ws *watchStream) Watch(key, end []byte, startRev int64, fcs ...FilterFunc) WatchID {
// prevent wrong range where key >= end lexicographically
// except special case for watch from key '\x00'
if len(end) != 0 && !bytes.Equal(end, []byte{0}) && bytes.Compare(key, end) != -1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

if len(end) != 0 && bytes.Compare(key, end) != -1 && !bytes.Equal(end, []byte{0})? common path is going to be key < end, right?

t.Fatalf("key > end range given; id expected -1, got %d", id)
}
// in case client sends 'WithFromKey' request with smallest bytes range end
if id := w.Watch([]byte("foo"), []byte{}, 1); id != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be []byte{0}? I think []byte{} will give the same result as end == nil once passed through grpc?

@gyuho
Copy link
Contributor Author

gyuho commented Nov 9, 2016

@heyitsanthony I removed bytes.Equal check because we convert \x00 into empty bytes in v3rpc https://github.com/coreos/etcd/blob/master/etcdserver/api/v3rpc/watch.go#L173. What do you think?

It's just empty bytes when passed to mvcc.

@heyitsanthony
Copy link
Contributor

heyitsanthony commented Nov 9, 2016

@gyuho OK. Is that WithFromKey test correct, though? I think it still needs to be []byte{0} so the v3rpc/watch stuff can convert it to []byte{}?

@gyuho gyuho force-pushed the watcher branch 2 times, most recently from 9fba1f7 to 1db58fb Compare November 9, 2016 00:55
@gyuho
Copy link
Contributor Author

gyuho commented Nov 9, 2016

@heyitsanthony Did you mean TestWatcherWatchWrongRange to use w.Watch([]byte("foo"), []byte{0}, 1)? How so, we don't use rpc in mvcc tests?

I added []byte{0} case to integration test to make sure we don't break WithFromKey

@heyitsanthony
Copy link
Contributor

Oh oops, you're right. What was I thinking. lgtm

@gyuho gyuho force-pushed the watcher branch 2 times, most recently from d84c768 to 29246e8 Compare November 9, 2016 01:00
@gyuho
Copy link
Contributor Author

gyuho commented Nov 9, 2016

Thanks merging in greens.

@gyuho gyuho merged commit dbb692e into etcd-io:master Nov 9, 2016
@gyuho gyuho deleted the watcher branch November 9, 2016 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants