forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc.go
367 lines (321 loc) · 9.14 KB
/
proc.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
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 proc
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/GoogleCloudPlatform/kubernetes/contrib/mesos/pkg/runtime"
log "github.com/golang/glog"
)
const (
// if the action processor crashes (if some Action panics) then we
// wait this long before spinning up the action processor again.
defaultActionHandlerCrashDelay = 100 * time.Millisecond
// how many actions we can store in the backlog
defaultActionQueueDepth = 1024
// wait this long before re-attempting to enqueue an action into
// the scheduling backlog
defaultMaxRescheduleWait = 5 * time.Millisecond
)
type procImpl struct {
Config
backlog chan Action // action queue
terminate chan struct{} // signaled via close()
wg sync.WaitGroup // End() terminates when the wait is over
done runtime.Signal
state *stateType
pid uint32
writeLock sync.Mutex // avoid data race between write and close of backlog
changed *sync.Cond // wait/signal for backlog changes
engine DoerFunc // isolated this for easier unit testing later on
running chan struct{} // closes once event loop processing starts
dead chan struct{} // closes upon completion of process termination
}
type Config struct {
// cooldown period in between deferred action crashes
actionHandlerCrashDelay time.Duration
// determines the size of the deferred action backlog
actionQueueDepth uint32
// wait this long before re-attempting to enqueue an action into
// the scheduling backlog
maxRescheduleWait time.Duration
}
var (
// default process configuration, used in the creation of all new processes
defaultConfig = Config{
actionHandlerCrashDelay: defaultActionHandlerCrashDelay,
actionQueueDepth: defaultActionQueueDepth,
maxRescheduleWait: defaultMaxRescheduleWait,
}
pid uint32 // global pid counter
closedErrChan <-chan error // singleton chan that's always closed
)
func init() {
ch := make(chan error)
close(ch)
closedErrChan = ch
}
func New() Process {
return newConfigured(defaultConfig)
}
func newConfigured(config Config) Process {
state := stateNew
pi := &procImpl{
Config: config,
backlog: make(chan Action, config.actionQueueDepth),
terminate: make(chan struct{}),
state: &state,
pid: atomic.AddUint32(&pid, 1),
running: make(chan struct{}),
dead: make(chan struct{}),
}
pi.engine = DoerFunc(pi.doLater)
pi.changed = sync.NewCond(&pi.writeLock)
pi.wg.Add(1) // symmetrical to wg.Done() in End()
pi.done = pi.begin()
return pi
}
// returns a chan that closes upon termination of the action processing loop
func (self *procImpl) Done() <-chan struct{} {
return self.done
}
func (self *procImpl) Running() <-chan struct{} {
return self.running
}
func (self *procImpl) begin() runtime.Signal {
if !self.state.transition(stateNew, stateRunning) {
panic(fmt.Errorf("failed to transition from New to Idle state"))
}
defer log.V(2).Infof("started process %d", self.pid)
var entered runtime.Latch
// execute actions on the backlog chan
return runtime.After(func() {
runtime.Until(func() {
if entered.Acquire() {
close(self.running)
self.wg.Add(1)
}
for action := range self.backlog {
select {
case <-self.terminate:
return
default:
// signal to indicate there's room in the backlog now
self.changed.Broadcast()
// rely on Until to handle action panics
action()
}
}
}, self.actionHandlerCrashDelay, self.terminate)
}).Then(func() {
log.V(2).Infof("finished processing action backlog for process %d", self.pid)
if !entered.Acquire() {
self.wg.Done()
}
})
}
// execute some action in the context of the current process. Actions
// executed via this func are to be executed in a concurrency-safe manner:
// no two actions should execute at the same time. invocations of this func
// should not block for very long, unless the action backlog is full or the
// process is terminating.
// returns errProcessTerminated if the process already ended.
func (self *procImpl) doLater(deferredAction Action) (err <-chan error) {
a := Action(func() {
self.wg.Add(1)
defer self.wg.Done()
deferredAction()
})
scheduled := false
self.writeLock.Lock()
defer self.writeLock.Unlock()
var timer *time.Timer
for err == nil && !scheduled {
switch s := self.state.get(); s {
case stateRunning:
select {
case self.backlog <- a:
scheduled = true
default:
if timer == nil {
timer = time.AfterFunc(self.maxRescheduleWait, self.changed.Broadcast)
} else {
timer.Reset(self.maxRescheduleWait)
}
self.changed.Wait()
timer.Stop()
}
case stateTerminal:
err = ErrorChan(errProcessTerminated)
default:
err = ErrorChan(errIllegalState)
}
}
return
}
// implementation of Doer interface, schedules some action to be executed via
// the current execution engine
func (self *procImpl) Do(a Action) <-chan error {
return self.engine(a)
}
// spawn a goroutine that waits for an error. if a non-nil error is read from the
// channel then the handler func is invoked, otherwise (nil error or closed chan)
// the handler is skipped. if a nil handler is specified then it's not invoked.
// the signal chan that's returned closes once the error process logic (and handler,
// if any) has completed.
func OnError(ch <-chan error, f func(error), abort <-chan struct{}) <-chan struct{} {
return runtime.After(func() {
if ch == nil {
return
}
select {
case err, ok := <-ch:
if ok && err != nil && f != nil {
f(err)
}
case <-abort:
if f != nil {
f(errProcessTerminated)
}
}
})
}
func (self *procImpl) OnError(ch <-chan error, f func(error)) <-chan struct{} {
return OnError(ch, f, self.Done())
}
func (self *procImpl) flush() {
log.V(2).Infof("flushing action backlog for process %d", self.pid)
i := 0
//TODO: replace with `for range self.backlog` once Go 1.3 support is dropped
for {
_, open := <-self.backlog
if !open {
break
}
i++
}
log.V(2).Infof("flushed %d backlog actions for process %d", i, self.pid)
}
func (self *procImpl) End() <-chan struct{} {
if self.state.transitionTo(stateTerminal, stateTerminal) {
go func() {
defer close(self.dead)
self.writeLock.Lock()
defer self.writeLock.Unlock()
log.V(2).Infof("terminating process %d", self.pid)
close(self.backlog)
close(self.terminate)
self.wg.Done()
self.changed.Broadcast()
log.V(2).Infof("waiting for deferred actions to complete")
// wait for all pending actions to complete, then flush the backlog
self.wg.Wait()
self.flush()
}()
}
return self.dead
}
type errorOnce struct {
once sync.Once
err chan error
abort <-chan struct{}
}
func NewErrorOnce(abort <-chan struct{}) ErrorOnce {
return &errorOnce{
err: make(chan error, 1),
abort: abort,
}
}
func (b *errorOnce) Err() <-chan error {
return b.err
}
func (b *errorOnce) Reportf(msg string, args ...interface{}) {
b.Report(fmt.Errorf(msg, args...))
}
func (b *errorOnce) Report(err error) {
b.once.Do(func() {
select {
case b.err <- err:
default:
}
})
}
func (b *errorOnce) Send(errIn <-chan error) ErrorOnce {
go b.forward(errIn)
return b
}
func (b *errorOnce) forward(errIn <-chan error) {
if errIn == nil {
b.Report(nil)
return
}
select {
case err, _ := <-errIn:
b.Report(err)
case <-b.abort:
b.Report(errProcessTerminated)
}
}
type processAdapter struct {
Process
delegate Doer
}
func (p *processAdapter) Do(a Action) <-chan error {
errCh := NewErrorOnce(p.Done())
go func() {
errOuter := p.Process.Do(func() {
errInner := p.delegate.Do(a)
errCh.forward(errInner)
})
// if the outer err is !nil then either the parent Process failed to schedule the
// the action, or else it backgrounded the scheduling task.
if errOuter != nil {
errCh.forward(errOuter)
}
}()
return errCh.Err()
}
// DoWith returns a process that, within its execution context, delegates to the specified Doer.
// Expect a panic if either the given Process or Doer are nil.
func DoWith(other Process, d Doer) Process {
if other == nil {
panic(fmt.Sprintf("cannot DoWith a nil process"))
}
if d == nil {
panic(fmt.Sprintf("cannot DoWith a nil doer"))
}
return &processAdapter{
Process: other,
delegate: d,
}
}
func ErrorChanf(msg string, args ...interface{}) <-chan error {
return ErrorChan(fmt.Errorf(msg, args...))
}
func ErrorChan(err error) <-chan error {
if err == nil {
return closedErrChan
}
ch := make(chan error, 1)
ch <- err
return ch
}
// invoke the f on action a. returns an illegal state error if f is nil.
func (f DoerFunc) Do(a Action) <-chan error {
if f != nil {
return f(a)
}
return ErrorChan(errIllegalState)
}