1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.curoverse.com/arvados.git/lib/dispatchcloud/test"
13 "git.curoverse.com/arvados.git/lib/dispatchcloud/worker"
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "git.curoverse.com/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 running map[string]time.Time
42 creates []arvados.InstanceType
48 func (p *stubPool) AtQuota() bool { return p.atQuota }
49 func (p *stubPool) Subscribe() <-chan struct{} { return p.notify }
50 func (p *stubPool) Unsubscribe(<-chan struct{}) {}
51 func (p *stubPool) Running() map[string]time.Time {
54 r := map[string]time.Time{}
55 for k, v := range p.running {
60 func (p *stubPool) Unallocated() map[arvados.InstanceType]int {
63 r := map[arvados.InstanceType]int{}
64 for it, n := range p.unalloc {
69 func (p *stubPool) Create(it arvados.InstanceType) bool {
72 p.creates = append(p.creates, it)
80 func (p *stubPool) KillContainer(uuid, reason string) {
83 delete(p.running, uuid)
85 func (p *stubPool) Shutdown(arvados.InstanceType) bool {
89 func (p *stubPool) CountWorkers() map[worker.State]int {
92 return map[worker.State]int{
93 worker.StateBooting: len(p.unalloc) - len(p.idle),
94 worker.StateIdle: len(p.idle),
95 worker.StateRunning: len(p.running),
98 func (p *stubPool) StartContainer(it arvados.InstanceType, ctr arvados.Container) bool {
101 p.starts = append(p.starts, ctr.UUID)
107 p.running[ctr.UUID] = time.Time{}
111 func chooseType(ctr *arvados.Container) (arvados.InstanceType, error) {
112 return test.InstanceType(ctr.RuntimeConstraints.VCPUs), nil
115 var _ = check.Suite(&SchedulerSuite{})
117 type SchedulerSuite struct{}
119 // Assign priority=4 container to idle node. Create a new instance for
120 // the priority=3 container. Don't try to start any priority<3
121 // containers because priority=3 container didn't start
122 // immediately. Don't try to create any other nodes after the failed
124 func (*SchedulerSuite) TestUseIdleWorkers(c *check.C) {
125 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
127 ChooseType: chooseType,
128 Containers: []arvados.Container{
130 UUID: test.ContainerUUID(1),
132 State: arvados.ContainerStateLocked,
133 RuntimeConstraints: arvados.RuntimeConstraints{
139 UUID: test.ContainerUUID(2),
141 State: arvados.ContainerStateLocked,
142 RuntimeConstraints: arvados.RuntimeConstraints{
148 UUID: test.ContainerUUID(3),
150 State: arvados.ContainerStateLocked,
151 RuntimeConstraints: arvados.RuntimeConstraints{
157 UUID: test.ContainerUUID(4),
159 State: arvados.ContainerStateLocked,
160 RuntimeConstraints: arvados.RuntimeConstraints{
169 unalloc: map[arvados.InstanceType]int{
170 test.InstanceType(1): 1,
171 test.InstanceType(2): 2,
173 idle: map[arvados.InstanceType]int{
174 test.InstanceType(1): 1,
175 test.InstanceType(2): 2,
177 running: map[string]time.Time{},
180 New(ctx, &queue, &pool, time.Millisecond, time.Millisecond).runQueue()
181 c.Check(pool.creates, check.DeepEquals, []arvados.InstanceType{test.InstanceType(1)})
182 c.Check(pool.starts, check.DeepEquals, []string{test.ContainerUUID(4)})
183 c.Check(pool.running, check.HasLen, 1)
184 for uuid := range pool.running {
185 c.Check(uuid, check.Equals, uuids[4])
189 // If Create() fails, shutdown some nodes, and don't call Create()
190 // again. Don't call Create() at all if AtQuota() is true.
191 func (*SchedulerSuite) TestShutdownAtQuota(c *check.C) {
192 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
193 for quota := 0; quota < 2; quota++ {
194 c.Logf("quota=%d", quota)
195 shouldCreate := []arvados.InstanceType{}
196 for i := 0; i < quota; i++ {
197 shouldCreate = append(shouldCreate, test.InstanceType(3))
200 ChooseType: chooseType,
201 Containers: []arvados.Container{
203 UUID: test.ContainerUUID(2),
205 State: arvados.ContainerStateLocked,
206 RuntimeConstraints: arvados.RuntimeConstraints{
212 UUID: test.ContainerUUID(3),
214 State: arvados.ContainerStateLocked,
215 RuntimeConstraints: arvados.RuntimeConstraints{
225 unalloc: map[arvados.InstanceType]int{
226 test.InstanceType(2): 2,
228 idle: map[arvados.InstanceType]int{
229 test.InstanceType(2): 2,
231 running: map[string]time.Time{},
232 creates: []arvados.InstanceType{},
236 New(ctx, &queue, &pool, time.Millisecond, time.Millisecond).runQueue()
237 c.Check(pool.creates, check.DeepEquals, shouldCreate)
238 c.Check(pool.starts, check.DeepEquals, []string{})
239 c.Check(pool.shutdowns, check.Not(check.Equals), 0)
243 // Start lower-priority containers while waiting for new/existing
244 // workers to come up for higher-priority containers.
245 func (*SchedulerSuite) TestStartWhileCreating(c *check.C) {
246 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
248 unalloc: map[arvados.InstanceType]int{
249 test.InstanceType(1): 2,
250 test.InstanceType(2): 2,
252 idle: map[arvados.InstanceType]int{
253 test.InstanceType(1): 1,
254 test.InstanceType(2): 1,
256 running: map[string]time.Time{},
260 ChooseType: chooseType,
261 Containers: []arvados.Container{
263 // create a new worker
264 UUID: test.ContainerUUID(1),
266 State: arvados.ContainerStateLocked,
267 RuntimeConstraints: arvados.RuntimeConstraints{
273 // tentatively map to unalloc worker
274 UUID: test.ContainerUUID(2),
276 State: arvados.ContainerStateLocked,
277 RuntimeConstraints: arvados.RuntimeConstraints{
283 // start now on idle worker
284 UUID: test.ContainerUUID(3),
286 State: arvados.ContainerStateLocked,
287 RuntimeConstraints: arvados.RuntimeConstraints{
293 // create a new worker
294 UUID: test.ContainerUUID(4),
296 State: arvados.ContainerStateLocked,
297 RuntimeConstraints: arvados.RuntimeConstraints{
303 // tentatively map to unalloc worker
304 UUID: test.ContainerUUID(5),
306 State: arvados.ContainerStateLocked,
307 RuntimeConstraints: arvados.RuntimeConstraints{
313 // start now on idle worker
314 UUID: test.ContainerUUID(6),
316 State: arvados.ContainerStateLocked,
317 RuntimeConstraints: arvados.RuntimeConstraints{
325 New(ctx, &queue, &pool, time.Millisecond, time.Millisecond).runQueue()
326 c.Check(pool.creates, check.DeepEquals, []arvados.InstanceType{test.InstanceType(2), test.InstanceType(1)})
327 c.Check(pool.starts, check.DeepEquals, []string{uuids[6], uuids[5], uuids[3], uuids[2]})
328 running := map[string]bool{}
329 for uuid, t := range pool.running {
331 running[uuid] = false
336 c.Check(running, check.DeepEquals, map[string]bool{uuids[3]: false, uuids[6]: false})
339 func (*SchedulerSuite) TestKillNonexistentContainer(c *check.C) {
340 ctx := ctxlog.Context(context.Background(), ctxlog.TestLogger(c))
342 unalloc: map[arvados.InstanceType]int{
343 test.InstanceType(2): 0,
345 idle: map[arvados.InstanceType]int{
346 test.InstanceType(2): 0,
348 running: map[string]time.Time{
349 test.ContainerUUID(2): time.Time{},
353 ChooseType: chooseType,
354 Containers: []arvados.Container{
356 // create a new worker
357 UUID: test.ContainerUUID(1),
359 State: arvados.ContainerStateLocked,
360 RuntimeConstraints: arvados.RuntimeConstraints{
368 sch := New(ctx, &queue, &pool, time.Millisecond, time.Millisecond)
369 c.Check(pool.running, check.HasLen, 1)
371 for deadline := time.Now().Add(time.Second); len(pool.Running()) > 0 && time.Now().Before(deadline); time.Sleep(time.Millisecond) {
373 c.Check(pool.Running(), check.HasLen, 0)