forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partitions.go
216 lines (168 loc) · 4.63 KB
/
partitions.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package engine
import (
"sort"
"sync"
"github.com/coreos/fleet/job"
)
const (
partitionSize = 5
)
type namedCount struct {
name string
count int
}
type namedCounts []*namedCount
func (a namedCounts) Len() int { return len(a) }
func (a namedCounts) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a namedCounts) Less(i, j int) bool { return a[i].count < a[j].count }
type cluster struct {
upToDate bool
mutex sync.Mutex
// some statistics about what goes on in the cluster
// this will get more sophisticated as needed
// where are jobs running
jobsToMachines map[string]string
// how many jobs on a machine
// each active machine has at least one job,
// otherwise it won't show up here
machineJobCount map[string]int
}
func newCluster() *cluster {
return &cluster{
jobsToMachines: make(map[string]string),
machineJobCount: make(map[string]int),
}
}
func (clust *cluster) machineCreated(machID string) {
clust.mutex.Lock()
defer clust.mutex.Unlock()
if !clust.upToDate {
return
}
clust.populateMachine(machID)
}
func (clust *cluster) machineRemoved(machID string) {
clust.mutex.Lock()
defer clust.mutex.Unlock()
if !clust.upToDate {
return
}
clust.deleteMachine(machID)
}
// jobScheduled handles the job scheduled event
func (clust *cluster) jobScheduled(jobName, target string) {
clust.mutex.Lock()
defer clust.mutex.Unlock()
if !clust.upToDate {
return
}
clust.populateJob(jobName, target)
}
// jobStopped handles the job stopped event
func (clust *cluster) jobStopped(jobName string) {
clust.mutex.Lock()
defer clust.mutex.Unlock()
if !clust.upToDate {
return
}
clust.deleteJob(jobName)
}
// isUpToDate returns whether the cluster has been told what happened before it got created
func (clust *cluster) isUpToDate() bool {
clust.mutex.Lock()
defer clust.mutex.Unlock()
return clust.upToDate
}
// refreshFrom refreshes from a specified cluster
func (clust *cluster) refreshFrom(cu *cluster, force bool) {
clust.mutex.Lock()
defer clust.mutex.Unlock()
if !force && clust.upToDate {
return
}
clust.jobsToMachines = cu.jobsToMachines
clust.machineJobCount = cu.machineJobCount
clust.upToDate = true
}
func (clust *cluster) populateJob(jobName string, machineID string) {
clust.jobsToMachines[jobName] = machineID
clust.machineJobCount[machineID] = clust.machineJobCount[machineID] + 1
}
func (clust *cluster) deleteJob(jobName string) {
machineID, ok := clust.jobsToMachines[jobName]
// TODO(uwedeportivo): this might be a signal that a refresh is needed
if !ok {
return
}
clust.machineJobCount[machineID] = clust.machineJobCount[machineID] - 1
delete(clust.jobsToMachines, jobName)
}
func (clust *cluster) populateMachine(machID string) {
clust.machineJobCount[machID] = 1
}
func (clust *cluster) deleteMachine(machID string) {
delete(clust.machineJobCount, machID)
}
func (clust *cluster) kLeastLoaded(k int) []string {
clust.mutex.Lock()
defer clust.mutex.Unlock()
mas := make(namedCounts, len(clust.machineJobCount))
cursor := 0
for k, v := range clust.machineJobCount {
mas[cursor] = &namedCount{k, v}
cursor++
}
sort.Sort(mas)
l := k
if l > len(mas) {
l = len(mas)
}
mbis := make([]string, l)
for i := 0; i < l; i++ {
mbis[i] = mas[i].name
}
return mbis
}
func (eg *Engine) refreshCluster(force bool) {
if !force && eg.clust.isUpToDate() {
return
}
cu := newCluster()
ms, _ := eg.registry.GetActiveMachines()
for _, m := range ms {
cu.populateMachine(m.ID)
}
jobs, _ := eg.registry.GetAllJobs()
for _, j := range jobs {
tgt, _ := eg.registry.GetJobTarget(j.Name)
if tgt != "" {
cu.populateJob(j.Name, tgt)
}
}
eg.clust.refreshFrom(cu, force)
}
// partitionCluster returns a slice of IDs from a subset of active machines
// that should be considered for scheduling the specified job.
// The returned slice is sorted by ascending lexicographical string value of machine boot id.
func (eg *Engine) partitionCluster(j *job.Job) ([]string, error) {
if machID, ok := j.RequiredTarget(); ok {
return []string{machID}, nil
}
// TODO(uwedeportivo): for now punt on jobs with requirements and offer to all machines
// because agents are decoding the requirements
if len(j.Requirements()) > 0 {
machines, _ := eg.registry.GetActiveMachines()
machineIDs := make([]string, len(machines))
for i, mach := range machines {
machineIDs[i] = mach.ID
}
sort.Strings(machineIDs)
return machineIDs, nil
}
// this is usually a cheap no-op
eg.refreshCluster(false)
// as an initial heuristic, choose the k least loaded, with k = partitionSize
machineIDs := eg.clust.kLeastLoaded(partitionSize)
sort.Strings(machineIDs)
return machineIDs, nil
}