X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6ac7e6d3d1f75cc6d6fe625d2ceaaf00b86a720d..ac0dc6c57a2b4f736b6faf62421de56a3355db04:/sdk/cwl/arvados_cwl/runner.py diff --git a/sdk/cwl/arvados_cwl/runner.py b/sdk/cwl/arvados_cwl/runner.py index 12a847503b..3235f4763c 100644 --- a/sdk/cwl/arvados_cwl/runner.py +++ b/sdk/cwl/arvados_cwl/runner.py @@ -2,31 +2,43 @@ # # SPDX-License-Identifier: Apache-2.0 +from future import standard_library +standard_library.install_aliases() +from future.utils import viewvalues, viewitems + import os -import urlparse +import sys +import re +import urllib.parse from functools import partial import logging import json -import subprocess from collections import namedtuple +from io import StringIO -from StringIO import StringIO +if os.name == "posix" and sys.version_info[0] < 3: + import subprocess32 as subprocess +else: + import subprocess from schema_salad.sourceline import SourceLine, cmap from cwltool.command_line_tool import CommandLineTool import cwltool.workflow -from cwltool.process import scandeps, UnsupportedRequirement, normalizeFilesDirs, shortname +from cwltool.process import scandeps, UnsupportedRequirement, normalizeFilesDirs, shortname, Process from cwltool.load_tool import fetch_document from cwltool.pathmapper import adjustFileObjs, adjustDirObjs, visit_class from cwltool.utils import aslist from cwltool.builder import substitute from cwltool.pack import pack +from cwltool.update import INTERNAL_VERSION +import schema_salad.validate as validate import arvados.collection +from .util import collectionUUID import ruamel.yaml as yaml -from .arvdocker import arv_docker_get_image +import arvados_cwl.arvdocker from .pathmapper import ArvPathMapper, trim_listing from ._version import __version__ from . import done @@ -61,7 +73,7 @@ def find_defaults(d, op): if "default" in d: op(d) else: - for i in d.itervalues(): + for i in viewvalues(d): find_defaults(i, op) def setSecondary(t, fileobj, discovered): @@ -79,6 +91,8 @@ def discover_secondary_files(inputs, job_order, discovered=None): if shortname(t["id"]) in job_order and t.get("secondaryFiles"): setSecondary(t, job_order[shortname(t["id"])], discovered) +collection_uuid_pattern = re.compile(r'^keep:([a-z0-9]{5}-4zz18-[a-z0-9]{15})(/.*)?$') +collection_pdh_pattern = re.compile(r'^keep:([0-9a-f]{32}\+\d+)(/.*)?') def upload_dependencies(arvrunner, name, document_loader, workflowobj, uri, loadref_run, @@ -98,7 +112,7 @@ def upload_dependencies(arvrunner, name, document_loader, loaded = set() def loadref(b, u): joined = document_loader.fetcher.urljoin(b, u) - defrg, _ = urlparse.urldefrag(joined) + defrg, _ = urllib.parse.urldefrag(joined) if defrg not in loaded: loaded.add(defrg) # Use fetch_text to get raw file (before preprocessing). @@ -128,11 +142,55 @@ def upload_dependencies(arvrunner, name, document_loader, loadref, urljoin=document_loader.fetcher.urljoin) sc = [] - def only_real(obj): - if obj.get("location", "").startswith("file:"): + uuids = {} + + def collect_uuids(obj): + loc = obj.get("location", "") + sp = loc.split(":") + if sp[0] == "keep": + # Collect collection uuids that need to be resolved to + # portable data hashes + gp = collection_uuid_pattern.match(loc) + if gp: + uuids[gp.groups()[0]] = obj + if collectionUUID in obj: + uuids[obj[collectionUUID]] = obj + + def collect_uploads(obj): + loc = obj.get("location", "") + sp = loc.split(":") + if len(sp) < 1: + return + if sp[0] in ("file", "http", "https"): + # Record local files than need to be uploaded, + # don't include file literals, keep references, etc. sc.append(obj) + collect_uuids(obj) + + visit_class(workflowobj, ("File", "Directory"), collect_uuids) + visit_class(sc_result, ("File", "Directory"), collect_uploads) + + # Resolve any collection uuids we found to portable data hashes + # and assign them to uuid_map + uuid_map = {} + fetch_uuids = list(uuids.keys()) + while fetch_uuids: + # For a large number of fetch_uuids, API server may limit + # response size, so keep fetching from API server has nothing + # more to give us. + lookups = arvrunner.api.collections().list( + filters=[["uuid", "in", fetch_uuids]], + count="none", + select=["uuid", "portable_data_hash"]).execute( + num_retries=arvrunner.num_retries) + + if not lookups["items"]: + break + + for l in lookups["items"]: + uuid_map[l["uuid"]] = l["portable_data_hash"] - visit_class(sc_result, ("File", "Directory"), only_real) + fetch_uuids = [u for u in fetch_uuids if u not in uuid_map] normalizeFilesDirs(sc) @@ -168,8 +226,13 @@ def upload_dependencies(arvrunner, name, document_loader, visit_class(workflowobj, ("CommandLineTool", "Workflow"), discover_default_secondary_files) - for d in discovered: - sc.extend(discovered[d]) + for d in list(discovered): + # Only interested in discovered secondaryFiles which are local + # files that need to be uploaded. + if d.startswith("file:"): + sc.extend(discovered[d]) + else: + del discovered[d] mapper = ArvPathMapper(arvrunner, sc, "", "keep:%s", @@ -178,8 +241,37 @@ def upload_dependencies(arvrunner, name, document_loader, single_collection=True) def setloc(p): - if "location" in p and (not p["location"].startswith("_:")) and (not p["location"].startswith("keep:")): + loc = p.get("location") + if loc and (not loc.startswith("_:")) and (not loc.startswith("keep:")): p["location"] = mapper.mapper(p["location"]).resolved + return + + if not loc: + return + + if collectionUUID in p: + uuid = p[collectionUUID] + if uuid not in uuid_map: + raise SourceLine(p, collectionUUID, validate.ValidationException).makeError( + "Collection uuid %s not found" % uuid) + gp = collection_pdh_pattern.match(loc) + if gp and uuid_map[uuid] != gp.groups()[0]: + # This file entry has both collectionUUID and a PDH + # location. If the PDH doesn't match the one returned + # the API server, raise an error. + raise SourceLine(p, "location", validate.ValidationException).makeError( + "Expected collection uuid %s to be %s but API server reported %s" % ( + uuid, gp.groups()[0], uuid_map[p[collectionUUID]])) + + gp = collection_uuid_pattern.match(loc) + if not gp: + return + uuid = gp.groups()[0] + if uuid not in uuid_map: + raise SourceLine(p, "location", validate.ValidationException).makeError( + "Collection uuid %s not found" % uuid) + p["location"] = "keep:%s%s" % (uuid_map[uuid], gp.groups()[1] if gp.groups()[1] else "") + p[collectionUUID] = uuid visit_class(workflowobj, ("File", "Directory"), setloc) visit_class(discovered, ("File", "Directory"), setloc) @@ -207,9 +299,9 @@ def upload_docker(arvrunner, tool): # TODO: can be supported by containers API, but not jobs API. raise SourceLine(docker_req, "dockerOutputDirectory", UnsupportedRequirement).makeError( "Option 'dockerOutputDirectory' of DockerRequirement not supported.") - arv_docker_get_image(arvrunner.api, docker_req, True, arvrunner.project_uuid) + arvados_cwl.arvdocker.arv_docker_get_image(arvrunner.api, docker_req, True, arvrunner.project_uuid) else: - arv_docker_get_image(arvrunner.api, {"dockerPull": "arvados/jobs"}, True, arvrunner.project_uuid) + arvados_cwl.arvdocker.arv_docker_get_image(arvrunner.api, {"dockerPull": "arvados/jobs"}, True, arvrunner.project_uuid) elif isinstance(tool, cwltool.workflow.Workflow): for s in tool.steps: upload_docker(arvrunner, s.embedded_tool) @@ -224,7 +316,7 @@ def packed_workflow(arvrunner, tool, merged_map): packed = pack(tool.doc_loader, tool.doc_loader.fetch(tool.tool["id"]), tool.tool["id"], tool.metadata, rewrite_out=rewrites) - rewrite_to_orig = {v: k for k,v in rewrites.items()} + rewrite_to_orig = {v: k for k,v in viewitems(rewrites)} def visit(v, cur_id): if isinstance(v, dict): @@ -236,6 +328,8 @@ def packed_workflow(arvrunner, tool, merged_map): v["location"] = merged_map[cur_id].resolved[v["location"]] if "location" in v and v["location"] in merged_map[cur_id].secondaryFiles: v["secondaryFiles"] = merged_map[cur_id].secondaryFiles[v["location"]] + if v.get("class") == "DockerRequirement": + v["http://arvados.org/cwl#dockerCollectionPDH"] = arvados_cwl.arvdocker.arv_docker_get_image(arvrunner.api, v, True, arvrunner.project_uuid) for l in v: visit(v[l], cur_id) if isinstance(v, list): @@ -316,10 +410,10 @@ def arvados_jobs_image(arvrunner, img): """Determine if the right arvados/jobs image version is available. If not, try to pull and upload it.""" try: - arv_docker_get_image(arvrunner.api, {"dockerPull": img}, True, arvrunner.project_uuid) + return arvados_cwl.arvdocker.arv_docker_get_image(arvrunner.api, {"dockerPull": img}, True, arvrunner.project_uuid) except Exception as e: raise Exception("Docker image %s is not available\n%s" % (img, e) ) - return img + def upload_workflow_collection(arvrunner, name, packed): collection = arvados.collection.Collection(api_client=arvrunner.api, @@ -346,23 +440,32 @@ def upload_workflow_collection(arvrunner, name, packed): return collection.portable_data_hash() -class Runner(object): +class Runner(Process): """Base class for runner processes, which submit an instance of arvados-cwl-runner and wait for the final result.""" - def __init__(self, runner, tool, job_order, enable_reuse, + def __init__(self, runner, tool, loadingContext, enable_reuse, output_name, output_tags, submit_runner_ram=0, name=None, on_error=None, submit_runner_image=None, intermediate_output_ttl=0, merged_map=None, - priority=None, secret_store=None): + priority=None, secret_store=None, + collection_cache_size=256, + collection_cache_is_default=True): + + loadingContext = loadingContext.copy() + loadingContext.metadata = loadingContext.metadata.copy() + loadingContext.metadata["cwlVersion"] = INTERNAL_VERSION + + super(Runner, self).__init__(tool.tool, loadingContext) + self.arvrunner = runner - self.tool = tool - self.job_order = job_order + self.embedded_tool = tool + self.job_order = None self.running = False if enable_reuse: # If reuse is permitted by command line arguments but # disabled by the workflow itself, disable it. - reuse_req, _ = self.tool.get_requirement("http://arvados.org/cwl#ReuseRequirement") + reuse_req, _ = self.embedded_tool.get_requirement("http://arvados.org/cwl#ReuseRequirement") if reuse_req: enable_reuse = reuse_req["enableReuse"] self.enable_reuse = enable_reuse @@ -377,16 +480,40 @@ class Runner(object): self.priority = priority self.secret_store = secret_store + self.submit_runner_cores = 1 + self.submit_runner_ram = 1024 # defaut 1 GiB + self.collection_cache_size = collection_cache_size + + runner_resource_req, _ = self.embedded_tool.get_requirement("http://arvados.org/cwl#WorkflowRunnerResources") + if runner_resource_req: + if runner_resource_req.get("coresMin"): + self.submit_runner_cores = runner_resource_req["coresMin"] + if runner_resource_req.get("ramMin"): + self.submit_runner_ram = runner_resource_req["ramMin"] + if runner_resource_req.get("keep_cache") and collection_cache_is_default: + self.collection_cache_size = runner_resource_req["keep_cache"] + if submit_runner_ram: + # Command line / initializer overrides default and/or spec from workflow self.submit_runner_ram = submit_runner_ram - else: - self.submit_runner_ram = 3000 if self.submit_runner_ram <= 0: - raise Exception("Value of --submit-runner-ram must be greater than zero") + raise Exception("Value of submit-runner-ram must be greater than zero") + + if self.submit_runner_cores <= 0: + raise Exception("Value of submit-runner-cores must be greater than zero") self.merged_map = merged_map or {} + def job(self, + job_order, # type: Mapping[Text, Text] + output_callbacks, # type: Callable[[Any, Any], Any] + runtimeContext # type: RuntimeContext + ): # type: (...) -> Generator[Any, None, None] + self.job_order = job_order + self._init_job(job_order, runtimeContext) + yield self + def update_pipeline_component(self, record): pass @@ -422,17 +549,17 @@ class Runner(object): keep_client=self.arvrunner.keep_client, num_retries=self.arvrunner.num_retries) if "cwl.output.json" in outc: - with outc.open("cwl.output.json") as f: + with outc.open("cwl.output.json", "rb") as f: if f.size() > 0: - outputs = json.load(f) + outputs = json.loads(f.read().decode()) def keepify(fileobj): path = fileobj["location"] if not path.startswith("keep:"): fileobj["location"] = "keep:%s/%s" % (record["output"], path) adjustFileObjs(outputs, keepify) adjustDirObjs(outputs, keepify) - except Exception as e: - logger.exception("[%s] While getting final output object: %s", self.name, e) + except Exception: + logger.exception("[%s] While getting final output object", self.name) self.arvrunner.output_callback({}, "permanentFail") else: self.arvrunner.output_callback(outputs, processStatus)