Skip to content
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

Expedite vm recommend filtering speed #1813

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/core/infra/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strconv"
"strings"
"sync"
"time"
"unicode"

"github.com/cloud-barista/cb-tumblebug/src/core/common"
Expand Down Expand Up @@ -110,15 +111,23 @@ func RecommendVm(nsId string, plan model.DeploymentPlan) ([]model.TbSpecInfo, er
// Filtering
log.Debug().Msg("[Filtering specs]")

startTime := time.Now()
filteredSpecs, err := resource.FilterSpecsByRange(nsId, *u)

if err != nil {
log.Error().Err(err).Msg("")
return []model.TbSpecInfo{}, err
}
elapsedTime := time.Since(startTime)
log.Info().
Int("filteredItemCount", len(filteredSpecs)).
Dur("elapsedTime", elapsedTime).
Msg("Filtering complete")

if len(filteredSpecs) == 0 {
return []model.TbSpecInfo{}, nil
}

// // sorting based on VCPU and MemoryGiB
// sort.Slice(filteredSpecs, func(i, j int) bool {
// // sort based on VCPU first
Expand All @@ -133,6 +142,7 @@ func RecommendVm(nsId string, plan model.DeploymentPlan) ([]model.TbSpecInfo, er
log.Debug().Msg("[Prioritizing specs]")
prioritySpecs := []model.TbSpecInfo{}

startTime = time.Now()
for _, v := range plan.Priority.Policy {
metric := v.Metric

Expand All @@ -156,6 +166,11 @@ func RecommendVm(nsId string, plan model.DeploymentPlan) ([]model.TbSpecInfo, er
prioritySpecs, err = RecommendVmCost(nsId, &filteredSpecs)
}

elapsedTime = time.Since(startTime)
log.Info().
Dur("elapsedTime", elapsedTime).
Msg("Sorting complete")

// limit the number of items in result list
result := []model.TbSpecInfo{}
limitNum, err := strconv.Atoi(plan.Limit)
Expand Down
7 changes: 7 additions & 0 deletions src/core/resource/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,20 @@ func FilterSpecsByRange(nsId string, filter model.FilterSpecsByRangeRequest) ([]
}
}

startTime := time.Now()

var specs []model.TbSpecInfo
err := session.Find(&specs)
if err != nil {
log.Error().Err(err).Msg("Failed to execute query")
return nil, err
}

elapsedTime := time.Since(startTime)
log.Info().
Dur("elapsedTime", elapsedTime).
Msg("ORM:session.Find(&specs)")

return specs, nil
}

Expand Down
36 changes: 36 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func init() {
log.Info().Msg("Table customImage set successfully..")
}

err = addIndexes()
if err != nil {
log.Error().Err(err).Msg("Cannot add indexes to the tables (ORM)")
}

setConfig()

_, err = common.GetNs(model.DefaultNamespace)
Expand Down Expand Up @@ -363,6 +368,37 @@ func setConfig() {

}

// addIndexes adds indexes to the tables for faster search
func addIndexes() error {

_, err := model.ORM.Exec("CREATE INDEX IF NOT EXISTS idx_namespace ON tb_spec_info (Namespace)")
if err != nil {
return err
}

_, err = model.ORM.Exec("CREATE INDEX IF NOT EXISTS idx_vcpu ON tb_spec_info (VCPU)")
if err != nil {
return err
}

_, err = model.ORM.Exec("CREATE INDEX IF NOT EXISTS idx_memorygib ON tb_spec_info (MemoryGiB)")
if err != nil {
return err
}

_, err = model.ORM.Exec("CREATE INDEX IF NOT EXISTS idx_cspspecname ON tb_spec_info (CspSpecName)")
if err != nil {
return err
}

_, err = model.ORM.Exec("CREATE INDEX IF NOT EXISTS idx_costperhour ON tb_spec_info (CostPerHour)")
if err != nil {
return err
}

return nil
}

// Main Body

// @title CB-Tumblebug REST API
Expand Down