| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
|
| 6 |
import hashlib |
| 7 |
import time |
| 8 |
|
| 9 |
TARGET_CACHE_INDEX = "{cache_prefix}.cache.level-{level}.{type}.{name}.hash.{digest}" |
| 10 |
TARGET_PR_CACHE_INDEX = "{cache_prefix}.cache.pr.{type}.{name}.hash.{digest}" |
| 11 |
EXTRA_CACHE_INDEXES = [ |
| 12 |
"{cache_prefix}.cache.level-{level}.{type}.{name}.latest", |
| 13 |
"{cache_prefix}.cache.level-{level}.{type}.{name}.pushdate.{build_date_long}", |
| 14 |
] |
| 15 |
|
| 16 |
|
| 17 |
def add_optimization( |
| 18 |
config, taskdesc, cache_type, cache_name, digest=None, digest_data=None |
| 19 |
): |
| 20 |
""" |
| 21 |
Allow the results of this task to be cached. This adds index routes to the |
| 22 |
task so it can be looked up for future runs, and optimization hints so that |
| 23 |
cached artifacts can be found. Exactly one of `digest` and `digest_data` |
| 24 |
must be passed. |
| 25 |
|
| 26 |
:param TransformConfig config: The configuration for the kind being transformed. |
| 27 |
:param dict taskdesc: The description of the current task. |
| 28 |
:param str cache_type: The type of task result being cached. |
| 29 |
:param str cache_name: The name of the object being cached. |
| 30 |
:param digest: A unique string identifying this version of the artifacts |
| 31 |
being generated. Typically this will be the hash of inputs to the task. |
| 32 |
:type digest: bytes or None |
| 33 |
:param digest_data: A list of bytes representing the inputs of this task. |
| 34 |
They will be concatenated and hashed to create the digest for this |
| 35 |
task. |
| 36 |
:type digest_data: list of bytes or None |
| 37 |
""" |
| 38 |
if (digest is None) == (digest_data is None): |
| 39 |
raise Exception("Must pass exactly one of `digest` and `digest_data`.") |
| 40 |
if digest is None: |
| 41 |
digest = hashlib.sha256("\n".join(digest_data).encode("utf-8")).hexdigest() # type: ignore |
| 42 |
|
| 43 |
if "cached-task-prefix" in config.graph_config["taskgraph"]: |
| 44 |
cache_prefix = config.graph_config["taskgraph"]["cached-task-prefix"] |
| 45 |
else: |
| 46 |
cache_prefix = config.graph_config["trust-domain"] |
| 47 |
|
| 48 |
subs = { |
| 49 |
"cache_prefix": cache_prefix, |
| 50 |
"type": cache_type, |
| 51 |
"name": cache_name, |
| 52 |
"digest": digest, |
| 53 |
} |
| 54 |
|
| 55 |
# We'll try to find a cached version of the toolchain at levels above and |
| 56 |
# including the current level, starting at the highest level. |
| 57 |
index_routes = [] |
| 58 |
min_level = int(config.params["level"]) |
| 59 |
for level in reversed(range(min_level, 4)): |
| 60 |
subs["level"] = level |
| 61 |
index_routes.append(TARGET_CACHE_INDEX.format(**subs)) |
| 62 |
|
| 63 |
# Pull requests use a different target cache index route. This way we can |
| 64 |
# be confident they won't be used by anything other than the pull request |
| 65 |
# that created the cache in the first place. |
| 66 |
if config.params["tasks_for"].startswith( |
| 67 |
"github-pull-request" |
| 68 |
) and config.graph_config["taskgraph"].get("cache-pull-requests", True): |
| 69 |
subs["head_ref"] = config.params["head_ref"] |
| 70 |
if subs["head_ref"].startswith("refs/heads/"): |
| 71 |
subs["head_ref"] = subs["head_ref"][11:] |
| 72 |
index_routes.append(TARGET_PR_CACHE_INDEX.format(**subs)) |
| 73 |
|
| 74 |
taskdesc["optimization"] = {"index-search": index_routes} |
| 75 |
|
| 76 |
# ... and cache at the lowest level. |
| 77 |
subs["level"] = config.params["level"] |
| 78 |
|
| 79 |
if config.params["tasks_for"].startswith("github-pull-request"): |
| 80 |
if config.graph_config["taskgraph"].get("cache-pull-requests", True): |
| 81 |
taskdesc.setdefault("routes", []).append( |
| 82 |
f"index.{TARGET_PR_CACHE_INDEX.format(**subs)}" |
| 83 |
) |
| 84 |
else: |
| 85 |
taskdesc.setdefault("routes", []).append( |
| 86 |
f"index.{TARGET_CACHE_INDEX.format(**subs)}" |
| 87 |
) |
| 88 |
|
| 89 |
# ... and add some extra routes for humans |
| 90 |
subs["build_date_long"] = time.strftime( |
| 91 |
"%Y.%m.%d.%Y%m%d%H%M%S", time.gmtime(config.params["build_date"]) |
| 92 |
) |
| 93 |
taskdesc["routes"].extend( |
| 94 |
[f"index.{route.format(**subs)}" for route in EXTRA_CACHE_INDEXES] |
| 95 |
) |
| 96 |
|
| 97 |
taskdesc["attributes"]["cached_task"] = { |
| 98 |
"type": cache_type, |
| 99 |
"name": cache_name, |
| 100 |
"digest": digest, |
| 101 |
} |