-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent_heartbeat.go
130 lines (105 loc) · 2.93 KB
/
agent_heartbeat.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
package service
import (
"reflect"
"sync"
"sync/atomic"
cModel "github.com/fwtpe/owl-backend/common/model"
cQueue "github.com/fwtpe/owl-backend/common/queue"
"github.com/fwtpe/owl-backend/modules/hbs/cache"
)
var (
elementType = reflect.TypeOf(new(cModel.FalconAgentHeartbeat))
)
type AgentHeartbeatService struct {
wg *sync.WaitGroup
safeQ *cQueue.Queue
qConfig *cQueue.Config
running bool
agentsPutCnt int64
heartbeatCall func([]*cModel.FalconAgentHeartbeat) (int64, int64)
rowsAffectedCnt int64
agentsDroppedCnt int64
}
func NewAgentHeartbeatService(config *cQueue.Config) *AgentHeartbeatService {
return &AgentHeartbeatService{
wg: &sync.WaitGroup{},
safeQ: cQueue.New(),
qConfig: config,
heartbeatCall: agentHeartbeatCall,
}
}
func (s *AgentHeartbeatService) Start() {
if s.running {
logger.Infoln("[Service] AgentHeartbeat is already running. Ineffectively start.")
return
}
s.running = true
logger.Infoln("[Service] AgentHeartbeat is starting.")
s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
if !s.running {
break
}
s.consumeHeartbeatQueue(false)
}
s.consumeHeartbeatQueue(true)
}()
}
func (s *AgentHeartbeatService) consumeHeartbeatQueue(flushing bool) {
agents := s.safeQ.DrainNWithDurationByReflectType(s.qConfig, elementType).([]*cModel.FalconAgentHeartbeat)
if len(agents) == 0 {
return
}
r, d := s.heartbeatCall(agents)
s.rowsAffectedCnt += r
s.agentsDroppedCnt += d
if flushing {
logger.Infoln("[Service] AgentHeartbeat is flushing. Number of agents:", len(agents))
s.consumeHeartbeatQueue(flushing)
}
}
func (s *AgentHeartbeatService) Stop() {
if !s.running {
logger.Infoln("[Service] AgentHeartbeat is already stopped. Ineffectively stop.")
return
}
s.running = false
logger.Infof("[Service] AgentHeartbeat is stopping. Size of queue: %d", s.CurrentSize())
/**
* Waiting for queue to be processed
*/
s.wg.Wait()
}
func (s *AgentHeartbeatService) Put(req *cModel.AgentReportRequest, updateTime int64) {
if !s.running {
logger.Infoln("[Service] AgentHeartbeat is stopped. Ineffectively put.")
return
}
cache.Agents.Put(req, updateTime)
hb := requestToHeartbeat(req, updateTime)
s.safeQ.Enqueue(hb)
atomic.AddInt64(&(s.agentsPutCnt), 1)
}
func (s *AgentHeartbeatService) CurrentSize() int {
return s.safeQ.Len()
}
func (s *AgentHeartbeatService) CumulativeAgentsDropped() int64 {
return s.agentsDroppedCnt
}
func (s *AgentHeartbeatService) CumulativeAgentsPut() int64 {
return s.agentsPutCnt
}
func (s *AgentHeartbeatService) CumulativeRowsAffected() int64 {
return s.rowsAffectedCnt
}
func requestToHeartbeat(req *cModel.AgentReportRequest, updateTime int64) *cModel.FalconAgentHeartbeat {
return &cModel.FalconAgentHeartbeat{
Hostname: req.Hostname,
IP: req.IP,
AgentVersion: req.AgentVersion,
PluginVersion: req.PluginVersion,
UpdateTime: updateTime,
}
}