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

Commit

Permalink
zetcd: increment zxid on path validation failures
Browse files Browse the repository at this point in the history
This commit implements the PerfectZXidMode functionality for failed path
validation, causing the ZXid of the server to be incremented on path validation
failures.

This brings the server in-line with upstream Zookeeper, which increments the
ZXid *before* potentially failing the request due to a bad path.

Building with PerfectZXidMode set to false turns the behavior into a noop.
  • Loading branch information
wrouesnel committed Mar 4, 2017
1 parent 73da355 commit b882298
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions zketcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func (z *zkEtcd) Create(xid Xid, op *CreateRequest) ZKResponse {
zkPath = fmt.Sprintf("%s1", zkPath)
}
if err := validatePath(zkPath); err != nil {
return mkZKErr(xid, z.s.ZXid(), errBadArguments)
zxid, err := z.incrementAndGetZxid()
if err != nil {
return mkErr(err)
}
return mkZKErr(xid, zxid, errBadArguments)
}

opts := []etcd.OpOption{}
Expand Down Expand Up @@ -323,7 +327,11 @@ func (z *zkEtcd) GetData(xid Xid, op *GetDataRequest) ZKResponse {

func (z *zkEtcd) SetData(xid Xid, op *SetDataRequest) ZKResponse {
if err := validatePath(op.Path); err != nil {
return mkZKErr(xid, z.s.ZXid(), errBadArguments)
zxid, err := z.incrementAndGetZxid()
if err != nil {
return mkErr(err)
}
return mkZKErr(xid, zxid, errBadArguments)
}

p := mkPath(op.Path)
Expand Down Expand Up @@ -401,7 +409,11 @@ func (z *zkEtcd) GetAcl(xid Xid, op *GetAclRequest) ZKResponse {

func (z *zkEtcd) SetAcl(xid Xid, op *SetAclRequest) ZKResponse {
if err := validatePath(op.Path); err != nil {
return mkZKErr(xid, z.s.ZXid(), errBadArguments)
zxid, err := z.incrementAndGetZxid()
if err != nil {
return mkErr(err)
}
return mkZKErr(xid, zxid, errBadArguments)
}
panic("setAcl")
}
Expand Down Expand Up @@ -546,6 +558,23 @@ func (z *zkEtcd) doSTM(applyf func(s v3sync.STM) error) (*etcd.TxnResponse, erro
return v3sync.NewSTMSerializable(z.c.Ctx(), z.c, applyf)
}

// incrementAndGetZxid forces a write to the err-node to increment the Zxid
// to keep the numbers aligned with Zookeeper's semantics. It is gated on the
// PerfectZxid global at the moment (which is hardcoded true).
func (z *zkEtcd) incrementAndGetZxid() (ZXid, error) {
applyf := func(s v3sync.STM) (err error) {
if PerfectZXidMode {
s.Put("/zk/err-node", "1")
}
return nil
}
resp, err := z.doSTM(applyf)
if err != nil {
return -1, err
}
return ZXid(resp.Header.Revision), nil
}

func encodeACLs(acls []ACL) string {
var b bytes.Buffer
gob.NewEncoder(&b).Encode(acls)
Expand Down

0 comments on commit b882298

Please sign in to comment.