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: add StateStarterUp to move expensive init ops to dedicated methods #330

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion internals/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ func runDaemon(rcmd *cmdRun, ch chan os.Signal, ready chan<- func()) error {
}

d.Version = cmd.Version
d.Start()
if err := d.Start(); err != nil {
return err
}

watchdog, err := runWatchdog(d)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internals/daemon/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (s *apiSuite) daemon(c *check.C) *Daemon {
d, err := New(&Options{Dir: s.pebbleDir})
c.Assert(err, check.IsNil)
d.addRoutes()

c.Assert(d.overlord.StartUp(), check.IsNil)

s.d = d
return d
}
Expand Down
10 changes: 8 additions & 2 deletions internals/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,18 +466,23 @@ func (d *Daemon) initStandbyHandling() {
d.standbyOpinions.Start()
}

func (d *Daemon) Start() {
func (d *Daemon) Start() error {
if d.rebootIsMissing {
// we need to schedule and wait for a system restart
d.tomb.Kill(nil)
// avoid systemd killing us again while we wait
systemdSdNotify("READY=1")
return
return nil
}
if d.overlord == nil {
panic("internal error: no Overlord")
}

// now perform expensive overlord/manages initialisation
if err := d.overlord.StartUp(); err != nil {
return err
}

d.StartTime = time.Now()

d.connTracker = &connTracker{conns: make(map[net.Conn]struct{})}
Expand Down Expand Up @@ -519,6 +524,7 @@ func (d *Daemon) Start() {

// notify systemd that we are ready
systemdSdNotify("READY=1")
return nil
}

// HandleRestart implements overlord.RestartBehavior.
Expand Down
33 changes: 17 additions & 16 deletions internals/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func (s *daemonSuite) TestExternalManager(c *C) {
OverlordExtension: &fakeExtension{},
})
c.Assert(err, IsNil)

err = d.overlord.StartUp()
c.Assert(err, IsNil)
err = d.overlord.StateEngine().Ensure()
c.Assert(err, IsNil)
extension, ok := d.overlord.Extension().(*fakeExtension)
Expand Down Expand Up @@ -185,7 +186,7 @@ func (s *daemonSuite) TestAddCommand(c *C) {

d := s.newDaemon(c)
d.Init()
d.Start()
c.Assert(d.Start(), IsNil)
defer d.Stop(nil)

result := d.router.Get(endpoint).GetHandler()
Expand All @@ -197,7 +198,7 @@ func (s *daemonSuite) TestExplicitPaths(c *C) {

d := s.newDaemon(c)
d.Init()
d.Start()
c.Assert(d.Start(), IsNil)
defer d.Stop(nil)

info, err := os.Stat(s.socketPath)
Expand Down Expand Up @@ -553,7 +554,7 @@ func (s *daemonSuite) TestStartStop(c *C) {
untrustedAccept := make(chan struct{})
d.untrustedListener = &witnessAcceptListener{Listener: l2, accept: untrustedAccept}

d.Start()
c.Assert(d.Start(), IsNil)

generalDone := make(chan struct{})
go func() {
Expand Down Expand Up @@ -594,7 +595,7 @@ func (s *daemonSuite) TestRestartWiring(c *C) {
untrustedAccept := make(chan struct{})
d.untrustedListener = &witnessAcceptListener{Listener: l, accept: untrustedAccept}

d.Start()
c.Assert(d.Start(), IsNil)
defer d.Stop(nil)

generalDone := make(chan struct{})
Expand Down Expand Up @@ -661,7 +662,7 @@ func (s *daemonSuite) TestGracefulStop(c *C) {
untrustedAccept := make(chan struct{})
d.untrustedListener = &witnessAcceptListener{Listener: untrustedL, accept: untrustedAccept}

d.Start()
c.Assert(d.Start(), IsNil)

generalAccepting := make(chan struct{})
go func() {
Expand Down Expand Up @@ -728,7 +729,7 @@ func (s *daemonSuite) TestRestartSystemWiring(c *C) {
untrustedAccept := make(chan struct{})
d.untrustedListener = &witnessAcceptListener{Listener: l, accept: untrustedAccept}

d.Start()
c.Assert(d.Start(), IsNil)
defer d.Stop(nil)

st := d.overlord.State()
Expand Down Expand Up @@ -875,7 +876,7 @@ func (s *daemonSuite) TestRestartShutdownWithSigtermInBetween(c *C) {
d := s.newDaemon(c)
makeDaemonListeners(c, d)

d.Start()
c.Assert(d.Start(), IsNil)
st := d.overlord.State()

st.Lock()
Expand Down Expand Up @@ -907,7 +908,7 @@ func (s *daemonSuite) TestRestartShutdown(c *C) {
d := s.newDaemon(c)
makeDaemonListeners(c, d)

d.Start()
c.Assert(d.Start(), IsNil)
st := d.overlord.State()

st.Lock()
Expand Down Expand Up @@ -954,7 +955,7 @@ func (s *daemonSuite) TestRestartExpectedRebootIsMissing(c *C) {
c.Check(err, IsNil)
c.Check(n, Equals, 1)

d.Start()
c.Assert(d.Start(), IsNil)

c.Check(s.notified, DeepEquals, []string{"READY=1"})

Expand Down Expand Up @@ -1030,11 +1031,11 @@ func (s *daemonSuite) TestRestartIntoSocketModeNoNewChanges(c *C) {
d := s.newDaemon(c)
makeDaemonListeners(c, d)

d.Start()
c.Assert(d.Start(), IsNil)

// pretend some ensure happened
for i := 0; i < 5; i++ {
d.overlord.StateEngine().Ensure()
c.Check(d.overlord.StateEngine().Ensure(), IsNil)
time.Sleep(5 * time.Millisecond)
}

Expand Down Expand Up @@ -1066,10 +1067,10 @@ func (s *daemonSuite) TestRestartIntoSocketModePendingChanges(c *C) {

st := d.overlord.State()

d.Start()
c.Assert(d.Start(), IsNil)
// pretend some ensure happened
for i := 0; i < 5; i++ {
d.overlord.StateEngine().Ensure()
c.Check(d.overlord.StateEngine().Ensure(), IsNil)
time.Sleep(5 * time.Millisecond)
}

Expand Down Expand Up @@ -1152,7 +1153,7 @@ func (s *daemonSuite) TestHTTPAPI(c *C) {
s.httpAddress = ":0" // Go will choose port (use listener.Addr() to find it)
d := s.newDaemon(c)
d.Init()
d.Start()
c.Assert(d.Start(), IsNil)
port := d.httpListener.Addr().(*net.TCPAddr).Port

request, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/v1/health", port), nil)
Expand Down Expand Up @@ -1195,7 +1196,7 @@ services:
d := s.newDaemon(c)
err := d.Init()
c.Assert(err, IsNil)
d.Start()
c.Assert(d.Start(), IsNil)

// Start the test service.
payload := bytes.NewBufferString(`{"action": "start", "services": ["test1"]}`)
Expand Down
2 changes: 2 additions & 0 deletions internals/overlord/managers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (s *mgrsSuite) SetUpTest(c *C) {

o, err := overlord.New(&overlord.Options{PebbleDir: s.dir})
c.Assert(err, IsNil)
err = o.StartUp()
c.Assert(err, IsNil)
s.o = o
}

Expand Down
17 changes: 15 additions & 2 deletions internals/overlord/overlord.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Overlord struct {

// managers
inited bool
startedUp bool
runner *state.TaskRunner
serviceMgr *servstate.ServiceManager
commandMgr *cmdstate.CommandManager
Expand Down Expand Up @@ -252,6 +253,14 @@ func initRestart(s *state.State, curBootID string, restartHandler restart.Handle
return restart.Init(s, curBootID, restartHandler)
}

func (o *Overlord) StartUp() error {
if o.startedUp {
return nil
}
o.startedUp = true
return o.stateEng.StartUp()
}

func (o *Overlord) ensureTimerSetup() {
o.ensureLock.Lock()
defer o.ensureLock.Unlock()
Expand Down Expand Up @@ -337,6 +346,10 @@ func (o *Overlord) Stop() error {
}

func (o *Overlord) settle(timeout time.Duration, beforeCleanups func()) error {
if err := o.StartUp(); err != nil {
return err
}

func() {
o.ensureLock.Lock()
defer o.ensureLock.Unlock()
Expand Down Expand Up @@ -406,7 +419,7 @@ func (o *Overlord) settle(timeout time.Duration, beforeCleanups func()) error {
// is scheduled. It then waits similarly for all ready changes to
// reach the clean state. Chiefly for tests. Cannot be used in
// conjunction with Loop. If timeout is non-zero and settling takes
// longer than timeout, returns an error.
// longer than timeout, returns an error. Calls StartUp as well.
func (o *Overlord) Settle(timeout time.Duration) error {
return o.settle(timeout, nil)
}
Expand All @@ -418,7 +431,7 @@ func (o *Overlord) Settle(timeout time.Duration) error {
// changes to reach the clean state, but calls once the provided
// callback before doing that. Chiefly for tests. Cannot be used in
// conjunction with Loop. If timeout is non-zero and settling takes
// longer than timeout, returns an error.
// longer than timeout, returns an error. Calls StartUp as well.
func (o *Overlord) SettleObserveBeforeCleanups(timeout time.Duration, beforeCleanups func()) error {
return o.settle(timeout, beforeCleanups)
}
Expand Down
31 changes: 30 additions & 1 deletion internals/overlord/overlord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ type witnessManager struct {
expectedEnsure int
ensureCalled chan struct{}
ensureCallback func(s *state.State) error
startedUp int
}

func (wm *witnessManager) StartUp() error {
wm.startedUp++
return nil
}

func (wm *witnessManager) Ensure() error {
Expand All @@ -186,6 +192,9 @@ func (ovs *overlordSuite) TestTrivialRunAndStop(c *C) {
o, err := overlord.New(&overlord.Options{PebbleDir: ovs.dir})
c.Assert(err, IsNil)

err = o.StartUp()
c.Assert(err, IsNil)

o.Loop()

err = o.Stop()
Expand Down Expand Up @@ -224,6 +233,9 @@ func (ovs *overlordSuite) TestEnsureLoopRunAndStop(c *C) {
}
o.AddManager(witness)

err := o.StartUp()
c.Assert(err, IsNil)

o.Loop()
defer o.Stop()

Expand All @@ -235,8 +247,10 @@ func (ovs *overlordSuite) TestEnsureLoopRunAndStop(c *C) {
}
c.Check(time.Since(t0) >= 10*time.Millisecond, Equals, true)

err := o.Stop()
err = o.Stop()
c.Assert(err, IsNil)

c.Check(witness.startedUp, Equals, 1)
}

func (ovs *overlordSuite) TestEnsureLoopMediatedEnsureBeforeImmediate(c *C) {
Expand All @@ -257,6 +271,8 @@ func (ovs *overlordSuite) TestEnsureLoopMediatedEnsureBeforeImmediate(c *C) {
}
o.AddManager(witness)

c.Assert(o.StartUp(), IsNil)

o.Loop()
defer o.Stop()

Expand Down Expand Up @@ -285,6 +301,8 @@ func (ovs *overlordSuite) TestEnsureLoopMediatedEnsureBefore(c *C) {
}
o.AddManager(witness)

c.Assert(o.StartUp(), IsNil)

o.Loop()
defer o.Stop()

Expand Down Expand Up @@ -314,6 +332,9 @@ func (ovs *overlordSuite) TestEnsureBeforeSleepy(c *C) {
}
o.AddManager(witness)

err := o.StartUp()
c.Assert(err, IsNil)

o.Loop()
defer o.Stop()

Expand Down Expand Up @@ -343,6 +364,8 @@ func (ovs *overlordSuite) TestEnsureBeforeLater(c *C) {
}
o.AddManager(witness)

c.Assert(o.StartUp(), IsNil)

o.Loop()
defer o.Stop()

Expand Down Expand Up @@ -372,6 +395,8 @@ func (ovs *overlordSuite) TestEnsureLoopMediatedEnsureBeforeOutsideEnsure(c *C)
}
o.AddManager(witness)

c.Assert(o.StartUp(), IsNil)

o.Loop()
defer o.Stop()

Expand Down Expand Up @@ -428,6 +453,8 @@ func (ovs *overlordSuite) TestEnsureLoopPrune(c *C) {
}
o.AddManager(witness)

c.Assert(o.StartUp(), IsNil)

o.Loop()

select {
Expand Down Expand Up @@ -864,6 +891,8 @@ func (ovs *overlordSuite) TestOverlordCanStandby(c *C) {
}
o.AddManager(witness)

c.Assert(o.StartUp(), IsNil)

// can only standby after loop ran once
c.Assert(o.CanStandby(), Equals, false)

Expand Down
Loading
Loading