Etcd: about v3 txn: why run once but put twice key?

Created on 11 Mar 2018  路  4Comments  路  Source: etcd-io/etcd

package main

import (
    "context"
    "fmt"
    v3 "github.com/coreos/etcd/clientv3"
)

func main() {
    client, err := v3.New(v3.Config{
        Endpoints: []string{"http://127.0.0.1:2379"},
    })
    if err != nil {
        fmt.Println(err)
        return
    }

    key := "/foo/bar/"
    txn := client.Txn(context.TODO()).If(v3.Compare(v3.CreateRevision(key), "=", 0)).
        Then(v3.OpPut(key, "foo"))
    resp, err := txn.Commit()
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(resp.Responses)
}

$ rm -rf default.etcd/
$ etcd --version
etcd Version: 3.3.2
Git SHA: c9d46ab37
Go Version: go1.9.4
Go OS/Arch: darwin/amd64

$ etcdctl version
etcdctl version: 3.3.2
API version: 3.3

$ etcd --debug=true

$ go run main.go
[response_put:

$ ETCDCTL_API=3 etcdctl get /foo/bar/ --rev 0
/foo/bar/
foo

$ ETCDCTL_API=3 etcdctl get /foo/bar/ --rev 1

$ ETCDCTL_API=3 etcdctl get /foo/bar/ --rev 2
/foo/bar/
foo

this is glide.yaml

package: hello
import:
- package: github.com/coreos/etcd
  version: ^3.3.2
  subpackages:
  - clientv3

why run once but put twice key? and skip revision 1 that keep revision 0 and revision 2.

thanks.

areclientv3 arequestion

Most helpful comment

The very first revision of etcd store is 1 (without any data written yet).

All 4 comments

Revision is global counter. Rev 0 is default value (returns latest).
Your first put was on revision 2. So, get request with rev 2 is returning the correct value.

i see. that means rev 1 is not uses? thanks.

The very first revision of etcd store is 1 (without any data written yet).

thanks!

Was this page helpful?
0 / 5 - 0 ratings