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 txn" does not properly parse newline characters #10169

Closed
jutley opened this issue Oct 10, 2018 · 10 comments
Closed

"etcdctl txn" does not properly parse newline characters #10169

jutley opened this issue Oct 10, 2018 · 10 comments

Comments

@jutley
Copy link
Contributor

jutley commented Oct 10, 2018

We are using etcdctl txn to make sure servers running in parallel do not both write a new value for a specific key. To do this, we are running a command from a bash script that looks something like this:

etcdctl txn <<< 'create("my_key") = "0"

put "my_key" "my
multiline
value"



'

Essentially: If the value does not already exist, write it. Otherwise, do nothing.

The problem is that etcdctl does not parse this as intended. Instead, we get this error:

Error: invalid txn compare request: multiline"

Looking at the source code, it's pretty clear that the command is strictly parsing for newline characters. I've verified that using my\nmultiline\nvalue as the value gives the desired effect, so we can get by. However, this functionality is inconsistent with etcdctl put, which DOES handle the newline characters.

These commands should parse consistently.

We are currently using etcd 3.3.2, though the changelog shows no changes to etcdctl txn in newer versions, so I would assume this is still an issue.

@hexfusion
Copy link
Contributor

hexfusion commented Oct 10, 2018

@jutley this isn't really an etcd issue in my opinion. How about something like this with a Heredoc?

etcdctl txn <<< 'create("my_key") = "0" 

put "my_key" << EOM
my
multiline
value
EOM
'

@hexfusion
Copy link
Contributor

But to your point, the put would work on its own so this probably needs more investigation.

etcdctl put my_key "my
multiline
value"

@jutley
Copy link
Contributor Author

jutley commented Oct 10, 2018

I've tried your literal example and a few alterations to try to get this to work, with no success. The only thing that I've gotten to work is escaping newlines in my value using a command like sed -e ':a;N;$!ba;s/\n/\\n/g'.

I think this is an issue with etcdctl because it makes the txn command really difficult to use in anything except the most simple of examples. We can work around the limitations, but it adds a lot of complexity on our end.

@hexfusion
Copy link
Contributor

@jutley I will dig into this for you to better understand what is going on. The error message is very specific so we might not support it for a reason but I will explore further and at least document. Thanks for the report.

@jutley
Copy link
Contributor Author

jutley commented Oct 10, 2018

I think the fundamental difference between the put and the txn is the when using put, bash is parsing the multiline string, and passing the result to etcdctl. With txn, etcdctl is handling the string parsing. So I can understand how they ended up showing inconsistent behavior.

If nothing else, it would be a big improvement to update the documentation to call out that values must have escaped newlines.

@hexfusion
Copy link
Contributor

@jutley I don't want to discourage you from exploring this as well. You seem to have a solid grasp of the situation and we would love the help :). If not I can review over the next week or so.

@wenjiaswe
Copy link
Contributor

/cc @jingyih

@jingyih
Copy link
Contributor

jingyih commented Oct 11, 2018

@jutley Your observation is correct. Currently the Txn command relies on '\n' to separate CMP, THEN, ELSE. It also relies on '\n' to separate multiple ops/requests. Thanks for pointing this out.

@jutley
Copy link
Contributor Author

jutley commented Oct 12, 2018

I'd be happy to at least make a PR to improve the documentation, but it'll be at least a week before I can get started.

@jingyih
Copy link
Contributor

jingyih commented Oct 12, 2018

@jutley Sounds good. Thanks!

jutley added a commit to jutley/etcd that referenced this issue Mar 1, 2019
jutley added a commit to jutley/etcd that referenced this issue Mar 1, 2019
Add documentation to clarify that when writing TXN commands, multi-line values should be written using "\n" and not a literal newline (as in other commands).

Fixes etcd-io#10169
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants