1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
20 const GiB arvados.ByteSize = 1 << 30
22 var _ = check.Suite(&PoolSuite{})
24 type lessChecker struct {
28 func (*lessChecker) Check(params []interface{}, names []string) (result bool, error string) {
29 return params[0].(int) < params[1].(int), ""
32 var less = &lessChecker{&check.CheckerInfo{Name: "less", Params: []string{"obtained", "expected"}}}
34 type PoolSuite struct{}
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)
43 instances := pool.Instances()
44 sort.Slice(instances, func(i, j int) bool {
45 return strings.Compare(instances[i].ArvadosInstanceType, instances[j].ArvadosInstanceType) < 0
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() {
58 c.Logf("pool.Instances() == %#v", instances)
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)
72 newExecutor := func(cloud.Instance) Executor {
74 response: map[string]stubResp{
75 "crunch-run --list": {},
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:",
91 InstanceTypes: arvados.InstanceTypeMap{
98 pool := NewPool(logger, arvados.NewClientFromEnv(), prometheus.NewRegistry(), instanceSetID, is, newExecutor, nil, cluster)
99 notify := pool.Subscribe()
100 defer pool.Unsubscribe(notify)
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)
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)
117 defer pool.mtx.RUnlock()
118 for _, wkr := range pool.workers {
119 if wkr.instType == type2 {
120 return wkr.instance.Tags()[tagKey] == string(IdleBehaviorHold)
125 if time.Now().After(deadline) {
128 time.Sleep(time.Millisecond * 10)
132 c.Log("------- starting new pool, waiting to recover state")
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)
143 c.Check(inst.IdleBehavior, check.Equals, IdleBehaviorRun)
149 func (suite *PoolSuite) TestDrain(c *check.C) {
150 logger := ctxlog.TestLogger(c)
151 driver := test.StubDriver{HoldCloudOps: true}
152 instanceSet, err := driver.InstanceSet(nil, "test-instance-set-id", nil, logger)
153 c.Assert(err, check.IsNil)
155 ac := arvados.NewClientFromEnv()
157 type1 := arvados.InstanceType{Name: "a1s", ProviderType: "a1.small", VCPUs: 1, RAM: 1 * GiB, Price: .01}
161 newExecutor: func(cloud.Instance) Executor { return &stubExecutor{} },
162 instanceSet: &throttledInstanceSet{InstanceSet: instanceSet},
163 instanceTypes: arvados.InstanceTypeMap{
167 notify := pool.Subscribe()
168 defer pool.Unsubscribe(notify)
170 c.Check(pool.Unallocated()[type1], check.Equals, 0)
172 c.Check(pool.Unallocated()[type1], check.Equals, 1)
174 // Unblock the pending Create call.
175 go driver.ReleaseCloudOps(1)
177 // Wait for the instance to either return from its Create
178 // call, or show up in a poll.
179 suite.wait(c, pool, notify, func() bool {
181 defer pool.mtx.RUnlock()
182 return len(pool.workers) == 1
187 idleBehavior IdleBehavior
190 {StateIdle, IdleBehaviorHold, false},
191 {StateIdle, IdleBehaviorDrain, false},
192 {StateIdle, IdleBehaviorRun, true},
195 for _, test := range tests {
196 for _, wkr := range pool.workers {
197 if wkr.instType == type1 {
198 wkr.state = test.state
199 wkr.idleBehavior = test.idleBehavior
203 // Try to start another container
204 started := pool.StartContainer(type1, arvados.Container{UUID: "testcontainer"})
205 c.Check(started, check.Equals, test.result)
209 func (suite *PoolSuite) TestCreateUnallocShutdown(c *check.C) {
210 logger := ctxlog.TestLogger(c)
211 driver := test.StubDriver{HoldCloudOps: true}
212 instanceSet, err := driver.InstanceSet(nil, "test-instance-set-id", nil, logger)
213 c.Assert(err, check.IsNil)
215 type1 := arvados.InstanceType{Name: "a1s", ProviderType: "a1.small", VCPUs: 1, RAM: 1 * GiB, Price: .01}
216 type2 := arvados.InstanceType{Name: "a2m", ProviderType: "a2.medium", VCPUs: 2, RAM: 2 * GiB, Price: .02}
217 type3 := arvados.InstanceType{Name: "a2l", ProviderType: "a2.large", VCPUs: 4, RAM: 4 * GiB, Price: .04}
220 newExecutor: func(cloud.Instance) Executor { return &stubExecutor{} },
221 instanceSet: &throttledInstanceSet{InstanceSet: instanceSet},
222 instanceTypes: arvados.InstanceTypeMap{
228 notify := pool.Subscribe()
229 defer pool.Unsubscribe(notify)
230 notify2 := pool.Subscribe()
231 defer pool.Unsubscribe(notify2)
233 c.Check(pool.Unallocated()[type1], check.Equals, 0)
234 c.Check(pool.Unallocated()[type2], check.Equals, 0)
235 c.Check(pool.Unallocated()[type3], check.Equals, 0)
240 c.Check(pool.Unallocated()[type1], check.Equals, 1)
241 c.Check(pool.Unallocated()[type2], check.Equals, 2)
242 c.Check(pool.Unallocated()[type3], check.Equals, 1)
244 // Unblock the pending Create calls.
245 go driver.ReleaseCloudOps(4)
247 // Wait for each instance to either return from its Create
248 // call, or show up in a poll.
249 suite.wait(c, pool, notify, func() bool {
251 defer pool.mtx.RUnlock()
252 return len(pool.workers) == 4
255 // Place type3 node on admin-hold
256 ivs := suite.instancesByType(pool, type3)
257 c.Assert(ivs, check.HasLen, 1)
258 type3instanceID := ivs[0].Instance
259 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorHold)
260 c.Check(err, check.IsNil)
262 // Check admin-hold behavior: refuse to shutdown, and don't
263 // report as Unallocated ("available now or soon").
264 c.Check(pool.Shutdown(type3), check.Equals, false)
265 suite.wait(c, pool, notify, func() bool {
266 return pool.Unallocated()[type3] == 0
268 c.Check(suite.instancesByType(pool, type3), check.HasLen, 1)
270 // Shutdown both type2 nodes
271 c.Check(pool.Shutdown(type2), check.Equals, true)
272 suite.wait(c, pool, notify, func() bool {
273 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 1
275 c.Check(pool.Shutdown(type2), check.Equals, true)
276 suite.wait(c, pool, notify, func() bool {
277 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 0
279 c.Check(pool.Shutdown(type2), check.Equals, false)
281 // Consume any waiting notifications to ensure the
282 // next one we get is from Shutdown.
291 // Shutdown type1 node
292 c.Check(pool.Shutdown(type1), check.Equals, true)
293 suite.wait(c, pool, notify, func() bool {
294 return pool.Unallocated()[type1] == 0 && pool.Unallocated()[type2] == 0 && pool.Unallocated()[type3] == 0
298 case <-time.After(time.Second):
299 c.Error("notify did not receive")
302 // Put type3 node back in service.
303 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorRun)
304 c.Check(err, check.IsNil)
305 suite.wait(c, pool, notify, func() bool {
306 return pool.Unallocated()[type3] == 1
309 // Check admin-drain behavior: shut down right away, and don't
310 // report as Unallocated.
311 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorDrain)
312 c.Check(err, check.IsNil)
313 suite.wait(c, pool, notify, func() bool {
314 return pool.Unallocated()[type3] == 0
316 suite.wait(c, pool, notify, func() bool {
317 ivs := suite.instancesByType(pool, type3)
318 return len(ivs) == 1 && ivs[0].WorkerState == StateShutdown.String()
321 // Unblock all pending Destroy calls. Pool calls Destroy again
322 // if a node still appears in the provider list after a
323 // previous attempt, so there might be more than 4 Destroy
325 go driver.ReleaseCloudOps(4444)
327 // Sync until all instances disappear from the provider list.
328 suite.wait(c, pool, notify, func() bool {
329 pool.getInstancesAndSync()
330 return len(pool.Instances()) == 0
334 func (suite *PoolSuite) instancesByType(pool *Pool, it arvados.InstanceType) []InstanceView {
335 var ivs []InstanceView
336 for _, iv := range pool.Instances() {
337 if iv.ArvadosInstanceType == it.Name {
338 ivs = append(ivs, iv)
344 func (suite *PoolSuite) wait(c *check.C, pool *Pool, notify <-chan struct{}, ready func() bool) {
345 timeout := time.NewTimer(time.Second).C
354 c.Check(ready(), check.Equals, true)