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

return revision when put_if_not_exists() succeed #1648

Open
PingHao opened this issue Aug 6, 2021 · 1 comment
Open

return revision when put_if_not_exists() succeed #1648

PingHao opened this issue Aug 6, 2021 · 1 comment

Comments

@PingHao
Copy link

PingHao commented Aug 6, 2021

Current implement of put_if_not_exists() only return bool indicate succeed or not, I think it's make more useful to return revision of key, for example, after put_if_not_exits(KEY), user add watch of KEY to monitor if it's been delete or not. without revision, it always has the chance that the next watch call didn't catch all event since PUT, unless use revision as parameter.

with revision, a better user code can be write as

status,revision = put_if_not_exists(client,KEY,VALUE)
# delay can happen between two calls
client.add_watch_callback(TEST_KEY,watch_cb,start_revision=revision+1)

I tried to write my own impl, but it end with unwrap RPC message layers, (resp.response_put.header.revision) not quite good.

def put_if_not_exists(client, key, value, lease=None):
    status, responses = client.transaction(
        compare=[
            client.transactions.version(key) == 0
        ],
        success=[
            client.transactions.put(key, value, lease)
        ],
        failure=[],
    )
    revision = 0
    if status:
        for resp in responses:
            revision = resp.response_put.header.revision
    return status, revision
PingHao pushed a commit to PingHao/python-etcd3 that referenced this issue Aug 6, 2021
@PingHao
Copy link
Author

PingHao commented Aug 7, 2021

FYI, this is a gist I wrote about leader election recipe compatible with "etcdctl elect" command, in there it use put_not_exist() return revision.
https://gist.github.com/PingHao/c9ab07b6e82c0887cce970b10466e307

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant