Skip to content

Commit

Permalink
zkplus: Always check for existance even if creates allowed (#218)
Browse files Browse the repository at this point in the history
This will avoid more issues when dealing wiht read-only root nodes
  • Loading branch information
benkeith-splunk authored Jun 28, 2021
1 parent a1d2d27 commit 86c8859
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion disco/disco_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestErrorNoRootCreate(t *testing.T) {
c := 0
z.SetErrorCheck(func(s string) error {
c++
if c < 2 {
if c < 3 {
return nil
}
return zk.ErrNoNode
Expand Down
24 changes: 13 additions & 11 deletions zkplus/zkplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,20 @@ func (z *ZkPlus) ensureRootPath(conn zktest.ZkConnSupported) error {
}
totalPath = totalPath + "/" + p

if z.createRoot {
_, err := conn.Create(totalPath, []byte(""), 0, zk.WorldACL(zk.PermAll))
if err != nil && err != zk.ErrNodeExists {
return errors.Annotatef(err, "cannot create path %s", totalPath)
}
} else {
exists, _, err := conn.Exists(totalPath)
if err != nil {
return errors.Annotatef(err, "cannot verify that node %q exists", totalPath)
}
exists, _, err := conn.Exists(totalPath)
if err != nil {
return errors.Annotatef(err, "cannot verify that node %q exists", totalPath)
}

if !exists {
if !exists {
if z.createRoot {
_, err := conn.Create(totalPath, []byte(""), 0, zk.WorldACL(zk.PermAll))
// There could be a race where the root is created in the
// meantime since the Exists check above, so ignore ErrNodeExists.
if err != nil && err != zk.ErrNodeExists {
return errors.Annotatef(err, "cannot create path %s", totalPath)
}
} else {
return fmt.Errorf("root node %q does not exist", totalPath)
}
}
Expand Down
2 changes: 2 additions & 0 deletions zkplus/zkplus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func TestErrorEnsureRootNoCreate(t *testing.T) {

zkp, err = NewBuilder().PathPrefix("/test").CreateRootNode(false).Connector(&StaticConnector{C: z, Ch: ch}).Build()
assert.NoError(t, err)
err = zkp.ensureRootPath(z)
assert.Equal(t, err, errors.New("root node \"/test\" does not exist"))
zkp.Close()
}

Expand Down

0 comments on commit 86c8859

Please sign in to comment.