mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
Stopping & Termination:
+ Modify handleFunc signature to return an error + if an error is returned, terminate the routine + finished channel will then return a termination reason
This commit is contained in:
parent
2b52b3cb3f
commit
9d84994d33
@ -6,7 +6,7 @@ type demuxer struct {
|
|||||||
eventbus chan Event
|
eventbus chan Event
|
||||||
scheduler *Routine
|
scheduler *Routine
|
||||||
processor *Routine
|
processor *Routine
|
||||||
finished chan struct{}
|
finished chan error
|
||||||
stopped chan struct{}
|
stopped chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,10 +16,12 @@ func newDemuxer(scheduler *Routine, processor *Routine) *demuxer {
|
|||||||
scheduler: scheduler,
|
scheduler: scheduler,
|
||||||
processor: processor,
|
processor: processor,
|
||||||
stopped: make(chan struct{}, 1),
|
stopped: make(chan struct{}, 1),
|
||||||
finished: make(chan struct{}, 1),
|
finished: make(chan error, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// What should the termination clause be?
|
||||||
|
// Is any of the subroutines finishe, the demuxer finishes
|
||||||
func (dm *demuxer) run() {
|
func (dm *demuxer) run() {
|
||||||
fmt.Printf("demuxer: run\n")
|
fmt.Printf("demuxer: run\n")
|
||||||
for {
|
for {
|
||||||
@ -30,7 +32,11 @@ func (dm *demuxer) run() {
|
|||||||
dm.stopped <- struct{}{}
|
dm.stopped <- struct{}{}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
oEvents := dm.handle(event)
|
oEvents, err := dm.handle(event)
|
||||||
|
if err != nil {
|
||||||
|
// TODO Termination time
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, event := range oEvents {
|
for _, event := range oEvents {
|
||||||
dm.eventbus <- event
|
dm.eventbus <- event
|
||||||
}
|
}
|
||||||
@ -38,9 +44,12 @@ func (dm *demuxer) run() {
|
|||||||
if !ok {
|
if !ok {
|
||||||
fmt.Printf("demuxer: scheduler output closed\n")
|
fmt.Printf("demuxer: scheduler output closed\n")
|
||||||
continue
|
continue
|
||||||
// todo: close?
|
|
||||||
}
|
}
|
||||||
oEvents := dm.handle(event)
|
oEvents, err := dm.handle(event)
|
||||||
|
if err != nil {
|
||||||
|
// TODO tTermination time
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, event := range oEvents {
|
for _, event := range oEvents {
|
||||||
dm.eventbus <- event
|
dm.eventbus <- event
|
||||||
}
|
}
|
||||||
@ -48,39 +57,35 @@ func (dm *demuxer) run() {
|
|||||||
if !ok {
|
if !ok {
|
||||||
fmt.Printf("demuxer: processor output closed\n")
|
fmt.Printf("demuxer: processor output closed\n")
|
||||||
continue
|
continue
|
||||||
// todo: close?
|
|
||||||
}
|
}
|
||||||
oEvents := dm.handle(event)
|
oEvents, err := dm.handle(event)
|
||||||
|
if err != nil {
|
||||||
|
// TODO tTermination time
|
||||||
|
return
|
||||||
|
}
|
||||||
for _, event := range oEvents {
|
for _, event := range oEvents {
|
||||||
dm.eventbus <- event
|
dm.eventbus <- event
|
||||||
}
|
}
|
||||||
|
case err := <-dm.scheduler.finished:
|
||||||
|
dm.finished <- err
|
||||||
|
case err := <-dm.processor.finished:
|
||||||
|
dm.finished <- err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: What is the corerct behaviour here?
|
func (dm *demuxer) handle(event Event) (Events, error) {
|
||||||
// onPcFinish, process no further events
|
received := dm.scheduler.send(event)
|
||||||
// OR onPcFinish, process all queued events and then close
|
if !received {
|
||||||
func (dm *demuxer) handle(event Event) Events {
|
return Events{scFull{}}, nil // backpressure
|
||||||
switch event.(type) {
|
|
||||||
case pcFinished:
|
|
||||||
// dm.stop()
|
|
||||||
fmt.Println("demuxer received pcFinished")
|
|
||||||
dm.finished <- struct{}{}
|
|
||||||
default:
|
|
||||||
received := dm.scheduler.send(event)
|
|
||||||
if !received {
|
|
||||||
return Events{scFull{}} // backpressure
|
|
||||||
}
|
|
||||||
|
|
||||||
received = dm.processor.send(event)
|
|
||||||
if !received {
|
|
||||||
return Events{pcFull{}} // backpressure
|
|
||||||
}
|
|
||||||
|
|
||||||
return Events{}
|
|
||||||
}
|
}
|
||||||
return Events{}
|
|
||||||
|
received = dm.processor.send(event)
|
||||||
|
if !received {
|
||||||
|
return Events{pcFull{}}, nil // backpressure
|
||||||
|
}
|
||||||
|
|
||||||
|
return Events{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dm *demuxer) send(event Event) bool {
|
func (dm *demuxer) send(event Event) bool {
|
||||||
@ -98,4 +103,13 @@ func (dm *demuxer) stop() {
|
|||||||
fmt.Printf("demuxer stop\n")
|
fmt.Printf("demuxer stop\n")
|
||||||
close(dm.eventbus)
|
close(dm.eventbus)
|
||||||
<-dm.stopped
|
<-dm.stopped
|
||||||
|
dm.terminate(fmt.Errorf("stopped"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *demuxer) terminate(reason error) {
|
||||||
|
dm.finished <- reason
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dm *demuxer) wait() error {
|
||||||
|
return <-dm.finished
|
||||||
}
|
}
|
||||||
|
@ -5,18 +5,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func schedulerHandle(event Event) Events {
|
func schedulerHandle(event Event) (Events, error) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case timeCheck:
|
case timeCheck:
|
||||||
fmt.Println("scheduler handle timeCheck")
|
fmt.Println("scheduler handle timeCheck")
|
||||||
case testEvent:
|
case testEvent:
|
||||||
fmt.Println("scheduler handle testEvent")
|
fmt.Println("scheduler handle testEvent")
|
||||||
return Events{scTestEvent{}}
|
return Events{scTestEvent{}}, nil
|
||||||
}
|
}
|
||||||
return Events{}
|
return Events{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processorHandle(event Event) Events {
|
func processorHandle(event Event) (Events, error) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case timeCheck:
|
case timeCheck:
|
||||||
fmt.Println("processor handle timeCheck")
|
fmt.Println("processor handle timeCheck")
|
||||||
@ -24,10 +24,9 @@ func processorHandle(event Event) Events {
|
|||||||
fmt.Println("processor handle testEvent")
|
fmt.Println("processor handle testEvent")
|
||||||
case scTestEvent:
|
case scTestEvent:
|
||||||
fmt.Println("processor handle scTestEvent")
|
fmt.Println("processor handle scTestEvent")
|
||||||
// should i stop myself?
|
return Events{}, fmt.Errorf("processor done")
|
||||||
return Events{pcFinished{}}
|
|
||||||
}
|
}
|
||||||
return Events{}
|
return Events{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// reactor
|
// reactor
|
||||||
@ -69,7 +68,6 @@ func (r *Reactor) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reactor) Wait() {
|
func (r *Reactor) Wait() {
|
||||||
<-r.demuxer.finished // maybe put this in a wait method
|
|
||||||
fmt.Println("completed routines")
|
fmt.Println("completed routines")
|
||||||
r.Stop()
|
r.Stop()
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,16 @@ package v2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// break out routines
|
|
||||||
// logging
|
// logging
|
||||||
// metrics
|
// metrics
|
||||||
|
|
||||||
type handleFunc = func(event Event) Events
|
type handleFunc = func(event Event) (Events, error)
|
||||||
|
|
||||||
|
// XXX: This should comply with the BaseService interface
|
||||||
// Routine
|
// Routine
|
||||||
type Routine struct {
|
type Routine struct {
|
||||||
name string
|
name string
|
||||||
@ -18,7 +19,8 @@ type Routine struct {
|
|||||||
errors chan error
|
errors chan error
|
||||||
output chan Event
|
output chan Event
|
||||||
stopped chan struct{}
|
stopped chan struct{}
|
||||||
finished chan struct{}
|
finished chan error
|
||||||
|
running *uint32
|
||||||
handle handleFunc
|
handle handleFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,42 +31,56 @@ func newRoutine(name string, output chan Event, handleFunc handleFunc) *Routine
|
|||||||
errors: make(chan error, 1),
|
errors: make(chan error, 1),
|
||||||
output: output,
|
output: output,
|
||||||
stopped: make(chan struct{}, 1),
|
stopped: make(chan struct{}, 1),
|
||||||
finished: make(chan struct{}, 1),
|
finished: make(chan error, 1),
|
||||||
|
running: new(uint32),
|
||||||
handle: handleFunc,
|
handle: handleFunc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: refactor the handle to return an second variable, error which can signal
|
|
||||||
//to the run looop when the handler is done
|
|
||||||
func (rt *Routine) run() {
|
func (rt *Routine) run() {
|
||||||
fmt.Printf("%s: run\n", rt.name)
|
fmt.Printf("%s: run\n", rt.name)
|
||||||
|
starting := atomic.CompareAndSwapUint32(rt.running, uint32(0), uint32(1))
|
||||||
|
if !starting {
|
||||||
|
panic("Routine has already started")
|
||||||
|
}
|
||||||
|
errorsDrained := false
|
||||||
for {
|
for {
|
||||||
|
if !rt.isRunning() {
|
||||||
|
break
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case iEvent, ok := <-rt.input:
|
case iEvent, ok := <-rt.input:
|
||||||
if !ok {
|
if !ok {
|
||||||
|
if !errorsDrained {
|
||||||
|
continue // wait for errors to be drainned
|
||||||
|
}
|
||||||
fmt.Printf("%s: stopping\n", rt.name)
|
fmt.Printf("%s: stopping\n", rt.name)
|
||||||
rt.stopped <- struct{}{}
|
rt.stopped <- struct{}{}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
oEvents := rt.handle(iEvent)
|
oEvents, err := rt.handle(iEvent)
|
||||||
// XXX: this should check for error and exit if error
|
if err != nil {
|
||||||
|
rt.terminate(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Printf("%s handled %d events\n", rt.name, len(oEvents))
|
fmt.Printf("%s handled %d events\n", rt.name, len(oEvents))
|
||||||
for _, event := range oEvents {
|
for _, event := range oEvents {
|
||||||
// check for finished
|
|
||||||
if _, ok := event.(routineFinished); ok {
|
|
||||||
fmt.Printf("%s: finished\n", rt.name)
|
|
||||||
rt.finished <- struct{}{}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println("writting back to output")
|
fmt.Println("writting back to output")
|
||||||
rt.output <- event
|
rt.output <- event
|
||||||
}
|
}
|
||||||
case iEvent, ok := <-rt.errors:
|
case iEvent, ok := <-rt.errors:
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Printf("%s: errors closed\n", rt.name)
|
fmt.Printf("%s: errors closed\n", rt.name)
|
||||||
|
errorsDrained = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
oEvents := rt.handle(iEvent)
|
// rename flush?
|
||||||
|
oEvents, err := rt.handle(iEvent)
|
||||||
|
if err != nil {
|
||||||
|
rt.terminate(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Printf("%s handled %d events from errors\n", rt.name, len(oEvents))
|
fmt.Printf("%s handled %d events from errors\n", rt.name, len(oEvents))
|
||||||
for _, event := range oEvents {
|
for _, event := range oEvents {
|
||||||
rt.output <- event
|
rt.output <- event
|
||||||
@ -80,6 +96,7 @@ func (rt *Routine) feedback() {
|
|||||||
|
|
||||||
func (rt *Routine) send(event Event) bool {
|
func (rt *Routine) send(event Event) bool {
|
||||||
fmt.Printf("%s: send\n", rt.name)
|
fmt.Printf("%s: send\n", rt.name)
|
||||||
|
// XXX: What if we arn't running? this will panic
|
||||||
if err, ok := event.(error); ok {
|
if err, ok := event.(error); ok {
|
||||||
select {
|
select {
|
||||||
case rt.errors <- err:
|
case rt.errors <- err:
|
||||||
@ -99,15 +116,29 @@ func (rt *Routine) send(event Event) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rt *Routine) isRunning() bool {
|
||||||
|
return atomic.LoadUint32(rt.running) == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// rename flush?
|
||||||
func (rt *Routine) stop() {
|
func (rt *Routine) stop() {
|
||||||
fmt.Printf("%s: stop\n", rt.name)
|
fmt.Printf("%s: stop\n", rt.name)
|
||||||
close(rt.errors)
|
|
||||||
close(rt.input)
|
close(rt.input)
|
||||||
<-rt.stopped
|
close(rt.errors)
|
||||||
|
<-rt.stopped // stuck here
|
||||||
|
rt.terminate(fmt.Errorf("routine stopped"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rt *Routine) terminate(reason error) {
|
||||||
|
stopped := atomic.CompareAndSwapUint32(rt.running, uint32(1), uint32(0))
|
||||||
|
if !stopped {
|
||||||
|
panic("called stop but already stopped")
|
||||||
|
}
|
||||||
|
rt.finished <- reason
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX: this should probably produced the finished
|
// XXX: this should probably produced the finished
|
||||||
// channel and let the caller deicde how long to wait
|
// channel and let the caller deicde how long to wait
|
||||||
func (rt *Routine) wait() {
|
func (rt *Routine) wait() error {
|
||||||
<-rt.finished
|
return <-rt.finished
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package v2
|
package v2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -8,14 +9,16 @@ import (
|
|||||||
type eventA struct{}
|
type eventA struct{}
|
||||||
type eventB struct{}
|
type eventB struct{}
|
||||||
|
|
||||||
func simpleHandler(event Event) Events {
|
var done = fmt.Errorf("done")
|
||||||
|
|
||||||
|
func simpleHandler(event Event) (Events, error) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case eventA:
|
case eventA:
|
||||||
return Events{eventB{}}
|
return Events{eventB{}}, nil
|
||||||
case eventB:
|
case eventB:
|
||||||
return Events{routineFinished{}}
|
return Events{routineFinished{}}, done
|
||||||
}
|
}
|
||||||
return Events{}
|
return Events{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRoutine(t *testing.T) {
|
func TestRoutine(t *testing.T) {
|
||||||
@ -32,17 +35,17 @@ func TestRoutine(t *testing.T) {
|
|||||||
|
|
||||||
func genStatefulHandler(maxCount int) handleFunc {
|
func genStatefulHandler(maxCount int) handleFunc {
|
||||||
counter := 0
|
counter := 0
|
||||||
return func(event Event) Events {
|
return func(event Event) (Events, error) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case eventA:
|
case eventA:
|
||||||
counter += 1
|
counter += 1
|
||||||
if counter >= maxCount {
|
if counter >= maxCount {
|
||||||
return Events{routineFinished{}}
|
return Events{}, done
|
||||||
}
|
}
|
||||||
|
|
||||||
return Events{eventA{}}
|
return Events{eventA{}}, nil
|
||||||
}
|
}
|
||||||
return Events{}
|
return Events{}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,14 +62,14 @@ func TestStatefulRoutine(t *testing.T) {
|
|||||||
routine.wait()
|
routine.wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleWithErrors(event Event) Events {
|
func handleWithErrors(event Event) (Events, error) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case eventA:
|
case eventA:
|
||||||
return Events{}
|
return Events{}, nil
|
||||||
case errEvent:
|
case errEvent:
|
||||||
return Events{routineFinished{}}
|
return Events{}, done
|
||||||
}
|
}
|
||||||
return Events{}
|
return Events{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorSaturation(t *testing.T) {
|
func TestErrorSaturation(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user