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 <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2023-05-22 13:41:01 +02:00
parent 688e6dbef1
commit 6dbc138339
2 changed files with 15 additions and 3 deletions

View File

@ -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 { func (s *BoltState) PruneContainerExitCodes() error {
if !s.valid { if !s.valid {
return define.ErrDBClosed return define.ErrDBClosed
@ -1472,7 +1473,17 @@ func (s *BoltState) PruneContainerExitCodes() error {
return err return err
} }
ctrsBucket, err := getCtrBucket(tx)
if err != nil {
return err
}
return timeStampBucket.ForEach(func(rawID, rawTimeStamp []byte) error { 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 var timeStamp time.Time
if err := timeStamp.UnmarshalText(rawTimeStamp); err != nil { 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) return fmt.Errorf("converting raw time stamp %v of container %s from DB: %w", rawTimeStamp, string(rawID), err)

View File

@ -943,7 +943,8 @@ func (s *SQLiteState) GetContainerExitCodeTimeStamp(id string) (*time.Time, erro
return &result, nil 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) { func (s *SQLiteState) PruneContainerExitCodes() (defErr error) {
if !s.valid { if !s.valid {
return define.ErrDBClosed 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) return fmt.Errorf("removing exit codes with timestamps older than 5 minutes: %w", err)
} }