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

CAS index if no key? #1864

Closed
lud4ik opened this issue Mar 22, 2016 · 1 comment
Closed

CAS index if no key? #1864

lud4ik opened this issue Mar 22, 2016 · 1 comment

Comments

@lud4ik
Copy link

lud4ik commented Mar 22, 2016

I perform get, use meta.LastIndex for ModifyIndex in CAS

             &api.KVPair{
                Key:         q.cacheKey,
                Value:       body,
                ModifyIndex: lastIndex,
            },

But if no key exists, it returns some random number for response meta.LastIndex and always fails on CAS operation (false for first param in api).
Consul 0.5.0

2016/03/22 13:15:35 [DEBUG] http: Request /v1/kv/service/go-asterisk/flush/originate?consistent=&dc=dc1 (109.407µs)
2016/03/22 13:15:35 [DEBUG] http: Request /v1/kv/service/go-asterisk/flush/originate?cas=531&dc=dc1 (227.414µs)

If key is present everything works ok.

@slackpad
Copy link
Contributor

slackpad commented Apr 5, 2016

Hi @lud4ik if you query and don't get a key back the index is set to the last index that modified the KV store, since there's no key present that you are querying (you could use this to do a blocking query to wait for that key to get created, for example). If the key doesn't exist you can use ?cas=0 which means "create the key only if it isn't already there". Hope that helps!

?cas= : This flag is used to turn the PUT into a Check-And-Set operation. This is very useful as a building block for more complex synchronization primitives. If the index is 0, Consul will only put the key if it does not already exist. If the index is non-zero, the key is only set if the index matches the ModifyIndex of that key.

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

2 participants