Move in-memory operations before disk operations in state

In-memory operations will check more exception conditions, so let them to first
to verify that it's safe to continue with on-disk operations. This should result
in fewer errors with on-disk operations.

Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
Matthew Heon 2017-04-06 20:07:44 -04:00
parent 225f639a3f
commit f25187d3f2

View file

@ -671,16 +671,15 @@ func (s *FileState) AddSandbox(sb *sandbox.Sandbox) error {
return fmt.Errorf("error syncing with on-disk state: %v", err) return fmt.Errorf("error syncing with on-disk state: %v", err)
} }
if s.memoryState.HasSandbox(sb.ID()) { if err := s.memoryState.AddSandbox(sb); err != nil {
return fmt.Errorf("sandbox with ID %v already exists", sb.ID()) return fmt.Errorf("error adding sandbox %v to in-memory state: %v", sb.ID(), err)
} }
if err := s.putSandboxToDisk(sb); err != nil { if err := s.putSandboxToDisk(sb); err != nil {
return err if err2 := s.memoryState.DeleteSandbox(sb.ID()); err2 != nil {
logrus.Errorf("error removing sandbox %s from in-memory state, states are desynced: %v", sb.ID(), err)
} }
return err
if err := s.memoryState.AddSandbox(sb); err != nil {
return fmt.Errorf("error adding sandbox %v to in-memory state: %v", sb.ID(), err)
} }
return nil return nil
@ -700,6 +699,7 @@ func (s *FileState) HasSandbox(id string) bool {
} }
// DeleteSandbox removes the given sandbox from the state // DeleteSandbox removes the given sandbox from the state
// TODO make atomic
func (s *FileState) DeleteSandbox(id string) error { func (s *FileState) DeleteSandbox(id string) error {
s.lockfile.Lock() s.lockfile.Lock()
defer s.lockfile.Unlock() defer s.lockfile.Unlock()
@ -708,18 +708,14 @@ func (s *FileState) DeleteSandbox(id string) error {
return fmt.Errorf("error syncing with on-disk state: %v", err) return fmt.Errorf("error syncing with on-disk state: %v", err)
} }
if !s.memoryState.HasSandbox(id) { if err := s.memoryState.DeleteSandbox(id); err != nil {
return fmt.Errorf("cannot remove sandbox %v as it does not exist", id) return fmt.Errorf("error removing sandbox %v from in-memory state: %v", id, err)
} }
if err := s.removeSandboxFromDisk(id); err != nil { if err := s.removeSandboxFromDisk(id); err != nil {
return err return err
} }
if err := s.memoryState.DeleteSandbox(id); err != nil {
return fmt.Errorf("error removing sandbox %v from in-memory state: %v", id, err)
}
return nil return nil
} }
@ -781,24 +777,15 @@ func (s *FileState) AddContainer(c *oci.Container, sandboxID string) error {
return fmt.Errorf("error syncing with on-disk state: %v", err) return fmt.Errorf("error syncing with on-disk state: %v", err)
} }
if s.memoryState.HasContainer(c.ID(), sandboxID) { if err := s.memoryState.AddContainer(c, sandboxID); err != nil {
return fmt.Errorf("container with id %v in sandbox %v already exists", c.ID(), sandboxID) return fmt.Errorf("error adding container %v to in-memory state: %v", c.ID(), err)
}
sb, err := s.memoryState.GetSandbox(sandboxID)
if err != nil {
return err
}
if sb.InfraContainer().ID() == c.ID() {
return fmt.Errorf("container is already infra container of sandbox %v, refusing to add", sandboxID)
} }
if err := s.putContainerToDisk(c, true); err != nil { if err := s.putContainerToDisk(c, true); err != nil {
return err if err2 := s.memoryState.DeleteContainer(c.ID(), sandboxID); err2 != nil {
logrus.Errorf("error removing container %v from in-memory state, states are desynced: %v", c.ID(), err2)
} }
return err
if err := s.memoryState.AddContainer(c, sandboxID); err != nil {
return fmt.Errorf("error adding container %v to in-memory state: %v", c.ID(), err)
} }
return nil return nil
@ -818,6 +805,7 @@ func (s *FileState) HasContainer(id, sandboxID string) bool {
} }
// DeleteContainer removes a container from a given sandbox in the state // DeleteContainer removes a container from a given sandbox in the state
// TODO make atomic
func (s *FileState) DeleteContainer(id, sandboxID string) error { func (s *FileState) DeleteContainer(id, sandboxID string) error {
s.lockfile.Lock() s.lockfile.Lock()
defer s.lockfile.Unlock() defer s.lockfile.Unlock()
@ -826,18 +814,14 @@ func (s *FileState) DeleteContainer(id, sandboxID string) error {
return fmt.Errorf("error syncing with on-disk state: %v", err) return fmt.Errorf("error syncing with on-disk state: %v", err)
} }
if !s.memoryState.HasContainer(id, sandboxID) { if err := s.memoryState.DeleteContainer(id, sandboxID); err != nil {
return fmt.Errorf("cannot remove container %v in sandbox %v as it does not exist", id, sandboxID) return fmt.Errorf("error removing container %v from in-memory state: %v", id, err)
} }
if err := s.removeContainerFromDisk(id, sandboxID); err != nil { if err := s.removeContainerFromDisk(id, sandboxID); err != nil {
return err return err
} }
if err := s.memoryState.DeleteContainer(id, sandboxID); err != nil {
return fmt.Errorf("error removing container %v from in-memory state: %v", id, err)
}
return nil return nil
} }