1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.arvados.org/arvados.git/lib/dispatchcloud/test"
13 "git.arvados.org/arvados.git/lib/dispatchcloud/worker"
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "git.arvados.org/arvados.git/sdk/go/ctxlog"
16 check "gopkg.in/check.v1"
20 // arbitrary example container UUIDs
21 uuids = func() (r []string) {
22 for i := 0; i < 16; i++ {
23 r = append(r, test.ContainerUUID(i))
29 type stubQuotaError struct {
33 func (stubQuotaError) IsQuotaError() bool { return true }
35 type stubPool struct {
36 notify <-chan struct{}
37 unalloc map[arvados.InstanceType]int // idle+booting+unknown
38 idle map[arvados.InstanceType]int
39 unknown map[arvados.InstanceType]int
40 running map[string]time.Time
43 creates []arvados.InstanceType
49 func (p *stubPool) AtQuota() bool { return p.atQuota }
50 func (p *stubPool) Subscribe() <-chan struct{} { return p.notify }
51 func (p *stubPool) Unsubscribe(<-chan struct{}) {}
52 func (p *stubPool) Running() map[string]time.Time {
55 r := map[string]time.Time{}
56 for k, v := range p.running {
61 func (p *stubPool) Unallocated() map[arvados.InstanceType]int {
64 r := map[arvados.InstanceType]int{}
65 for it, n := range p.unalloc {
66 r[it] = n - p.unknown[it]
70 func (p *stubPool) Create(it arvados.InstanceType) bool {
73 p.creates = append(p.creates, it)
81 func (p *stubPool) ForgetContainer(uuid string) {
83 func (p *stubPool) KillContainer(uuid, reason string) bool {
86 delete(p.running, uuid)
89 func (p *stubPool) Shutdown(arvados.InstanceType) bool {
93 func (p *stubPool) CountWorkers() map[worker.State]int {
96 return map[worker.State]int{
97 worker.StateBooting: len(p.unalloc) - len(p.idle),
98 worker.StateIdle: len(p.idle),
99 worker.StateRunning: len(p.running),
100 worker.StateUnknown: len(p.unknown),
103 func (p *stubPool) StartContainer(it arvados.InstanceType, ctr arvados.Container) bool {
106 p.starts = append(p.starts, ctr.UUID)
112 p.running[ctr.UUID] = time.Time{}
116 func chooseType(ctr *arvados.Container) (arvados.InstanceType, error) {
117 return test.InstanceType(ctr.RuntimeConstraints.VCPUs), nil
120 var _ = check.Suite(&SchedulerSuite{})
122 type SchedulerSuite struct{}
124 // Assign priority=4 container to idle node. Create a new instance for
125 // the priority=3 container. Don't try to start any priority<3
126 // containers because priority=3 container didn't start
127 // immediately. Don't try to create any other nodes after the failed
129 func (*SchedulerSuite) TestUseIdleWorkers(c *check.C) {
130 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
132 ChooseType: chooseType,
133 Containers: []arvados.Container{
135 UUID: test.ContainerUUID(1),
137 State: arvados.ContainerStateLocked,
138 RuntimeConstraints: arvados.RuntimeConstraints{
144 UUID: test.ContainerUUID(2),
146 State: arvados.ContainerStateLocked,
147 RuntimeConstraints: arvados.RuntimeConstraints{
153 UUID: test.ContainerUUID(3),
155 State: arvados.ContainerStateLocked,
156 RuntimeConstraints: arvados.RuntimeConstraints{
162 UUID: test.ContainerUUID(4),
164 State: arvados.ContainerStateLocked,
165 RuntimeConstraints: arvados.RuntimeConstraints{
174 unalloc: map[arvados.InstanceType]int{
175 test.InstanceType(1): 1,
176 test.InstanceType(2): 2,
178 idle: map[arvados.InstanceType]int{
179 test.InstanceType(1): 1,
180 test.InstanceType(2): 2,
182 running: map[string]time.Time{},
185 New(ctx, &queue, &pool, time.Millisecond, time.Millisecond).runQueue()
186 c.Check(pool.creates, check.DeepEquals, []arvados.InstanceType{test.InstanceType(1)})
187 c.Check(pool.starts, check.DeepEquals, []string{test.ContainerUUID(4)})
188 c.Check(pool.running, check.HasLen, 1)
189 for uuid := range pool.running {
190 c.Check(uuid, check.Equals, uuids[4])
194 // If Create() fails, shutdown some nodes, and don't call Create()
195 // again. Don't call Create() at all if AtQuota() is true.
196 func (*SchedulerSuite) TestShutdownAtQuota(c *check.C) {
197 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
198 for quota := 0; quota < 2; quota++ {
199 c.Logf("quota=%d", quota)
200 shouldCreate := []arvados.InstanceType{}
201 for i := 0; i < quota; i++ {
202 shouldCreate = append(shouldCreate, test.InstanceType(3))
205 ChooseType: chooseType,
206 Containers: []arvados.Container{
208 UUID: test.ContainerUUID(2),
210 State: arvados.ContainerStateLocked,
211 RuntimeConstraints: arvados.RuntimeConstraints{
217 UUID: test.ContainerUUID(3),
219 State: arvados.ContainerStateLocked,
220 RuntimeConstraints: arvados.RuntimeConstraints{
230 unalloc: map[arvados.InstanceType]int{
231 test.InstanceType(2): 2,
233 idle: map[arvados.InstanceType]int{
234 test.InstanceType(2): 2,
236 running: map[string]time.Time{},
237 creates: []arvados.InstanceType{},
241 New(ctx, &queue, &pool, time.Millisecond, time.Millisecond).runQueue()
242 c.Check(pool.creates, check.DeepEquals, shouldCreate)
243 c.Check(pool.starts, check.DeepEquals, []string{})
244 c.Check(pool.shutdowns, check.Not(check.Equals), 0)
248 // Start lower-priority containers while waiting for new/existing
249 // workers to come up for higher-priority containers.
250 func (*SchedulerSuite) TestStartWhileCreating(c *check.C) {
251 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
253 unalloc: map[arvados.InstanceType]int{
254 test.InstanceType(1): 2,
255 test.InstanceType(2): 2,
257 idle: map[arvados.InstanceType]int{
258 test.InstanceType(1): 1,
259 test.InstanceType(2): 1,
261 running: map[string]time.Time{},
265 ChooseType: chooseType,
266 Containers: []arvados.Container{
268 // create a new worker
269 UUID: test.ContainerUUID(1),
271 State: arvados.ContainerStateLocked,
272 RuntimeConstraints: arvados.RuntimeConstraints{
278 // tentatively map to unalloc worker
279 UUID: test.ContainerUUID(2),
281 State: arvados.ContainerStateLocked,
282 RuntimeConstraints: arvados.RuntimeConstraints{
288 // start now on idle worker
289 UUID: test.ContainerUUID(3),
291 State: arvados.ContainerStateLocked,
292 RuntimeConstraints: arvados.RuntimeConstraints{
298 // create a new worker
299 UUID: test.ContainerUUID(4),
301 State: arvados.ContainerStateLocked,
302 RuntimeConstraints: arvados.RuntimeConstraints{
308 // tentatively map to unalloc worker
309 UUID: test.ContainerUUID(5),
311 State: arvados.ContainerStateLocked,
312 RuntimeConstraints: arvados.RuntimeConstraints{
318 // start now on idle worker
319 UUID: test.ContainerUUID(6),
321 State: arvados.ContainerStateLocked,
322 RuntimeConstraints: arvados.RuntimeConstraints{
330 New(ctx, &queue, &pool, time.Millisecond, time.Millisecond).runQueue()
331 c.Check(pool.creates, check.DeepEquals, []arvados.InstanceType{test.InstanceType(2), test.InstanceType(1)})
332 c.Check(pool.starts, check.DeepEquals, []string{uuids[6], uuids[5], uuids[3], uuids[2]})
333 running := map[string]bool{}
334 for uuid, t := range pool.running {
336 running[uuid] = false
341 c.Check(running, check.DeepEquals, map[string]bool{uuids[3]: false, uuids[6]: false})
344 func (*SchedulerSuite) TestKillNonexistentContainer(c *check.C) {
345 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
347 unalloc: map[arvados.InstanceType]int{
348 test.InstanceType(2): 0,
350 idle: map[arvados.InstanceType]int{
351 test.InstanceType(2): 0,
353 running: map[string]time.Time{
354 test.ContainerUUID(2): time.Time{},
358 ChooseType: chooseType,
359 Containers: []arvados.Container{
361 // create a new worker
362 UUID: test.ContainerUUID(1),
364 State: arvados.ContainerStateLocked,
365 RuntimeConstraints: arvados.RuntimeConstraints{
373 sch := New(ctx, &queue, &pool, time.Millisecond, time.Millisecond)
374 c.Check(pool.running, check.HasLen, 1)
376 for deadline := time.Now().Add(time.Second); len(pool.Running()) > 0 && time.Now().Before(deadline); time.Sleep(time.Millisecond) {
378 c.Check(pool.Running(), check.HasLen, 0)