Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : zk problem #346

Merged
merged 3 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 52 additions & 31 deletions remoting/zookeeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
var (
errNilZkClientConn = perrors.New("zookeeperclient{conn} is nil")
errNilChildren = perrors.Errorf("has none children")
errNilNode = perrors.Errorf("node does not exist")
)

// ZookeeperClient ...
Expand Down Expand Up @@ -260,11 +261,13 @@ LOOP:
logger.Warnf("zk{addr:%s} state is StateDisconnected, so close the zk client{name:%s}.", z.ZkAddrs, z.name)
z.stop()
z.Lock()
if z.Conn != nil {
z.Conn.Close()
z.Conn = nil
}
conn := z.Conn
z.Conn = nil
z.Unlock()
if conn != nil {
conn.Close()
}

break LOOP
case (int)(zk.EventNodeDataChanged), (int)(zk.EventNodeChildrenChanged):
logger.Infof("zkClient{%s} get zk node changed event{path:%s}", z.name, event.Path)
Expand Down Expand Up @@ -379,11 +382,13 @@ func (z *ZookeeperClient) Close() {
z.stop()
z.Wait.Wait()
z.Lock()
if z.Conn != nil {
z.Conn.Close()
z.Conn = nil
}
conn := z.Conn
z.Conn = nil
z.Unlock()
if conn != nil {
conn.Close()
}

logger.Warnf("zkClient{name:%s, zk addr:%s} exit now.", z.name, z.ZkAddrs)
}

Expand All @@ -399,10 +404,12 @@ func (z *ZookeeperClient) Create(basePath string) error {
tmpPath = path.Join(tmpPath, "/", str)
err = errNilZkClientConn
z.Lock()
if z.Conn != nil {
_, err = z.Conn.Create(tmpPath, []byte(""), 0, zk.WorldACL(zk.PermAll))
}
conn := z.Conn
z.Unlock()
if conn != nil {
_, err = conn.Create(tmpPath, []byte(""), 0, zk.WorldACL(zk.PermAll))
}

if err != nil {
if err == zk.ErrNodeExists {
logger.Infof("zk.create(\"%s\") exists\n", tmpPath)
Expand All @@ -424,10 +431,11 @@ func (z *ZookeeperClient) Delete(basePath string) error {

err = errNilZkClientConn
z.Lock()
if z.Conn != nil {
err = z.Conn.Delete(basePath, -1)
}
conn := z.Conn
z.Unlock()
if conn != nil {
err = conn.Delete(basePath, -1)
}

return perrors.WithMessagef(err, "Delete(basePath:%s)", basePath)
}
Expand All @@ -445,10 +453,12 @@ func (z *ZookeeperClient) RegisterTemp(basePath string, node string) (string, er
data = []byte("")
zkPath = path.Join(basePath) + "/" + node
z.Lock()
if z.Conn != nil {
tmpPath, err = z.Conn.Create(zkPath, data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
}
conn := z.Conn
z.Unlock()
if conn != nil {
tmpPath, err = conn.Create(zkPath, data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
}

//if err != nil && err != zk.ErrNodeExists {
if err != nil {
logger.Warnf("conn.Create(\"%s\", zk.FlagEphemeral) = error(%v)\n", zkPath, perrors.WithStack(err))
Expand All @@ -468,15 +478,17 @@ func (z *ZookeeperClient) RegisterTempSeq(basePath string, data []byte) (string,

err = errNilZkClientConn
z.Lock()
if z.Conn != nil {
tmpPath, err = z.Conn.Create(
conn := z.Conn
z.Unlock()
if conn != nil {
tmpPath, err = conn.Create(
path.Join(basePath)+"/",
data,
zk.FlagEphemeral|zk.FlagSequence,
zk.WorldACL(zk.PermAll),
)
}
z.Unlock()

logger.Debugf("zookeeperClient.RegisterTempSeq(basePath{%s}) = tempPath{%s}", basePath, tmpPath)
if err != nil && err != zk.ErrNodeExists {
logger.Errorf("zkClient{%s} conn.Create(\"%s\", \"%s\", zk.FlagEphemeral|zk.FlagSequence) error(%v)\n",
Expand All @@ -499,19 +511,24 @@ func (z *ZookeeperClient) GetChildrenW(path string) ([]string, <-chan zk.Event,

err = errNilZkClientConn
z.Lock()
if z.Conn != nil {
children, stat, event, err = z.Conn.ChildrenW(path)
}
conn := z.Conn
z.Unlock()
if conn != nil {
children, stat, event, err = conn.ChildrenW(path)
}

if err != nil {
if err == zk.ErrNoChildrenForEphemerals {
return nil, nil, errNilChildren
}
if err == zk.ErrNoNode {
return nil, nil, perrors.Errorf("path{%s} has none children", path)
return nil, nil, errNilNode
}
logger.Errorf("zk.ChildrenW(path{%s}) = error(%v)", path, err)
return nil, nil, perrors.WithMessagef(err, "zk.ChildrenW(path:%s)", path)
}
if stat == nil {
return nil, nil, perrors.Errorf("path{%s} has none children", path)
return nil, nil, perrors.Errorf("path{%s} get stat is nil", path)
}
if len(children) == 0 {
return nil, nil, errNilChildren
Expand All @@ -530,10 +547,12 @@ func (z *ZookeeperClient) GetChildren(path string) ([]string, error) {

err = errNilZkClientConn
z.Lock()
if z.Conn != nil {
children, stat, err = z.Conn.Children(path)
}
conn := z.Conn
z.Unlock()
if conn != nil {
children, stat, err = conn.Children(path)
}

if err != nil {
if err == zk.ErrNoNode {
return nil, perrors.Errorf("path{%s} has none children", path)
Expand Down Expand Up @@ -561,10 +580,12 @@ func (z *ZookeeperClient) ExistW(zkPath string) (<-chan zk.Event, error) {

err = errNilZkClientConn
z.Lock()
if z.Conn != nil {
exist, _, event, err = z.Conn.ExistsW(zkPath)
}
conn := z.Conn
z.Unlock()
if conn != nil {
exist, _, event, err = conn.ExistsW(zkPath)
}

if err != nil {
logger.Warnf("zkClient{%s}.ExistsW(path{%s}) = error{%v}.", z.name, zkPath, perrors.WithStack(err))
return nil, perrors.WithMessagef(err, "zk.ExistsW(path:%s)", zkPath)
Expand Down
7 changes: 6 additions & 1 deletion remoting/zookeeper/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (l *ZkEventListener) listenDirEvent(zkPath string, listener remoting.DataLi
if MaxFailTimes <= failTimes {
failTimes = MaxFailTimes
}
logger.Warnf("listenDirEvent(path{%s}) = error{%v}", zkPath, err)
logger.Infof("listenDirEvent(path{%s}) = error{%v}", zkPath, err)
// clear the event channel
CLEAR:
for {
Expand All @@ -201,6 +201,11 @@ func (l *ZkEventListener) listenDirEvent(zkPath string, listener remoting.DataLi
}
}
l.client.RegisterEvent(zkPath, &event)
if err == errNilNode {
logger.Warnf("listenDirEvent(path{%s}) got errNilNode,so exit listen", zkPath)
l.client.UnregisterEvent(zkPath, &event)
return
}
select {
case <-getty.GetTimeWheel().After(timeSecondDuration(failTimes * ConnDelay)):
l.client.UnregisterEvent(zkPath, &event)
Expand Down