a85f7383ab3cdc59fcc1bd0e7ad936703666ca2f
[arvados.git] / lib / dispatchcloud / worker / pool_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package worker
6
7 import (
8         "sort"
9         "strings"
10         "time"
11
12         "git.arvados.org/arvados.git/lib/cloud"
13         "git.arvados.org/arvados.git/lib/dispatchcloud/test"
14         "git.arvados.org/arvados.git/sdk/go/arvados"
15         "git.arvados.org/arvados.git/sdk/go/ctxlog"
16         "github.com/prometheus/client_golang/prometheus"
17         check "gopkg.in/check.v1"
18 )
19
20 const GiB arvados.ByteSize = 1 << 30
21
22 var _ = check.Suite(&PoolSuite{})
23
24 type lessChecker struct {
25         *check.CheckerInfo
26 }
27
28 func (*lessChecker) Check(params []interface{}, names []string) (result bool, error string) {
29         return params[0].(int) < params[1].(int), ""
30 }
31
32 var less = &lessChecker{&check.CheckerInfo{Name: "less", Params: []string{"obtained", "expected"}}}
33
34 type PoolSuite struct{}
35
36 func (suite *PoolSuite) TestResumeAfterRestart(c *check.C) {
37         type1 := test.InstanceType(1)
38         type2 := test.InstanceType(2)
39         type3 := test.InstanceType(3)
40         waitForIdle := func(pool *Pool, notify <-chan struct{}) {
41                 timeout := time.NewTimer(time.Second)
42                 for {
43                         instances := pool.Instances()
44                         sort.Slice(instances, func(i, j int) bool {
45                                 return strings.Compare(instances[i].ArvadosInstanceType, instances[j].ArvadosInstanceType) < 0
46                         })
47                         if len(instances) == 3 &&
48                                 instances[0].ArvadosInstanceType == type1.Name &&
49                                 instances[0].WorkerState == StateIdle.String() &&
50                                 instances[1].ArvadosInstanceType == type1.Name &&
51                                 instances[1].WorkerState == StateIdle.String() &&
52                                 instances[2].ArvadosInstanceType == type2.Name &&
53                                 instances[2].WorkerState == StateIdle.String() {
54                                 return
55                         }
56                         select {
57                         case <-timeout.C:
58                                 c.Logf("pool.Instances() == %#v", instances)
59                                 c.Error("timed out")
60                                 return
61                         case <-notify:
62                         }
63                 }
64         }
65
66         logger := ctxlog.TestLogger(c)
67         driver := &test.StubDriver{}
68         instanceSetID := cloud.InstanceSetID("test-instance-set-id")
69         is, err := driver.InstanceSet(nil, instanceSetID, nil, logger)
70         c.Assert(err, check.IsNil)
71
72         newExecutor := func(cloud.Instance) Executor {
73                 return &stubExecutor{
74                         response: map[string]stubResp{
75                                 "crunch-run --list": {},
76                                 "true":              {},
77                         },
78                 }
79         }
80
81         cluster := &arvados.Cluster{
82                 Containers: arvados.ContainersConfig{
83                         CloudVMs: arvados.CloudVMsConfig{
84                                 BootProbeCommand:   "true",
85                                 MaxProbesPerSecond: 1000,
86                                 ProbeInterval:      arvados.Duration(time.Millisecond * 10),
87                                 SyncInterval:       arvados.Duration(time.Millisecond * 10),
88                                 TagKeyPrefix:       "testprefix:",
89                         },
90                 },
91                 InstanceTypes: arvados.InstanceTypeMap{
92                         type1.Name: type1,
93                         type2.Name: type2,
94                         type3.Name: type3,
95                 },
96         }
97
98         pool := NewPool(logger, arvados.NewClientFromEnv(), prometheus.NewRegistry(), instanceSetID, is, newExecutor, nil, cluster)
99         notify := pool.Subscribe()
100         defer pool.Unsubscribe(notify)
101         pool.Create(type1)
102         pool.Create(type1)
103         pool.Create(type2)
104         waitForIdle(pool, notify)
105         var heldInstanceID cloud.InstanceID
106         for _, inst := range pool.Instances() {
107                 if inst.ArvadosInstanceType == type2.Name {
108                         heldInstanceID = cloud.InstanceID(inst.Instance)
109                         pool.SetIdleBehavior(heldInstanceID, IdleBehaviorHold)
110                 }
111         }
112         // Wait for the tags to save to the cloud provider
113         tagKey := cluster.Containers.CloudVMs.TagKeyPrefix + tagKeyIdleBehavior
114         deadline := time.Now().Add(time.Second)
115         for !func() bool {
116                 pool.mtx.RLock()
117                 defer pool.mtx.RUnlock()
118                 for _, wkr := range pool.workers {
119                         if wkr.instType == type2 {
120                                 return wkr.instance.Tags()[tagKey] == string(IdleBehaviorHold)
121                         }
122                 }
123                 return false
124         }() {
125                 if time.Now().After(deadline) {
126                         c.Fatal("timeout")
127                 }
128                 time.Sleep(time.Millisecond * 10)
129         }
130         pool.Stop()
131
132         c.Log("------- starting new pool, waiting to recover state")
133
134         pool2 := NewPool(logger, arvados.NewClientFromEnv(), prometheus.NewRegistry(), instanceSetID, is, newExecutor, nil, cluster)
135         notify2 := pool2.Subscribe()
136         defer pool2.Unsubscribe(notify2)
137         waitForIdle(pool2, notify2)
138         for _, inst := range pool2.Instances() {
139                 if inst.ArvadosInstanceType == type2.Name {
140                         c.Check(inst.Instance, check.Equals, heldInstanceID)
141                         c.Check(inst.IdleBehavior, check.Equals, IdleBehaviorHold)
142                 } else {
143                         c.Check(inst.IdleBehavior, check.Equals, IdleBehaviorRun)
144                 }
145         }
146         pool2.Stop()
147 }
148
149 func (suite *PoolSuite) TestDrain(c *check.C) {
150         logger := ctxlog.TestLogger(c)
151         driver := test.StubDriver{}
152         instanceSet, err := driver.InstanceSet(nil, "test-instance-set-id", nil, logger)
153         c.Assert(err, check.IsNil)
154
155         ac := arvados.NewClientFromEnv()
156
157         type1 := test.InstanceType(1)
158         pool := &Pool{
159                 arvClient:   ac,
160                 logger:      logger,
161                 newExecutor: func(cloud.Instance) Executor { return &stubExecutor{} },
162                 instanceSet: &throttledInstanceSet{InstanceSet: instanceSet},
163                 instanceTypes: arvados.InstanceTypeMap{
164                         type1.Name: type1,
165                 },
166         }
167         notify := pool.Subscribe()
168         defer pool.Unsubscribe(notify)
169
170         pool.Create(type1)
171
172         // Wait for the instance to either return from its Create
173         // call, or show up in a poll.
174         suite.wait(c, pool, notify, func() bool {
175                 pool.mtx.RLock()
176                 defer pool.mtx.RUnlock()
177                 return len(pool.workers) == 1
178         })
179
180         tests := []struct {
181                 state        State
182                 idleBehavior IdleBehavior
183                 result       bool
184         }{
185                 {StateIdle, IdleBehaviorHold, false},
186                 {StateIdle, IdleBehaviorDrain, false},
187                 {StateIdle, IdleBehaviorRun, true},
188         }
189
190         for _, test := range tests {
191                 for _, wkr := range pool.workers {
192                         wkr.state = test.state
193                         wkr.idleBehavior = test.idleBehavior
194                 }
195
196                 // Try to start a container
197                 started := pool.StartContainer(type1, arvados.Container{UUID: "testcontainer"})
198                 c.Check(started, check.Equals, test.result)
199         }
200 }
201
202 func (suite *PoolSuite) TestNodeCreateThrottle(c *check.C) {
203         logger := ctxlog.TestLogger(c)
204         driver := test.StubDriver{HoldCloudOps: true}
205         instanceSet, err := driver.InstanceSet(nil, "test-instance-set-id", nil, logger)
206         c.Assert(err, check.IsNil)
207
208         type1 := test.InstanceType(1)
209         pool := &Pool{
210                 logger:                         logger,
211                 instanceSet:                    &throttledInstanceSet{InstanceSet: instanceSet},
212                 maxConcurrentInstanceCreateOps: 1,
213                 instanceTypes: arvados.InstanceTypeMap{
214                         type1.Name: type1,
215                 },
216         }
217
218         c.Check(pool.Unallocated()[type1], check.Equals, 0)
219         res := pool.Create(type1)
220         c.Check(pool.Unallocated()[type1], check.Equals, 1)
221         c.Check(res, check.Equals, true)
222
223         res = pool.Create(type1)
224         c.Check(pool.Unallocated()[type1], check.Equals, 1)
225         c.Check(res, check.Equals, false)
226
227         pool.instanceSet.throttleCreate.err = nil
228         pool.maxConcurrentInstanceCreateOps = 2
229
230         res = pool.Create(type1)
231         c.Check(pool.Unallocated()[type1], check.Equals, 2)
232         c.Check(res, check.Equals, true)
233
234         pool.instanceSet.throttleCreate.err = nil
235         pool.maxConcurrentInstanceCreateOps = 0
236
237         res = pool.Create(type1)
238         c.Check(pool.Unallocated()[type1], check.Equals, 3)
239         c.Check(res, check.Equals, true)
240 }
241
242 func (suite *PoolSuite) TestCreateUnallocShutdown(c *check.C) {
243         logger := ctxlog.TestLogger(c)
244         driver := test.StubDriver{HoldCloudOps: true}
245         instanceSet, err := driver.InstanceSet(nil, "test-instance-set-id", nil, logger)
246         c.Assert(err, check.IsNil)
247
248         type1 := arvados.InstanceType{Name: "a1s", ProviderType: "a1.small", VCPUs: 1, RAM: 1 * GiB, Price: .01}
249         type2 := arvados.InstanceType{Name: "a2m", ProviderType: "a2.medium", VCPUs: 2, RAM: 2 * GiB, Price: .02}
250         type3 := arvados.InstanceType{Name: "a2l", ProviderType: "a2.large", VCPUs: 4, RAM: 4 * GiB, Price: .04}
251         pool := &Pool{
252                 logger:      logger,
253                 newExecutor: func(cloud.Instance) Executor { return &stubExecutor{} },
254                 instanceSet: &throttledInstanceSet{InstanceSet: instanceSet},
255                 instanceTypes: arvados.InstanceTypeMap{
256                         type1.Name: type1,
257                         type2.Name: type2,
258                         type3.Name: type3,
259                 },
260         }
261         notify := pool.Subscribe()
262         defer pool.Unsubscribe(notify)
263         notify2 := pool.Subscribe()
264         defer pool.Unsubscribe(notify2)
265
266         c.Check(pool.Unallocated()[type1], check.Equals, 0)
267         c.Check(pool.Unallocated()[type2], check.Equals, 0)
268         c.Check(pool.Unallocated()[type3], check.Equals, 0)
269         pool.Create(type2)
270         pool.Create(type1)
271         pool.Create(type2)
272         pool.Create(type3)
273         c.Check(pool.Unallocated()[type1], check.Equals, 1)
274         c.Check(pool.Unallocated()[type2], check.Equals, 2)
275         c.Check(pool.Unallocated()[type3], check.Equals, 1)
276
277         // Unblock the pending Create calls.
278         go driver.ReleaseCloudOps(4)
279
280         // Wait for each instance to either return from its Create
281         // call, or show up in a poll.
282         suite.wait(c, pool, notify, func() bool {
283                 pool.mtx.RLock()
284                 defer pool.mtx.RUnlock()
285                 return len(pool.workers) == 4
286         })
287
288         // Place type3 node on admin-hold
289         ivs := suite.instancesByType(pool, type3)
290         c.Assert(ivs, check.HasLen, 1)
291         type3instanceID := ivs[0].Instance
292         err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorHold)
293         c.Check(err, check.IsNil)
294
295         // Check admin-hold behavior: refuse to shutdown, and don't
296         // report as Unallocated ("available now or soon").
297         c.Check(pool.Shutdown(type3), check.Equals, false)
298         suite.wait(c, pool, notify, func() bool {
299                 return pool.Unallocated()[type3] == 0
300         })
301         c.Check(suite.instancesByType(pool, type3), check.HasLen, 1)
302
303         // Shutdown both type2 nodes
304         c.Check(pool.Shutdown(type2), check.Equals, true)
305         suite.wait(c, pool, notify, func() bool {
306                 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 1
307         })
308         c.Check(pool.Shutdown(type2), check.Equals, true)
309         suite.wait(c, pool, notify, func() bool {
310                 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 0
311         })
312         c.Check(pool.Shutdown(type2), check.Equals, false)
313         for {
314                 // Consume any waiting notifications to ensure the
315                 // next one we get is from Shutdown.
316                 select {
317                 case <-notify:
318                         continue
319                 default:
320                 }
321                 break
322         }
323
324         // Shutdown type1 node
325         c.Check(pool.Shutdown(type1), check.Equals, true)
326         suite.wait(c, pool, notify, func() bool {
327                 return pool.Unallocated()[type1] == 0 && pool.Unallocated()[type2] == 0 && pool.Unallocated()[type3] == 0
328         })
329         select {
330         case <-notify2:
331         case <-time.After(time.Second):
332                 c.Error("notify did not receive")
333         }
334
335         // Put type3 node back in service.
336         err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorRun)
337         c.Check(err, check.IsNil)
338         suite.wait(c, pool, notify, func() bool {
339                 return pool.Unallocated()[type3] == 1
340         })
341
342         // Check admin-drain behavior: shut down right away, and don't
343         // report as Unallocated.
344         err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorDrain)
345         c.Check(err, check.IsNil)
346         suite.wait(c, pool, notify, func() bool {
347                 return pool.Unallocated()[type3] == 0
348         })
349         suite.wait(c, pool, notify, func() bool {
350                 ivs := suite.instancesByType(pool, type3)
351                 return len(ivs) == 1 && ivs[0].WorkerState == StateShutdown.String()
352         })
353
354         // Unblock all pending Destroy calls. Pool calls Destroy again
355         // if a node still appears in the provider list after a
356         // previous attempt, so there might be more than 4 Destroy
357         // calls to unblock.
358         go driver.ReleaseCloudOps(4444)
359
360         // Sync until all instances disappear from the provider list.
361         suite.wait(c, pool, notify, func() bool {
362                 pool.getInstancesAndSync()
363                 return len(pool.Instances()) == 0
364         })
365 }
366
367 func (suite *PoolSuite) instancesByType(pool *Pool, it arvados.InstanceType) []InstanceView {
368         var ivs []InstanceView
369         for _, iv := range pool.Instances() {
370                 if iv.ArvadosInstanceType == it.Name {
371                         ivs = append(ivs, iv)
372                 }
373         }
374         return ivs
375 }
376
377 func (suite *PoolSuite) wait(c *check.C, pool *Pool, notify <-chan struct{}, ready func() bool) {
378         timeout := time.NewTimer(time.Second).C
379         for !ready() {
380                 select {
381                 case <-notify:
382                         continue
383                 case <-timeout:
384                 }
385                 break
386         }
387         c.Check(ready(), check.Equals, true)
388 }