Skip to content

Commit

Permalink
fix(region): clean invalid skus (#18594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Nov 7, 2023
1 parent c8b0d6f commit 20f62a1
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions pkg/compute/models/server_skus.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,30 +1032,14 @@ func (manager *SServerSkuManager) GetSkus(provider string, cpu, memMB int) ([]SS
}

// 删除表中zone not found的记录
func (manager *SServerSkuManager) PendingDeleteInvalidSku() error {
sq := ZoneManager.Query("id").Distinct().SubQuery()
skus := make([]SServerSku, 0)
q := manager.Query()
q = q.NotIn("zone_id", sq).IsNotEmpty("zone_id")
err := db.FetchModelObjects(manager, q, &skus)
if err != nil {
log.Errorln(err)
return httperrors.NewInternalServerError("query sku list failed.")
}

for i := range skus {
sku := skus[i]
_, err = db.Update(&sku, func() error {
return sku.MarkDelete()
})

if err != nil {
log.Errorln(err)
return httperrors.NewInternalServerError("delete sku %s failed.", sku.Id)
}
}

return nil
func (manager *SServerSkuManager) DeleteInvalidSkus() error {
_, err := sqlchemy.GetDB().Exec(
fmt.Sprintf(
"delete from %s where length(zone_id) > 0 and zone_id not in (select id from zones_tbl where deleted=0)",
manager.TableSpec().Name(),
),
)
return err
}

func (manager *SServerSkuManager) SyncPrivateCloudSkus(
Expand Down Expand Up @@ -1202,9 +1186,10 @@ func (region *SCloudregion) newPublicCloudSku(ctx context.Context, userCred mccl
}

if len(sku.ZoneId) > 0 {
zoneId := yunionmeta.GetZoneIdBySuffix(zoneMaps, sku.ZoneId)
if len(zoneId) > 0 {
sku.ZoneId = zoneId
zoneId := sku.ZoneId
sku.ZoneId = yunionmeta.GetZoneIdBySuffix(zoneMaps, zoneId)
if len(sku.ZoneId) == 0 {
return errors.Wrapf(cloudprovider.ErrNotFound, zoneId)
}
}

Expand Down Expand Up @@ -1600,6 +1585,10 @@ func fetchSkuSyncCloudregions() []SCloudregion {

// 全量同步sku列表.
func SyncServerSkus(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
// 清理无效的sku
log.Debugf("DeleteInvalidSkus in processing...")
err := ServerSkuManager.DeleteInvalidSkus()

if isStart {
cnt, err := ServerSkuManager.GetPublicCloudSkuCount()
if err != nil {
Expand Down Expand Up @@ -1645,9 +1634,6 @@ func SyncServerSkus(ctx context.Context, userCred mcclient.TokenCredential, isSt
log.Debugf(notes)
}

// 清理无效的sku
log.Debugf("DeleteInvalidSkus in processing...")
ServerSkuManager.PendingDeleteInvalidSku()
}

// 同步指定region sku列表
Expand Down

0 comments on commit 20f62a1

Please sign in to comment.