-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmanager.go
380 lines (346 loc) · 8.9 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
// Copyright 2016 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 (
"io"
"log"
"os"
"runtime"
"sync"
"time"
)
type Manager struct {
services map[*Service]bool
name string
baseDir string
logger *log.Logger
mylog *log.Logger
log *Log
mlog *MultiLogger
writer io.Writer
cleanup bool
monitoring bool
serial int64
listSerial int64
listStamp time.Time
createTime time.Time
updateTime time.Time
mx sync.Mutex
cvs map[*sync.Cond]bool
}
type ManagerInfo struct {
Name string
Serial int64
UpdateTime time.Time
CreateTime time.Time
}
func (m *Manager) lock() {
m.mx.Lock()
}
func (m *Manager) unlock() {
m.mx.Unlock()
}
func (m *Manager) wakeUp() {
// NB: If the lock is not held here, then there is a risk
// that the woken goroutines won't get see the updated
// serial number!!
for cv := range m.cvs {
cv.Broadcast()
}
}
// bumpSerial increments the serial and notifies watchers. It returns
// the new serial number, so that it can be stored in services.
// Call with lock held.
func (m *Manager) bumpSerial() int64 {
m.updateTime = time.Now()
m.serial++
rv := m.serial
m.wakeUp()
return rv
}
// watchSerial monitors for a change in a specific serial number. It returns
// the new serial number when it changes. If the serial number has not
// changed in the given duration then the old value is returned. A poll
// can be done by supplying 0 for the expiration.
func (m *Manager) watchSerial(old int64, src *int64, expire time.Duration) int64 {
expired := false
cv := sync.NewCond(&m.mx)
var timer *time.Timer
var rv int64
// Schedule timeout
if expire > 0 {
timer = time.AfterFunc(expire, func() {
m.lock()
expired = true
cv.Broadcast()
m.unlock()
})
} else {
expired = true
}
m.lock()
m.cvs[cv] = true
for {
rv = *src
if rv != old || expired {
break
}
cv.Wait()
}
delete(m.cvs, cv)
m.unlock()
if timer != nil {
timer.Stop()
}
return rv
}
// WatchSerial monitors for a change in the global serial number.
func (m *Manager) WatchSerial(old int64, expire time.Duration) int64 {
return m.watchSerial(old, &m.serial, expire)
}
// WatchServices monitors for a change in the list of services.
func (m *Manager) WatchServices(old int64, expire time.Duration) int64 {
return m.watchSerial(old, &m.listSerial, expire)
}
// Serial returns the global serial number. This is incremented
// anytime a service has a state change.
func (m *Manager) Serial() int64 {
m.lock()
rv := m.serial
m.unlock()
return rv
}
// Name returns the name the manager was allocated with. This makes it
// possible to distinguish between separate manager instances. This name
// can influence logged messages and file/directory names.
func (m *Manager) Name() string {
return m.name
}
// GetInfo returns top-level information about the Manager. This is done
// in a manner that ensures that the info is consistent.
func (m *Manager) GetInfo() *ManagerInfo {
m.lock()
i := &ManagerInfo{
Name: m.name,
Serial: m.serial,
CreateTime: m.createTime,
UpdateTime: m.updateTime,
}
m.unlock()
return i
}
// AddService adds a service, registering it, to the manager.
func (m *Manager) AddService(s *Service) error {
m.lock()
for s2 := range m.services {
if s.Name() == s2.Name() {
m.unlock()
m.logf("[%s] Failed to add service [%s]: %v",
m.Name(), s.Name(), ErrNameExists)
return ErrNameExists
}
}
s.setManager(m)
m.listSerial = m.bumpSerial()
s.serial = m.bumpSerial()
m.listStamp = time.Now()
m.logf("[%s] Added service [%s]: %s", m.Name(), s.Name(),
s.Description())
m.unlock()
return nil
}
// DeleteService deletes a service from the manager.
func (m *Manager) DeleteService(s *Service) error {
m.lock()
if s.enabled {
m.unlock()
return ErrIsEnabled
}
s.delManager()
m.logf("[%s] Deleted service [%s]", m.Name(), s.Name())
m.listSerial = m.bumpSerial()
s.serial = m.bumpSerial()
m.listStamp = time.Now()
m.unlock()
return nil
}
// Services returns all of our services. Note that the order is
// arbitrary. (At present it happens to be done based on order of
// addition.)
func (m *Manager) Services() ([]*Service, int64, time.Time) {
m.lock()
rv := make([]*Service, 0, len(m.services))
for s := range m.services {
rv = append(rv, s)
}
ts := m.listStamp
sn := m.listSerial
m.unlock()
return rv, sn, ts
}
// FindServices finds the list of services that have either a matching
// Name, or Provides. That is, they find all of our services, where the
// service.Match() would return true for the string match.
func (m *Manager) FindServices(match string) []*Service {
rv := []*Service{}
m.lock()
for s := range m.services {
if s.Matches(match) {
rv = append(rv, s)
}
}
m.unlock()
return rv
}
func (m *Manager) setBaseDir() {
m.baseDir = os.Getenv("GOVISORDIR")
switch runtime.GOOS {
case "nacl", "plan9":
m.baseDir = ""
case "windows":
if len(m.baseDir) == 0 {
m.baseDir = os.Getenv("HOME")
}
if len(m.baseDir) == 0 {
m.baseDir = "C:\\"
}
default:
if len(m.baseDir) == 0 {
if os.Geteuid() == 0 {
m.baseDir = "/var"
} else {
m.baseDir = os.Getenv("HOME")
}
}
if len(m.baseDir) == 0 {
m.baseDir = "."
}
}
}
// SetLogger is used to establish a logger. It overrides the default, so it
// shouldn't be used unless you want to control all logging.
func (m *Manager) SetLogger(l *log.Logger) {
if m.logger != nil {
m.mlog.DelLogger(m.logger)
}
m.logger = l
m.mlog.AddLogger(l)
}
func (m *Manager) getLogger(s *Service) *log.Logger {
return log.New(m.mlog, "", 0)
}
func (m *Manager) monitor() {
finish := false
for !finish {
m.lock()
if m.monitoring {
for s := range m.services {
if s.enabled {
if e := s.checkService(); e != nil {
s.selfHeal()
}
}
}
}
if m.cleanup {
m.monitoring = false
finish = true
}
m.unlock()
// a "prime" number of milliseconds, to ensure a more
// or less even distribution of clock events
time.Sleep(time.Millisecond * 587)
}
}
// notify is called asynchronously by services, when they detect a failure.
// It MUST NOT be called by the service as part of a synchronous call to
// the check routine. We do add a check to prevent infinite recursion, but
// again, the caller should be careful not to do this. Notification should
// only be done when a service transitions from succeeding to failing, or vice
// versa.
func (m *Manager) notify(s *Service) {
if s.checking {
// No need for notification, and lets not recurse!
return
}
if s.enabled {
if e := s.checkService(); e != nil {
s.selfHeal()
}
}
}
func (m *Manager) logf(format string, v ...interface{}) {
if m.mylog != nil {
m.mylog.Printf(format, v...)
} else {
log.Printf(format, v...)
}
}
func (m *Manager) StopMonitoring() {
m.lock()
m.monitoring = false
m.unlock()
m.logf("*** Govisor stopping monitoring: %s ***", m.name)
}
func (m *Manager) StartMonitoring() {
m.logf("*** Govisor starting monitoring: %s ***", m.name)
m.lock()
m.monitoring = true
m.unlock()
}
// Shutdown stops all services, and stops monitoring too. Finally, it removes
// them all from the manager. Think of this as effectively tearing down the
// entire thing.
func (m *Manager) Shutdown() {
m.lock()
m.monitoring = false
for s := range m.services {
s.enabled = false
s.stopRecurse("Shutting down")
s.delManager()
}
m.unlock()
m.logf("*** Govisor shut down: %s ***", m.name)
}
func (m *Manager) GetLog(lastid int64) ([]LogRecord, int64) {
m.lock()
defer m.unlock()
return m.log.GetRecords(lastid)
}
func (m *Manager) WatchLog(old int64, expire time.Duration) int64 {
return m.log.Watch(old, expire)
}
func NewManager(name string) *Manager {
if name == "" {
name = "govisor"
}
// We set the origin serial number to the current timestamp in nsec.
// The assumption here is that we won't have changes to serial number
// occur at frequency > 1GHz. Hence, it should be safe for us to use
// these as unique values, and this may help clients that cache force
// an invalidation if the server for some reason restarts.
m := &Manager{name: name, serial: time.Now().UnixNano()}
m.services = make(map[*Service]bool)
m.cvs = make(map[*sync.Cond]bool)
m.createTime = time.Now()
m.updateTime = m.createTime
m.mlog = NewMultiLogger()
m.log = NewLog()
m.mlog.AddLogger(log.New(m.log, "", 0))
m.logger = log.New(os.Stderr, "", 0)
m.mylog = m.getLogger(nil)
m.setBaseDir()
go m.monitor()
return m
}