-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathzookeeper.go
176 lines (157 loc) · 4.6 KB
/
zookeeper.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package solr
import (
"bytes"
"encoding/json"
"fmt"
"github.com/samuel/go-zookeeper/zk"
"strings"
"time"
)
type zookeeper struct {
connectionString string
zkConnection *zk.Conn
collection string
zkRoot string
pollSleep time.Duration
}
type stateChanged func([]byte, error)
type Zookeeper interface {
IsConnected() bool
Connect() error
GetConnectionString() string
Get(path string) ([]byte, int, error)
Poll(path string, cb stateChanged)
GetClusterState() (map[string]Collection, int, error)
GetClusterStateW() (map[string]Collection, int, <-chan zk.Event, error)
GetLiveNodes() ([]string, error)
GetLiveNodesW() ([]string, <-chan zk.Event, error)
GetLeaderElectW() (<-chan zk.Event, error)
GetClusterProps() (ClusterProps, error)
ZKLogger(l Logger)
}
func NewZookeeper(connectionString string, zkRoot string, collection string) Zookeeper {
return &zookeeper{
connectionString: connectionString,
zkRoot: zkRoot,
collection: collection,
pollSleep: time.Duration(1) * time.Second,
}
}
func (z *zookeeper) Connect() error {
servers := strings.Split(z.connectionString, ",")
zkConnection, _, err := zk.Connect(servers, time.Second) //*10)
if err != nil {
return err
}
z.zkConnection = zkConnection
return nil
}
func (z *zookeeper) ZKLogger(l Logger) {
if z.zkConnection != nil {
z.zkConnection.SetLogger(l)
}
}
func (z *zookeeper) IsConnected() bool {
if z.zkConnection == nil {
return false
}
return z.zkConnection.State() != zk.StateDisconnected &&
z.zkConnection.State() != zk.StateExpired &&
z.zkConnection.State() != zk.StateAuthFailed
}
func (z *zookeeper) Get(node string) ([]byte, int, error) {
bytes, stat, err := z.zkConnection.Get(node)
if err != nil {
return nil, 0, err
}
return bytes, int(stat.Version), nil
}
func (z *zookeeper) GetConnectionString() string {
return z.connectionString
}
func (z *zookeeper) Poll(path string, cb stateChanged) {
for {
bytes, _, err := z.Get(path)
cb(bytes, err)
time.Sleep(z.pollSleep)
}
}
func (z *zookeeper) GetClusterStateW() (map[string]Collection, int, <-chan zk.Event, error) {
node, stat, events, err := z.zkConnection.GetW(z.getClusterStatePath(z.zkRoot, z.collection))
if err != nil {
return nil, 0, events, err
}
cs, err := deserializeClusterState(node)
if err != nil {
return cs, 0, events, err
}
return cs, int(stat.Version), events, nil
}
func (z *zookeeper) GetClusterState() (map[string]Collection, int, error) {
node, stat, err := z.zkConnection.Get(z.getClusterStatePath(z.zkRoot, z.collection))
if err != nil {
return nil, 0, err
}
cs, err := deserializeClusterState(node)
if err != nil {
return cs, 0, err
}
return cs, int(stat.Version), nil
}
func (z *zookeeper) GetLeaderElectW() (<-chan zk.Event, error) {
_, _, events, err := z.zkConnection.GetW(fmt.Sprintf("/%s/collections/%s/leader_elect", z.zkRoot, z.collection))
return events, err
}
func (z *zookeeper) GetClusterProps() (ClusterProps, error) {
node, _, err := z.zkConnection.Get(fmt.Sprintf("/%s/clusterprops.json", z.zkRoot))
if err != nil {
if err == zk.ErrNoNode {
return ClusterProps{UrlScheme: "http"}, nil
}
return ClusterProps{}, err
}
cp, err := deserializeClusterProps(node)
return cp, err
}
func (z *zookeeper) GetLiveNodesW() ([]string, <-chan zk.Event, error) {
children, _, events, err := z.zkConnection.ChildrenW(z.getLiveNodesPath(z.zkRoot))
if err != nil {
return children, events, err
}
for i, node := range children {
children[i] = strings.Replace(node, "_solr", "", -1)
}
return children, events, nil
}
func (z *zookeeper) GetLiveNodes() ([]string, error) {
children, _, err := z.zkConnection.Children(z.getLiveNodesPath(z.zkRoot))
if err != nil {
return children, err
}
for i, node := range children {
children[i] = strings.Replace(node, "_solr", "", -1)
}
return children, nil
}
func (z *zookeeper) getLiveNodesPath(root string) string {
return fmt.Sprintf("/%s/live_nodes", root)
}
func deserializeClusterState(node []byte) (map[string]Collection, error) {
var collections map[string]Collection
decoder := json.NewDecoder(bytes.NewBuffer(node))
if err := decoder.Decode(&collections); err != nil {
return nil, err
}
return collections, nil
}
func deserializeClusterProps(node []byte) (ClusterProps, error) {
var clusterProps ClusterProps
decoder := json.NewDecoder(bytes.NewBuffer(node))
if err := decoder.Decode(&clusterProps); err != nil {
return ClusterProps{}, err
}
return clusterProps, nil
}
func (z *zookeeper) getClusterStatePath(root string, collection string) string {
return fmt.Sprintf("/%s/collections/%s/state.json", root, collection)
}