Skip to content

Commit 9fa629b

Browse files
committed
RHINENG-25740: mark systems stale one by one
to aviod locking system_inventory for long time
1 parent 8dccf71 commit 9fa629b

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

tasks/system_culling/system_culling.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,44 @@ func deleteCulledSystems(tx *gorm.DB, limitDeleted int) (nDeleted int64, err err
7878
return nDeleted, nil
7979
}
8080

81-
func markSystemsStale(tx *gorm.DB, markedLimit int) (nMarked int, err error) {
82-
var nMarkedArr []int
83-
err = tx.Raw("select mark_stale_systems(?)", markedLimit).
84-
Find(&nMarkedArr).Error
85-
if len(nMarkedArr) > 0 {
86-
nMarked = nMarkedArr[0]
81+
// each update runs in its own transaction to avoid holding locks across many rows
82+
func markSystemsStale(tx *gorm.DB, markedLimit int) (nMarked int64, err error) {
83+
var candidates []struct {
84+
RhAccountID int `gorm:"column:rh_account_id"`
85+
ID int64 `gorm:"column:id"`
86+
Expired bool `gorm:"column:expired"`
87+
}
88+
now := time.Now()
89+
err = tx.Model(&models.SystemInventory{}).
90+
Select("rh_account_id, id, (stale_warning_timestamp < ?) as expired", now).
91+
Where("stale != (stale_warning_timestamp < ?)", now).
92+
Order("rh_account_id").Order("id").
93+
Limit(markedLimit).
94+
Find(&candidates).Error
95+
if err != nil {
96+
return 0, err
97+
}
98+
99+
for _, c := range candidates {
100+
var rowsAffected int64
101+
markErr := tasks.CancelableDB().Transaction(func(tx2 *gorm.DB) error {
102+
res := tx2.Model(&models.SystemInventory{}).
103+
Where("rh_account_id = ? AND id = ?", c.RhAccountID, c.ID).
104+
Update("stale", c.Expired)
105+
if res.Error != nil {
106+
return res.Error
107+
}
108+
rowsAffected = res.RowsAffected
109+
return nil
110+
})
111+
if markErr != nil {
112+
utils.LogWarn("rhAccountID", c.RhAccountID, "systemID", c.ID, "err", markErr.Error(), "Mark stale system")
113+
continue
114+
}
115+
nMarked += rowsAffected
87116
}
88117

89-
return nMarked, err
118+
return nMarked, nil
90119
}
91120

92121
func pruneDeletedSystems(tx *gorm.DB, limitDeleted int) (int64, error) {

0 commit comments

Comments
 (0)