Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve processor stopped state management #449

Merged
merged 6 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/IBM/sarama"
"github.com/hashicorp/go-multierror"

"github.com/lovoo/goka/multierr"
"github.com/lovoo/goka/storage"
)
Expand All @@ -25,6 +26,8 @@ const (
ProcStateRunning
// ProcStateStopping indicates a stopping partition processor
ProcStateStopping
// ProcStateStopped indicates a stopped partition processor
ProcStateStopped
)

// ProcessCallback function is called for every message received by the
Expand Down Expand Up @@ -60,6 +63,8 @@ type Processor struct {

state *Signal

errMux sync.Mutex
err error
done chan struct{}
cancel context.CancelFunc
}
Expand Down Expand Up @@ -128,7 +133,7 @@ func NewProcessor(brokers []string, gg *GroupGraph, options ...ProcessorOption)

graph: gg,

state: NewSignal(ProcStateIdle, ProcStateStarting, ProcStateSetup, ProcStateRunning, ProcStateStopping).SetState(ProcStateIdle),
state: NewSignal(ProcStateIdle, ProcStateStarting, ProcStateSetup, ProcStateRunning, ProcStateStopping, ProcStateStopped).SetState(ProcStateIdle),
done: make(chan struct{}),
}

Expand Down Expand Up @@ -260,7 +265,10 @@ func (g *Processor) Run(ctx context.Context) (rerr error) {
// collect all errors before leaving
var errs *multierror.Error
defer func() {
rerr = multierror.Append(errs, rerr).ErrorOrNil()
g.errMux.Lock()
defer g.errMux.Unlock()
g.err = multierror.Append(errs, rerr).ErrorOrNil()
rerr = g.err
}()

var err error
Expand Down Expand Up @@ -711,7 +719,7 @@ func (g *Processor) Cleanup(session sarama.ConsumerGroupSession) error {
defer g.log.Debugf("Cleaning up for %d ... done", session.GenerationID())

g.state.SetState(ProcStateStopping)
defer g.state.SetState(ProcStateIdle)
defer g.state.SetState(ProcStateStopped)
errg, _ := multierr.NewErrGroup(session.Context())
g.mTables.RLock()
for part, partition := range g.partitions {
Expand Down Expand Up @@ -926,6 +934,18 @@ func (g *Processor) Stop() {
g.cancel()
}

// Done returns a channel that is closed when the processor is stopped.
func (g *Processor) Done() <-chan struct{} {
return g.done
}

// Error returns the error that caused the processor to stop.
func (g *Processor) Error() error {
g.errMux.Lock()
defer g.errMux.Unlock()
return g.err
}

// VisitAllWithStats visits all keys in parallel by passing the visit request
// to all partitions.
// The optional argument "meta" will be forwarded to the visit-function of each key of the table.
Expand Down
Loading
Loading