-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC: add job to migrate data from system_package #1316
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
package migration | ||
|
||
import ( | ||
"app/base/core" | ||
"app/base/models" | ||
"app/base/utils" | ||
"app/evaluator" | ||
"app/tasks" | ||
"encoding/json" | ||
"fmt" | ||
"sync" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
var memoryPackageCache *evaluator.PackageCache | ||
|
||
func RunSystemPackageDataMigration() { | ||
tasks.HandleContextCancel(tasks.WaitAndExit) | ||
core.ConfigureApp() | ||
packageCacheSize := utils.GetIntEnvOrDefault("PACKAGE_CACHE_SIZE", 1000000) | ||
packageNameCacheSize := utils.GetIntEnvOrDefault("PACKAGE_NAME_CACHE_SIZE", 60000) | ||
memoryPackageCache = evaluator.NewPackageCache(true, true, packageCacheSize, packageNameCacheSize) | ||
memoryPackageCache.Load() | ||
utils.LogInfo("Migrating installable/applicable advisories from system_package to system_package2") | ||
MigrateSystemPackageData() | ||
} | ||
|
||
type AccSys struct { | ||
RhAccountID int | ||
SystemID int64 | ||
} | ||
|
||
type SystemPackageRecord struct { | ||
NameID int64 | ||
PackageID int64 | ||
UpdateDataJSON json.RawMessage `gorm:"column:update_data"` | ||
} | ||
|
||
type UpdateData struct { | ||
Evra string `json:"evra" gorm:"-"` | ||
Status string `json:"status" gorm:"-"` | ||
} | ||
|
||
type Package struct { | ||
ID int64 | ||
Evra string | ||
} | ||
|
||
func MigrateSystemPackageData() { | ||
var wg sync.WaitGroup | ||
var partitions []string | ||
|
||
err := tasks.WithReadReplicaTx(func(db *gorm.DB) error { | ||
return db.Table("pg_tables"). | ||
Where("tablename ~ '^system_package_[0-9]+$'"). | ||
Pluck("tablename", &partitions).Error | ||
}) | ||
if err != nil { | ||
utils.LogError("err", err.Error(), "Couldn't get partitions for system_package") | ||
return | ||
} | ||
|
||
for i, part := range partitions { | ||
utils.LogInfo("#", i, "partition", part, "Migrating partition") | ||
accSys := getAccSys(part, i) | ||
|
||
// process at most 4 systems at once | ||
guard := make(chan struct{}, 4) | ||
|
||
for _, as := range accSys { | ||
guard <- struct{}{} | ||
wg.Add(1) | ||
go func(as AccSys, i int, part string) { | ||
defer func() { | ||
<-guard | ||
wg.Done() | ||
}() | ||
updates := getUpdates(as, part, i) | ||
for _, u := range updates { | ||
updateData := getUpdateData(u, as, part, i) | ||
latestApplicable, latestInstallable := getEvraApplicability(updateData) | ||
applicableID, installableID := getPackageIDs(u, i, latestApplicable, latestInstallable) | ||
if applicableID != 0 && installableID != 0 { | ||
// insert ids to system_package2 | ||
err = tasks.WithTx(func(db *gorm.DB) error { | ||
return db.Table("system_package2"). | ||
Where("installable_id IS NULL AND applicable_id IS NULL"). | ||
Save(models.SystemPackage{ | ||
RhAccountID: as.RhAccountID, | ||
SystemID: as.SystemID, | ||
PackageID: u.PackageID, | ||
NameID: u.NameID, | ||
InstallableID: &installableID, | ||
ApplicableID: &applicableID, | ||
}).Error | ||
}) | ||
if err != nil { | ||
utils.LogWarn("#", i, "Failed to update system_package2") | ||
} | ||
} | ||
} | ||
}(as, i, part) | ||
} | ||
wg.Wait() | ||
utils.LogInfo("#", i, "partition", part, "Partition migrated") | ||
} | ||
} | ||
|
||
func getAccSys(part string, i int) []AccSys { | ||
// get systems from system_package partition | ||
accSys := make([]AccSys, 0) | ||
err := tasks.WithReadReplicaTx(func(db *gorm.DB) error { | ||
return db.Table(part). | ||
Distinct("rh_account_id, system_id"). | ||
Order("rh_account_id"). | ||
Order("system_id"). | ||
Find(&accSys).Error | ||
}) | ||
if err != nil { | ||
utils.LogWarn("#", i, "partition", part, "Failed to load data from partition") | ||
return accSys | ||
} | ||
|
||
utils.LogInfo("#", i, "partition", part, "count", len(accSys), "Migrating systems") | ||
return accSys | ||
} | ||
|
||
func getUpdates(as AccSys, part string, i int) []SystemPackageRecord { | ||
var updates []SystemPackageRecord | ||
|
||
// get update_data from system_package for given system | ||
err := tasks.WithReadReplicaTx(func(db *gorm.DB) error { | ||
return db.Table(part). | ||
Select("name_id, package_id, update_data"). | ||
Where("rh_account_id = ?", as.RhAccountID). | ||
Where("system_id = ?", as.SystemID). | ||
Find(&updates).Error | ||
}) | ||
if err != nil { | ||
utils.LogWarn("#", i, "partition", part, "rh_account_id", as.RhAccountID, "system_id", as.SystemID, | ||
"err", err.Error(), "Couldn't get update_data") | ||
} | ||
return updates | ||
} | ||
|
||
func getUpdateData(u SystemPackageRecord, as AccSys, part string, i int) []UpdateData { | ||
var updateData []UpdateData | ||
if err := json.Unmarshal(u.UpdateDataJSON, &updateData); err != nil { | ||
utils.LogWarn("#", i, "partition", part, "rh_account_id", as.RhAccountID, "system_id", as.SystemID, | ||
"update_data", string(u.UpdateDataJSON), | ||
"err", err.Error(), "Couldn't unmarshal update_data") | ||
} | ||
return updateData | ||
} | ||
|
||
func getEvraApplicability(udpateData []UpdateData) (string, string) { | ||
// get latest applicable and installable evra | ||
var latestInstallable, latestApplicable string | ||
for i := len(udpateData) - 1; i >= 0; i-- { | ||
if len(latestInstallable) > 0 && len(latestApplicable) > 0 { | ||
break | ||
} | ||
evra := udpateData[i].Evra | ||
switch udpateData[i].Status { | ||
case "Installable": | ||
if len(latestInstallable) == 0 { | ||
latestInstallable = evra | ||
} | ||
if len(latestApplicable) == 0 { | ||
latestApplicable = evra | ||
} | ||
case "Applicable": | ||
if len(latestApplicable) == 0 { | ||
latestApplicable = evra | ||
} | ||
} | ||
} | ||
|
||
return latestApplicable, latestInstallable | ||
} | ||
|
||
func getPackageIDs(u SystemPackageRecord, i int, latestApplicable, latestInstallable string) (int64, int64) { | ||
// get package_id for latest installable and applicable packages | ||
if len(latestApplicable) == 0 && len(latestInstallable) == 0 { | ||
return 0, 0 | ||
} | ||
|
||
var applicableID, installableID int64 | ||
|
||
name, ok := memoryPackageCache.GetNameByID(u.NameID) | ||
if ok { | ||
var applicable, installable *evaluator.PackageCacheMetadata | ||
// assume both evras will be found in cache | ||
applicableInCache := true | ||
installableInCache := true | ||
|
||
if len(latestApplicable) > 0 { | ||
nevraApplicable := fmt.Sprintf("%s-%s", name, latestApplicable) | ||
applicable, applicableInCache = memoryPackageCache.GetByNevra(nevraApplicable) | ||
if applicableInCache { | ||
applicableID = applicable.ID | ||
} | ||
} | ||
|
||
if len(latestInstallable) > 0 { | ||
nevraInstallable := fmt.Sprintf("%s-%s", name, latestInstallable) | ||
installable, installableInCache = memoryPackageCache.GetByNevra(nevraInstallable) | ||
if installableInCache { | ||
installableID = installable.ID | ||
} | ||
} | ||
|
||
if applicableInCache && installableInCache { | ||
// return ids only if both evras are found in cache | ||
return applicableID, installableID | ||
} | ||
} | ||
|
||
var packages []Package | ||
err := tasks.WithReadReplicaTx(func(db *gorm.DB) error { | ||
return db.Table("package"). | ||
Select("id, evra"). | ||
Where("evra IN (?)", []string{latestApplicable, latestInstallable}). | ||
Where("name_id = ?", u.NameID). | ||
Find(&packages).Error | ||
}) | ||
if err != nil { | ||
utils.LogWarn("#", i, "Failed to load packages") | ||
} | ||
|
||
for _, p := range packages { | ||
if p.Evra == latestApplicable { | ||
applicableID = p.ID | ||
} | ||
if p.Evra == latestInstallable { | ||
installableID = p.ID | ||
} | ||
} | ||
|
||
return applicableID, installableID | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the fact that it will query same EVRAs over and over - can't we read them all into map (memory) at the start of the job?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can try to re-use the same cache as we are using in evaluator, sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I;m not sure how much memory we will need so I set the limit to 1024Mi