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)
110 c.Log("------- starting new pool, waiting to recover state")
112 pool2 := NewPool(logger, arvados.NewClientFromEnv(), prometheus.NewRegistry(), is, newExecutor, nil, cluster)
113 notify2 := pool2.Subscribe()
114 defer pool2.Unsubscribe(notify2)
115 waitForIdle(pool2, notify2)
116 for _, inst := range pool2.Instances() {
117 if inst.ArvadosInstanceType == type2.Name {
118 c.Check(inst.Instance, check.Equals, heldInstanceID)
119 c.Check(inst.IdleBehavior, check.Equals, IdleBehaviorHold)
121 c.Check(inst.IdleBehavior, check.Equals, IdleBehaviorRun)
127 func (suite *PoolSuite) TestCreateUnallocShutdown(c *check.C) {
128 logger := ctxlog.TestLogger(c)
129 driver := test.StubDriver{HoldCloudOps: true}
130 instanceSet, err := driver.InstanceSet(nil, "", logger)
131 c.Assert(err, check.IsNil)
133 type1 := arvados.InstanceType{Name: "a1s", ProviderType: "a1.small", VCPUs: 1, RAM: 1 * GiB, Price: .01}
134 type2 := arvados.InstanceType{Name: "a2m", ProviderType: "a2.medium", VCPUs: 2, RAM: 2 * GiB, Price: .02}
135 type3 := arvados.InstanceType{Name: "a2l", ProviderType: "a2.large", VCPUs: 4, RAM: 4 * GiB, Price: .04}
138 newExecutor: func(cloud.Instance) Executor { return stubExecutor{} },
139 instanceSet: &throttledInstanceSet{InstanceSet: instanceSet},
140 instanceTypes: arvados.InstanceTypeMap{
146 notify := pool.Subscribe()
147 defer pool.Unsubscribe(notify)
148 notify2 := pool.Subscribe()
149 defer pool.Unsubscribe(notify2)
151 c.Check(pool.Unallocated()[type1], check.Equals, 0)
152 c.Check(pool.Unallocated()[type2], check.Equals, 0)
153 c.Check(pool.Unallocated()[type3], check.Equals, 0)
158 c.Check(pool.Unallocated()[type1], check.Equals, 1)
159 c.Check(pool.Unallocated()[type2], check.Equals, 2)
160 c.Check(pool.Unallocated()[type3], check.Equals, 1)
162 // Unblock the pending Create calls.
163 go driver.ReleaseCloudOps(4)
165 // Wait for each instance to either return from its Create
166 // call, or show up in a poll.
167 suite.wait(c, pool, notify, func() bool {
169 defer pool.mtx.RUnlock()
170 return len(pool.workers) == 4
173 // Place type3 node on admin-hold
174 ivs := suite.instancesByType(pool, type3)
175 c.Assert(ivs, check.HasLen, 1)
176 type3instanceID := ivs[0].Instance
177 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorHold)
178 c.Check(err, check.IsNil)
180 // Check admin-hold behavior: refuse to shutdown, and don't
181 // report as Unallocated ("available now or soon").
182 c.Check(pool.Shutdown(type3), check.Equals, false)
183 suite.wait(c, pool, notify, func() bool {
184 return pool.Unallocated()[type3] == 0
186 c.Check(suite.instancesByType(pool, type3), check.HasLen, 1)
188 // Shutdown both type2 nodes
189 c.Check(pool.Shutdown(type2), check.Equals, true)
190 suite.wait(c, pool, notify, func() bool {
191 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 1
193 c.Check(pool.Shutdown(type2), check.Equals, true)
194 suite.wait(c, pool, notify, func() bool {
195 return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 0
197 c.Check(pool.Shutdown(type2), check.Equals, false)
199 // Consume any waiting notifications to ensure the
200 // next one we get is from Shutdown.
209 // Shutdown type1 node
210 c.Check(pool.Shutdown(type1), check.Equals, true)
211 suite.wait(c, pool, notify, func() bool {
212 return pool.Unallocated()[type1] == 0 && pool.Unallocated()[type2] == 0 && pool.Unallocated()[type3] == 0
216 case <-time.After(time.Second):
217 c.Error("notify did not receive")
220 // Put type3 node back in service.
221 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorRun)
222 c.Check(err, check.IsNil)
223 suite.wait(c, pool, notify, func() bool {
224 return pool.Unallocated()[type3] == 1
227 // Check admin-drain behavior: shut down right away, and don't
228 // report as Unallocated.
229 err = pool.SetIdleBehavior(type3instanceID, IdleBehaviorDrain)
230 c.Check(err, check.IsNil)
231 suite.wait(c, pool, notify, func() bool {
232 return pool.Unallocated()[type3] == 0
234 suite.wait(c, pool, notify, func() bool {
235 ivs := suite.instancesByType(pool, type3)
236 return len(ivs) == 1 && ivs[0].WorkerState == StateShutdown.String()
239 // Unblock all pending Destroy calls. Pool calls Destroy again
240 // if a node still appears in the provider list after a
241 // previous attempt, so there might be more than 4 Destroy
243 go driver.ReleaseCloudOps(4444)
245 // Sync until all instances disappear from the provider list.
246 suite.wait(c, pool, notify, func() bool {
247 pool.getInstancesAndSync()
248 return len(pool.Instances()) == 0
252 func (suite *PoolSuite) instancesByType(pool *Pool, it arvados.InstanceType) []InstanceView {
253 var ivs []InstanceView
254 for _, iv := range pool.Instances() {
255 if iv.ArvadosInstanceType == it.Name {
256 ivs = append(ivs, iv)
262 func (suite *PoolSuite) wait(c *check.C, pool *Pool, notify <-chan struct{}, ready func() bool) {
263 timeout := time.NewTimer(time.Second).C
272 c.Check(ready(), check.Equals, true)