1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.curoverse.com/arvados.git/lib/cloud"
13 "git.curoverse.com/arvados.git/lib/dispatchcloud/test"
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
15 "git.curoverse.com/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 is, err := driver.InstanceSet(nil, "", logger)
69 c.Assert(err, check.IsNil)
71 newExecutor := func(cloud.Instance) Executor {
73 "crunch-run --list": stubResp{},
78 cluster := &arvados.Cluster{
79 Dispatch: arvados.Dispatch{
80 MaxProbesPerSecond: 1000,
81 ProbeInterval: arvados.Duration(time.Millisecond * 10),
83 CloudVMs: arvados.CloudVMs{
84 BootProbeCommand: "true",
85 SyncInterval: arvados.Duration(time.Millisecond * 10),
87 InstanceTypes: arvados.InstanceTypeMap{
94 pool := NewPool(logger, arvados.NewClientFromEnv(), prometheus.NewRegistry(), is, newExecutor, nil, cluster)
95 notify := pool.Subscribe()
96 defer pool.Unsubscribe(notify)
100 waitForIdle(pool, notify)
101 var heldInstanceID cloud.InstanceID
102 for _, inst := range pool.Instances() {
103 if inst.ArvadosInstanceType == type2.Name {
104 heldInstanceID = cloud.InstanceID(inst.Instance)
105 pool.SetIdleBehavior(heldInstanceID, IdleBehaviorHold)
108 // Wait for the tags to save to the cloud provider
109 deadline := time.Now().Add(time.Second)
112 defer pool.mtx.RUnlock()
113 for _, wkr := range pool.workers {
114 if wkr.instType == type2 {
115 return wkr.instance.Tags()[tagKeyIdleBehavior] == string(IdleBehaviorHold)
120 if time.Now().After(deadline) {
123 time.Sleep(time.Millisecond * 10)
127 c.Log("------- starting new pool, waiting to recover state")
129 pool2 := NewPool(logger, arvados.NewClientFromEnv(), prometheus.NewRegistry(), is, newExecutor, nil, cluster)
130 notify2 := pool2.Subscribe()
131 defer pool2.Unsubscribe(notify2)
132 waitForIdle(pool2, notify2)
133 for _, inst := range pool2.Instances() {
134 if inst.ArvadosInstanceType == type2.Name {
135 c.Check(inst.Instance, check.Equals, heldInstanceID)
136 c.Check(inst.IdleBehavior, check.Equals, IdleBehaviorHold)
138 c.Check(inst.IdleBehavior, check.Equals, IdleBehaviorRun)
144 func (suite *PoolSuite) TestCreateUnallocShutdown(c *check.C) {
145 logger := ctxlog.TestLogger(c)
146 driver := test.StubDriver{HoldCloudOps: true}
147 instanceSet, err := driver.InstanceSet(nil, "", logger)
148 c.Assert(err, check.IsNil)
150 type1 := arvados.InstanceType{Name: "a1s", ProviderType: "a1.small", VCPUs: 1, RAM: 1 * GiB, Price: .01}
151 type2 := arvados.InstanceType{Name: "a2m", ProviderType: "a2.medium", VCPUs: 2, RAM: 2 * GiB, Price: .02}
152 type3 := arvados.InstanceType{Name: "a2l", ProviderType: "a2.large", VCPUs: 4, RAM: 4 * GiB, Price: .04}
155 newExecutor: func(cloud.Instance) Executor { return stubExecutor{} },
156 instanceSet: &throttledInstanceSet{InstanceSet: instanceSet},
157 instanceTypes: arvados.InstanceTypeMap{
163 notify := pool.Subscribe()
164 defer pool.Unsubscribe(notify)
165 notify2 := pool.Subscribe()
166 defer pool.Unsubscribe(notify2)
168 c.Check(pool.Unallocated()[type1], check.Equals, 0)
169 c.Check(pool.Unallocated()[type2], check.Equals, 0)
170 c.Check(pool.Unallocated()[type3], check.Equals, 0)
175 c.Check(pool.Unallocated()[type1], check.Equals, 1)
176 c.Check(pool.Unallocated()[type2], check.Equals, 2)
177 c.Check(pool.Unallocated()[type3], check.Equals, 1)
179 // Unblock the pending Create calls.
180 go driver.ReleaseCloudOps(4)
182 // Wait for each instance to either return from its Create
183 // call, or show up in a poll.
184 suite.wait(c, pool, notify, func() bool {
186 defer pool.mtx.RUnlock()
187 return len(pool.workers) == 4
190 // Place type3 node on admin-hold
191 ivs := suite.instancesByType(pool, type3)
192 c.Assert(ivs, check.HasLen, 1)
193 type3instanceID := ivs[0].Instance
194 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorHold)
195 c.Check(err, check.IsNil)
197 // Check admin-hold behavior: refuse to shutdown, and don't
198 // report as Unallocated ("available now or soon").
199 c.Check(pool.Shutdown(type3), check.Equals, false)
200 suite.wait(c, pool, notify, func() bool {
201 return pool.Unallocated()[type3] == 0
203 c.Check(suite.instancesByType(pool, type3), check.HasLen, 1)
205 // Shutdown both type2 nodes
206 c.Check(pool.Shutdown(type2), check.Equals, true)
207 suite.wait(c, pool, notify, func() bool {
208 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 1
210 c.Check(pool.Shutdown(type2), check.Equals, true)
211 suite.wait(c, pool, notify, func() bool {
212 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 0
214 c.Check(pool.Shutdown(type2), check.Equals, false)
216 // Consume any waiting notifications to ensure the
217 // next one we get is from Shutdown.
226 // Shutdown type1 node
227 c.Check(pool.Shutdown(type1), check.Equals, true)
228 suite.wait(c, pool, notify, func() bool {
229 return pool.Unallocated()[type1] == 0 && pool.Unallocated()[type2] == 0 && pool.Unallocated()[type3] == 0
233 case <-time.After(time.Second):
234 c.Error("notify did not receive")
237 // Put type3 node back in service.
238 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorRun)
239 c.Check(err, check.IsNil)
240 suite.wait(c, pool, notify, func() bool {
241 return pool.Unallocated()[type3] == 1
244 // Check admin-drain behavior: shut down right away, and don't
245 // report as Unallocated.
246 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorDrain)
247 c.Check(err, check.IsNil)
248 suite.wait(c, pool, notify, func() bool {
249 return pool.Unallocated()[type3] == 0
251 suite.wait(c, pool, notify, func() bool {
252 ivs := suite.instancesByType(pool, type3)
253 return len(ivs) == 1 && ivs[0].WorkerState == StateShutdown.String()
256 // Unblock all pending Destroy calls. Pool calls Destroy again
257 // if a node still appears in the provider list after a
258 // previous attempt, so there might be more than 4 Destroy
260 go driver.ReleaseCloudOps(4444)
262 // Sync until all instances disappear from the provider list.
263 suite.wait(c, pool, notify, func() bool {
264 pool.getInstancesAndSync()
265 return len(pool.Instances()) == 0
269 func (suite *PoolSuite) instancesByType(pool *Pool, it arvados.InstanceType) []InstanceView {
270 var ivs []InstanceView
271 for _, iv := range pool.Instances() {
272 if iv.ArvadosInstanceType == it.Name {
273 ivs = append(ivs, iv)
279 func (suite *PoolSuite) wait(c *check.C, pool *Pool, notify <-chan struct{}, ready func() bool) {
280 timeout := time.NewTimer(time.Second).C
289 c.Check(ready(), check.Equals, true)