/
zkController.go
517 lines (485 loc) · 17.2 KB
/
zkController.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* 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 controller
import (
"fmt"
"log"
"path/filepath"
"strings"
"sync"
"time"
"github.com/Tencent/bk-bcs/bcs-common/common/zkclient"
"github.com/Tencent/bk-bcs/bcs-common/pkg/cache"
"github.com/Tencent/bk-bcs/bcs-services/bcs-dns/plugin/bcsscheduler/metrics"
bcsSchedulerUtil "github.com/Tencent/bk-bcs/bcs-services/bcs-dns/plugin/bcsscheduler/util"
clientGoCache "k8s.io/client-go/tools/cache"
"github.com/samuel/go-zookeeper/zk"
"golang.org/x/net/context"
)
//ZkClient interface to define zk operation
//interface is only use for dependency injection
type ZkClient interface {
Close()
Get(path string) ([]byte, *zk.Stat, error)
GetW(path string) ([]byte, *zk.Stat, <-chan zk.Event, error)
Children(path string) ([]string, *zk.Stat, error)
ChildrenW(path string) ([]string, *zk.Stat, <-chan zk.Event, error)
Exists(path string) (bool, error)
ExistsW(path string) (bool, *zk.Stat, <-chan zk.Event, error)
}
//wrapperClient wrapper for zk client in bcs-common.zkclient
type wrapperClient struct {
client *zkclient.ZkClient
}
func (wrapper *wrapperClient) Close() {
wrapper.client.Close()
}
func (wrapper *wrapperClient) Get(path string) ([]byte, *zk.Stat, error) {
return wrapper.client.GetEx(path)
}
func (wrapper *wrapperClient) GetW(path string) ([]byte, *zk.Stat, <-chan zk.Event, error) {
return wrapper.client.GetW(path)
}
func (wrapper *wrapperClient) Children(path string) ([]string, *zk.Stat, error) {
return wrapper.client.GetChildrenEx(path)
}
func (wrapper *wrapperClient) ChildrenW(path string) ([]string, *zk.Stat, <-chan zk.Event, error) {
return wrapper.client.ChildrenW(path)
}
func (wrapper *wrapperClient) Exists(path string) (bool, error) {
return wrapper.client.Exist(path)
}
func (wrapper *wrapperClient) ExistsW(path string) (bool, *zk.Stat, <-chan zk.Event, error) {
return wrapper.client.ExistW(path)
}
type ZkController struct {
conCxt context.Context //context for exit signal
conCancel context.CancelFunc //stop all goroutine
resyncperiod int //resync all data period
resynced bool //status resynced
zkHost []string //zk host iplist
watchpath string //zookeeper watch path
client ZkClient //zookeeper client
storage cache.Store //cache storage
nsStorage map[string]context.CancelFunc //storage for all namespace watcher
nsLock sync.Mutex //lock for nsStorage
decoder bcsSchedulerUtil.Decoder //decoder for storage
funcs *clientGoCache.ResourceEventHandlerFuncs //funcs for callback
}
//NewZkController create controller according Store, Decoder end EventFuncs
func NewZkController(zkHost []string, path string, period int, cache cache.Store, decoder bcsSchedulerUtil.Decoder, eventFunc *clientGoCache.ResourceEventHandlerFuncs) (*ZkController, error) {
if len(zkHost) == 0 {
return nil, fmt.Errorf("create Controller failed, empty zookeeper host list")
}
if len(path) == 0 {
return nil, fmt.Errorf("create Controller failed, empty watch path")
}
cxt, cancel := context.WithCancel(context.Background())
controller := &ZkController{
conCxt: cxt,
conCancel: cancel,
resyncperiod: period,
watchpath: path,
zkHost: zkHost,
storage: cache,
nsStorage: make(map[string]context.CancelFunc),
decoder: decoder,
funcs: eventFunc,
}
//create connection to zookeeper
client := zkclient.NewZkClient(zkHost)
if conErr := client.ConnectEx(time.Second * 5); conErr != nil {
return nil, fmt.Errorf("controller create zk conection failed, %s", conErr.Error())
}
controller.client = &wrapperClient{
client: client,
}
return controller, nil
}
//RunController running controller, create goroutine watch zookeeper data
func (ctrl *ZkController) RunController(stopCh <-chan struct{}) error {
//check path exist
if err := ctrl.existWatch(ctrl.watchpath); err != nil {
return err
}
//get all children node & setting watch
if err := ctrl.dataTypeWatch(ctrl.watchpath); err != nil {
return fmt.Errorf("Controller watch %s err, %s", ctrl.watchpath, err.Error())
}
//create resync event for all data
tick := time.NewTicker(time.Second * time.Duration(ctrl.resyncperiod))
defer tick.Stop()
for {
select {
case <-ctrl.conCxt.Done():
log.Printf("[WARN] controller ask exist")
return nil
case now := <-tick.C:
//resync all data from watchpath
if ctrl.Resynced() {
log.Printf("[WARN] controller %s under resync now [%s], drop this tick.", ctrl.watchpath, now.String())
continue
}
ctrl.resynced = true
log.Printf("[INFO] resync %s tick, now: %s", ctrl.watchpath, now.String())
ctrl.dataResync()
log.Printf("[INFO] resync %s tick, end: %s", ctrl.watchpath, time.Now().String())
ctrl.resynced = false
}
}
}
//StopController stop controller event, clean all data
func (ctrl *ZkController) StopController() {
log.Printf("[INFO] controller %s stop", ctrl.watchpath)
ctrl.conCancel()
}
//Resynced check controller is under resynced
func (ctrl *ZkController) Resynced() bool {
return ctrl.resynced
}
/**
* inner method for watching zookeeper data
*/
//existWatch check path is exist, block until path exist
func (ctrl *ZkController) existWatch(path string) error {
if len(path) == 0 {
return fmt.Errorf("controller err, get empty path")
}
ok, err := ctrl.client.Exists(path)
if err != nil {
return fmt.Errorf("controller check path %s err, %s", path, err.Error())
}
if !ok {
//path do not exist, create block watch waits for path created
log.Printf("[WARN] controller check path %s do not exist, setting watch waiting for create event.", path)
_, _, events, err := ctrl.client.ExistsW(path)
if err != nil {
log.Printf("[ERROR] controller create %s exist watch failed, %s", path, err.Error())
return err
}
select {
case <-ctrl.conCxt.Done():
log.Printf("[WARN] controller asked to exit when watching path %s", path)
return fmt.Errorf("controller forced exit")
case <-events:
log.Printf("[INFO] controller get path %s existence event.", path)
return nil
}
}
return nil
}
//typeWatch watch data type node from zookeeper.
//data type comes from bcs-scheduler
func (ctrl *ZkController) dataTypeWatch(path string) error {
log.Printf("[INFO] controller create watch for %s", path)
namespaces, stat, events, err := ctrl.client.ChildrenW(path)
if err != nil {
log.Printf("[ERROR] Watch Node %s children failed, %s", path, err.Error())
return err
} else if stat == nil {
log.Printf("[ERROR] Wath node %s state return nil, setting watch failed.", path)
return fmt.Errorf("error watch stat for %s", path)
}
ctrl.namespaceCheck(namespaces)
//wait for namespace node event
go func() {
select {
case <-ctrl.conCxt.Done():
log.Printf("controller ask to stop watch %s children event", path)
return
case event := <-events:
if event.Type == zk.EventNodeChildrenChanged {
allNS, err := ctrl.listChildrenNode(path)
if err != nil {
//list failed
log.Printf("[ERROR] Controller list init path %s children failed in ChildrenChangeEvent, %s", path, err.Error())
} else {
ctrl.namespaceCheck(allNS)
}
} else {
log.Printf("path trigger event we don't care, event: %s", event.Type.String())
}
//register watch again
if err := ctrl.dataTypeWatch(path); err != nil {
log.Printf("controller create init path %s watch failed, %s", path, err.Error())
}
return
}
}()
//doing good if we get here
return nil
}
//listNamespace list all namespace node
func (ctrl *ZkController) listChildrenNode(path string) ([]string, error) {
children, stat, err := ctrl.client.Children(path)
if err != nil {
log.Printf("[ERROR] Get Node %s children failed, %s", path, err.Error())
return nil, err
} else if stat == nil {
log.Printf("[ERROR] Get Node %s state return nil, setting watch failed.", path)
return nil, fmt.Errorf("error watch stat for %s", path)
}
return children, nil
}
//namespaces check namespace is under watch
func (ctrl *ZkController) namespaceCheck(ns []string) {
if len(ns) == 0 {
return
}
for _, item := range ns {
ctrl.nsLock.Lock()
if _, ok := ctrl.nsStorage[item]; ok {
log.Printf("[INFO] controller already got namespace %s in cache", item)
} else {
//create watch for namespace
ns := filepath.Join(ctrl.watchpath, item)
nsCxt, nsCancel := context.WithCancel(ctrl.conCxt)
if err := ctrl.namespaceWatch(nsCxt, ns); err != nil {
log.Printf("[ERROR] controller watch data namespace %s failed, %s. data will be fixed in next Resync event.", ns, err.Error())
} else {
ctrl.nsStorage[item] = nsCancel
log.Printf("[WARN] controller add new namespace %s success", ns)
}
}
ctrl.nsLock.Unlock()
}
}
//namespaceClean clean name with name
func (ctrl *ZkController) namespaceClean(ns string) {
if len(ns) == 0 {
return
}
ctrl.nsLock.Lock()
if item, ok := ctrl.nsStorage[ns]; ok {
delete(ctrl.nsStorage, ns)
item()
log.Printf("controller got namespace %s, cancel all subContext and release", ns)
} else {
log.Printf("[ERROR] controller lost namespace %s in cache when clean", ns)
}
ctrl.nsLock.Unlock()
}
//namespaceWatch watch data node under namespace
func (ctrl *ZkController) namespaceWatch(cxt context.Context, path string) error {
log.Printf("[INFO] controller prepare to watch namespace %s children", path)
ns := filepath.Base(path)
dataNodes, stat, events, err := ctrl.client.ChildrenW(path)
if err != nil {
log.Printf("[ERROR] Watch Node %s children failed, %s", path, err.Error())
return err
} else if stat == nil {
log.Printf("[ERROR] Wath node %s state return nil, setting watch failed.", path)
return fmt.Errorf("error watch stat for %s", path)
}
//goroutine watch
ctrl.dataNodeCheck(cxt, ns, dataNodes)
//wait for namespace node event
go func() {
//todo(developer): add panic protection for cleaning nsStorage dirty data
select {
case <-cxt.Done():
log.Printf("[WARN] controller ask to stop watch %s children event", path)
return
case event := <-events:
if event.Type == zk.EventNodeChildrenChanged {
allData, err := ctrl.listChildrenNode(path)
if err != nil {
//list failed
log.Printf("[ERROR] Controller list namespace path %s children failed in ChildrenChangeEvent, %s", path, err.Error())
} else {
ctrl.dataNodeCheck(cxt, ns, allData)
}
} else if event.Type == zk.EventNodeDeleted {
//namespace node delete, clean namespace data & clean all context
log.Printf("[WARN] controller get namespace %s delete event, stop watch.", path)
ctrl.namespaceClean(ns)
return
} else {
log.Printf("[WARN] path %s trigger event we don't care, event: %s", path, event.Type.String())
}
//register watch again
if err := ctrl.namespaceWatch(cxt, path); err != nil {
log.Printf("[FATAL] controller create init path %s watch failed, %s", path, err.Error())
}
return
}
}()
//doing good if we get here
return nil
}
func (ctrl *ZkController) dataNodeCheck(cxt context.Context, ns string, nodeName []string) {
if len(nodeName) == 0 {
log.Printf("[WARN] controller get no data under %s/%s", ctrl.watchpath, ns)
return
}
for _, node := range nodeName {
key := ns + "/" + node
if _, exist, _ := ctrl.storage.GetByKey(key); !exist {
//create watch for this new node
dataCxt, _ := context.WithCancel(cxt)
path := filepath.Join(ctrl.watchpath, ns, node)
go ctrl.dataWatch(dataCxt, ns, path)
log.Printf("[WARN] controller set path %s under watch", path)
}
//todo(developer): maybe we need to add checking mechanism
//for checking goroutine of data node is healthy
}
}
//dataContentWatch watch detail data content node
func (ctrl *ZkController) dataWatch(cxt context.Context, ns string, path string) {
data, stat, events, err := ctrl.client.GetW(path)
if err != nil {
log.Printf("[ERROR] controller get node data %s failed: %s, watch stop", path, err.Error())
return
} else if stat == nil {
log.Printf("[ERROR] controller get node data %s failed: state return nil", path)
return
}
cur, derr := ctrl.decoder.Decode(data)
if derr != nil {
log.Printf("[ERROR] controller decode %s data failed: %s, wait next watch to fix", path, derr.Error())
} else {
old, exist, _ := ctrl.storage.Get(cur)
if exist {
ctrl.storage.Update(cur)
ctrl.funcs.UpdateFunc(old, cur)
log.Printf("[INFO] %s update content", path)
metrics.ZkNotifyTotal.WithLabelValues(metrics.UpdateOperation).Inc()
} else {
ctrl.storage.Add(cur)
ctrl.funcs.AddFunc(cur)
log.Printf("[INFO] %s add new data object", path)
metrics.DnsTotal.Inc()
metrics.ZkNotifyTotal.WithLabelValues(metrics.AddOperation).Inc()
}
}
//wait for watch event
for {
select {
case <-cxt.Done():
log.Printf("[WARN] controller stop path %s watching", path)
return
case event := <-events:
if event.Type == zk.EventNodeDataChanged {
go ctrl.dataWatch(cxt, ns, path)
return
} else if event.Type == zk.EventNodeDeleted {
node := filepath.Base(path)
ctrl.dataClean(ns, node)
return
} else {
log.Printf("[WARN] data path %s trigger event we don't care, event: %s", path, event.Type.String())
//todo(developer): watch will be lost if we go here
//we need [[ go ctrl.dataWatch(cxt, ns, path)]]
return
}
}
}
}
//dataClean clean data object by key
func (ctrl *ZkController) dataClean(ns, node string) {
if len(node) == 0 {
return
}
key := filepath.Join(ns, node)
key = strings.ToLower(key)
old, exist, _ := ctrl.storage.GetByKey(key)
if exist {
ctrl.storage.Delete(old)
ctrl.funcs.DeleteFunc(old)
log.Printf("[WARN] controller delete %s under %s in cache", key, ctrl.watchpath)
metrics.DnsTotal.Dec()
metrics.ZkNotifyTotal.WithLabelValues(metrics.DeleteOperation).Inc()
} else {
log.Printf("[ERROR] controller lost %s in cache, somewhere go wrong", key)
}
}
//dataResync resync all data from zookeeper
func (ctrl *ZkController) dataResync() {
//list all namespace node
namespaces, _, err := ctrl.client.Children(ctrl.watchpath)
if err != nil {
log.Printf("[ERROR] controller get path %s children failed, %s", ctrl.watchpath, err.Error())
return
}
if len(namespaces) == 0 {
log.Printf("[WARN] controller check path %s no children, wait next resync", ctrl.watchpath)
return
}
//when we iterator all data in zookeeper, we checking:
//1. update cache with zookeeper data by force
//2. cache data is dirty or not(exist in cache but lost in zookeeper)
//step 1
existsIndex := make(map[string]bool)
for _, ns := range namespaces {
//get all data under namespace
nspath := filepath.Join(ctrl.watchpath, ns)
dataNode, err := ctrl.listChildrenNode(nspath)
if err != nil {
log.Printf("[ERROR] controller get path %s children data failed, %s, try next one", nspath, err.Error())
continue
}
if len(dataNode) == 0 {
log.Printf("[INFO] [INFO] controller get no children of %s", nspath)
continue
}
//list all data
for _, node := range dataNode {
nodepath := filepath.Join(nspath, node)
data, _, err := ctrl.client.Get(nodepath)
if err != nil {
log.Printf("[ERROR] controller get %s err, %s", nodepath, err.Error())
continue
}
cur, derr := ctrl.decoder.Decode(data)
if derr != nil {
log.Printf("[ERROR] controller decode %s data failed: %s, wait next watch to fix", nodepath, derr.Error())
} else {
key := ns + "/" + node
old, exist, _ := ctrl.storage.Get(cur)
if exist {
ctrl.storage.Update(cur)
ctrl.funcs.UpdateFunc(old, cur)
existsIndex[key] = true
} else {
//todo(developer): lost data in cache, we add it to cache,
//and we still need add watch goroutine for
//updating data from zookeeper
log.Printf("[WARN] RESYNC found ###%s### lost in cache", nodepath)
ctrl.storage.Add(cur)
ctrl.funcs.AddFunc(cur)
metrics.DnsTotal.Inc()
}
}
}
}
//step 2
cacheIndexs := ctrl.storage.ListKeys()
for _, indexKey := range cacheIndexs {
if _, ok := existsIndex[indexKey]; ok {
continue
}
checkpath := filepath.Join(ctrl.watchpath, indexKey)
exist, err := ctrl.client.Exists(checkpath)
if err != nil {
log.Printf("[ERROR] controller check cache index %s error in zookeeper, %s", indexKey, err.Error())
continue
}
if exist {
log.Printf("[WARN] %s all in cache& zk, but lost in tmp map, maybe added latest or trigger RESYNC warn", indexKey)
continue
}
log.Printf("[WARN] %s lost in zookeeper, TODO: delete in cache.", indexKey)
//todo(developer): logs statistic for repairing this warnning
}
}