From 6dbc138339e07226e13695b3b3ceccfa52296dbd Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 22 May 2023 13:41:01 +0200 Subject: [PATCH] prune exit codes only when container doesn't exist Make sure to prune container exit codes only when the associated container does not exist anymore. This is needed when checking if any container in kube-play exited non-zero and a building block for the below linked Jira card. [NO NEW TESTS NEEDED] - there are no unit tests for exit code pruning. Jira: https://issues.redhat.com/browse/RUN-1776 Signed-off-by: Valentin Rothberg --- libpod/boltdb_state.go | 13 ++++++++++++- libpod/sqlite_state.go | 5 +++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/libpod/boltdb_state.go b/libpod/boltdb_state.go index 174dffeeb0..3edd6e6d47 100644 --- a/libpod/boltdb_state.go +++ b/libpod/boltdb_state.go @@ -1451,7 +1451,8 @@ func (s *BoltState) GetContainerExitCodeTimeStamp(id string) (*time.Time, error) }) } -// PruneExitCodes removes exit codes older than 5 minutes. +// PruneExitCodes removes exit codes older than 5 minutes unless the associated +// container still exists. func (s *BoltState) PruneContainerExitCodes() error { if !s.valid { return define.ErrDBClosed @@ -1472,7 +1473,17 @@ func (s *BoltState) PruneContainerExitCodes() error { return err } + ctrsBucket, err := getCtrBucket(tx) + if err != nil { + return err + } + return timeStampBucket.ForEach(func(rawID, rawTimeStamp []byte) error { + if ctrsBucket.Bucket(rawID) != nil { + // If the container still exists, don't prune + // its exit code since we may still need it. + return nil + } var timeStamp time.Time if err := timeStamp.UnmarshalText(rawTimeStamp); err != nil { return fmt.Errorf("converting raw time stamp %v of container %s from DB: %w", rawTimeStamp, string(rawID), err) diff --git a/libpod/sqlite_state.go b/libpod/sqlite_state.go index 7c94c6f208..fd207acb36 100644 --- a/libpod/sqlite_state.go +++ b/libpod/sqlite_state.go @@ -943,7 +943,8 @@ func (s *SQLiteState) GetContainerExitCodeTimeStamp(id string) (*time.Time, erro return &result, nil } -// PruneExitCodes removes exit codes older than 5 minutes. +// PruneExitCodes removes exit codes older than 5 minutes unless the associated +// container still exists. func (s *SQLiteState) PruneContainerExitCodes() (defErr error) { if !s.valid { return define.ErrDBClosed @@ -963,7 +964,7 @@ func (s *SQLiteState) PruneContainerExitCodes() (defErr error) { } }() - if _, err := tx.Exec("DELETE FROM ContainerExitCode WHERE (Timestamp <= ?);", fiveMinsAgo); err != nil { + if _, err := tx.Exec("DELETE FROM ContainerExitCode WHERE (Timestamp <= ?) AND (ID NOT IN (SELECT ID FROM ContainerConfig))", fiveMinsAgo); err != nil { return fmt.Errorf("removing exit codes with timestamps older than 5 minutes: %w", err) }