forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
377 lines (321 loc) · 8.37 KB
/
pool.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package worker
import (
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/dbng"
)
//go:generate counterfeiter . WorkerProvider
type WorkerProvider interface {
RunningWorkers() ([]Worker, error)
GetWorker(string) (Worker, bool, error)
FindWorkerForContainer(
logger lager.Logger,
teamID int,
handle string,
) (Worker, bool, error)
FindWorkerForResourceCheckContainer(
logger lager.Logger,
teamID int,
resourceUser dbng.ResourceUser,
resourceType string,
resourceSource atc.Source,
types atc.VersionedResourceTypes,
) (Worker, bool, error)
FindWorkerForBuildContainer(
logger lager.Logger,
teamID int,
buildID int,
planID atc.PlanID,
) (Worker, bool, error)
}
var (
ErrNoWorkers = errors.New("no workers")
ErrMissingWorker = errors.New("worker for container is missing")
)
type NoCompatibleWorkersError struct {
Spec WorkerSpec
Workers []Worker
}
func (err NoCompatibleWorkersError) Error() string {
availableWorkers := ""
for _, worker := range err.Workers {
availableWorkers += "\n - " + worker.Description()
}
return fmt.Sprintf(
"no workers satisfying: %s\n\navailable workers: %s",
err.Spec.Description(),
availableWorkers,
)
}
type pool struct {
provider WorkerProvider
rand *rand.Rand
}
func NewPool(provider WorkerProvider) Client {
return &pool{
provider: provider,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
func shuffleWorkers(slice []Worker) {
for i := range slice {
j := rand.Intn(i + 1)
slice[i], slice[j] = slice[j], slice[i]
}
}
func (pool *pool) RunningWorkers() ([]Worker, error) {
return pool.provider.RunningWorkers()
}
func (pool *pool) GetWorker(workerName string) (Worker, error) {
worker, found, err := pool.provider.GetWorker(workerName)
if err != nil {
return nil, err
}
if !found {
return nil, ErrNoWorkers
}
return worker, nil
}
func (pool *pool) AllSatisfying(spec WorkerSpec, resourceTypes atc.VersionedResourceTypes) ([]Worker, error) {
workers, err := pool.provider.RunningWorkers()
if err != nil {
return nil, err
}
if len(workers) == 0 {
return nil, ErrNoWorkers
}
compatibleTeamWorkers := []Worker{}
compatibleGeneralWorkers := []Worker{}
for _, worker := range workers {
satisfyingWorker, err := worker.Satisfying(spec, resourceTypes)
if err == nil {
if worker.IsOwnedByTeam() {
compatibleTeamWorkers = append(compatibleTeamWorkers, satisfyingWorker)
} else {
compatibleGeneralWorkers = append(compatibleGeneralWorkers, satisfyingWorker)
}
}
}
if len(compatibleTeamWorkers) != 0 {
shuffleWorkers(compatibleTeamWorkers)
return compatibleTeamWorkers, nil
}
if len(compatibleGeneralWorkers) != 0 {
shuffleWorkers(compatibleGeneralWorkers)
return compatibleGeneralWorkers, nil
}
return nil, NoCompatibleWorkersError{
Spec: spec,
Workers: workers,
}
}
func (pool *pool) Satisfying(spec WorkerSpec, resourceTypes atc.VersionedResourceTypes) (Worker, error) {
compatibleWorkers, err := pool.AllSatisfying(spec, resourceTypes)
if err != nil {
return nil, err
}
randomWorker := compatibleWorkers[pool.rand.Intn(len(compatibleWorkers))]
return randomWorker, nil
}
func (pool *pool) FindOrCreateBuildContainer(
logger lager.Logger,
signals <-chan os.Signal,
delegate ImageFetchingDelegate,
buildID int,
planID atc.PlanID,
metadata dbng.ContainerMetadata,
spec ContainerSpec,
resourceTypes atc.VersionedResourceTypes,
) (Container, error) {
worker, found, err := pool.provider.FindWorkerForBuildContainer(
logger.Session("find-worker"),
spec.TeamID, // XXX: better place for this?
buildID,
planID,
)
if err != nil {
return nil, err
}
if !found {
compatibleWorkers, err := pool.AllSatisfying(spec.WorkerSpec(), resourceTypes)
if err != nil {
return nil, err
}
workersByCount := map[int][]Worker{}
var highestCount int
for _, w := range compatibleWorkers {
candidateInputCount := 0
for _, inputSource := range spec.Inputs {
_, found, err := inputSource.Source().VolumeOn(w)
if err != nil {
return nil, err
}
if found {
candidateInputCount++
}
}
workersByCount[candidateInputCount] = append(workersByCount[candidateInputCount], w)
if candidateInputCount >= highestCount {
highestCount = candidateInputCount
}
}
workers := workersByCount[highestCount]
worker = workers[rand.Intn(len(workers))]
}
return worker.FindOrCreateBuildContainer(
logger,
signals,
delegate,
buildID,
planID,
metadata,
spec,
resourceTypes,
)
}
func (pool *pool) CreateResourceGetContainer(
logger lager.Logger,
resourceUser dbng.ResourceUser,
cancel <-chan os.Signal,
delegate ImageFetchingDelegate,
metadata dbng.ContainerMetadata,
spec ContainerSpec,
resourceTypes atc.VersionedResourceTypes,
resourceType string,
version atc.Version,
source atc.Source,
params atc.Params,
) (Container, error) {
worker, err := pool.Satisfying(spec.WorkerSpec(), resourceTypes)
if err != nil {
return nil, err
}
return worker.CreateResourceGetContainer(
logger,
resourceUser,
cancel,
delegate,
metadata,
spec,
resourceTypes,
resourceType,
version,
source,
params,
)
}
func (pool *pool) FindOrCreateResourceCheckContainer(
logger lager.Logger,
resourceUser dbng.ResourceUser,
cancel <-chan os.Signal,
delegate ImageFetchingDelegate,
metadata dbng.ContainerMetadata,
spec ContainerSpec,
resourceTypes atc.VersionedResourceTypes,
resourceType string,
source atc.Source,
) (Container, error) {
worker, found, err := pool.provider.FindWorkerForResourceCheckContainer(
logger.Session("find-worker"),
spec.TeamID, // XXX: better place for this?
resourceUser,
resourceType,
source,
resourceTypes,
)
if err != nil {
return nil, err
}
if !found {
worker, err = pool.Satisfying(spec.WorkerSpec(), resourceTypes)
if err != nil {
return nil, err
}
}
return worker.FindOrCreateResourceCheckContainer(
logger,
resourceUser,
cancel,
delegate,
metadata,
spec,
resourceTypes,
resourceType,
source,
)
}
func (pool *pool) FindContainerByHandle(logger lager.Logger, teamID int, handle string) (Container, bool, error) {
worker, found, err := pool.provider.FindWorkerForContainer(
logger.Session("find-worker"),
teamID,
handle,
)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
return worker.FindContainerByHandle(logger, teamID, handle)
}
func (*pool) FindResourceTypeByPath(string) (atc.WorkerResourceType, bool) {
return atc.WorkerResourceType{}, false
}
func (*pool) CreateVolumeForResourceCache(lager.Logger, VolumeSpec, *dbng.UsedResourceCache) (Volume, error) {
return nil, errors.New("CreateVolumeForResourceCache not implemented for pool")
}
func (*pool) FindInitializedVolumeForResourceCache(logger lager.Logger, resourceCache *dbng.UsedResourceCache) (Volume, bool, error) {
return nil, false, errors.New("FindInitializedVolumeForResourceCache not implemented for pool")
}
func (*pool) LookupVolume(lager.Logger, string) (Volume, bool, error) {
return nil, false, errors.New("LookupVolume not implemented for pool")
}
func (pool *pool) findCompatibleWorker(
containerSpec ContainerSpec,
resourceTypes atc.VersionedResourceTypes,
sources map[string]ArtifactSource,
) (Worker, []VolumeMount, []string, error) {
compatibleWorkers, err := pool.AllSatisfying(containerSpec.WorkerSpec(), resourceTypes)
if err != nil {
return nil, nil, nil, err
}
// find the worker with the most volumes
mounts := []VolumeMount{}
missingSources := []string{}
var chosenWorker Worker
// for each worker that matches tags, platform, etc -- what is the etc?
for _, w := range compatibleWorkers {
candidateMounts := []VolumeMount{}
missing := []string{}
for name, source := range sources {
// look at all the inputs/outputs we're looking for
ourVolume, found, err := source.VolumeOn(w)
if err != nil {
return nil, nil, nil, err
}
if found {
candidateMounts = append(candidateMounts, VolumeMount{
Volume: ourVolume,
MountPath: resourcesDir("put/" + name),
})
} else {
missing = append(missing, name)
}
}
if len(candidateMounts) >= len(mounts) {
mounts = candidateMounts
missingSources = missing
chosenWorker = w
}
}
return chosenWorker, mounts, missingSources, nil
}
func resourcesDir(suffix string) string {
return filepath.Join("/tmp", "build", suffix)
}