Skip to content

Commit

Permalink
Parallel GetResources
Browse files Browse the repository at this point in the history
  • Loading branch information
whywaita committed Dec 24, 2021
1 parent 5f1c92f commit d8bd961
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions server/pkg/api/server_add_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package api

import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"net/url"
"sort"
"strconv"
"strings"
"sync"

"golang.org/x/sync/errgroup"

lxd "github.com/lxc/lxd/client"
"github.com/lxc/lxd/shared/api"
Expand Down Expand Up @@ -121,29 +123,50 @@ type targetHost struct {
}

func (s *ShoesLXDMultiServer) scheduleHost(targetLXDHosts []lxdclient.LXDHost) (*lxdclient.LXDHost, error) {
targets, err := getResources(targetLXDHosts)
if err != nil {
return nil, fmt.Errorf("failed to get resources: %w", err)
}

target, err := schedule(targets, s.overCommitPercent)
if err != nil {
return nil, fmt.Errorf("failed to schedule: %w", err)
}
return &(target.host), nil
}

func getResources(targetLXDHosts []lxdclient.LXDHost) ([]targetHost, error) {
var targets []targetHost

eg := errgroup.Group{}
mu := sync.Mutex{}

for _, t := range targetLXDHosts {
resources, err := lxdclient.GetResource(t.HostConfig)
if err != nil {
if errors.Is(err, lxdclient.ErrTimeoutConnectLXD) {
continue
t := t
eg.Go(func() error {
resources, err := lxdclient.GetResource(t.HostConfig)
if err != nil {
log.Printf("failed to get resource (host: %s): %+v\n", t.HostConfig.LxdHost, err)
return nil
}

return nil, fmt.Errorf("failed to get resource (host: %s): %w", t.HostConfig.LxdHost, err)
}
mu.Lock()
targets = append(targets, targetHost{
host: t,
resource: *resources,
percentOverCommit: lxdclient.GetCPUOverCommitPercent(*resources),
})
mu.Unlock()

targets = append(targets, targetHost{
host: t,
resource: *resources,
percentOverCommit: lxdclient.GetCPUOverCommitPercent(*resources),
return nil
})
}
target, err := schedule(targets, s.overCommitPercent)
if err != nil {
return nil, fmt.Errorf("failed to schedule: %w", err)

if err := eg.Wait(); err != nil {
return nil, fmt.Errorf("failed to get resources: %w", err)
}
return &(target.host), nil

return targets, nil
}

var (
Expand Down

0 comments on commit d8bd961

Please sign in to comment.