/
hostsnap.go
697 lines (637 loc) · 17.6 KB
/
hostsnap.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 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 datacollection
import (
"fmt"
"io"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/rs/xid"
"github.com/tidwall/gjson"
redis "gopkg.in/redis.v5"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/storage"
)
var (
getMasterProcIntervalTime = time.Second * 10
masterProcLockLiveTime = getMasterProcIntervalTime + time.Second*10
clearMsgChanTime int64 = 120
masterProcLockContent string
fetchDBInterval = time.Minute * 10
maxconcurrent = runtime.NumCPU()
)
type HostSnap struct {
id string
hostChanName []string
msgChan chan string
maxSize int
ts time.Time
lastMesgTs time.Time
isMaster bool
sync.Mutex
interrupt chan error
maxconcurrent int
doneCh chan struct{}
resetHandle chan struct{}
redisCli *redis.Client
snapCli *redis.Client
subscribing bool
cache *Cache
db storage.DI
wg *sync.WaitGroup
}
type Cache struct {
cache map[bool]*HostCache
flag bool
}
func NewHostSnap(chanName []string, maxSize int, redisCli, snapCli *redis.Client, db storage.DI) *HostSnap {
if 0 == maxSize {
maxSize = 100
}
hostSnapInstance := &HostSnap{
hostChanName: chanName,
msgChan: make(chan string, maxSize*4),
interrupt: make(chan error),
resetHandle: make(chan struct{}),
maxSize: maxSize,
redisCli: redisCli,
snapCli: snapCli,
db: db,
ts: time.Now(),
id: xid.New().String()[5:],
maxconcurrent: maxconcurrent,
wg: &sync.WaitGroup{},
doneCh: make(chan struct{}),
cache: &Cache{
cache: map[bool]*HostCache{},
flag: false,
},
}
return hostSnapInstance
}
func (h *HostSnap) Start() {
go func() {
h.Run()
for {
time.Sleep(time.Second * 10)
NewHostSnap(h.hostChanName, h.maxSize, h.redisCli, h.snapCli, h.db).Run()
}
}()
}
func (h *HostSnap) Run() {
defer func() {
syserr := recover()
if syserr != nil {
blog.Errorf("emergency error happened %s, we will try again 10s later, stack: \n%s", syserr, debug.Stack())
}
close(h.doneCh)
h.isMaster = false
return
}()
blog.Infof("datacollection start with maxconcurrent: %d", h.maxconcurrent)
ticker := time.NewTicker(getMasterProcIntervalTime)
var err error
var msg string
var msgs []string
var addCount int
var waitCnt int
go h.fetchDB()
if h.saveRunning() {
go h.subChan(h.snapCli, h.hostChanName)
} else {
blog.Infof("run: there is other master process exists, recheck after %v ", getMasterProcIntervalTime)
}
for {
select {
case <-ticker.C:
if h.saveRunning() {
if !h.subscribing {
go h.subChan(h.snapCli, h.hostChanName)
}
}
case msg = <-h.msgChan:
h.Lock()
h.ts = time.Now()
msgs = make([]string, 0, h.maxSize*2)
timeoutCh := time.After(time.Second)
msgs = append(msgs, msg)
addCount = 0
f:
for {
select {
case <-timeoutCh:
break f
case msg = <-h.msgChan:
addCount++
msgs = append(msgs, msg)
}
if addCount > h.maxSize {
break f
}
}
h.Unlock()
waitCnt = 0
for {
if waitCnt > h.maxconcurrent*2 {
blog.Warnf("reset handlers")
close(h.resetHandle)
waitCnt = 0
h.resetHandle = make(chan struct{})
}
if atomic.LoadInt64(&routeCnt) < int64(h.maxconcurrent) {
atomic.AddInt64(&routeCnt, 1)
go h.handleMsg(msgs, h.resetHandle)
break
}
waitCnt++
time.Sleep(time.Millisecond * 100)
}
case err = <-h.interrupt:
blog.Warn("interrupted", err.Error())
h.concede()
}
}
}
var routeCnt = int64(0)
var handleCnt = int64(0)
var handlelock = sync.Mutex{}
var handlets = time.Now()
func (h *HostSnap) handleMsg(msgs []string, resetHandle chan struct{}) error {
defer atomic.AddInt64(&routeCnt, -1)
blog.Infof("handle %d num mesg, routines %d", len(msgs), atomic.LoadInt64(&routeCnt))
for index, msg := range msgs {
if msg == "" {
continue
}
handlelock.Lock()
handleCnt++
if handleCnt%10000 == 0 {
blog.Infof("handle rate: %d/sec", int(float64(handleCnt)/time.Now().Sub(handlets).Seconds()))
handleCnt = 0
handlets = time.Now()
}
handlelock.Unlock()
select {
case <-resetHandle:
blog.Warnf("reset handler, handled %d, set maxSize to %d ", index, h.maxSize)
return nil
case <-h.doneCh:
blog.Warnf("close handler, handled %d")
return nil
default:
var data = msg
if !gjson.Get(msg, "cloudid").Exists() {
data = gjson.Get(msg, "data").String()
}
val := gjson.Parse(data)
host := h.getHostByVal(&val)
if host == nil {
blog.Warnf("host not found, continue, %s", val.String())
continue
}
hostid := fmt.Sprint(host.get(common.BKHostIDField))
if hostid == "" {
blog.Warnf("host id not found, continue, %s", val.String())
continue
}
if err := h.redisCli.Set(common.RedisSnapKeyPrefix+hostid, data, time.Minute*10).Err(); err != nil {
blog.Errorf("save snapshot %s to redis faile: %s", common.RedisSnapKeyPrefix+hostid, err.Error())
}
condition := map[string]interface{}{common.BKHostIDField: host.get(common.BKHostIDField)}
innerip, ok := host.get(common.BKHostInnerIPField).(string)
if !ok {
blog.Infof("innerip is empty, continue, %s", val.String())
continue
}
outip, ok := host.get(common.BKHostOuterIPField).(string)
if !ok {
blog.Warnf("outip is not string, %s", val.String())
}
setter := parseSetter(&val, innerip, outip)
if needToUpdate(setter, host) {
blog.Infof("update by %v, to %v", condition, setter)
if err := h.db.UpdateByCondition(common.BKTableNameBaseHost, setter, condition); err != nil {
blog.Error("update host error:", err.Error())
continue
}
copyVal(setter, host)
}
}
}
return nil
}
func copyVal(a map[string]interface{}, b *HostInst) {
for k, v := range a {
b.set(k, v)
}
}
func needToUpdate(a map[string]interface{}, b *HostInst) bool {
for k, v := range a {
if b.get(k) != v {
return true
}
}
return false
}
func parseSetter(val *gjson.Result, innerIP, outerIP string) map[string]interface{} {
var cpumodule = val.Get("data.cpu.cpuinfo.0.modelName").String()
var cupnum int64
for _, core := range val.Get("data.cpu.cpuinfo.#.cores").Array() {
cupnum += core.Int()
}
var CPUMhz = val.Get("data.cpu.cpuinfo.0.mhz").Int()
var disk int64
for _, disktotal := range val.Get("data.disk.usage.#.total").Array() {
disk += disktotal.Int()
}
var mem = val.Get("data.mem.meminfo.total").Int()
var hostname = val.Get("data.system.info.hostname").String()
var ostype = val.Get("data.system.info.os").String()
var osname string
platform := val.Get("data.system.info.platform").String()
version := val.Get("data.system.info.platformVersion").String()
switch strings.ToLower(ostype) {
case "linux":
version = strings.Replace(version, ".x86_64", "", 1)
version = strings.Replace(version, ".i386", "", 1)
osname = fmt.Sprintf("%s %s", ostype, platform)
ostype = common.HostOSTypeEnumLinux
case "windows":
version = strings.Replace(version, "Microsoft ", "", 1)
platform = strings.Replace(platform, "Microsoft ", "", 1)
osname = fmt.Sprintf("%s", platform)
ostype = common.HostOSTypeEnumWindows
default:
osname = fmt.Sprintf("%s", platform)
}
var OuterMAC, InnerMAC string
for _, inter := range val.Get("data.net.interface").Array() {
for _, addr := range inter.Get("addrs.#.addr").Array() {
ip := strings.Split(addr.String(), "/")[0]
if ip == innerIP {
InnerMAC = inter.Get("hardwareaddr").String()
} else if ip == outerIP {
OuterMAC = inter.Get("hardwareaddr").String()
}
}
}
osbit := val.Get("data.system.info.systemtype").String()
dockerClientVersion := val.Get("data.system.docker.Client.Version").String()
dockerServerVersion := val.Get("data.system.docker.Server.Version").String()
setter := map[string]interface{}{
"bk_cpu": cupnum,
"bk_cpu_module": cpumodule,
"bk_cpu_mhz": CPUMhz,
"bk_disk": disk / 1024 / 1024 / 1024,
"bk_mem": mem / 1024 / 1024,
"bk_os_type": ostype,
"bk_os_name": osname,
"bk_os_version": version,
"bk_host_name": hostname,
"bk_outer_mac": OuterMAC,
"bk_mac": InnerMAC,
"bk_os_bit": osbit,
common.HostFieldDockerClientVersion: dockerClientVersion,
common.HostFieldDockerServerVersion: dockerServerVersion,
}
if cupnum <= 0 {
blog.Infof("bk_cpu not found in message for %s", innerIP)
}
if cpumodule == "" {
blog.Infof("bk_cpu_module not found in message for %s", innerIP)
}
if CPUMhz <= 0 {
blog.Infof("bk_cpu_mhz not found in message for %s", innerIP)
}
if disk <= 0 {
blog.Infof("bk_disk not found in message for %s", innerIP)
}
if mem <= 0 {
blog.Infof("bk_mem not found in message for %s", innerIP)
}
if ostype == "" {
blog.Infof("bk_os_type not found in message for %s", innerIP)
}
if osname == "" {
blog.Infof("bk_os_name not found in message for %s", innerIP)
}
if version == "" {
blog.Infof("bk_os_version not found in message for %s", innerIP)
}
if hostname == "" {
blog.Infof("bk_host_name not found in message for %s", innerIP)
}
if outerIP != "" && OuterMAC == "" {
blog.Infof("bk_outer_mac not found in message for %s", innerIP)
}
if InnerMAC == "" {
blog.Infof("bk_mac not found in message for %s", innerIP)
}
return setter
}
func getIPS(val *gjson.Result) (ips []string) {
interfaces := val.Get("data.net.interface.#.addrs.#.addr").Array()
for _, addrs := range interfaces {
for _, addr := range addrs.Array() {
ip := strings.Split(addr.String(), "/")[0]
if strings.HasPrefix(ip, "127.0.0.") {
continue
}
ips = append(ips, ip)
}
}
if !strings.HasPrefix(val.Get("ip").String(), "127.0.0.") {
ips = append(ips, val.Get("ip").String())
}
return ips
}
func (h *HostSnap) getHostByVal(val *gjson.Result) *HostInst {
cloudid := val.Get("cloudid").String()
ownerID := val.Get("bizid").String()
ips := getIPS(val)
if len(ips) > 0 {
blog.Infof("handle clouid: %s ips: %v", cloudid, ips)
for _, ip := range ips {
if host := h.getCache().get(cloudid + "::" + ip); host != nil {
return host
}
}
blog.Infof("ips not in cache clouid: %s,ip: %v", cloudid, ips)
clouidInt, err := strconv.Atoi(cloudid)
if nil != err {
blog.Infof("cloudid \"%s\" not integer", cloudid)
return nil
}
condition := map[string]interface{}{
common.BKCloudIDField: clouidInt,
common.BKHostInnerIPField: map[string]interface{}{
common.BKDBIN: ips,
},
common.BKOwnerIDField: ownerID,
}
result := []map[string]interface{}{}
err = h.db.GetMutilByCondition(common.BKTableNameBaseHost, nil, condition, &result, "", 0, 0)
if err != nil {
blog.Errorf("fetch db error %v", err)
}
for index := range result {
cloudid := fmt.Sprint(result[index][common.BKCloudIDField])
innerip := fmt.Sprint(result[index][common.BKHostInnerIPField])
inst := &HostInst{data: result[index]}
h.setCache(cloudid+"::"+innerip, inst)
return inst
}
blog.Infof("ips not in cache and db, clouid: %v, ip: %v", cloudid, ips)
} else {
blog.Errorf("message has no ip, message:%s", val.String())
}
return nil
}
func (h *HostSnap) concede() {
blog.Info("concede")
h.isMaster = false
h.subscribing = false
val := h.redisCli.Get(MasterProcLockKey).Val()
if val != h.id {
h.redisCli.Del(MasterProcLockKey)
}
}
func (h *HostSnap) saveRunning() (ok bool) {
var err error
if h.isMaster {
var val string
val, err = h.redisCli.Get(MasterProcLockKey).Result()
if err != nil {
blog.Errorf("master: saveRunning err %v", err)
h.isMaster = false
} else if val == h.id {
blog.Infof("master check : i am still master")
h.redisCli.Set(MasterProcLockKey, h.id, masterProcLockLiveTime)
ok = true
h.isMaster = true
} else {
blog.Infof("exit master,val = %v, id = %v", val, h.id)
h.isMaster = false
ok = false
}
} else {
ok, err = h.redisCli.SetNX(MasterProcLockKey, h.id, masterProcLockLiveTime).Result()
if err != nil {
blog.Errorf("slave: saveRunning err %v", err)
h.isMaster = false
} else if ok {
blog.Infof("slave check: ok")
blog.Infof("i am master from now")
h.isMaster = true
} else {
blog.Infof("slave check: there is other master process exists, recheck after %v ", getMasterProcIntervalTime)
h.isMaster = false
}
}
return ok
}
func (h *HostSnap) subChan(snapcli *redis.Client, chanName []string) {
defer func() {
syserr := recover()
if syserr != nil {
blog.Errorf("subChan emergency error happened %s, we will try again 10s later, stack: \n%s", syserr, debug.Stack())
}
h.subscribing = false
}()
h.subscribing = true
var chanlen int
subChan, err := snapcli.Subscribe(chanName...)
if nil != err {
h.interrupt <- err
blog.Error("subscribe channel faile ", err.Error())
}
closeChan := make(chan struct{})
go h.healthCheck(closeChan)
defer func() {
h.subscribing = false
close(closeChan)
blog.Infof("subChan Close")
subChan.Unsubscribe(chanName...)
}()
var ts = time.Now()
var cnt int64
blog.Infof("subcribing channel %v", chanName)
for {
if false == h.isMaster {
blog.Info("This is not master process, subChan Close")
return
}
received, err := subChan.Receive()
if err == redis.Nil || err == io.EOF {
continue
}
msg, ok := received.(*redis.Message)
if !ok {
continue
}
if nil != err {
blog.Debug("receive messave err", err.Error())
h.interrupt <- err
continue
}
if "" == msg.Payload {
continue
}
chanlen = len(h.msgChan)
if h.maxSize*2 <= chanlen {
blog.Infof("msgChan full, maxsize %d, len %d", h.maxSize, chanlen)
h.clearMsgChan()
}
if chanlen != 0 && chanlen%10 == 0 {
blog.Infof("buff len %d", chanlen)
}
h.lastMesgTs = time.Now()
h.msgChan <- msg.Payload
cnt++
if cnt%10000 == 0 {
blog.Infof("receive rate: %d/sec", int(float64(cnt)/time.Now().Sub(ts).Seconds()))
cnt = 0
ts = time.Now()
}
}
}
func (h *HostSnap) clearMsgChan() {
ts := h.ts
msgCnt := len(h.msgChan) - h.maxSize
blog.Warnf("start clear %d", msgCnt)
var cnt int
for msgCnt > cnt {
h.Lock()
cnt++
if ts != h.ts {
msgCnt = len(h.msgChan) - h.maxSize
} else {
select {
case <-time.After(time.Second * 10):
case <-h.msgChan:
}
}
h.Unlock()
}
if ts == h.ts {
close(h.resetHandle)
}
blog.Warnf("cleared %d", cnt)
}
var cachelock = sync.RWMutex{}
func (h *HostSnap) getCache() *HostCache {
cachelock.RLock()
defer cachelock.RUnlock()
return h.cache.cache[h.cache.flag]
}
func (h *HostSnap) setCache(key string, val *HostInst) {
cachelock.Lock()
h.cache.cache[h.cache.flag].set(key, val)
cachelock.Unlock()
}
func (h *HostSnap) fetchDB() {
cachelock.Lock()
h.cache.cache[h.cache.flag] = h.fetch()
cachelock.Unlock()
go func() {
ticker := time.NewTicker(fetchDBInterval)
for {
select {
case <-ticker.C:
cache := h.fetch()
cachelock.Lock()
h.cache.cache[!h.cache.flag] = cache
h.cache.flag = !h.cache.flag
cachelock.Unlock()
case <-h.doneCh:
blog.Warnf("close fetchDB")
return
}
}
}()
}
func (h *HostSnap) fetch() *HostCache {
result := []map[string]interface{}{}
err := h.db.GetMutilByCondition(common.BKTableNameBaseHost, nil, nil, &result, "", 0, 0)
if err != nil {
blog.Errorf("fetch db error %v", err)
}
hostcache := &HostCache{data: map[string]*HostInst{}}
for index := range result {
cloudid := fmt.Sprint(result[index][common.BKCloudIDField])
innerip := fmt.Sprint(result[index][common.BKHostInnerIPField])
hostcache.data[cloudid+"::"+innerip] = &HostInst{data: result[index]}
}
blog.Infof("success fetch %d collections to cache", len(result))
return hostcache
}
type HostInst struct {
sync.RWMutex
data map[string]interface{}
}
func (h *HostInst) get(key string) interface{} {
h.RLock()
value := h.data[key]
h.RUnlock()
return value
}
func (h *HostInst) set(key string, value interface{}) {
h.Lock()
h.data[key] = value
h.Unlock()
}
type HostCache struct {
sync.RWMutex
data map[string]*HostInst
}
func (h *HostCache) get(key string) *HostInst {
h.RLock()
value := h.data[key]
h.RUnlock()
return value
}
func (h *HostCache) set(key string, value *HostInst) {
h.Lock()
h.data[key] = value
h.Unlock()
}
func (h *HostSnap) healthCheck(closeChan chan struct{}) {
ticker := time.NewTicker(time.Minute)
for {
select {
case <-h.doneCh:
ticker.Stop()
return
case <-closeChan:
ticker.Stop()
return
case <-ticker.C:
channelstatus := 0
if err := h.snapCli.Ping().Err(); err != nil {
channelstatus = common.CCErrHostGetSnapshotChannelClose
blog.Errorf("snap redis server connection error: %s", err.Error())
} else if time.Now().Sub(h.lastMesgTs) > time.Minute {
blog.Errorf("snapchannel was empty in last 1 min ")
channelstatus = common.CCErrHostGetSnapshotChannelEmpty
} else {
channelstatus = common.CCSuccess
}
h.redisCli.Set(RedisSnapKeyChannelStatus, channelstatus, time.Minute*2)
}
}
}