X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a08e3bb86caa758df7d33a3df3f6b8c333e47838..8680023bb3804d3bd1f2dcac7d1a86ff14053fca:/sdk/cwl/arvados_cwl/arvworkflow.py diff --git a/sdk/cwl/arvados_cwl/arvworkflow.py b/sdk/cwl/arvados_cwl/arvworkflow.py index fdf506effa..bdc2e274b0 100644 --- a/sdk/cwl/arvados_cwl/arvworkflow.py +++ b/sdk/cwl/arvados_cwl/arvworkflow.py @@ -14,21 +14,26 @@ from cwltool.load_tool import fetch_document from cwltool.process import shortname from cwltool.workflow import Workflow, WorkflowException from cwltool.pathmapper import adjustFileObjs, adjustDirObjs, visit_class +from cwltool.builder import Builder import ruamel.yaml as yaml -from .runner import upload_dependencies, packed_workflow, upload_workflow_collection, trim_anonymous_location, remove_redundant_fields -from .pathmapper import trim_listing +from .runner import (upload_dependencies, packed_workflow, upload_workflow_collection, + trim_anonymous_location, remove_redundant_fields, discover_secondary_files) +from .pathmapper import ArvPathMapper, trim_listing from .arvtool import ArvadosCommandTool from .perf import Perf logger = logging.getLogger('arvados.cwl-runner') metrics = logging.getLogger('arvados.cwl-runner.metrics') +max_res_pars = ("coresMin", "coresMax", "ramMin", "ramMax", "tmpdirMin", "tmpdirMax") +sum_res_pars = ("outdirMin", "outdirMax") + def upload_workflow(arvRunner, tool, job_order, project_uuid, uuid=None, - submit_runner_ram=0, name=None): + submit_runner_ram=0, name=None, merged_map=None): - packed = packed_workflow(arvRunner, tool) + packed = packed_workflow(arvRunner, tool, merged_map) adjustDirObjs(job_order, trim_listing) adjustFileObjs(job_order, trim_anonymous_location) @@ -70,6 +75,37 @@ def dedup_reqs(reqs): dedup[r["class"]] = r return [dedup[r] for r in sorted(dedup.keys())] +def get_overall_res_req(res_reqs): + """Take the overall of a list of ResourceRequirement, + i.e., the max of coresMin, coresMax, ramMin, ramMax, tmpdirMin, tmpdirMax + and the sum of outdirMin, outdirMax.""" + + all_res_req = {} + exception_msgs = [] + for a in max_res_pars + sum_res_pars: + all_res_req[a] = [] + for res_req in res_reqs: + if a in res_req: + if isinstance(res_req[a], int): # integer check + all_res_req[a].append(res_req[a]) + else: + msg = SourceLine(res_req, a).makeError( + "Non-top-level ResourceRequirement in single container cannot have expressions") + exception_msgs.append(msg) + if exception_msgs: + raise WorkflowException("\n".join(exception_msgs)) + else: + overall_res_req = {} + for a in all_res_req: + if all_res_req[a]: + if a in max_res_pars: + overall_res_req[a] = max(all_res_req[a]) + elif a in sum_res_pars: + overall_res_req[a] = sum(all_res_req[a]) + if overall_res_req: + overall_res_req["class"] = "ResourceRequirement" + return cmap(overall_res_req) + class ArvadosWorkflow(Workflow): """Wrap cwltool Workflow to override selected methods.""" @@ -78,6 +114,8 @@ class ArvadosWorkflow(Workflow): self.arvrunner = arvrunner self.work_api = kwargs["work_api"] self.wf_pdh = None + self.dynamic_resource_req = [] + self.static_resource_req = [] def job(self, joborder, output_callback, **kwargs): kwargs["work_api"] = self.work_api @@ -88,6 +126,8 @@ class ArvadosWorkflow(Workflow): raise WorkflowException("%s object must have 'id'" % (self.tool["class"])) document_loader, workflowobj, uri = (self.doc_loader, self.doc_loader.fetch(self.tool["id"]), self.tool["id"]) + discover_secondary_files(self.tool["inputs"], joborder) + with Perf(metrics, "subworkflow upload_deps"): upload_dependencies(self.arvrunner, os.path.basename(joborder.get("id", "#")), @@ -102,6 +142,38 @@ class ArvadosWorkflow(Workflow): packed = pack(document_loader, workflowobj, uri, self.metadata) + builder = Builder() + builder.job = joborder + builder.requirements = workflowobj["requirements"] + builder.hints = workflowobj["hints"] + builder.resources = {} + + def visit(item): + for t in ("hints", "requirements"): + if t not in item: + continue + for req in item[t]: + if req["class"] == "ResourceRequirement": + dyn = False + for k in max_res_pars + sum_res_pars: + if k in req: + if isinstance(req[k], basestring): + if item["id"] == "#main": + # only the top-level requirements/hints may contain expressions + self.dynamic_resource_req.append(req) + dyn = True + break + else: + with SourceLine(req, k, WorkflowException): + raise WorkflowException("Non-top-level ResourceRequirement in single container cannot have expressions") + if not dyn: + self.static_resource_req.append(req) + + visit_class(packed["$graph"], ("Workflow", "CommandLineTool"), visit) + + if self.static_resource_req: + self.static_resource_req = [get_overall_res_req(self.static_resource_req)] + upload_dependencies(self.arvrunner, kwargs.get("name", ""), document_loader, @@ -109,9 +181,37 @@ class ArvadosWorkflow(Workflow): uri, False) + if self.dynamic_resource_req: + builder = Builder() + builder.job = joborder + builder.requirements = self.requirements + builder.hints = self.hints + builder.resources = {} + + # Evaluate dynamic resource requirements using current builder + rs = copy.copy(self.static_resource_req) + for dyn_rs in self.dynamic_resource_req: + eval_req = {"class": "ResourceRequirement"} + for a in max_res_pars + sum_res_pars: + if a in dyn_rs: + eval_req[a] = builder.do_eval(dyn_rs[a]) + rs.append(eval_req) + job_res_reqs = [get_overall_res_req(rs)] + else: + job_res_reqs = self.static_resource_req + with Perf(metrics, "subworkflow adjust"): + joborder_resolved = copy.deepcopy(joborder) joborder_keepmount = copy.deepcopy(joborder) + reffiles = [] + visit_class(joborder_keepmount, ("File", "Directory"), lambda x: reffiles.append(x)) + + mapper = ArvPathMapper(self.arvrunner, reffiles, kwargs["basedir"], + "/keep/%s", + "/keep/%s/%s", + **kwargs) + def keepmount(obj): remove_redundant_fields(obj) with SourceLine(obj, None, WorkflowException, logger.isEnabledFor(logging.DEBUG)): @@ -119,7 +219,7 @@ class ArvadosWorkflow(Workflow): raise WorkflowException("%s object is missing required 'location' field: %s" % (obj["class"], obj)) with SourceLine(obj, "location", WorkflowException, logger.isEnabledFor(logging.DEBUG)): if obj["location"].startswith("keep:"): - obj["location"] = "/keep/" + obj["location"][5:] + obj["location"] = mapper.mapper(obj["location"]).target if "listing" in obj: del obj["listing"] elif obj["location"].startswith("_:"): @@ -129,6 +229,12 @@ class ArvadosWorkflow(Workflow): visit_class(joborder_keepmount, ("File", "Directory"), keepmount) + def resolved(obj): + if obj["location"].startswith("keep:"): + obj["location"] = mapper.mapper(obj["location"]).resolved + + visit_class(joborder_resolved, ("File", "Directory"), resolved) + if self.wf_pdh is None: adjustFileObjs(packed, keepmount) adjustDirObjs(packed, keepmount) @@ -140,7 +246,7 @@ class ArvadosWorkflow(Workflow): "inputs": self.tool["inputs"], "outputs": self.tool["outputs"], "stdout": "cwl.output.json", - "requirements": workflowobj["requirements"]+[ + "requirements": self.requirements+job_res_reqs+[ { "class": "InitialWorkDirRequirement", "listing": [{ @@ -154,11 +260,12 @@ class ArvadosWorkflow(Workflow): "entry": json.dumps(joborder_keepmount, indent=2, sort_keys=True, separators=(',',': ')).replace("\\", "\\\\").replace('$(', '\$(').replace('${', '\${') }] }], - "hints": workflowobj["hints"], - "arguments": ["--no-container", "--move-outputs", "--preserve-entire-environment", "workflow.cwl#main", "cwl.input.yml"] + "hints": self.hints, + "arguments": ["--no-container", "--move-outputs", "--preserve-entire-environment", "workflow.cwl#main", "cwl.input.yml"], + "id": "#" }) kwargs["loader"] = self.doc_loader kwargs["avsc_names"] = self.doc_schema - return ArvadosCommandTool(self.arvrunner, wf_runner, **kwargs).job(joborder, output_callback, **kwargs) + return ArvadosCommandTool(self.arvrunner, wf_runner, **kwargs).job(joborder_resolved, output_callback, **kwargs) else: return super(ArvadosWorkflow, self).job(joborder, output_callback, **kwargs)