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

etcdctl: fix mk command with PrevNoExist #3685

Merged
merged 1 commit into from
Oct 15, 2015

Conversation

gyuho
Copy link
Contributor

@gyuho gyuho commented Oct 15, 2015

This attempts to fix #3676. PrevNoExist checks if the key previously exists and if so, it returns an error, which is how mk command is supposed to work. The previous code ignores the previous key and overwrites with the later value.

/cc @yichengq

@gyuho
Copy link
Contributor Author

gyuho commented Oct 15, 2015

yichen says:

Create is just a wrapper around Set for simplicity, and you could use Set with PrevNoExist in etcdctl to make it fixed.

Will change it to use Set now.

@gyuho gyuho changed the title etcdctl/command: mk command with Create etcdctl: use Set but with PrevNoExist for mk Oct 15, 2015
@yichengq
Copy link
Contributor

@gyuho Have you manually tested that it could resolve the bug in #3676 ?

Moreover, I would appreciate if you could write about problem and how it is fixed in the commit message, instead of writing too much context around API or previous PR.

@gyuho
Copy link
Contributor Author

gyuho commented Oct 15, 2015

Yes, in my local machine, I ran the following command:

export INFRA_IP=127.0.0.1;

etcd -name infra0 \
    -initial-cluster-token my-infra \
    -initial-cluster infra0=http://$INFRA_IP:2380 \
    -initial-cluster-state new \
    -initial-advertise-peer-urls http://$INFRA_IP:2380 \
    -listen-peer-urls http://$INFRA_IP:2380 \
    -listen-client-urls http://$INFRA_IP:2379 \
    -advertise-client-urls http://$INFRA_IP:2379;

And use etcdctl:

$ etcdctl mk /foo hello
hello

$ etcdctl mk /foo hello
Error:  105: Key already exists (/foo) [8]

I changed the commit message to be more clearer.
Thanks,

Please let me know if we need more tests.

@gyuho gyuho changed the title etcdctl: use Set but with PrevNoExist for mk etcdctl: fix mk command with PrevNoExist Oct 15, 2015
// By passing client.PrevNoExist the Set method checks if
// the key had existed previously, and returns error when
// it tries to overwrite the key, which is how mk command
// is supposed to work.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need to mention PrevIgnore here because we don't use PrevIgnore here.

It would be better if you could make the second sentence more concrete.

@gyuho
Copy link
Contributor Author

gyuho commented Oct 15, 2015

I agree. I made some changes on comments. Please let me know if you have any other feedback.

@@ -51,7 +51,9 @@ func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
ttl := c.Int("ttl")

ctx, cancel := contextWithTotalTimeout(c)
resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevIgnore})
// PrevNoExist means that the Node must not exist.
// Set method returns error if the key already exists.
Copy link
Contributor

Choose a reason for hiding this comment

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

Set method returns error if the key already exists. This sentence is not true if Set method doesn't use PrevNoExist.

Also the comments miss the part that why you explain how Set returns error here.

@gyuho
Copy link
Contributor Author

gyuho commented Oct 15, 2015

@yichengq Sorry. I should have made it clear that the Set method in this case.

Is it clear now?

// Since PrevNoExist means that the Node must not exist,
// this Set method would return error if the key already exists.

@@ -51,7 +51,9 @@ func mkCommandFunc(c *cli.Context, ki client.KeysAPI) {
ttl := c.Int("ttl")

ctx, cancel := contextWithTotalTimeout(c)
resp, err := ki.Set(ctx, key, value, &client.SetOptions{TTL: time.Duration(ttl) * time.Second, PrevExist: client.PrevIgnore})
// Since PrevNoExist means that the Node must not exist,
Copy link
Contributor

Choose a reason for hiding this comment

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

must not exist previously

This attempts to fix etcd-io#3676. `PrevNoExist` checks if the key previously exists
and if so, it returns an error, which is how `mk` command is supposed to work.
The previous code ignores the previous key and overwrites with the later value.

/cc @yichengq
@gyuho
Copy link
Contributor Author

gyuho commented Oct 15, 2015

Since PrevNoExist means that the Node must not exist previously,
this Set method always creates a new key. Therefore, mk command
succeeds only if the key did not previously exist, and the command
prevents one from overwriting values accidentally.

This is the reason I think one would need use mk command for.
Please let me know if you have any other correction. Thanks!

@yichengq
Copy link
Contributor

The comment is a little verbosal IMO, but it is good enough. 👍

LGTM. Thanks for fixing it!

yichengq added a commit that referenced this pull request Oct 15, 2015
etcdctl: fix mk command with PrevNoExist
@yichengq yichengq merged commit 9ce79db into etcd-io:master Oct 15, 2015
@gyuho
Copy link
Contributor Author

gyuho commented Oct 16, 2015

Thanks!

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.

Make new key updates the key if it already exists
2 participants