Skip to content
This repository has been archived by the owner on Oct 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from heyitsanthony/cver-version
Browse files Browse the repository at this point in the history
zetcd: use etcd versions for tracking cver
  • Loading branch information
heyitsanthony committed Dec 28, 2016
2 parents b89c380 + 0084abc commit bca6863
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion stat.go
Expand Up @@ -51,7 +51,7 @@ func statTxn(txnresp *etcd.TxnResponse) (s Stat) {
s.Version = Ver(mtime.Kvs[0].Version - 1)
}
if len(cver.Kvs) != 0 {
s.Cversion = Ver(decodeInt64(cver.Kvs[0].Value))
s.Cversion = Ver(cver.Kvs[0].Version - 1)
}
if len(aver.Kvs) != 0 {
s.Aversion = Ver(decodeInt64(aver.Kvs[0].Value))
Expand Down
23 changes: 9 additions & 14 deletions zketcd.go
Expand Up @@ -61,7 +61,7 @@ func (z *zkEtcd) Create(xid Xid, op *CreateRequest) ZKResponse {
if len(op.Acl) == 0 {
return ErrInvalidACL
}
if s.Rev(pkey) == 0 && len(pp) != 2 {
if len(pp) != 2 && s.Rev("/zk/ctime/"+pp) == 0 {
// no parent
return ErrNoNode
}
Expand All @@ -75,22 +75,23 @@ func (z *zkEtcd) Create(xid Xid, op *CreateRequest) ZKResponse {
respPath += cstr
count++
s.Put("/zk/count/"+pp, encodeInt64(int64(count)))
} else if s.Rev("/zk/ver/"+p) != 0 {
} else if s.Rev("/zk/ctime/"+p) != 0 {
return ErrNodeExists
}

t := encodeTime()

nextCVer := encodeInt64(decodeInt64([]byte(s.Get(pkey))) + 1)
s.Put("/zk/cver/"+pp, nextCVer)
// update key's version by blindly writing an empty value
s.Put(pkey, "")

// creating a znode will NOT update its parent mtime
// s.Put("/zk/mtime/"+pp, t)

s.Put("/zk/key/"+p, string(op.Data), opts...)
s.Put("/zk/ctime/"+p, t, opts...)
s.Put("/zk/mtime/"+p, t, opts...)
s.Put("/zk/ver/"+p, encodeInt64(0), opts...)
s.Put("/zk/cver/"+p, encodeInt64(0), opts...)
s.Put("/zk/cver/"+p, "", opts...)
s.Put("/zk/aver/"+p, encodeInt64(0), opts...)
s.Put("/zk/acl/"+p, encodeACLs(op.Acl), opts...)
s.Put("/zk/count/"+p, encodeInt64(0), opts...)
Expand Down Expand Up @@ -174,17 +175,16 @@ func (z *zkEtcd) Delete(xid Xid, op *DeleteRequest) ZKResponse {
p := mkPath(op.Path)
pp := mkPath(path.Dir(op.Path))
key := "/zk/key/" + p
pkey := "/zk/cver/" + pp

applyf := func(s v3sync.STM) error {
if s.Rev(pkey) == 0 && len(pp) != 2 {
if len(pp) != 2 && s.Rev("/zk/ctime/"+pp) == 0 {
// no parent
if PerfectZXidMode {
s.Put("/zk/err-node", "1")
}
return ErrNoNode
}
if s.Rev("/zk/ver/"+p) == 0 {
if s.Rev("/zk/ctime/"+p) == 0 {
if PerfectZXidMode {
s.Put("/zk/err-node", "1")
}
Expand All @@ -195,12 +195,7 @@ func (z *zkEtcd) Delete(xid Xid, op *DeleteRequest) ZKResponse {
return ErrBadVersion
}

if decodeInt64([]byte(s.Get("/zk/cver/"+p))) != 0 {
panic("how to delete children")
}

nextCVer := encodeInt64(decodeInt64([]byte(s.Get(pkey))) + 1)
s.Put("/zk/cver/"+pp, nextCVer)
s.Put("/zk/cver/"+pp, "")
s.Put("/zk/mtime/"+pp, encodeTime())

s.Del(key)
Expand Down

0 comments on commit bca6863

Please sign in to comment.