Commit a5fa5673 authored by James DeFelice's avatar James DeFelice

rewrite Process implementation from scratch, unskip previously failing test:

- fix races in unit tests - fix flaky singleActionEndsProcess test; further simplify Process impl - fix flakey error handling in processAdapter - eliminate time-based test assertions
parent a078eba5
/*
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"
)
type processAdapter struct {
Process
delegate Doer
}
// reportAnyError waits for an error to arrive from source, or for the process to end;
// errors are reported through errOnce. returns true if an error is reported through
// errOnce, otherwise false.
func (p *processAdapter) reportAnyError(source <-chan error, errOnce ErrorOnce) bool {
select {
case err, ok := <-source:
if ok && err != nil {
// failed to schedule/execute the action
errOnce.Report(err)
return true
}
// action was scheduled/executed just fine.
case <-p.Done():
// double-check that there's no errror waiting for us in source
select {
case err, ok := <-source:
if ok {
// parent failed to schedule/execute the action
errOnce.Report(err)
return true
}
default:
}
errOnce.Report(errProcessTerminated)
return true
}
return false
}
func (p *processAdapter) Do(a Action) <-chan error {
errCh := NewErrorOnce(p.Done())
go func() {
ch := NewErrorOnce(p.Done())
errOuter := p.Process.Do(func() {
errInner := p.delegate.Do(a)
ch.forward(errInner)
})
// order is important here: check errOuter before ch
if p.reportAnyError(errOuter, errCh) {
return
}
if !p.reportAnyError(ch.Err(), errCh) {
errCh.Report(nil)
}
}()
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,
}
}
......@@ -18,13 +18,24 @@ package proc
import (
"errors"
"fmt"
"k8s.io/kubernetes/contrib/mesos/pkg/runtime"
)
var (
errProcessTerminated = errors.New("cannot execute action because process has terminated")
errIllegalState = errors.New("illegal state, cannot execute action")
closedErrChan <-chan error // singleton chan that's always closed
)
func init() {
ch := make(chan error)
close(ch)
closedErrChan = ch
}
func IsProcessTerminated(err error) bool {
return err == errProcessTerminated
}
......@@ -32,3 +43,43 @@ func IsProcessTerminated(err error) bool {
func IsIllegalState(err error) bool {
return err == errIllegalState
}
// OnError spawns 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)
}
}
})
}
// ErrorChanf is a convenience func that returns a chan that yields an error
// generated from the given msg format and args.
func ErrorChanf(msg string, args ...interface{}) <-chan error {
return ErrorChan(fmt.Errorf(msg, args...))
}
// ErrorChan is a convenience func that returns a chan that yields the given error.
// If err is nil then a closed chan is returned.
func ErrorChan(err error) <-chan error {
if err == nil {
return closedErrChan
}
ch := make(chan error, 1)
ch <- err
return ch
}
/*
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"
"time"
)
type errorOnce struct {
once sync.Once
err chan error
abort <-chan struct{}
}
// NewErrorOnce creates an ErrorOnce that aborts blocking func calls once
// the given abort chan has closed.
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:
// double-check that errIn was blocked: don't falsely return
// errProcessTerminated if errIn was really ready
select {
case err, _ := <-errIn:
b.Report(err)
default:
b.Report(errProcessTerminated)
}
}
}
func (b *errorOnce) WaitFor(d time.Duration) (error, bool) {
t := time.NewTimer(d)
select {
case err, _ := <-b.err:
t.Stop()
return err, true
case <-t.C:
return nil, false
}
}
......@@ -16,7 +16,11 @@ limitations under the License.
package proc
// something that executes in the context of a process
import (
"time"
)
// Action is something that executes in the context of a process
type Action func()
type Context interface {
......@@ -36,9 +40,17 @@ type Doer interface {
Do(Action) <-chan error
}
// adapter func for Doer interface
// DoerFunc is an adapter func for Doer interface
type DoerFunc func(Action) <-chan error
// 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)
}
type Process interface {
Context
Doer
......@@ -71,4 +83,7 @@ type ErrorOnce interface {
// Send is non-blocking; it spins up a goroutine that reports an error (if any) that occurs on the error chan.
Send(<-chan error) ErrorOnce
// WaitFor returns true if an error is received within the specified duration, otherwise false
WaitFor(time.Duration) (error, bool)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment