Skip to content

Commit

Permalink
fix(scheduler): optimize get guests by hosts ids
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed Jul 1, 2024
1 parent 6366d35 commit 9fa4aee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pkg/scheduler/cache/candidate/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package candidate

import (
"yunion.io/x/log"
"yunion.io/x/pkg/util/sets"
"yunion.io/x/sqlchemy"

computeapi "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
Expand Down Expand Up @@ -53,9 +55,12 @@ func GetHostIds(hosts []models.SHost) []string {
return ids
}

func FetchGuestByHostIDs(ids []string) ([]models.SGuest, error) {
func FetchGuestByHostIDsQuery(idsQuery sqlchemy.IQuery) ([]models.SGuest, error) {
gs := make([]models.SGuest, 0)
q := models.GuestManager.Query().In("host_id", ids)

q := models.GuestManager.Query()
hostIdsSubq := idsQuery.SubQuery()
q = q.Join(hostIdsSubq, sqlchemy.Equals(q.Field("host_id"), hostIdsSubq.Field("id")))
err := db.FetchModelObjects(models.GuestManager, q, &gs)
return gs, err
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/scheduler/cache/candidate/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ func (b *HostBuilder) FetchHosts(ids []string) ([]computemodels.SHost, error) {
}

func (b *HostBuilder) setGuests(hosts []computemodels.SHost, errMessageChannel chan error) {
ids := GetHostIds(hosts)
guests, err := FetchGuestByHostIDs(ids)
idsQuery := b.AllIDsQuery()
guests, err := FetchGuestByHostIDsQuery(idsQuery)
if err != nil {
errMessageChannel <- err
return
Expand Down Expand Up @@ -681,6 +681,12 @@ func (b *HostBuilder) AllIDs() ([]string, error) {
return FetchModelIds(q)
}

func (b *HostBuilder) AllIDsQuery() sqlchemy.IQuery {
q := computemodels.HostManager.Query("id")
q = q.Filter(sqlchemy.NotEquals(q.Field("host_type"), computeapi.HOST_TYPE_BAREMETAL))
return q
}

func (b *HostBuilder) InitFuncs() []InitFunc {
return []InitFunc{
// b.setSchedtags,
Expand Down

0 comments on commit 9fa4aee

Please sign in to comment.