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

feat(overlord): allow adding external managers #273

Merged
merged 20 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions internals/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func userFromRequest(state interface{}, r *http.Request) (*UserState, error) {
return nil, nil
}

func (d *Daemon) Overlord() *overlord.Overlord {
return d.overlord
}

func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request) {
st := c.d.state
st.Lock()
Expand Down
11 changes: 1 addition & 10 deletions internals/overlord/overlord.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ type Overlord struct {
pruneTicker *time.Ticker

// managers
inited bool
runner *state.TaskRunner
serviceMgr *servstate.ServiceManager
commandMgr *cmdstate.CommandManager
Expand All @@ -78,7 +77,6 @@ func New(pebbleDir string, restartHandler restart.Handler, serviceOutput io.Writ
o := &Overlord{
pebbleDir: pebbleDir,
loopTomb: new(tomb.Tomb),
inited: true,
}

if !filepath.IsAbs(pebbleDir) {
Expand Down Expand Up @@ -130,8 +128,7 @@ func New(pebbleDir string, restartHandler restart.Handler, serviceOutput io.Writ
// Tell service manager about check failures.
o.checkMgr.NotifyCheckFailed(o.serviceMgr.CheckFailed)

// the shared task runner should be added last!
o.stateEng.AddManager(o.runner)
o.stateEng.SetTaskRunner(o.runner)
paul-rodriguez marked this conversation as resolved.
Show resolved Hide resolved

return o, nil
}
Expand Down Expand Up @@ -427,20 +424,14 @@ func Fake() *Overlord {
func FakeWithState(handleRestart func(restart.RestartType)) *Overlord {
o := &Overlord{
loopTomb: new(tomb.Tomb),
inited: false,
}
s := state.New(fakeBackend{o: o})
o.stateEng = NewStateEngine(s)
o.runner = state.NewTaskRunner(s)
return o
}

// AddManager adds a manager to a fake overlord. It cannot be used for
// a normally initialized overlord those are already fully populated.
func (o *Overlord) AddManager(mgr StateManager) {
if o.inited {
panic("internal error: cannot add managers to a fully initialized Overlord")
}
flotter marked this conversation as resolved.
Show resolved Hide resolved
o.addManager(mgr)
}

Expand Down
32 changes: 27 additions & 5 deletions internals/overlord/stateengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,24 @@ type StateEngine struct {
state *state.State
stopped bool
// managers in use
mgrLock sync.Mutex
managers []StateManager
mgrLock sync.Mutex
managers []StateManager
taskRunner StateManager
paul-rodriguez marked this conversation as resolved.
Show resolved Hide resolved
}

// orderedManagers returns all the managers added to this engine.
//
// The managers are ordered by their precedence when executing Ensure, Stop
// and Wait: the task runner is the last item in the result.
func (se *StateEngine) orderedManagers() []StateManager {
result := make([]StateManager, 0, len(se.managers)+1)
for _, m := range se.managers {
result = append(result, m)
}
if se.taskRunner != nil {
result = append(result, se.taskRunner)
}
return result
}

// NewStateEngine returns a new state engine.
Expand Down Expand Up @@ -95,7 +111,7 @@ func (se *StateEngine) Ensure() error {
return fmt.Errorf("state engine already stopped")
}
var errs []error
for _, m := range se.managers {
for _, m := range se.orderedManagers() {
err := m.Ensure()
if err != nil {
logger.Noticef("state ensure error: %v", err)
Expand All @@ -115,14 +131,20 @@ func (se *StateEngine) AddManager(m StateManager) {
se.managers = append(se.managers, m)
}

func (se *StateEngine) SetTaskRunner(taskRunner StateManager) {
se.mgrLock.Lock()
defer se.mgrLock.Unlock()
se.taskRunner = taskRunner
}

// Wait waits for all managers current activities.
func (se *StateEngine) Wait() {
se.mgrLock.Lock()
defer se.mgrLock.Unlock()
if se.stopped {
return
}
for _, m := range se.managers {
for _, m := range se.orderedManagers() {
if waiter, ok := m.(StateWaiter); ok {
waiter.Wait()
}
Expand All @@ -136,7 +158,7 @@ func (se *StateEngine) Stop() {
if se.stopped {
return
}
for _, m := range se.managers {
for _, m := range se.orderedManagers() {
if stopper, ok := m.(StateStopper); ok {
stopper.Stop()
}
Expand Down
Loading