-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
73 lines (58 loc) · 1.91 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package scheduler
import (
"errors"
"github.com/geolocate-orchestration/scheduler/algorithms"
"github.com/geolocate-orchestration/scheduler/algorithms/location"
"github.com/geolocate-orchestration/scheduler/algorithms/naivelocation"
"github.com/geolocate-orchestration/scheduler/algorithms/random"
"github.com/geolocate-orchestration/scheduler/nodes"
)
// NewScheduler create a new instance of the IScheduler interface
func NewScheduler(algorithm string) (IScheduler, error) {
s := &Scheduler{}
if !algorithmExists(algorithm) {
return nil, errors.New("selected algorithm does not exist")
}
s.inodes = nodes.New()
s.algorithm = s.initAlgorithm(algorithm)
return s, nil
}
// ScheduleWorkload select a node based on used algorithm for the given workload
func (s *Scheduler) ScheduleWorkload(workload *algorithms.Workload) (*nodes.Node, error) {
return s.algorithm.GetNode(workload)
}
// AddNode adds information about a new cluster node to the algorithm
func (s *Scheduler) AddNode(node *nodes.Node) {
s.inodes.AddNode(node)
}
// UpdateNode updates information about a cluster node in the algorithm
func (s *Scheduler) UpdateNode(oldNode *nodes.Node, newNode *nodes.Node) {
s.inodes.UpdateNode(oldNode, newNode)
}
// DeleteNode deletes information about a cluster node from the algorithm
func (s *Scheduler) DeleteNode(node *nodes.Node) {
s.inodes.DeleteNode(node)
}
// Unexported
func algorithmExists(algorithmName string) bool {
for _, algorithm := range AvailableAlgorithms {
if algorithm == algorithmName {
return true
}
}
return false
}
func (s *Scheduler) initAlgorithm(algorithmName string) algorithms.Algorithm {
var algorithm algorithms.Algorithm
switch algorithmName {
case "random":
algorithm = random.New(s.inodes)
case "naivelocation":
algorithm = naivelocation.New(s.inodes)
case "location":
algorithm = location.New(s.inodes)
default:
algorithm = random.New(s.inodes)
}
return algorithm
}