Skip to content

Commit 18c356d

Browse files
DugowitchMichaelMraka
authored andcommitted
RHINENG-21214: use new tables for insert/update/delete directly
Views do not support these operations.
1 parent 084c91e commit 18c356d

13 files changed

Lines changed: 160 additions & 120 deletions

base/models/models.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,69 @@ func (s *SystemPlatform) GetInventoryID() string {
9292
return s.InventoryID
9393
}
9494

95+
type SystemInventory struct {
96+
ID int64 `gorm:"primaryKey"`
97+
InventoryID string `gorm:"unique"`
98+
RhAccountID int `gorm:"primaryKey"`
99+
VmaasJSON *string
100+
JSONChecksum *string
101+
LastUpdated *time.Time `gorm:"default:null"`
102+
UnchangedSince *time.Time `gorm:"default:null"`
103+
LastUpload *time.Time `gorm:"default:null"`
104+
Stale bool
105+
DisplayName string
106+
ReporterID *int
107+
YumUpdates []byte `gorm:"column:yum_updates"`
108+
YumChecksum *string `gorm:"column:yum_checksum"`
109+
SatelliteManaged bool `gorm:"column:satellite_managed"`
110+
BuiltPkgcache bool `gorm:"column:built_pkgcache"`
111+
Arch *string
112+
Bootc bool
113+
Tags []byte `gorm:"column:tags"`
114+
Created time.Time
115+
Workspaces []string
116+
StaleTimestamp time.Time
117+
StaleWarningTimestamp time.Time
118+
CulledTimestamp time.Time
119+
OSName *string
120+
OSMajor *int16
121+
OSMinor *int16
122+
RhsmVersion *string
123+
SubscriptionManagerID *string
124+
SapWorkload bool
125+
SapWorkloadSIDs []string
126+
AnsibleWorkload bool
127+
AnsibleWorkloadControllerVersion *string
128+
MssqlWorkload bool
129+
MssqlWorkloadVersion *string
130+
}
131+
132+
func (s *SystemInventory) GetInventoryID() string {
133+
if s == nil {
134+
return ""
135+
}
136+
return s.InventoryID
137+
}
138+
139+
type SystemPatch struct {
140+
SystemID int64 `gorm:"primaryKey"`
141+
RhAccountID int `gorm:"primaryKey"`
142+
LastEvaluation *time.Time `gorm:"default:null"` // TODO: trigger sets it to current time?
143+
InstallableAdvisoryCountCache int
144+
InstallableAdvisoryEnhCountCache int
145+
InstallableAdvisoryBugCountCache int
146+
InstallableAdvisorySecCountCache int
147+
PackagesInstalled int
148+
PackagesInstallable int
149+
PackagesApplicable int
150+
ThirdParty bool
151+
ApplicableAdvisoryCountCache int
152+
ApplicableAdvisoryEnhCountCache int
153+
ApplicableAdvisoryBugCountCache int
154+
ApplicableAdvisorySecCountCache int
155+
TemplateID *int64 `gorm:"column:template_id"`
156+
}
157+
95158
type String struct {
96159
ID []byte `gorm:"primaryKey"`
97160
Value string

base/notification/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type SystemTag struct {
8181
Value string `json:"value,omitempty"`
8282
}
8383

84-
func MakeNotification(system *models.SystemPlatform, systemTags []SystemTag, orgID string,
84+
func MakeNotification(system *models.SystemInventory, systemTags []SystemTag, orgID string,
8585
eventType string, events []Event) (*Notification, error) {
8686
if orgID == "" || orgID == "null" {
8787
return nil, errors.New("invalid orgID")

base/utils/vmaas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func RemoveNonLatestPackages(updates *vmaas.UpdatesV3Response) {
122122
updates.UpdateList = &updateList
123123
}
124124

125-
func ParseVmaasJSON(system *models.SystemPlatform) (vmaas.UpdatesV3Request, error) {
125+
func ParseVmaasJSON(system *models.SystemInventory) (vmaas.UpdatesV3Request, error) {
126126
var updatesReq vmaas.UpdatesV3Request
127127
err := sonic.Unmarshal([]byte(*system.VmaasJSON), &updatesReq)
128128
return updatesReq, err

evaluator/evaluate.go

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ func runEvaluate(
200200
}
201201

202202
func evaluateInDatabase(ctx context.Context, event *mqueue.PlatformEvent, inventoryID string) (
203-
*models.SystemPlatform, *vmaas.UpdatesV3Response, error) {
204-
system, err := tryGetSystem(event.AccountID, inventoryID, event.Timestamp)
203+
*models.SystemInventory, *vmaas.UpdatesV3Response, error) {
204+
system, systemPatch, err := tryGetSystem(event.AccountID, inventoryID, event.Timestamp)
205205
if err != nil {
206206
return nil, nil, errors.Wrap(err, "unable to get system")
207207
}
@@ -214,9 +214,9 @@ func evaluateInDatabase(ctx context.Context, event *mqueue.PlatformEvent, invent
214214
if err != nil {
215215
return nil, nil, errors.Wrap(err, "repo analysis failed")
216216
}
217-
system.ThirdParty = thirdParty // to set "system_platform.third_party" column
217+
systemPatch.ThirdParty = thirdParty // to set "system_platform.third_party" column
218218

219-
updatesData, err := getUpdatesData(ctx, system)
219+
updatesData, err := getUpdatesData(ctx, system, systemPatch)
220220
if err != nil {
221221
return nil, nil, errors.Wrap(err, "unable to get updates data")
222222
}
@@ -225,15 +225,15 @@ func evaluateInDatabase(ctx context.Context, event *mqueue.PlatformEvent, invent
225225
return nil, nil, nil
226226
}
227227

228-
vmaasData, err := evaluateWithVmaas(updatesData, system, event)
228+
vmaasData, err := evaluateWithVmaas(updatesData, system, systemPatch, event)
229229
if err != nil {
230230
return nil, nil, errors.Wrap(err, "evaluation with vmaas failed")
231231
}
232232

233233
return system, vmaasData, nil
234234
}
235235

236-
func tryGetYumUpdates(system *models.SystemPlatform) (*vmaas.UpdatesV3Response, error) {
236+
func tryGetYumUpdates(system *models.SystemInventory) (*vmaas.UpdatesV3Response, error) {
237237
if system.YumUpdates == nil {
238238
return nil, nil
239239
}
@@ -246,7 +246,7 @@ func tryGetYumUpdates(system *models.SystemPlatform) (*vmaas.UpdatesV3Response,
246246
updatesMap := resp.GetUpdateList()
247247
if len(updatesMap) == 0 {
248248
// TODO: do we need evaluationCnt.WithLabelValues("error-no-yum-packages").Inc()?
249-
utils.LogWarn("inventoryID", system.GetInventoryID(), "No yum_updates")
249+
utils.LogWarn("inventoryID", system.InventoryID, "No yum_updates")
250250
return nil, nil
251251
}
252252

@@ -276,19 +276,20 @@ func tryGetYumUpdates(system *models.SystemPlatform) (*vmaas.UpdatesV3Response,
276276
return &resp, nil
277277
}
278278

279-
func evaluateWithVmaas(updatesData *vmaas.UpdatesV3Response,
280-
system *models.SystemPlatform, event *mqueue.PlatformEvent) (*vmaas.UpdatesV3Response, error) {
279+
func evaluateWithVmaas(updatesData *vmaas.UpdatesV3Response, system *models.SystemInventory,
280+
systemPatch *models.SystemPatch, event *mqueue.PlatformEvent) (*vmaas.UpdatesV3Response, error) {
281281
tStart := time.Now()
282282
defer utils.ObserveSecondsSince(tStart, evaluationPartDuration.WithLabelValues("evaluate-with-vmaas-full"))
283283

284-
err := evaluateAndStore(system, updatesData, event)
284+
err := evaluateAndStore(system, systemPatch, updatesData, event)
285285
if err != nil {
286286
return nil, errors.Wrap(err, "Unable to evaluate and store results")
287287
}
288288
return updatesData, nil
289289
}
290290

291-
func getUpdatesData(ctx context.Context, system *models.SystemPlatform) (*vmaas.UpdatesV3Response, error) {
291+
func getUpdatesData(ctx context.Context, system *models.SystemInventory, systemPatch *models.SystemPatch,
292+
) (*vmaas.UpdatesV3Response, error) {
292293
tStart := time.Now()
293294
defer utils.ObserveSecondsSince(tStart, evaluationPartDuration.WithLabelValues("get-updates-data"))
294295

@@ -302,7 +303,7 @@ func getUpdatesData(ctx context.Context, system *models.SystemPlatform) (*vmaas.
302303
}
303304
}
304305

305-
vmaasData, vmaasErr := getVmaasUpdates(ctx, system)
306+
vmaasData, vmaasErr := getVmaasUpdates(ctx, system, systemPatch)
306307
if vmaasErr != nil {
307308
if errors.Is(vmaasErr, errVmaasBadRequest) {
308309
// vmaas bad request means we either created wrong vmaas request
@@ -318,7 +319,7 @@ func getUpdatesData(ctx context.Context, system *models.SystemPlatform) (*vmaas.
318319
utils.LogWarn("Vmaas response error, continuing with yum updates only", vmaasErr.Error())
319320
}
320321

321-
if system.SatelliteManaged || system.TemplateID != nil {
322+
if system.SatelliteManaged || systemPatch.TemplateID != nil {
322323
// satellite managed systems and systems using template has vmaas updates APPLICABLE instead of INSTALLABLE
323324
mergedUpdateList := vmaasData.GetUpdateList()
324325
for nevra := range mergedUpdateList {
@@ -330,7 +331,8 @@ func getUpdatesData(ctx context.Context, system *models.SystemPlatform) (*vmaas.
330331
return merged, nil
331332
}
332333

333-
func getVmaasUpdates(ctx context.Context, system *models.SystemPlatform) (*vmaas.UpdatesV3Response, error) {
334+
func getVmaasUpdates(ctx context.Context, system *models.SystemInventory, systemPatch *models.SystemPatch,
335+
) (*vmaas.UpdatesV3Response, error) {
334336
tStart := time.Now()
335337
defer utils.ObserveSecondsSince(tStart, evaluationPartDuration.WithLabelValues("vmaas-updates-prepare"))
336338

@@ -355,8 +357,8 @@ func getVmaasUpdates(ctx context.Context, system *models.SystemPlatform) (*vmaas
355357
return nil, nil
356358
}
357359

358-
updatesReq.ThirdParty = utils.PtrBool(system.ThirdParty) // enable "third_party" updates in VMaaS if needed
359-
useOptimisticUpdates := system.ThirdParty || vmaasCallUseOptimisticUpdates
360+
updatesReq.ThirdParty = utils.PtrBool(systemPatch.ThirdParty) // enable "third_party" updates in VMaaS if needed
361+
useOptimisticUpdates := systemPatch.ThirdParty || vmaasCallUseOptimisticUpdates
360362
updatesReq.OptimisticUpdates = utils.PtrBool(useOptimisticUpdates)
361363
updatesReq.EpochRequired = utils.PtrBool(true)
362364

@@ -377,10 +379,10 @@ func getVmaasUpdates(ctx context.Context, system *models.SystemPlatform) (*vmaas
377379
return vmaasData, nil
378380
}
379381

380-
func tryGetVmaasRequest(system *models.SystemPlatform) (*vmaas.UpdatesV3Request, error) {
382+
func tryGetVmaasRequest(system *models.SystemInventory) (*vmaas.UpdatesV3Request, error) {
381383
if system == nil || system.VmaasJSON == nil {
382384
evaluationCnt.WithLabelValues("error-parse-vmaas-json").Inc()
383-
utils.LogWarn("inventoryID", system.GetInventoryID(), "system with empty vmaas json")
385+
utils.LogWarn("inventoryID", system.InventoryID, "system with empty vmaas json")
384386
// skip the system
385387
// don't return error as it will cause panic of evaluator pod
386388
return nil, nil
@@ -394,44 +396,44 @@ func tryGetVmaasRequest(system *models.SystemPlatform) (*vmaas.UpdatesV3Request,
394396

395397
if len(updatesReq.PackageList) == 0 {
396398
evaluationCnt.WithLabelValues("error-no-packages").Inc()
397-
utils.LogWarn("inventoryID", system.GetInventoryID(), "Empty package list")
399+
utils.LogWarn("inventoryID", system.InventoryID, "Empty package list")
398400
return nil, nil
399401
}
400402

401403
if len(updatesReq.RepositoryList) == 0 {
402404
// system without any repositories won't have any advisories evaluated by vmaas
403405
evaluationCnt.WithLabelValues("error-no-repositories").Inc()
404-
utils.LogWarn("inventoryID", system.GetInventoryID(), "Empty repository list")
406+
utils.LogWarn("inventoryID", system.InventoryID, "Empty repository list")
405407
return nil, nil
406408
}
407409
return &updatesReq, nil
408410
}
409411

410412
func tryGetSystem(accountID int, inventoryID string,
411-
requested *types.Rfc3339Timestamp) (*models.SystemPlatform, error) {
412-
system, err := loadSystemData(accountID, inventoryID)
413+
requested *types.Rfc3339Timestamp) (*models.SystemInventory, *models.SystemPatch, error) {
414+
system, systemPatch, err := loadSystemData(accountID, inventoryID)
413415
if err != nil {
414416
evaluationCnt.WithLabelValues("error-db-read-inventory-data").Inc()
415-
return nil, base.WrapFatalDBError(err, "error loading system from DB")
417+
return nil, nil, base.WrapFatalDBError(err, "error loading system from DB")
416418
}
417419
if system.ID == 0 {
418420
evaluationCnt.WithLabelValues("error-db-read-inventory-data").Inc()
419421
utils.LogWarn("inventoryID", inventoryID, "System not found in DB")
420-
return nil, nil
422+
return nil, nil, nil
421423
}
422424

423425
if system.Stale && !enableStaleSysEval {
424426
evaluationCnt.WithLabelValues("skipping-stale").Inc()
425427
utils.LogWarn("inventoryID", inventoryID, "Skipping stale system")
426-
return nil, nil
428+
return nil, nil, nil
427429
}
428430

429-
if requested != nil && system.LastEvaluation != nil && requested.Time().Before(*system.LastEvaluation) {
431+
if requested != nil && systemPatch.LastEvaluation != nil && requested.Time().Before(*systemPatch.LastEvaluation) {
430432
evaluationCnt.WithLabelValues("skip-old-msg").Inc()
431433
utils.LogWarn("inventoryID", inventoryID, "Skipping old message")
432-
return nil, nil
434+
return nil, nil, nil
433435
}
434-
return system, nil
436+
return system, systemPatch, nil
435437
}
436438

437439
func commitWithObserve(tx *gorm.DB) error {
@@ -447,7 +449,7 @@ func commitWithObserve(tx *gorm.DB) error {
447449

448450
// EvaluateAndStore first loads advisories and packages (including change evaluation)
449451
// and then executes all deletions, updates, and insertions in a single transaction.
450-
func evaluateAndStore(system *models.SystemPlatform,
452+
func evaluateAndStore(system *models.SystemInventory, systemPatch *models.SystemPatch,
451453
vmaasData *vmaas.UpdatesV3Response, event *mqueue.PlatformEvent) error {
452454
advisoriesByName, err := lazySaveAndLoadAdvisories(system, vmaasData)
453455
if err != nil {
@@ -475,7 +477,7 @@ func evaluateAndStore(system *models.SystemPlatform,
475477
return errors.Wrap(err, "Unable to update system packages")
476478
}
477479

478-
err = updateSystemPlatform(tx, system, systemAdvisoriesNew, installed, installable, applicable)
480+
err = updateSystemPatch(tx, system, systemPatch, systemAdvisoriesNew, installed, installable, applicable)
479481
if err != nil {
480482
evaluationCnt.WithLabelValues("error-update-system").Inc()
481483
return errors.Wrap(err, "Unable to update system")
@@ -500,7 +502,7 @@ func evaluateAndStore(system *models.SystemPlatform,
500502
return nil
501503
}
502504

503-
func analyzeRepos(system *models.SystemPlatform) (thirdParty bool, err error) {
505+
func analyzeRepos(system *models.SystemInventory) (thirdParty bool, err error) {
504506
if !enableRepoAnalysis {
505507
utils.LogInfo("repo analysis disabled, skipping")
506508
return false, nil
@@ -539,15 +541,15 @@ func incrementAdvisoryTypeCounts(advisory models.AdvisoryMetadata, enhCount, bug
539541
}
540542

541543
// nolint: funlen
542-
func updateSystemPlatform(tx *gorm.DB, system *models.SystemPlatform,
544+
func updateSystemPatch(tx *gorm.DB, system *models.SystemInventory, systemPatch *models.SystemPatch,
543545
advisories SystemAdvisoryMap, installed, installable, applicable int) error {
544546
tStart := time.Now()
545547
defer utils.ObserveSecondsSince(tStart, evaluationPartDuration.WithLabelValues("system-update"))
546548
if system.LastUpload != nil {
547549
defer utils.ObserveSecondsSince(*system.LastUpload, uploadEvaluationDelay)
548550
}
549-
if system.LastEvaluation != nil {
550-
defer utils.ObserveHoursSince(*system.LastEvaluation, twoEvaluationsInterval)
551+
if systemPatch.LastEvaluation != nil {
552+
defer utils.ObserveHoursSince(*systemPatch.LastEvaluation, twoEvaluationsInterval)
551553
}
552554

553555
data := make(map[string]interface{}, 8)
@@ -592,10 +594,10 @@ func updateSystemPlatform(tx *gorm.DB, system *models.SystemPlatform,
592594
}
593595

594596
if enableRepoAnalysis {
595-
data["third_party"] = system.ThirdParty
597+
data["third_party"] = systemPatch.ThirdParty
596598
}
597599

598-
err := tx.Model(system).Updates(data).Error
600+
err := tx.Model(systemPatch).Updates(data).Error
599601

600602
now := time.Now()
601603
if system.LastUpload != nil && system.LastUpload.Sub(now) > time.Hour {
@@ -630,20 +632,29 @@ func callVMaas(ctx context.Context, request *vmaas.UpdatesV3Request) (*vmaas.Upd
630632
return vmaasDataPtr.(*vmaas.UpdatesV3Response), nil
631633
}
632634

633-
func loadSystemData(accountID int, inventoryID string) (*models.SystemPlatform, error) {
635+
func loadSystemData(accountID int, inventoryID string) (*models.SystemInventory, *models.SystemPatch, error) {
634636
tStart := time.Now()
635637
defer utils.ObserveSecondsSince(tStart, evaluationPartDuration.WithLabelValues("data-loading"))
636638

637-
var system models.SystemPlatform
639+
var system models.SystemInventory
638640
err := database.DB.Where("rh_account_id = ?", accountID).
639-
Where("inventory_id = ?::uuid", inventoryID).
641+
Where("system_id = ?", system.ID).
640642
Find(&system).Error
641-
return &system, err
643+
if err != nil {
644+
return nil, nil, err
645+
}
646+
647+
var systemPatch models.SystemPatch
648+
err = database.DB.Where("rh_account_id = ?", accountID).
649+
Where("rh_account_id = ?", system.RhAccountID).
650+
Find(&systemPatch).Error
651+
652+
return &system, &systemPatch, err
642653
}
643654

644655
func validSystem(accountID int, systemID int64) bool {
645656
var foundID int64
646-
err := database.DB.Model(models.SystemPlatform{}).
657+
err := database.DB.Model(models.SystemInventory{}).
647658
Where("rh_account_id = ? and id = ?", accountID, systemID).
648659
Select("id").
649660
Clauses(clause.Locking{Strength: "SHARE", Table: clause.Table{Name: clause.CurrentTable}}).
@@ -654,7 +665,7 @@ func validSystem(accountID int, systemID int64) bool {
654665
return systemID == foundID
655666
}
656667

657-
func parseVmaasJSON(system *models.SystemPlatform) (vmaas.UpdatesV3Request, error) {
668+
func parseVmaasJSON(system *models.SystemInventory) (vmaas.UpdatesV3Request, error) {
658669
tStart := time.Now()
659670
defer utils.ObserveSecondsSince(tStart, evaluationPartDuration.WithLabelValues("parse-vmaas-json"))
660671
return utils.ParseVmaasJSON(system)

0 commit comments

Comments
 (0)