samuel / go-zookeeper

Native ZooKeeper client for Go. This project is no longer maintained. Please use https://github.com/go-zookeeper/zk instead.
BSD 3-Clause "New" or "Revised" License
1.64k stars 672 forks source link

Delete node(s) fails to delete without any error #242

Closed sandeepkalra closed 3 years ago

sandeepkalra commented 4 years ago

I have this simple code that tries to delete all the nodes recursively. The code does not produce errors, but at the same time, the nodes aren't deleted. Note: There may be more than 1 active users accessing this node (and this may be the reason for not able to delete)... Code:

package main

import (
    "fmt"
    "time"

    "github.com/samuel/go-zookeeper/zk"
)

// Patroni Config
const (
    PATRONI_SCOPE   = "blueplanet"
    PATRONI_CLUSTER = "postgres"
    ROOT            = "/" + PATRONI_SCOPE + "/" + PATRONI_CLUSTER
)
// paths are held to be deleted. 
var paths []string = []string{ROOT}

// Main
func main() {
    c, _, e := zk.Connect([]string{"172.16.0.18"}, time.Second)
    if e != nil {
        return
    }

    defer c.Close()
    countIndex := 1
    totalCount := 1
    for i := countIndex - 1; i < totalCount; i++ {
        path := paths[i]
        children, _, e := c.Children(path)
        if e == nil && len(children) != 0 {
            new_children := []string{}
            for _, v := range children {
                new_children = append(new_children, path+"/"+v)
            }
            paths = append(paths, new_children...)
            totalCount += len(children)
        }
    }
  // reverse path so that we have children first, then its next level parents and so on.
    for i := len(paths)/2 - 1; i >= 0; i-- {
        opp := len(paths) - 1 - i
        paths[i], paths[opp] = paths[opp], paths[i]
    }
// start deleting the nodes 
    for _, v := range paths {
        if err := c.Delete(v, -1); err != nil && err != zk.ErrNoNode {
            fmt.Printf("Delete returned error:%+v %+v", v, err)
        } else {
            fmt.Println("path deleted :", v)
        }
    }
    return
}

Running the executable:

(SandeepCloud) clearZK § ./clearzk 
2020/04/17 20:10:28 Connected to 172.16.0.18:2181
2020/04/17 20:10:28 authenticated: id=103655287882057480, timeout=4000
2020/04/17 20:10:28 re-submitting `0` credentials after reconnect
path deleted : /blueplanet/postgres/members/pg_3
path deleted : /blueplanet/postgres/members/pg_2
path deleted : /blueplanet/postgres/members/pg_1
path deleted : /blueplanet/postgres/members/pg_0
path deleted : /blueplanet/postgres/members/pg_4
path deleted : /blueplanet/postgres/members
path deleted : /blueplanet/postgres/config
path deleted : /blueplanet/postgres/initialize
path deleted : /blueplanet/postgres/leader
path deleted : /blueplanet/postgres
2020/04/17 20:10:28 recv loop terminated: err=EOF
2020/04/17 20:10:28 send loop terminated: err=<nil>

Now go to ZK and check the nodes:

$ zkCli.sh
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
:
:
[zk: localhost:2181(CONNECTED) 7] ls /blueplanet/postgres/members
[pg_4, pg_0, pg_1, pg_2, pg_3]

The zk nodes are still present