container rm: save once for exec removal and state change

Do not save the container each for changing the state and for removing
running exec sessions.  Saving the container is expensive and avoiding
the redundant save makes `container rm` 1.2 times faster on my
workstation.

[NO NEW TESTS NEEDED]

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg 2023-01-30 15:43:03 +01:00
parent 929d03a5ea
commit 29b346deab
2 changed files with 14 additions and 18 deletions

View File

@ -1072,14 +1072,6 @@ func (c *Container) removeAllExecSessions() error {
}
c.state.ExecSessions = nil
c.state.LegacyExecSessions = nil
if err := c.save(); err != nil {
if !errors.Is(err, define.ErrCtrRemoved) {
if lastErr != nil {
logrus.Errorf("Stopping container %s exec sessions: %v", c.ID(), lastErr)
}
lastErr = err
}
}
return lastErr
}

View File

@ -776,16 +776,6 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force, remo
cleanupErr = fmt.Errorf("cleaning up container %s: %w", c.ID(), err)
}
// Set ContainerStateRemoving
c.state.State = define.ContainerStateRemoving
if err := c.save(); err != nil {
if cleanupErr != nil {
logrus.Errorf(err.Error())
}
return fmt.Errorf("unable to set container %s removing state in database: %w", c.ID(), err)
}
// Remove all active exec sessions
// removing the exec sessions might temporarily unlock the container's lock. Using it
// after setting the state to ContainerStateRemoving will prevent that the container is
@ -798,6 +788,20 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force, remo
}
}
// Set ContainerStateRemoving as an intermediate state (we may get
// killed at any time) and save the container.
c.state.State = define.ContainerStateRemoving
if err := c.save(); err != nil {
if !errors.Is(err, define.ErrCtrRemoved) {
if cleanupErr == nil {
cleanupErr = err
} else {
logrus.Errorf("Saving container: %v", err)
}
}
}
// Stop the container's storage
if err := c.teardownStorage(); err != nil {
if cleanupErr == nil {