forked from gdamore/govisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
830 lines (764 loc) · 19.7 KB
/
service.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
// Copyright 2015 The Govisor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use 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 govisor
import (
"log"
"strings"
"time"
)
// Service describes a generic system service -- such as a process, or
// group of processes. Applications are expected to use the Service
// structure to interact with all managed services.
//
// Implementors can provide custom services (which may be any kind of entity)
// by implementing the Provider interface.
//
// Service methods are not thread safe, until the service is added to a
// Manager. Once the service is added to a Manager, the Manager's lock
// will protect concurrent accesses.
//
// Services go through a number of possible states as illustrated in the
// following state diagram. Note that these states are logical, as there is
// no formal state machine in the code. This diagram is for illustration
// purposes only.
//
// +------------+
// | |
// +---------> Disabled <-------+
// | | | |
// | +----+--A----+ |
// | | | |
// +-----+----+ +----V--+---+ |
// | | | | |
// | Failed +----> DepWait <----+ |
// | | | | | |
// +-----A--A-+ +----+------+ | |
// | | | | |
// | | +----v-------+ | |
// | | | | | |
// | +------+ Starting | | |
// | | | | |
// | +----+-------+ | |
// | | | |
// | +---V---+ | |
// | | | | |
// +----------+ Run +-------+---+
// | |
// +-------+
//
type Service struct {
prov Provider
mgr *Manager
name string
desc string
depends []string
conflicts []string
provides []string
enabled bool
running bool
stopping bool
failed bool
restart bool
checking bool
healthy bool
err error
parents map[string]map[*Service]bool
children map[*Service]bool
incompat map[*Service]bool
logger *log.Logger
stamp time.Time
reason string
starts int
rateLog bool
rateLimit int
ratePeriod time.Duration
startTimes []time.Time
notify func()
slog *Log
mlog *MultiLogger
serial int64
}
// The service name. This takes either the form <base> or <base>:<variant>.
// Except for the colon used to separate the <base> from <variant>, no
// punctuation characters other than underscores are permitted. When
// attempting to resolve dependencies, a dependency can list either the
// full <base>:<variant> name or just <base>. In the former case, the
// full service name must match. In the latter case, any service with
// the same <base> component matches.
func (s *Service) Name() string {
return s.name
}
// Description returns a descriptive name for the service. If possible,
// user interfaces should try to allocate at least 32 characters of horizontal
// space when displaying descriptions.
func (s *Service) Description() string {
return s.desc
}
// Provides is a way to indicate other service names that this service
// offers. This permits a service instance to support multiple real
// capabilities, or to provide multiple aliases. For example, a daemon
// might offer "http" and "ftp" both.
func (s *Service) Provides() []string {
return s.provides
}
// Depends returns a list of service names. See the Name description
// for how these are used.
func (s *Service) Depends() []string {
return s.depends
}
func (s *Service) Serial() int64 {
var rv int64
if m := s.mgr; m != nil {
m.lock()
rv = s.serial
m.unlock()
}
return rv
}
// Status returns the most reason status message, and the time when the
// status was recorded.
func (s *Service) Status() (string, time.Time) {
if m := s.mgr; m != nil {
m.lock()
defer m.unlock()
}
return s.reason, s.stamp
}
func (s *Service) WatchService(old int64, expire time.Duration) int64 {
if m := s.mgr; m != nil {
return m.watchSerial(old, &s.serial, expire)
}
// This isn't perfect, as it won't wake up when if a manager is
// added later. But really, nobody ought to be calling this unless
// the service is added to a manager.
if old == s.serial {
time.Sleep(expire)
}
return s.serial
}
func (s *Service) WatchLog(old int64, expire time.Duration) int64 {
return s.slog.Watch(old, expire)
}
// Conflicts returns a list of strings or service names that
// cannot be enabled with this one. The system will make sure that
// attempts to enable the service are rejected. Note that the scope
// of conflict is limited to a single Manager; that is the check will
// not prevent two conflicting services running under the control of
// different Managers.
func (s *Service) Conflicts() []string {
return s.conflicts
}
// Enabled checks if a service is enabled.
func (s *Service) Enabled() bool {
if m := s.mgr; m == nil {
return false
} else {
m.lock()
rv := s.enabled
m.unlock()
return rv
}
}
// Running checks if a service is running. This will be false if the
// service has failed for any reason, or is unable to run due to a missing
// dependency.
func (s *Service) Running() bool {
if m := s.mgr; m == nil {
return false
} else {
m.lock()
rv := s.running && !s.stopping
m.unlock()
return rv
}
}
// Failed returns true if the service is in a failure state.
func (s *Service) Failed() bool {
if m := s.mgr; m == nil {
return false
} else {
m.lock()
rv := s.failed
m.unlock()
return rv
}
}
// Enable enables the service. This will also start any services that may
// have not been running due to unsatisfied dependencies, but which now
// are able to (and were otherwise enabled.)
func (s *Service) Enable() error {
if s.mgr == nil {
return ErrNoManager
}
s.mgr.lock()
defer s.mgr.unlock()
if s.enabled {
return nil
}
for c := range s.incompat {
if c.enabled {
s.logf("Cannot enable %s: conflicts with %s",
s.Name(), c.Name())
s.reason = "Disabled due to conflict"
s.serial = s.mgr.bumpSerial()
s.stamp = time.Now()
return ErrConflict
}
}
s.serial = s.mgr.bumpSerial()
s.reason = "Enabled"
s.stamp = time.Now()
s.logf("Enabling service %s", s.Name())
s.enabled = true
s.starts = 0
s.startRecurse("Enabled service")
return nil
}
// Disable disables the service, stopping it. It also will stop any services
// which will no longer have satisfied dependencies as a result of this
// service being disabled. It also clears the error state.
func (s *Service) Disable() error {
if s.mgr == nil {
return ErrNoManager
}
s.mgr.lock()
defer s.mgr.unlock()
if !s.enabled && s.reason == "Disabled" {
return nil
}
s.serial = s.mgr.bumpSerial()
s.logf("Disabling service %s", s.Name())
s.stamp = time.Now()
s.reason = "Disabled"
s.enabled = false
s.failed = false
s.err = nil
s.stopRecurse("Disabled")
return nil
}
// Restart restarts a service. It also clears any failure condition
// that may have occurred.
func (s *Service) Restart() error {
if s.mgr == nil {
return ErrNoManager
}
s.mgr.lock()
defer s.mgr.unlock()
if !s.enabled {
return nil
}
s.serial = s.mgr.bumpSerial()
s.logf("Restarting service %s", s.Name())
s.enabled = false
s.stopRecurse("Stopping for restart")
s.stamp = time.Now()
s.reason = "Restarting"
s.starts = 0
s.failed = false
s.err = nil
s.enabled = true
s.startRecurse("Restarting")
return nil
}
// Clear clears any error condition in the service, without actually
// enabling it. It will attempt to start the service if it isn't
// already running, and is enabled.
func (s *Service) Clear() {
if s.mgr == nil {
return
}
s.mgr.lock()
defer s.mgr.unlock()
s.serial = s.mgr.bumpSerial()
if s.failed {
s.reason = "Cleared fault"
s.stamp = time.Now()
s.logf("Clearing fault on %s", s.Name())
}
s.starts = 0
s.failed = false
s.err = nil
s.startRecurse("Cleared fault")
}
// Check checks if a service is running, and performs any appropriate health
// checks. It returns nil if the service is running and healthy, or false
// otherwise. If it returns false, it will stop the service, as well as
// dependent services, and put the service into failed state.
func (s *Service) Check() error {
if s.mgr == nil {
return ErrNoManager
}
return s.checkService()
}
// matchServiceNames matches if the first (concrete) name matches
// the second. This is true if either the variant of s1 is empty,
// or the two variants collide.
func serviceMatches(s1, s2 string) bool {
a1 := strings.SplitN(s1, ":", 2)
a2 := strings.SplitN(s2, ":", 2)
if a1[0] != a2[0] {
return false
}
if len(a1) == 1 {
return true
}
if len(a2) == 1 {
return false
}
return a1[1] == a2[1]
}
// Matches returns true if the service name matches the check. This is
// true if either the check is a complete match, or if the first part of
// our name (or Provides) is identical to the check. For example, if our
// name is "x:y", then this would return true for a check of "x", or "x:y",
// but not for "x:z", nor "z:y".
func (s *Service) Matches(check string) bool {
if serviceMatches(check, s.Name()) {
return true
}
for _, p := range s.Provides() {
if serviceMatches(check, p) {
return true
}
}
return false
}
// SetProperty sets a property on the service.
func (s *Service) SetProperty(n PropertyName, v interface{}) error {
if m := s.mgr; m != nil {
m.lock()
defer m.unlock()
}
if e := s.setProp(n, v); e != nil {
s.logf("Failed to set property %s: %v", s.Name(), e)
return e
}
return nil
}
func (s *Service) setProp(n PropertyName, v interface{}) error {
// Lock it if we are already added. Some properties cannot be
// set once a the service is added.
if m := s.mgr; m != nil {
switch n {
case PropName,
PropDescription,
PropConflicts,
PropDepends,
PropProvides:
// These properties cannot be altered once they are
// added to a service.
return ErrPropReadOnly
}
// We might fail, but better to bump serial number than to
// not bump it when we should have
s.serial = m.bumpSerial()
}
switch n {
case PropLogger:
if v, ok := v.(*log.Logger); ok {
if s.enabled {
// Cannot change logger while service enabled.
return ErrPropReadOnly
}
if s.logger != nil {
s.mlog.DelLogger(s.logger)
}
s.logger = v
s.mlog.AddLogger(s.logger)
} else {
return ErrBadPropType
}
case PropRestart:
if v, ok := v.(bool); ok {
s.restart = v
} else {
return ErrBadPropType
}
case PropRateLimit:
if v, ok := v.(int); ok {
s.starts = 0
if v > 0 {
s.startTimes = make([]time.Time, v)
} else {
s.startTimes = nil
}
s.rateLimit = v
} else {
return ErrBadPropType
}
case PropRatePeriod:
if v, ok := v.(time.Duration); ok {
s.starts = 0
s.ratePeriod = v
} else {
return ErrBadPropType
}
case PropName:
if v, ok := v.(string); ok {
s.name = v
} else {
return ErrBadPropType
}
case PropDescription:
if v, ok := v.(string); ok {
s.desc = v
} else {
return ErrBadPropType
}
case PropConflicts:
if v, ok := v.([]string); ok {
s.conflicts = append([]string{}, v...)
} else {
return ErrBadPropType
}
case PropDepends:
if v, ok := v.([]string); ok {
s.depends = append([]string{}, v...)
} else {
return ErrBadPropType
}
case PropProvides:
if v, ok := v.([]string); ok {
s.provides = append([]string{}, v...)
} else {
return ErrBadPropType
}
case PropNotify:
if v, ok := v.(func()); ok {
s.notify = v
// We don't want to pass this one down, as we've
// registered ourselves there.
return nil
} else {
return ErrBadPropType
}
default:
return s.prov.SetProperty(n, v)
}
// Pass the new property to the provider. The provider doesn't get a
// a chance to veto properties we've already dealt with though.
s.prov.SetProperty(n, v)
return nil
}
func (s *Service) GetProperty(n PropertyName) (interface{}, error) {
if m := s.mgr; m != nil {
m.lock()
defer m.unlock()
}
switch n {
case PropLogger:
return s.logger, nil
case PropRestart:
return s.restart, nil
case PropRateLimit:
return s.rateLimit, nil
case PropRatePeriod:
return s.ratePeriod, nil
case PropName:
return s.name, nil
case PropDescription:
return s.desc, nil
case PropConflicts:
return append([]string{}, s.conflicts...), nil
case PropDepends:
return append([]string{}, s.depends...), nil
case PropProvides:
return append([]string{}, s.provides...), nil
case PropNotify:
return s.notify, nil
}
return s.prov.Property(n)
}
func (s *Service) GetLog(lastid int64) ([]LogRecord, int64) {
if m := s.mgr; m != nil {
m.lock()
defer m.unlock()
}
return s.slog.GetRecords(lastid)
}
// setManager is called by the framework when the service is added to
// the manager. This calculates the various dependency graphs, updating
// links to other services in the manager.
func (s *Service) setManager(mgr *Manager) {
if s.mgr != nil {
// This is a serious programmer mistake
panic("Already added to a manager")
}
s.mlog.AddLogger(mgr.getLogger(s))
s.mgr = mgr
s.incompat = make(map[*Service]bool)
s.children = make(map[*Service]bool)
s.parents = make(map[string]map[*Service]bool)
for _, d := range s.Depends() {
s.parents[d] = make(map[*Service]bool)
}
for t := range mgr.services {
// do we satisfy a dependency of t?
for _, d := range t.Depends() {
if s.Matches(d) {
t.parents[d][s] = true
s.children[t] = true
break
}
}
// does t satisfy a dependency of s?
for _, d := range s.Depends() {
if t.Matches(d) {
s.parents[d][t] = true
t.children[s] = true
break
}
}
// do we conflict with t?
for _, c := range t.Conflicts() {
if s.Matches(c) {
s.incompat[t] = true
t.incompat[s] = true
}
}
for _, c := range s.Conflicts() {
if t.Matches(c) {
s.incompat[t] = true
t.incompat[s] = true
}
}
}
s.stamp = time.Now()
s.reason = "Added service"
s.logf("Added service %s to %s: %s", s.Name(), mgr.Name(),
s.Description())
mgr.services[s] = true
}
func (s *Service) delManager() {
if s.mgr == nil {
return
}
// remove the item
delete(s.mgr.services, s)
// remove from each of our conflicts
for c := range s.incompat {
delete(c.incompat, s)
delete(s.incompat, c)
}
// our children (things that may depend upon us)
for c := range s.children {
for p := range c.parents {
delete(c.parents[p], s)
}
delete(s.children, c)
}
// our parents (this we depend upon)
for d, p := range s.parents {
for t := range p {
delete(p, t)
delete(t.children, s)
}
delete(s.parents, d)
}
s.reason = "Removed service"
s.stamp = time.Now()
s.mgr = nil
}
func (s *Service) logf(fmt string, v ...interface{}) {
s.mlog.Logger().Printf(fmt, v...)
}
func (s *Service) startRecurse(detail string) {
if s.running {
return
}
if !s.canRun() {
return
}
if e := s.tooQuickly(); e != nil {
return
}
if s.rateLimit > 0 {
s.startTimes[s.starts%s.rateLimit] = time.Now()
}
s.starts++
s.serial = s.mgr.bumpSerial()
if e := s.prov.Start(); e != nil {
s.logf("Failed to start %s: %v", s.Name(), e)
s.reason = "Failed start:" + e.Error()
s.stamp = time.Now()
s.err = e
s.failed = true
return
}
s.reason = "Started"
s.stamp = time.Now()
s.logf("Started %s: %s", s.Name(), detail)
s.running = true
s.failed = false
for child := range s.children {
child.startRecurse("Dependency running")
}
}
func (s *Service) stopRecurse(detail string) {
if !s.running || s.stopping {
return
}
s.stopping = true
for child := range s.children {
if child.canRun() {
continue
}
child.stopRecurse("Unmet dependency")
}
s.serial = s.mgr.bumpSerial()
s.prov.Stop()
s.reason = detail
s.stamp = time.Now()
s.logf("Stopped %s: %s", s.Name(), detail)
s.running = false
s.stopping = false
}
func (s *Service) canRun() bool {
if s.stopping || !s.enabled {
return false
}
for _, deps := range s.parents {
sat := false
for d := range deps {
if d.enabled && d.running && !d.stopping && !d.failed {
sat = true
break
}
}
if !sat {
if s.reason != "Unmet dependency" {
s.reason = "Unmet dependency"
s.serial = s.mgr.bumpSerial()
s.stamp = time.Now()
}
return false
}
}
for c := range s.incompat {
if c.enabled {
return false
}
}
return true
}
func (s *Service) checkService() error {
if s.failed {
return s.err
}
if !s.running {
return ErrNotRunning
}
s.checking = true
if e := s.prov.Check(); e != nil {
s.serial = s.mgr.bumpSerial()
s.logf("Service %s faulted: %v", s.Name(), e)
s.failed = true
s.stopRecurse("Faulted: " + e.Error())
s.err = e
s.checking = false
return e
}
if s.reason != "Healthy" {
s.serial = s.mgr.bumpSerial()
s.reason = "Healthy"
s.logf("Service healthy")
s.stamp = time.Now()
}
s.checking = false
return nil
}
// A service is restarting too quickly if it restarts more than a specified
// number of times in an interval. Once we hit that threshold, we wait for
// a full interval count before we will restart. Effectively, this means
// that if we hit the threshold, we actually won't restart for *another*
// interval, reducing our rate to 1/2 the configured rate, punishing us for
// bad behavior.
func (s *Service) tooQuickly() error {
if s.rateLimit == 0 {
return nil
}
if s.starts < s.rateLimit {
return nil
}
// If we've restarted more than n times in the last period,
// then rate limit us.
idx := (s.starts - 1) % s.rateLimit
end := s.startTimes[idx]
if time.Now().Before(end.Add(s.ratePeriod)) {
// Log it if not already done.
if !s.rateLog {
s.logf("Service %s restarting too quickly", s.Name())
}
// And we uncoditionally mark this to note cool down.
s.rateLog = true
return ErrRateLimited
}
// If we haven't restarted recently too quickly, we're done.
if !s.rateLog {
// Not in cool down mode.
return nil
}
// Check to see if cool down from prior rate limit is expired.
idx = (s.starts - 2) % s.rateLimit
end = s.startTimes[idx]
if time.Now().Before(end.Add(s.ratePeriod)) {
return ErrRateLimited
}
// All cool downs expired.
s.rateLog = false
return nil
}
func (s *Service) selfHeal() {
if s.failed && s.restart {
s.logf("Attempting self-healing")
s.startRecurse("Self-healing attempt")
}
}
func (s *Service) doNotify() {
go func() {
var cb func()
if m := s.mgr; m != nil {
m.lock()
m.notify(s)
cb = s.notify
m.unlock()
} else {
cb = s.notify
}
if cb != nil {
go cb()
}
}()
}
// NewService allocates a service instance from a Provider. The intention
// is that Providers use this in their own constructors to present only a
// Service interface to applications.
func NewService(p Provider) *Service {
s := &Service{prov: p}
s.ratePeriod = time.Minute
s.rateLimit = 10
s.startTimes = make([]time.Time, s.rateLimit)
s.name = p.Name()
s.desc = p.Description()
s.conflicts = append([]string{}, p.Conflicts()...)
s.depends = append([]string{}, p.Depends()...)
s.provides = append([]string{}, p.Provides()...)
s.mlog = NewMultiLogger()
s.mlog.Logger().SetPrefix("[" + s.Name() + "] ")
s.prov.SetProperty(PropLogger, s.mlog.Logger())
s.slog = NewLog()
s.mlog.AddLogger(log.New(s.slog, "", 0))
p.SetProperty(PropNotify, s.doNotify)
return s
}