forked from Terry-Mao/gopush-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zk.go
150 lines (137 loc) · 4.19 KB
/
zk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gopush-cluster.
// gopush-cluster is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gopush-cluster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gopush-cluster. If not, see <http://www.gnu.org/licenses/>.
// github.com/samuel/go-zookeeper
// Copyright (c) 2013, Samuel Stauffer <samuel@descolada.com>
// All rights reserved.
package zk
import (
"errors"
"github.com/golang/glog"
"github.com/samuel/go-zookeeper/zk"
"os"
"path"
"strings"
"syscall"
"time"
)
var (
// error
ErrNoChild = errors.New("zk: children is nil")
ErrNodeNotExist = errors.New("zk: node not exist")
)
// Connect connect to zookeeper, and start a goroutine log the event.
func Connect(addr []string, timeout time.Duration) (*zk.Conn, error) {
conn, session, err := zk.Connect(addr, timeout)
if err != nil {
glog.Errorf("zk.Connect(\"%v\", %d) error(%v)", addr, timeout, err)
return nil, err
}
go func() {
for {
event := <-session
glog.V(1).Infof("zookeeper get a event: %s", event.State.String())
}
}()
return conn, nil
}
// Create create zookeeper path, if path exists ignore error
func Create(conn *zk.Conn, fpath string) error {
// create zk root path
tpath := ""
for _, str := range strings.Split(fpath, "/")[1:] {
tpath = path.Join(tpath, "/", str)
glog.V(1).Infof("create zookeeper path: \"%s\"", tpath)
_, err := conn.Create(tpath, []byte(""), 0, zk.WorldACL(zk.PermAll))
if err != nil {
if err == zk.ErrNodeExists {
glog.Warningf("zk.create(\"%s\") exists", tpath)
} else {
glog.Errorf("zk.create(\"%s\") error(%v)", tpath, err)
return err
}
}
}
return nil
}
// RegisterTmp create a ephemeral node, and watch it, if node droped then send a SIGQUIT to self.
func RegisterTemp(conn *zk.Conn, fpath, data string) error {
tpath, err := conn.Create(path.Join(fpath)+"/", []byte(data), zk.FlagEphemeral|zk.FlagSequence, zk.WorldACL(zk.PermAll))
if err != nil {
glog.Errorf("conn.Create(\"%s\", \"%s\", zk.FlagEphemeral|zk.FlagSequence) error(%v)", fpath, data, err)
return err
}
glog.V(1).Infof("create a zookeeper node:%s", tpath)
// watch self
go func() {
for {
glog.Infof("zk path: \"%s\" set a watch", tpath)
exist, _, watch, err := conn.ExistsW(tpath)
if err != nil {
glog.Errorf("zk.ExistsW(\"%s\") error(%v)", tpath, err)
glog.Warningf("zk path: \"%s\" set watch failed, kill itself", tpath)
killSelf()
return
}
if !exist {
glog.Warningf("zk path: \"%s\" not exist, kill itself", tpath)
killSelf()
return
}
event := <-watch
glog.Infof("zk path: \"%s\" receive a event %v", tpath, event)
}
}()
return nil
}
// GetNodesW get all child from zk path with a watch.
func GetNodesW(conn *zk.Conn, path string) ([]string, <-chan zk.Event, error) {
nodes, stat, watch, err := conn.ChildrenW(path)
if err != nil {
if err == zk.ErrNoNode {
return nil, nil, ErrNodeNotExist
}
glog.Errorf("zk.ChildrenW(\"%s\") error(%v)", path, err)
return nil, nil, err
}
if stat == nil {
return nil, nil, ErrNodeNotExist
}
if len(nodes) == 0 {
return nil, nil, ErrNoChild
}
return nodes, watch, nil
}
// GetNodes get all child from zk path.
func GetNodes(conn *zk.Conn, path string) ([]string, error) {
nodes, stat, err := conn.Children(path)
if err != nil {
if err == zk.ErrNoNode {
return nil, ErrNodeNotExist
}
glog.Errorf("zk.Children(\"%s\") error(%v)", path, err)
return nil, err
}
if stat == nil {
return nil, ErrNodeNotExist
}
if len(nodes) == 0 {
return nil, ErrNoChild
}
return nodes, nil
}
// killSelf send a SIGQUIT to self.
func killSelf() {
if err := syscall.Kill(os.Getpid(), syscall.SIGQUIT); err != nil {
glog.Errorf("syscall.Kill(%d, SIGQUIT) error(%v)", os.Getpid(), err)
}
}