/
strategy.go
135 lines (111 loc) · 3.7 KB
/
strategy.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2015 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scheduler
import (
"github.com/control-center/serviced/datastore"
"github.com/control-center/serviced/domain/host"
"github.com/control-center/serviced/domain/service"
"github.com/control-center/serviced/domain/servicedefinition"
"github.com/control-center/serviced/facade"
"github.com/control-center/serviced/scheduler/strategy"
zkservice "github.com/control-center/serviced/zzk/service"
"github.com/zenoss/glog"
)
// Verify we implement all the interfaces
var (
_ strategy.Host = &StrategyHost{}
_ strategy.ServiceConfig = &StrategyRunningService{}
_ strategy.ServiceConfig = &StrategyService{}
)
type StrategyHost struct {
host host.Host
services []strategy.ServiceConfig
}
type StrategyRunningService struct {
svc service.StrategyInstance
}
type StrategyService struct {
svc *zkservice.ServiceNode
}
func StrategySelectHost(sn *zkservice.ServiceNode, hosts []host.Host, strat strategy.Strategy, facade *facade.Facade) (string, error) {
glog.V(2).Infof("Applying %s strategy for service %s", strat.Name(), sn.ID)
hostmap := map[string]*StrategyHost{}
hostids := []string{}
for _, h := range hosts {
// Create a StrategyHost for the host
hostmap[h.ID] = &StrategyHost{h, []strategy.ServiceConfig{}}
// Save off the hostid
hostids = append(hostids, h.ID)
}
// Look up all running services for the hosts
glog.V(2).Infof("Looking up instances for hosts: %+v", hostids)
svcs, err := facade.GetHostStrategyInstances(datastore.Get(), hosts)
if err != nil {
return "", err
}
// Assign the services to the StrategyHosts
for _, s := range svcs {
if h, ok := hostmap[s.HostID]; ok {
h.services = append(h.services, &StrategyRunningService{*s})
}
}
shosts := []strategy.Host{}
for _, h := range hostmap {
glog.V(2).Infof("Host %s is running %d service instances", h.HostID(), len(h.services))
shosts = append(shosts, h)
}
if result, err := strat.SelectHost(&StrategyService{sn}, shosts); result == nil || err != nil {
return "", err
} else {
h := result.(*StrategyHost).host
glog.V(2).Infof("Deploying service %s to host %s", sn.ID, h.ID)
return h.ID, nil
}
}
// Implement everything
func (h *StrategyHost) HostID() string {
return h.host.ID
}
func (h *StrategyHost) RunningServices() []strategy.ServiceConfig {
return h.services
}
func (h *StrategyHost) TotalCores() int {
return h.host.Cores
}
func (h *StrategyHost) TotalMemory() uint64 {
return h.host.TotalRAM()
}
func (s *StrategyService) GetServiceID() string {
return s.svc.ID
}
func (s *StrategyService) RequestedCorePercent() int {
return s.svc.CPUCommitment
}
func (s *StrategyService) RequestedMemoryBytes() uint64 {
return s.svc.RAMCommitment.Value
}
func (s *StrategyService) HostPolicy() servicedefinition.HostPolicy {
return s.svc.HostPolicy
}
func (s *StrategyRunningService) GetServiceID() string {
return s.svc.ServiceID
}
func (s *StrategyRunningService) RequestedCorePercent() int {
return s.svc.CPUCommitment
}
func (s *StrategyRunningService) RequestedMemoryBytes() uint64 {
return s.svc.RAMCommitment
}
func (s *StrategyRunningService) HostPolicy() servicedefinition.HostPolicy {
return s.svc.HostPolicy
}