forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_impl_statuscheck.go
303 lines (253 loc) · 8.15 KB
/
plugin_impl_statuscheck.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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 statuscheck
import (
"context"
"sync"
"time"
"github.com/ligato/cn-infra/core"
"github.com/ligato/cn-infra/datasync"
"github.com/ligato/cn-infra/health/statuscheck/model/status"
"github.com/ligato/cn-infra/logging"
)
const (
// Init state means that the initialization of the plugin is in progress.
Init PluginState = "init"
// OK state means that the plugin is healthy.
OK PluginState = "ok"
// Error state means that some error has occurred in the plugin.
Error PluginState = "error"
// frequency of periodic writes of state data into ETCD
periodicWriteTimeout time.Duration = time.Second * 10
// frequency of periodic plugin state probing
periodicProbingTimeout time.Duration = time.Second * 5
)
// Plugin struct holds all plugin-related data.
type Plugin struct {
Deps
access sync.Mutex // lock for the Plugin data
agentStat *status.AgentStatus // overall agent status
pluginStat map[string]*status.PluginStatus // plugin's status
pluginProbe map[string]PluginStateProbe // registered status probes
ctx context.Context
cancel context.CancelFunc // cancel can be used to cancel all goroutines and their jobs inside of the plugin
wg sync.WaitGroup // wait group that allows to wait until all goroutines of the plugin have finished
}
// Deps lists the dependencies of statuscheck plugin.
type Deps struct {
Log logging.PluginLogger // inject
PluginName core.PluginName // inject
Transport datasync.KeyProtoValWriter // inject (optional)
}
// Init prepares the initial status data.
func (p *Plugin) Init() error {
// write initial status data into ETCD
p.agentStat = &status.AgentStatus{
BuildVersion: core.BuildVersion,
BuildDate: core.BuildDate,
State: status.OperationalState_INIT,
StartTime: time.Now().Unix(),
LastChange: time.Now().Unix(),
}
// init pluginStat map
p.pluginStat = make(map[string]*status.PluginStatus)
// init map with plugin state probes
p.pluginProbe = make(map[string]PluginStateProbe)
// prepare context for all go routines
p.ctx, p.cancel = context.WithCancel(context.Background())
return nil
}
// AfterInit starts go routines for periodic probing and periodic updates.
// Initial state data are published via the injected transport.
func (p *Plugin) AfterInit() error {
p.access.Lock()
defer p.access.Unlock()
// do periodic status probing for plugins that have provided the probe function
go p.periodicProbing(p.ctx)
// do periodic updates of the state data in ETCD
go p.periodicUpdates(p.ctx)
p.publishAgentData()
// transition to OK state if there are no plugins
if len(p.pluginStat) == 0 {
p.agentStat.State = status.OperationalState_OK
p.agentStat.LastChange = time.Now().Unix()
p.publishAgentData()
}
return nil
}
// Close stops go routines for periodic probing and periodic updates.
func (p *Plugin) Close() error {
p.cancel()
p.wg.Wait()
return nil
}
// Register a plugin for status change reporting.
func (p *Plugin) Register(pluginName core.PluginName, probe PluginStateProbe) {
p.access.Lock()
defer p.access.Unlock()
stat := &status.PluginStatus{
State: status.OperationalState_INIT,
LastChange: time.Now().Unix(),
}
p.pluginStat[string(pluginName)] = stat
if probe != nil {
p.pluginProbe[string(pluginName)] = probe
}
// write initial status data into ETCD
p.publishPluginData(pluginName, stat)
p.Log.Infof("Plugin %v: status check probe registered", pluginName)
}
// ReportStateChange can be used to report a change in the status
// of a previously registered plugin.
func (p *Plugin) ReportStateChange(pluginName core.PluginName, state PluginState, lastError error) {
p.access.Lock()
defer p.access.Unlock()
stat, ok := p.pluginStat[string(pluginName)]
if !ok {
p.Log.Errorf("Unregistered plugin %s is reporting the state, ignoring.", pluginName)
return
}
// update the state only if it has really changed
changed := true
if stateToProto(state) == stat.State {
if lastError == nil && stat.Error == "" {
changed = false
}
if lastError != nil && lastError.Error() == stat.Error {
changed = false
}
}
if !changed {
return
}
p.Log.WithFields(map[string]interface{}{"plugin": pluginName, "state": state, "lastErr": lastError}).Info(
"Agent plugin state update.")
// update plugin state
stat.State = stateToProto(state)
stat.LastChange = time.Now().Unix()
if lastError != nil {
stat.Error = lastError.Error()
} else {
stat.Error = ""
}
p.publishPluginData(pluginName, stat)
// update global state if needed
changeGlobalState := true
if state == OK {
// by transition to OK state, check if all plugins are OK
for _, s := range p.pluginStat {
if s.State != status.OperationalState_OK {
changeGlobalState = false
break
}
}
}
if changeGlobalState {
p.agentStat.State = stateToProto(state)
p.agentStat.LastChange = time.Now().Unix()
p.publishAgentData()
}
}
// publishAgentData writes the current global agent state into ETCD.
func (p *Plugin) publishAgentData() error {
p.agentStat.LastUpdate = time.Now().Unix()
if p.Transport != nil {
return p.Transport.Put(status.AgentStatusKey(), p.agentStat)
}
return nil
}
// publishPluginData writes the current plugin state into ETCD.
func (p *Plugin) publishPluginData(pluginName core.PluginName, pluginStat *status.PluginStatus) error {
pluginStat.LastUpdate = time.Now().Unix()
if p.Transport != nil {
return p.Transport.Put(status.PluginStatusKey(string(pluginName)), pluginStat)
}
return nil
}
// publishAllData publishes global agent + all plugins state data into ETCD.
func (p *Plugin) publishAllData() {
p.access.Lock()
defer p.access.Unlock()
p.publishAgentData()
for name, s := range p.pluginStat {
p.publishPluginData(core.PluginName(name), s)
}
}
// periodicProbing does periodic status probing for all plugins
// that have registered probe functions.
func (p *Plugin) periodicProbing(ctx context.Context) {
p.wg.Add(1)
defer p.wg.Done()
for {
select {
case <-time.After(periodicProbingTimeout):
for pluginName, probe := range p.pluginProbe {
state, lastErr := probe()
p.ReportStateChange(core.PluginName(pluginName), state, lastErr)
// just check in-between probes if the plugin is closing
select {
case <-ctx.Done():
return
default:
continue
}
}
case <-ctx.Done():
return
}
}
}
// periodicUpdates does periodic writes of state data into ETCD.
func (p *Plugin) periodicUpdates(ctx context.Context) {
p.wg.Add(1)
defer p.wg.Done()
for {
select {
case <-time.After(periodicWriteTimeout):
p.publishAllData()
case <-ctx.Done():
return
}
}
}
// getAgentState return current global operational state of the agent.
func (p *Plugin) getAgentState() status.OperationalState {
p.access.Lock()
defer p.access.Unlock()
return p.agentStat.State
}
// GetAgentStatus return current global operational state of the agent.
func (p *Plugin) GetAgentStatus() status.AgentStatus {
p.access.Lock()
defer p.access.Unlock()
return *(p.agentStat)
}
// stateToProto converts agent state type into protobuf agent state type.
func stateToProto(state PluginState) status.OperationalState {
switch state {
case Init:
return status.OperationalState_INIT
case OK:
return status.OperationalState_OK
default:
return status.OperationalState_ERROR
}
}
// GetAllPluginStatus returns a map containing pluginname and its status, for all plugins
func (p *Plugin) GetAllPluginStatus() map[string]*status.PluginStatus {
//TODO - used currently, will be removed after incoporating improvements for exposing copy of map
p.access.Lock()
defer p.access.Unlock()
return p.pluginStat
}