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

fix: avoid need to lock state for restart.Pending() #451

Merged
merged 3 commits into from
Jul 17, 2024
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
6 changes: 2 additions & 4 deletions internals/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,7 @@ func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rsp := rspf(c, r, user)

if rsp, ok := rsp.(*resp); ok {
st := c.d.state
st.Lock()
_, rst := restart.Pending(st)
st.Unlock()
_, rst := c.d.overlord.RestartManager().Pending()
switch rst {
case restart.RestartSystem:
rsp.transmitMaintenance(errorKindSystemRestart, "system is restarting")
Expand All @@ -247,6 +244,7 @@ func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rsp.transmitMaintenance(errorKindDaemonRestart, "daemon is stopping to wait for socket activation")
}
if rsp.Type != ResponseTypeError {
st := c.d.state
st.Lock()
count, stamp := st.WarningsSummary()
st.Unlock()
Expand Down
5 changes: 2 additions & 3 deletions internals/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (s *daemonSuite) TestCommandRestartingState(c *C) {
state := d.overlord.State()

state.Lock()
restart.FakePending(state, restart.RestartSystem)
d.overlord.RestartManager().FakePending(restart.RestartSystem)
state.Unlock()
rec = httptest.NewRecorder()
cmd.ServeHTTP(rec, req)
Expand All @@ -298,7 +298,7 @@ func (s *daemonSuite) TestCommandRestartingState(c *C) {
})

state.Lock()
restart.FakePending(state, restart.RestartDaemon)
d.overlord.RestartManager().FakePending(restart.RestartDaemon)
state.Unlock()
rec = httptest.NewRecorder()
cmd.ServeHTTP(rec, req)
Expand Down Expand Up @@ -1591,7 +1591,6 @@ func (s *utilsSuite) TestExitOnPanic(c *C) {
panicHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("before"))
panic("PANIC!")
w.Write([]byte(r.URL.Path))
})
stderr.Reset()
recorder = httptest.NewRecorder()
Expand Down
23 changes: 10 additions & 13 deletions internals/overlord/restart/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package restart

import (
"errors"
"sync/atomic"

"github.com/canonical/pebble/internals/overlord/state"
)
Expand Down Expand Up @@ -60,7 +61,7 @@ type restartManagerKey struct{}
// RestartManager takes care of restart-related state.
type RestartManager struct {
state *state.State
restarting RestartType
restarting atomic.Int32 // really of type RestartType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In future we could use an atomic.Pointer[RestartType] instead, although unnecessary heap allocation for such a small type, keeps the type.

h Handler
bootID string
}
Expand Down Expand Up @@ -149,24 +150,20 @@ func Request(st *state.State, t RestartType) {
case RestartSystem, RestartSystemNow, RestartSystemHaltNow, RestartSystemPoweroffNow:
st.Set("system-restart-from-boot-id", rm.bootID)
}
rm.restarting = t
rm.restarting.Store(int32(t))
rm.handleRestart(t)
}

// Pending returns whether a restart was requested with Request and of which type.
func Pending(st *state.State) (bool, RestartType) {
cached := st.Cached(restartManagerKey{})
if cached == nil {
return false, RestartUnset
}
rm := cached.(*RestartManager)
return rm.restarting != RestartUnset, rm.restarting
// NOTE: the state does not need to be locked to fetch this information.
func (rm *RestartManager) Pending() (bool, RestartType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an "ok" bool is usually the last return value (like error)

e.g.
v, ok := myMap[k]

Since this is existing functionality, I don't mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was actually tempted to get rid of the bool entirely, because it's just b != RestartUnset, which is easy for the caller to do. However, given it has this signature in snapd, I want to keep it as is.

restarting := RestartType(rm.restarting.Load())
return restarting != RestartUnset, restarting
}

func FakePending(st *state.State, restarting RestartType) RestartType {
rm := restartManager(st, "internal error: cannot mock a restart request before RestartManager initialization")
old := rm.restarting
rm.restarting = restarting
func (rm *RestartManager) FakePending(restarting RestartType) RestartType {
old := RestartType(rm.restarting.Load())
rm.restarting.Store(int32(restarting))
return old
}

Expand Down
21 changes: 8 additions & 13 deletions internals/overlord/restart/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,21 @@ func (s *restartSuite) TestRequestRestartDaemon(c *C) {
st.Lock()
defer st.Unlock()

// uninitialized
ok, t := restart.Pending(st)
c.Check(ok, Equals, false)
c.Check(t, Equals, restart.RestartUnset)

h := &testHandler{}

_, err := restart.Manager(st, "boot-id-1", h)
manager, err := restart.Manager(st, "boot-id-1", h)
c.Assert(err, IsNil)
c.Check(h.rebootAsExpected, Equals, true)

ok, t = restart.Pending(st)
ok, t := manager.Pending()
c.Check(ok, Equals, false)
c.Check(t, Equals, restart.RestartUnset)

restart.Request(st, restart.RestartDaemon)

c.Check(h.restartRequested, Equals, true)

ok, t = restart.Pending(st)
ok, t = manager.Pending()
c.Check(ok, Equals, true)
c.Check(t, Equals, restart.RestartDaemon)
}
Expand All @@ -97,12 +92,12 @@ func (s *restartSuite) TestRequestRestartDaemonNoHandler(c *C) {
st.Lock()
defer st.Unlock()

_, err := restart.Manager(st, "boot-id-1", nil)
manager, err := restart.Manager(st, "boot-id-1", nil)
c.Assert(err, IsNil)

restart.Request(st, restart.RestartDaemon)

ok, t := restart.Pending(st)
ok, t := manager.Pending()
c.Check(ok, Equals, true)
c.Check(t, Equals, restart.RestartDaemon)
}
Expand All @@ -113,19 +108,19 @@ func (s *restartSuite) TestRequestRestartSystemAndVerifyReboot(c *C) {
defer st.Unlock()

h := &testHandler{}
_, err := restart.Manager(st, "boot-id-1", h)
manager, err := restart.Manager(st, "boot-id-1", h)
c.Assert(err, IsNil)
c.Check(h.rebootAsExpected, Equals, true)

ok, t := restart.Pending(st)
ok, t := manager.Pending()
c.Check(ok, Equals, false)
c.Check(t, Equals, restart.RestartUnset)

restart.Request(st, restart.RestartSystem)

c.Check(h.restartRequested, Equals, true)

ok, t = restart.Pending(st)
ok, t = manager.Pending()
c.Check(ok, Equals, true)
c.Check(t, Equals, restart.RestartSystem)

Expand Down
Loading