-
Notifications
You must be signed in to change notification settings - Fork 248
/
zookeeper.go
365 lines (325 loc) · 10.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package zookeeper xxx
package zookeeper
import (
"fmt"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/common/zkclient"
"github.com/Tencent/bk-bcs/bcs-services/bcs-netservice/storage"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
operatorTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "netservice",
Subsystem: "storage",
Name: "operator_total",
Help: "The total number of operator to storage.",
}, []string{"operator", "status"})
operatorLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "netservice",
Subsystem: "storage",
Name: "operator_latency_seconds",
Help: "BCS netservice storage operation latency statistic.",
Buckets: []float64{0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 3.0},
}, []string{"operator", "status"})
)
const (
statusSuccess = "SUCCESS"
statusFailure = "FAILURE"
)
// init prometheus metrics for zookeeper
func init() {
prometheus.MustRegister(operatorTotal)
prometheus.MustRegister(operatorLatency)
}
func reportMetrics(operator, status string, started time.Time) {
operatorTotal.WithLabelValues(operator, status).Inc()
operatorLatency.WithLabelValues(operator, status).Observe(time.Since(started).Seconds())
}
// ZkClient interface to define zk operation
// interface is only use for dependency injection
type ZkClient interface {
Get(path string) ([]byte, *zkclient.Stat, error)
GetW(path string) ([]byte, *zkclient.Stat, <-chan zkclient.Event, error)
Children(path string) ([]string, *zkclient.Stat, error)
ChildrenW(path string) ([]string, *zkclient.Stat, <-chan zkclient.Event, error)
Exists(path string) (bool, error)
ExistsW(path string) (bool, *zkclient.Stat, <-chan zkclient.Event, error)
Set(path string, data []byte, version int32) error
Create(path string, data []byte) (string, error)
CreateEphAndSeq(path string, data []byte) (string, error)
CreateEphAndSeqEx(path string, data []byte) (string, error)
Delete(path string, version int32) error
Close()
}
// wrapperClient wrapper for zk client in bcs-common.zkclient
type wrapperClient struct {
client *zkclient.ZkClient
}
// Close xxx
func (wrapper *wrapperClient) Close() {
wrapper.client.Close()
}
// Get xxx
func (wrapper *wrapperClient) Get(path string) ([]byte, *zkclient.Stat, error) {
return wrapper.client.GetEx(path)
}
// GetW xxx
func (wrapper *wrapperClient) GetW(path string) ([]byte, *zkclient.Stat, <-chan zkclient.Event, error) {
return wrapper.client.GetW(path)
}
// Children xxx
func (wrapper *wrapperClient) Children(path string) ([]string, *zkclient.Stat, error) {
return wrapper.client.GetChildrenEx(path)
}
// ChildrenW xxx
func (wrapper *wrapperClient) ChildrenW(path string) ([]string, *zkclient.Stat, <-chan zkclient.Event, error) {
return wrapper.client.ChildrenW(path)
}
// Exists xxx
func (wrapper *wrapperClient) Exists(path string) (bool, error) {
return wrapper.client.Exist(path)
}
// ExistsW xxx
func (wrapper *wrapperClient) ExistsW(path string) (bool, *zkclient.Stat, <-chan zkclient.Event, error) {
return wrapper.client.ExistW(path)
}
// Set xxx
func (wrapper *wrapperClient) Set(path string, data []byte, version int32) error {
return wrapper.client.Set(path, string(data), version)
}
// Create xxx
func (wrapper *wrapperClient) Create(path string, data []byte) (string, error) {
return path, wrapper.client.Update(path, string(data))
}
// CreateEphAndSeq xxx
func (wrapper *wrapperClient) CreateEphAndSeq(path string, data []byte) (string, error) {
return path, wrapper.client.CreateEphAndSeq(path, data)
}
// CreateEphAndSeqEx xxx
func (wrapper *wrapperClient) CreateEphAndSeqEx(path string, data []byte) (string, error) {
return wrapper.client.CreateEphAndSeqEx(path, data)
}
// Delete xxx
func (wrapper *wrapperClient) Delete(path string, version int32) error {
return wrapper.client.Del(path, version)
}
// ConLocker wrapper lock for release connection
type ConLocker struct {
path string // path for lock
lock *zkclient.ZkLock // bcs common lock
}
// Lock try to Lock
func (cl *ConLocker) Lock() error {
started := time.Now()
err := cl.lock.LockEx(cl.path, time.Second*3)
if err != nil {
reportMetrics("lock", statusFailure, started)
} else {
reportMetrics("lock", statusSuccess, started)
}
return err
}
// Unlock release lock and connection
func (cl *ConLocker) Unlock() error {
started := time.Now()
if err := cl.lock.UnLock(); err != nil {
reportMetrics("unlock", statusFailure, started)
} else {
reportMetrics("unlock", statusSuccess, started)
}
return nil
}
// NewStorage create etcd storage
func NewStorage(hosts string) storage.Storage {
// create zookeeper connection
host := strings.Split(hosts, ",")
blog.Info("Storage create zookeeper connection with %s", hosts)
// conn, _, conErr := zk.Connect(host, time.Second*5)
// if conErr != nil {
// blog.Error("Storage create zookeeper connection failed: %v", conErr)
// return nil
// }
bcsClient := zkclient.NewZkClient(host)
if conErr := bcsClient.ConnectEx(time.Second * 5); conErr != nil {
blog.Errorf("Storage create zookeeper connection failed: %v", conErr)
return nil
}
blog.Infof("Storage connect to zookeeper %s success", hosts)
s := &zkStorage{
zkHost: host,
zkClient: &wrapperClient{
client: bcsClient,
},
}
return s
}
// eStorage storage data in etcd
type zkStorage struct {
zkHost []string // zookeeper host info, for reconnection
zkClient ZkClient // zookeeper client for operation
}
// Stop xxx
func (zks *zkStorage) Stop() {
zks.zkClient.Close()
}
// GetLocker xxx
func (zks *zkStorage) GetLocker(key string) (storage.Locker, error) {
started := time.Now()
defer func() {
reportMetrics("getLocker", statusSuccess, started)
}()
blog.Infof("zkStorage create %s locker", key)
bcsLock := zkclient.NewZkLock(zks.zkHost)
wrap := &ConLocker{
path: key,
lock: bcsLock,
}
return wrap, nil
}
// Register register self node
func (zks *zkStorage) Register(path string, data []byte) error {
started := time.Now()
_, err := zks.zkClient.CreateEphAndSeq(path, data)
if err != nil {
blog.Errorf("zkStorage register %s failed, %v", path, err)
reportMetrics("createEphAndSeq", statusFailure, started)
return err
}
reportMetrics("createEphAndSeq", statusSuccess, started)
return nil
}
// RegisterAndWatch register and watch self node
func (zks *zkStorage) RegisterAndWatch(path string, data []byte) error {
newPath, err := zks.zkClient.CreateEphAndSeqEx(path, data)
if err != nil {
blog.Errorf("fail to register server node(%s), err %s", path, err.Error())
return fmt.Errorf("fail to register server node(%s), err %s", path, err.Error())
}
go func() {
tick := time.NewTicker(20 * time.Second)
defer tick.Stop()
for {
select {
case <-tick.C:
started := time.Now()
isExisted, err := zks.zkClient.Exists(newPath)
if err != nil {
reportMetrics("exist", statusFailure, started)
blog.Warnf("zkClient.exists failed, wait next tick, err %s", err.Error())
} else {
reportMetrics("exist", statusFailure, started)
}
if !isExisted {
started = time.Now()
blog.Warnf("server node(%s) does not exist, try to create new server node", path)
newPath, err = zks.zkClient.CreateEphAndSeqEx(path, data)
if err != nil {
blog.Warnf("failed to create node(%s), wait next tick", path)
reportMetrics("createEphAndSeq", statusFailure, started)
} else {
blog.Warnf("create server node(%s) successfully", newPath)
reportMetrics("createEphAndSeq", statusSuccess, started)
}
}
}
}
}()
fmt.Printf("finish register server node(%s) and watch it\n", path)
return nil
}
// Add add data
func (zks *zkStorage) Add(key string, value []byte) error {
started := time.Now()
_, err := zks.zkClient.Create(key, value)
if err != nil {
blog.Errorf("zkStorage add %s with value %s err, %v", key, string(value), err)
reportMetrics("create", statusFailure, started)
return err
}
reportMetrics("create", statusFailure, started)
return nil
}
// Delete delete node by key
func (zks *zkStorage) Delete(key string) ([]byte, error) {
// done(DeveloperJim): get data before delete
started := time.Now()
data, err := zks.Get(key)
if err != nil {
reportMetrics("get", statusFailure, started)
return []byte(""), err
}
reportMetrics("get", statusSuccess, started)
started = time.Now()
err = zks.zkClient.Delete(key, -1)
if err != nil {
reportMetrics("delete", statusFailure, started)
return data, err
}
reportMetrics("delete", statusSuccess, started)
return data, nil
}
// Update update node by value
func (zks *zkStorage) Update(key string, value []byte) error {
started := time.Now()
err := zks.zkClient.Set(key, value, -1)
if err != nil {
reportMetrics("set", statusFailure, started)
} else {
reportMetrics("set", statusSuccess, started)
}
return err
}
// Get get data of path
func (zks *zkStorage) Get(key string) ([]byte, error) {
started := time.Now()
data, stat, err := zks.zkClient.Get(key)
if err != nil {
reportMetrics("get", statusFailure, started)
return nil, err
} else if stat == nil {
reportMetrics("get", statusFailure, started)
return nil, fmt.Errorf("lost %s stat", key)
}
reportMetrics("get", statusSuccess, started)
return data, nil
}
// List all children nodes
func (zks *zkStorage) List(key string) ([]string, error) {
started := time.Now()
list, stat, err := zks.zkClient.Children(key)
if err != nil {
reportMetrics("children", statusFailure, started)
return nil, err
}
if stat == nil {
reportMetrics("children", statusFailure, started)
return nil, fmt.Errorf("path %s status lost", key)
}
reportMetrics("children", statusSuccess, started)
return list, nil
}
// Exist check path exist
func (zks *zkStorage) Exist(key string) (bool, error) {
started := time.Now()
e, err := zks.zkClient.Exists(key)
if err != nil {
reportMetrics("exist", statusFailure, started)
return false, err
}
reportMetrics("exist", statusFailure, started)
return e, nil
}