forked from goodrain/rainbond
/
manager.go
429 lines (391 loc) · 10.5 KB
/
manager.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
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package prober
import (
"context"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/util"
probe "github.com/goodrain/rainbond/util/prober/probes"
v1 "github.com/goodrain/rainbond/util/prober/types/v1"
)
//Prober Prober
type Prober interface {
GetServiceHealthy(serviceName string) (*v1.HealthStatus, bool)
WatchServiceHealthy(serviceName string) Watcher
CloseWatch(serviceName string, id string) error
Start()
AddServices(in []*v1.Service)
CheckIfExist(in *v1.Service) bool
SetServices([]*v1.Service)
GetServices() []*v1.Service
GetServiceHealth() map[string]*v1.HealthStatus
SetAndUpdateServices([]*v1.Service) error
AddAndUpdateServices([]*v1.Service) error
UpdateServiceProbe(service *v1.Service)
UpdateServicesProbe(services []*v1.Service)
Stop() error
DisableWatcher(serviceName, watcherID string)
EnableWatcher(serviceName, watcherID string)
GetProbe(name string) probe.Probe
StopProbes(names []string)
}
// NewProber creates a new prober.
func NewProber(ctx context.Context, cancel context.CancelFunc) Prober {
statusChan := make(chan *v1.HealthStatus, 100)
status := make(map[string]*v1.HealthStatus)
watches := make(map[string]map[string]*watcher)
m := &probeManager{
ctx: ctx,
cancel: cancel,
statusChan: statusChan,
status: status,
watches: watches,
serviceProbe: make(map[string]probe.Probe),
services: []*v1.Service{},
}
return m
}
//Watcher watcher
type Watcher interface {
GetID() string
GetServiceName() string
Watch() <-chan *v1.HealthStatus
Close() error
}
type probeManager struct {
services []*v1.Service
serviceProbe map[string]probe.Probe
status map[string]*v1.HealthStatus
ctx context.Context
cancel context.CancelFunc
watches map[string]map[string]*watcher
statusChan chan *v1.HealthStatus
lock sync.Mutex
}
type watcher struct {
manager Prober
statusChan chan *v1.HealthStatus
id string
serviceName string
enable bool
}
func (p *probeManager) Start() {
go p.handleStatus()
p.updateAllServicesProbe()
}
func (p *probeManager) Stop() error {
p.cancel()
return nil
}
func (p *probeManager) GetServiceHealthy(serviceName string) (*v1.HealthStatus, bool) {
v, ok := p.status[serviceName]
return v, ok
}
func (p *probeManager) WatchServiceHealthy(serviceName string) Watcher {
logrus.Debugf("service name: %s;watch service healthy...", serviceName)
healthCh := make(chan *v1.HealthStatus, 10)
w := &watcher{
manager: p,
statusChan: healthCh,
id: util.NewUUID(),
serviceName: serviceName,
}
p.lock.Lock()
defer p.lock.Unlock()
if s, ok := p.watches[serviceName]; ok {
s[w.id] = w
} else {
p.watches[serviceName] = map[string]*watcher{
w.id: w,
}
}
return w
}
func (p *probeManager) CloseWatch(serviceName string, id string) error {
p.lock.Lock()
defer p.lock.Unlock()
channel := p.watches[serviceName][id].statusChan
close(channel)
delete(p.watches[serviceName], id)
return nil
}
func (p *probeManager) handleStatus() {
logrus.Debugf("start handling status...")
for {
select {
case status := <-p.statusChan:
p.updateServiceStatus(status)
p.lock.Lock()
if watcherMap, ok := p.watches[status.Name]; ok {
for _, watcher := range watcherMap {
if watcher.enable {
watcher.statusChan <- status
}
}
}
p.lock.Unlock()
case <-p.ctx.Done():
return
}
}
}
func (p *probeManager) updateServiceStatus(status *v1.HealthStatus) {
logrus.Debugf("who: %s, status; %s, info: %s", status.Name, status.Status, status.Info)
p.lock.Lock()
defer p.lock.Unlock()
exist, ok := p.status[status.Name]
if !ok {
p.status[status.Name] = status
return
}
if exist.LastStatus != status.Status {
status.StatusChange = true
} else {
status.StatusChange = false
}
status.LastStatus = status.Status
if status.Status != v1.StatHealthy {
number := exist.ErrorNumber + 1
status.ErrorNumber = number
if exist.StartErrorTime.IsZero() {
status.StartErrorTime = time.Now()
} else {
status.StartErrorTime = exist.StartErrorTime
}
status.ErrorDuration = time.Now().Sub(exist.StartErrorTime)
p.status[status.Name] = status
} else {
status.ErrorNumber = 0
status.ErrorDuration = 0
var zero time.Time
status.StartErrorTime = zero
p.status[status.Name] = status
}
}
func (p *probeManager) SetServices(in []*v1.Service) {
p.services = in
}
func (p *probeManager) GetServices() []*v1.Service {
return p.services
}
func (p *probeManager) SetAndUpdateServices(inner []*v1.Service) error {
p.services = inner
p.updateAllServicesProbe()
return nil
}
// AddAndUpdateServices adds services, then updates all services.
func (p *probeManager) AddAndUpdateServices(in []*v1.Service) error {
for _, i := range in {
exist := false
for index, svc := range p.services {
if svc.Name == i.Name {
(p.services)[index] = i
exist = true
}
}
if !exist {
p.services = append(p.services, i)
}
}
p.services = append(p.services, in...)
p.updateAllServicesProbe()
return nil
}
// AddServices adds services.
func (p *probeManager) AddServices(in []*v1.Service) {
for _, i := range in {
exist := false
for index, svc := range p.services {
if svc.Name == i.Name {
(p.services)[index] = i
exist = true
}
}
if !exist {
p.services = append(p.services, i)
}
}
p.services = append(p.services, in...)
}
// CheckAndAddService checks if the input service exists, if it does not exist, add it.
func (p *probeManager) CheckIfExist(in *v1.Service) bool {
exist := false
for _, svc := range p.services {
if svc.Name == in.Name {
exist = true
}
}
return exist
}
func (p *probeManager) isNeedUpdate(new *v1.Service) bool {
var old *v1.Service
for _, svc := range p.services {
if svc.Name == new.Name {
old = svc
}
}
if old == nil {
// not found old one
return true
}
return !new.Equal(old)
}
func (p *probeManager) GetServiceHealth() map[string]*v1.HealthStatus {
return p.status
}
func (p *probeManager) EnableWatcher(serviceName, watcherID string) {
logrus.Info("Enable check healthy status of service: ", serviceName)
p.lock.Lock()
defer p.lock.Unlock()
if s, ok := p.watches[serviceName]; ok {
if w, ok := s[watcherID]; ok {
w.enable = true
}
} else {
logrus.Error("Can not enable the watcher: Not found service: ", serviceName)
}
}
func (p *probeManager) DisableWatcher(serviceName, watcherID string) {
logrus.Info("Disable check healthy status of service: ", serviceName)
p.lock.Lock()
defer p.lock.Unlock()
if s, ok := p.watches[serviceName]; ok {
if w, ok := s[watcherID]; ok {
w.enable = false
}
} else {
logrus.Error("Can not disable the watcher: Not found service: ", serviceName)
}
}
func (w *watcher) GetServiceName() string {
return w.serviceName
}
func (w *watcher) GetID() string {
return w.id
}
func (w *watcher) Watch() <-chan *v1.HealthStatus {
return w.statusChan
}
func (w *watcher) Close() error {
return w.manager.CloseWatch(w.serviceName, w.id)
}
func (p *probeManager) updateAllServicesProbe() {
if p.services == nil || len(p.services) == 0 {
return
}
for _, pro := range p.serviceProbe {
pro.Stop()
}
p.serviceProbe = make(map[string]probe.Probe, len(p.services))
for _, v := range p.services {
if v.ServiceHealth == nil {
continue
}
if v.Disable {
continue
}
serviceProbe := probe.CreateProbe(p.ctx, p.statusChan, v)
if serviceProbe != nil {
p.serviceProbe[v.Name] = serviceProbe
serviceProbe.Check()
}
}
}
// UpdateServiceProbe updates and runs one service probe.
func (p *probeManager) UpdateServiceProbe(service *v1.Service) {
if service.ServiceHealth == nil || service.Disable || !p.isNeedUpdate(service) {
return
}
logrus.Debugf("Probe: %s; update probe.", service.Name)
// stop old probe
old := p.serviceProbe[service.Name]
if old != nil {
old.Stop()
}
// create new probe
serviceProbe := probe.CreateProbe(p.ctx, p.statusChan, service)
if serviceProbe != nil {
p.serviceProbe[service.Name] = serviceProbe
serviceProbe.Check()
}
}
// UpdateServicesProbe updates and runs services probe.
func (p *probeManager) UpdateServicesProbe(services []*v1.Service) {
if services == nil || len(services) == 0 {
return
}
oldSvcs := p.ListServicesBySid(services[0].Sid)
for _, v := range services {
delete(oldSvcs, v.Name)
if v.ServiceHealth == nil || !p.isNeedUpdate(v) {
continue
}
if v.Disable {
p.StopProbes([]string{v.Name})
continue
}
logrus.Debugf("Probe: %s; update probe.", v.Name)
// stop old probe
old := p.serviceProbe[v.Name]
if old != nil {
old.Stop()
}
if !p.CheckIfExist(v) {
p.services = append(p.services, v)
}
// create new probe
serviceProbe := probe.CreateProbe(p.ctx, p.statusChan, v)
if serviceProbe != nil {
p.serviceProbe[v.Name] = serviceProbe
serviceProbe.Check()
}
}
for _, svc := range oldSvcs {
p.StopProbes([]string{svc.Name})
}
logrus.Debugf("current have probe of third apps number is %d", len(p.serviceProbe))
}
// ListServicesBySid lists services corresponding to sid.
func (p *probeManager) ListServicesBySid(sid string) map[string]*v1.Service {
res := make(map[string]*v1.Service)
for _, svc := range p.services {
if svc.Sid == sid {
res[svc.Name] = svc
}
}
return res
}
// GetProbe returns a probe associated with name.
func (p *probeManager) GetProbe(name string) probe.Probe {
return p.serviceProbe[name]
}
func (p *probeManager) StopProbes(names []string) {
for _, name := range names {
probe := p.serviceProbe[name]
if probe == nil {
continue
}
probe.Stop()
delete(p.serviceProbe, name)
for idx, svc := range p.services {
if svc.Name == name {
p.services = append(p.services[:idx], p.services[idx+1:]...)
}
}
logrus.Debugf("Name: %s; Probe stopped", name)
}
}