1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.arvados.org/arvados.git/lib/config"
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 "git.arvados.org/arvados.git/sdk/go/arvadostest"
21 "git.arvados.org/arvados.git/sdk/go/ctxlog"
22 "github.com/prometheus/client_golang/prometheus"
26 func Test(t *testing.T) {
30 var _ = check.Suite(&suite{})
34 crTooBig arvados.ContainerRequest
35 crPending arvados.ContainerRequest
36 crCUDARequest arvados.ContainerRequest
39 func (s *suite) TearDownTest(c *check.C) {
40 arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
43 func (s *suite) SetUpTest(c *check.C) {
44 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
45 c.Assert(err, check.IsNil)
46 cluster, err := cfg.GetCluster("")
47 c.Assert(err, check.IsNil)
48 cluster.Containers.ReserveExtraRAM = 256 << 20
49 cluster.Containers.CloudVMs.PollInterval = arvados.Duration(time.Second / 4)
50 cluster.Containers.MinRetryPeriod = arvados.Duration(time.Second / 4)
51 cluster.InstanceTypes = arvados.InstanceTypeMap{
52 "biggest_available_node": arvados.InstanceType{
53 RAM: 100 << 30, // 100 GiB
55 IncludedScratch: 100 << 30,
58 s.disp = newHandler(context.Background(), cluster, arvadostest.Dispatch1Token, prometheus.NewRegistry()).(*dispatcher)
59 s.disp.lsfcli.stubCommand = func(string, ...string) *exec.Cmd {
60 return exec.Command("bash", "-c", "echo >&2 unimplemented stub; false")
62 err = arvados.NewClientFromEnv().RequestAndDecode(&s.crTooBig, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{
63 "container_request": map[string]interface{}{
64 "runtime_constraints": arvados.RuntimeConstraints{
68 "container_image": arvadostest.DockerImage112PDH,
69 "command": []string{"sleep", "1"},
70 "mounts": map[string]arvados.Mount{"/mnt/out": {Kind: "tmp", Capacity: 1000}},
71 "output_path": "/mnt/out",
72 "state": arvados.ContainerRequestStateCommitted,
74 "container_count_max": 1,
77 c.Assert(err, check.IsNil)
79 err = arvados.NewClientFromEnv().RequestAndDecode(&s.crPending, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{
80 "container_request": map[string]interface{}{
81 "runtime_constraints": arvados.RuntimeConstraints{
85 "container_image": arvadostest.DockerImage112PDH,
86 "command": []string{"sleep", "1"},
87 "mounts": map[string]arvados.Mount{"/mnt/out": {Kind: "tmp", Capacity: 1000}},
88 "output_path": "/mnt/out",
89 "state": arvados.ContainerRequestStateCommitted,
91 "container_count_max": 1,
94 c.Assert(err, check.IsNil)
96 err = arvados.NewClientFromEnv().RequestAndDecode(&s.crCUDARequest, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{
97 "container_request": map[string]interface{}{
98 "runtime_constraints": arvados.RuntimeConstraints{
101 CUDA: arvados.CUDARuntimeConstraints{
103 DriverVersion: "11.0",
104 HardwareCapability: "8.0",
107 "container_image": arvadostest.DockerImage112PDH,
108 "command": []string{"sleep", "1"},
109 "mounts": map[string]arvados.Mount{"/mnt/out": {Kind: "tmp", Capacity: 1000}},
110 "output_path": "/mnt/out",
111 "state": arvados.ContainerRequestStateCommitted,
113 "container_count_max": 1,
116 c.Assert(err, check.IsNil)
120 type lsfstub struct {
125 func (stub lsfstub) stubCommand(s *suite, c *check.C) func(prog string, args ...string) *exec.Cmd {
128 fakejobq := map[int]string{}
129 return func(prog string, args ...string) *exec.Cmd {
130 c.Logf("stubCommand: %q %q", prog, args)
131 if rand.Float64() < stub.errorRate {
132 return exec.Command("bash", "-c", "echo >&2 'stub random failure' && false")
134 if stub.sudoUser != "" && len(args) > 3 &&
138 args[2] == stub.sudoUser {
139 prog, args = args[3], args[4:]
143 defaultArgs := s.disp.Cluster.Containers.LSF.BsubArgumentsList
144 if args[5] == s.crCUDARequest.ContainerUUID {
145 c.Assert(len(args), check.Equals, len(defaultArgs)+len(s.disp.Cluster.Containers.LSF.BsubCUDAArguments))
147 c.Assert(len(args), check.Equals, len(defaultArgs))
149 // %%J must have been rewritten to %J
150 c.Check(args[1], check.Equals, "/tmp/crunch-run.%J.out")
153 case arvadostest.LockedContainerUUID:
154 c.Check(args, check.DeepEquals, []string{
155 "-J", arvadostest.LockedContainerUUID,
158 "-R", "rusage[mem=11701MB:tmp=0MB] span[hosts=1]",
159 "-R", "select[mem>=11701MB]",
160 "-R", "select[tmp>=0MB]",
161 "-R", "select[ncpus>=4]"})
163 fakejobq[nextjobid] = args[1]
166 case arvadostest.QueuedContainerUUID:
167 c.Check(args, check.DeepEquals, []string{
168 "-J", arvadostest.QueuedContainerUUID,
171 "-R", "rusage[mem=11701MB:tmp=45777MB] span[hosts=1]",
172 "-R", "select[mem>=11701MB]",
173 "-R", "select[tmp>=45777MB]",
174 "-R", "select[ncpus>=4]"})
176 fakejobq[nextjobid] = args[1]
179 case s.crPending.ContainerUUID:
180 c.Check(args, check.DeepEquals, []string{
181 "-J", s.crPending.ContainerUUID,
184 "-R", "rusage[mem=352MB:tmp=8448MB] span[hosts=1]",
185 "-R", "select[mem>=352MB]",
186 "-R", "select[tmp>=8448MB]",
187 "-R", "select[ncpus>=2]"})
189 fakejobq[nextjobid] = args[1]
192 case s.crCUDARequest.ContainerUUID:
193 c.Check(args, check.DeepEquals, []string{
194 "-J", s.crCUDARequest.ContainerUUID,
197 "-R", "rusage[mem=528MB:tmp=256MB] span[hosts=1]",
198 "-R", "select[mem>=528MB]",
199 "-R", "select[tmp>=256MB]",
200 "-R", "select[ncpus>=1]",
203 fakejobq[nextjobid] = args[1]
207 c.Errorf("unexpected uuid passed to bsub: args %q", args)
208 return exec.Command("false")
210 return exec.Command("echo", "submitted job")
212 c.Check(args, check.DeepEquals, []string{"-u", "all", "-o", "jobid stat job_name pend_reason", "-json"})
213 var records []map[string]interface{}
214 for jobid, uuid := range fakejobq {
215 stat, reason := "RUN", ""
216 if uuid == s.crPending.ContainerUUID {
217 // The real bjobs output includes a trailing ';' here:
218 stat, reason = "PEND", "There are no suitable hosts for the job;"
220 records = append(records, map[string]interface{}{
221 "JOBID": fmt.Sprintf("%d", jobid),
224 "PEND_REASON": reason,
227 out, err := json.Marshal(map[string]interface{}{
229 "JOBS": len(fakejobq),
235 c.Logf("bjobs out: %s", out)
236 return exec.Command("printf", string(out))
238 killid, _ := strconv.Atoi(args[0])
239 if uuid, ok := fakejobq[killid]; !ok {
240 return exec.Command("bash", "-c", fmt.Sprintf("printf >&2 'Job <%d>: No matching job found\n'", killid))
241 } else if uuid == "" {
242 return exec.Command("bash", "-c", fmt.Sprintf("printf >&2 'Job <%d>: Job has already finished\n'", killid))
245 time.Sleep(time.Millisecond)
247 delete(fakejobq, killid)
250 return exec.Command("bash", "-c", fmt.Sprintf("printf 'Job <%d> is being terminated\n'", killid))
253 return exec.Command("bash", "-c", fmt.Sprintf("echo >&2 'stub: command not found: %+q'", prog))
258 func (s *suite) TestSubmit(c *check.C) {
259 s.disp.lsfcli.stubCommand = lsfstub{
261 sudoUser: s.disp.Cluster.Containers.LSF.BsubSudoUser,
265 deadline := time.Now().Add(20 * time.Second)
266 for range time.NewTicker(time.Second).C {
267 if time.Now().After(deadline) {
271 // "crTooBig" should never be submitted to lsf because
272 // it is bigger than any configured instance type
273 if ent, ok := s.disp.lsfqueue.Lookup(s.crTooBig.ContainerUUID); ok {
274 c.Errorf("Lookup(crTooBig) == true, ent = %#v", ent)
277 // "queuedcontainer" should be running
278 if _, ok := s.disp.lsfqueue.Lookup(arvadostest.QueuedContainerUUID); !ok {
279 c.Log("Lookup(queuedcontainer) == false")
282 // "crPending" should be pending
283 if ent, ok := s.disp.lsfqueue.Lookup(s.crPending.ContainerUUID); !ok {
284 c.Logf("Lookup(crPending) == false", ent)
287 // "lockedcontainer" should be cancelled because it
288 // has priority 0 (no matching container requests)
289 if ent, ok := s.disp.lsfqueue.Lookup(arvadostest.LockedContainerUUID); ok {
290 c.Logf("Lookup(lockedcontainer) == true, ent = %#v", ent)
293 var ctr arvados.Container
294 if err := s.disp.arvDispatcher.Arv.Get("containers", arvadostest.LockedContainerUUID, nil, &ctr); err != nil {
295 c.Logf("error getting container state for %s: %s", arvadostest.LockedContainerUUID, err)
297 } else if ctr.State != arvados.ContainerStateQueued {
298 c.Logf("LockedContainer is not in the LSF queue but its arvados record has not been updated to state==Queued (state is %q)", ctr.State)
302 if err := s.disp.arvDispatcher.Arv.Get("containers", s.crTooBig.ContainerUUID, nil, &ctr); err != nil {
303 c.Logf("error getting container state for %s: %s", s.crTooBig.ContainerUUID, err)
305 } else if ctr.State != arvados.ContainerStateCancelled {
306 c.Logf("container %s is not in the LSF queue but its arvados record has not been updated to state==Cancelled (state is %q)", s.crTooBig.ContainerUUID, ctr.State)
309 c.Check(ctr.RuntimeStatus["error"], check.Equals, "constraints not satisfiable by any configured instance type")
311 c.Log("reached desired state")