2019-09-11 21:17:02 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import argparse
|
2019-10-18 03:23:53 +03:00
|
|
|
import json
|
2019-09-11 21:17:02 +03:00
|
|
|
import os
|
2020-02-15 22:16:21 +03:00
|
|
|
import time
|
2020-02-24 16:38:15 +03:00
|
|
|
import traceback
|
2019-09-18 14:10:22 +03:00
|
|
|
from datetime import datetime
|
2019-09-11 21:17:02 +03:00
|
|
|
from logging import INFO, basicConfig, getLogger
|
|
|
|
|
2020-02-24 16:38:15 +03:00
|
|
|
import adr
|
2019-09-18 14:10:22 +03:00
|
|
|
import dateutil.parser
|
2020-02-24 16:38:15 +03:00
|
|
|
import mozci
|
2019-09-18 14:10:22 +03:00
|
|
|
from dateutil.relativedelta import relativedelta
|
2019-09-19 02:32:01 +03:00
|
|
|
from tqdm import tqdm
|
2019-09-11 21:17:02 +03:00
|
|
|
|
2019-11-10 21:57:05 +03:00
|
|
|
from bugbug import commit_features, db, repository, test_scheduling
|
2019-11-18 17:26:03 +03:00
|
|
|
from bugbug.utils import (
|
|
|
|
download_check_etag,
|
|
|
|
open_tar_zst,
|
|
|
|
zstd_compress,
|
|
|
|
zstd_decompress,
|
|
|
|
)
|
2019-09-11 21:17:02 +03:00
|
|
|
|
|
|
|
basicConfig(level=INFO)
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2019-09-24 23:42:44 +03:00
|
|
|
JOBS_TO_CONSIDER = ("test-", "build-")
|
2019-11-05 14:31:04 +03:00
|
|
|
JOBS_TO_IGNORE = ("build-docker-image-",)
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2019-11-07 14:16:52 +03:00
|
|
|
ADR_CACHE_DB = "data/adr_cache.tar"
|
|
|
|
db.register(
|
|
|
|
ADR_CACHE_DB,
|
2020-02-16 04:50:51 +03:00
|
|
|
"https://s3-us-west-2.amazonaws.com/communitytc-bugbug/data/adr_cache.tar.zst",
|
2020-02-11 18:30:39 +03:00
|
|
|
3,
|
2019-11-07 14:16:52 +03:00
|
|
|
)
|
2020-02-19 15:15:03 +03:00
|
|
|
PUSH_DATA_URL = "https://community-tc.services.mozilla.com/api/index/v1/task/project.relman.bugbug.data_test_scheduling_history_push_data.latest/artifacts/public/push_data_{granularity}.json.zst"
|
2019-09-11 21:17:02 +03:00
|
|
|
|
2019-09-24 23:32:16 +03:00
|
|
|
TRAINING_MONTHS = 6
|
|
|
|
|
2019-09-11 21:17:02 +03:00
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
def filter_runnables(runnables, all_runnables, granularity):
|
2019-11-10 21:57:05 +03:00
|
|
|
return tuple(
|
2020-02-19 15:15:03 +03:00
|
|
|
runnable
|
|
|
|
for runnable in runnables
|
|
|
|
if runnable in all_runnables
|
|
|
|
and (
|
|
|
|
granularity == "group"
|
|
|
|
or (
|
|
|
|
any(runnable.startswith(j) for j in JOBS_TO_CONSIDER)
|
|
|
|
and not any(runnable.startswith(j) for j in JOBS_TO_IGNORE)
|
|
|
|
)
|
|
|
|
)
|
2019-11-10 21:57:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-04 22:44:11 +03:00
|
|
|
# Handle "meaningless" labeling changes ("meaningless" as they shouldn't really affect test scheduling).
|
|
|
|
def rename_tasks(tasks):
|
|
|
|
return [task.replace("test-linux64-", "test-linux1804-64-") for task in tasks]
|
|
|
|
|
|
|
|
|
2019-09-11 21:17:02 +03:00
|
|
|
class Retriever(object):
|
2019-10-18 15:08:08 +03:00
|
|
|
def __init__(self):
|
2019-09-18 14:10:22 +03:00
|
|
|
os.makedirs("data", exist_ok=True)
|
2020-02-15 22:16:21 +03:00
|
|
|
self.cache_path = os.path.splitext(ADR_CACHE_DB)[0]
|
2019-09-11 21:17:02 +03:00
|
|
|
|
2020-02-24 16:38:15 +03:00
|
|
|
def generate_push_data(self, runnable, from_months):
|
2020-02-15 22:16:21 +03:00
|
|
|
def upload_adr_cache():
|
|
|
|
with open_tar_zst(f"{ADR_CACHE_DB}.zst") as tar:
|
|
|
|
tar.add(self.cache_path)
|
|
|
|
|
|
|
|
db.upload(ADR_CACHE_DB)
|
|
|
|
|
2020-02-24 16:38:15 +03:00
|
|
|
pushes = mozci.push.make_push_objects(
|
|
|
|
from_date=f"today-{from_months}month",
|
|
|
|
to_date="today-3day",
|
|
|
|
branch="autoland",
|
2019-09-11 21:17:02 +03:00
|
|
|
)
|
|
|
|
|
2020-02-24 16:38:15 +03:00
|
|
|
start_time = time.monotonic()
|
|
|
|
|
|
|
|
num_cached = 0
|
|
|
|
|
|
|
|
push_data = []
|
|
|
|
|
|
|
|
for push in tqdm(pushes):
|
|
|
|
key = f"push_data.{runnable}.{push.rev}"
|
|
|
|
|
|
|
|
logger.info(f"Analyzing {push.rev} at the {runnable} level...")
|
|
|
|
|
|
|
|
if adr.config.cache.has(key):
|
|
|
|
num_cached += 1
|
|
|
|
push_data.append(adr.config.cache.get(key))
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if runnable == "label":
|
|
|
|
runnables = push.task_labels
|
|
|
|
elif runnable == "group":
|
|
|
|
runnables = push.group_summaries.keys()
|
|
|
|
|
|
|
|
value = [
|
|
|
|
push.revs,
|
|
|
|
list(runnables),
|
|
|
|
list(push.get_possible_regressions(runnable)),
|
|
|
|
list(push.get_likely_regressions(runnable)),
|
|
|
|
]
|
|
|
|
push_data.append(value)
|
|
|
|
adr.config.cache.forever(key, value)
|
|
|
|
except adr.errors.MissingDataError:
|
|
|
|
logger.warning(
|
|
|
|
f"Tasks for push {push.rev} can't be found on ActiveData"
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
if time.monotonic() - start_time >= 3600:
|
2020-02-15 22:16:21 +03:00
|
|
|
upload_adr_cache()
|
2020-02-24 16:38:15 +03:00
|
|
|
start_time = time.monotonic()
|
|
|
|
|
|
|
|
logger.info(f"{num_cached} pushes were already cached out of {len(pushes)}")
|
2020-02-15 22:16:21 +03:00
|
|
|
|
|
|
|
upload_adr_cache()
|
|
|
|
|
2020-02-24 16:38:15 +03:00
|
|
|
with open(f"push_data_{runnable}.json", "w") as f:
|
|
|
|
json.dump(push_data, f)
|
2020-02-24 02:34:19 +03:00
|
|
|
|
2020-02-11 18:30:39 +03:00
|
|
|
def retrieve_push_data(self):
|
|
|
|
# Download previous cache.
|
|
|
|
db.download(ADR_CACHE_DB)
|
|
|
|
|
|
|
|
# Setup adr cache configuration.
|
|
|
|
os.makedirs(os.path.expanduser("~/.config/adr"), exist_ok=True)
|
|
|
|
with open(os.path.expanduser("~/.config/adr/config.toml"), "w") as f:
|
|
|
|
f.write(
|
|
|
|
f"""[adr.cache.stores]
|
2020-02-15 22:16:21 +03:00
|
|
|
file = {{ driver = "file", path = "{os.path.abspath(self.cache_path)}" }}
|
2020-02-11 18:30:39 +03:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
# We'll use the past TRAINING_MONTHS months only for training the model,
|
|
|
|
# but we use 3 months more than that to calculate the failure statistics.
|
2020-02-24 16:38:15 +03:00
|
|
|
self.generate_push_data("label", TRAINING_MONTHS + 3)
|
2020-02-11 18:30:39 +03:00
|
|
|
|
|
|
|
# For groups, we only have 12 weeks in ActiveData. Getting previous data
|
|
|
|
# from task artifacts is slow, so for now we only get what we can get from
|
|
|
|
# ActiveData and we'll see if it's enough to train a satisfying model.
|
2020-02-24 16:38:15 +03:00
|
|
|
self.generate_push_data("group", 3)
|
2020-02-11 18:30:39 +03:00
|
|
|
|
|
|
|
zstd_compress("push_data_label.json")
|
|
|
|
zstd_compress("push_data_group.json")
|
2019-10-18 15:33:53 +03:00
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
def generate_test_scheduling_history(self, granularity):
|
|
|
|
push_data_path = f"push_data_{granularity}.json"
|
|
|
|
updated = download_check_etag(PUSH_DATA_URL.format(granularity=granularity))
|
2020-01-11 17:31:01 +03:00
|
|
|
if updated:
|
2020-02-19 15:15:03 +03:00
|
|
|
zstd_decompress(push_data_path)
|
|
|
|
assert os.path.exists(push_data_path), "Decompressed push data file exists"
|
2019-10-18 15:33:53 +03:00
|
|
|
|
2019-10-18 15:08:08 +03:00
|
|
|
# Get the commits DB.
|
2019-11-19 21:07:42 +03:00
|
|
|
assert db.download(repository.COMMITS_DB)
|
2019-10-18 15:08:08 +03:00
|
|
|
|
2019-09-24 23:32:16 +03:00
|
|
|
HISTORY_DATE_START = datetime.now() - relativedelta(months=TRAINING_MONTHS)
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
if granularity == "label":
|
|
|
|
test_scheduling_db = test_scheduling.TEST_LABEL_SCHEDULING_DB
|
|
|
|
past_failures_db = os.path.join(
|
|
|
|
"data", test_scheduling.PAST_FAILURES_LABEL_DB
|
|
|
|
)
|
|
|
|
elif granularity == "group":
|
|
|
|
test_scheduling_db = test_scheduling.TEST_GROUP_SCHEDULING_DB
|
|
|
|
past_failures_db = os.path.join(
|
|
|
|
"data", test_scheduling.PAST_FAILURES_GROUP_DB
|
|
|
|
)
|
|
|
|
|
|
|
|
db.download(test_scheduling_db, support_files_too=True)
|
2019-10-18 14:59:24 +03:00
|
|
|
|
2019-11-19 21:07:42 +03:00
|
|
|
last_node = None
|
2020-02-19 15:15:03 +03:00
|
|
|
for test_data in test_scheduling.get_test_scheduling_history(granularity):
|
2019-11-10 21:57:05 +03:00
|
|
|
last_node = test_data["revs"][0]
|
2019-10-18 14:59:24 +03:00
|
|
|
|
2019-11-18 20:33:44 +03:00
|
|
|
def generate_all_data():
|
2020-02-19 15:15:03 +03:00
|
|
|
past_failures = test_scheduling.get_past_failures(granularity)
|
2019-10-09 17:02:58 +03:00
|
|
|
|
2019-11-18 20:33:44 +03:00
|
|
|
push_num = past_failures["push_num"] if "push_num" in past_failures else 0
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2019-10-18 14:59:24 +03:00
|
|
|
# We can start once we get to the last revision we added in the previous run.
|
|
|
|
can_start = True if last_node is None else False
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2019-11-10 21:57:05 +03:00
|
|
|
commit_map = {}
|
|
|
|
for commit_data in tqdm(repository.get_commits()):
|
|
|
|
if not can_start:
|
|
|
|
if last_node == commit_data["node"]:
|
|
|
|
can_start = True
|
2019-10-22 19:42:38 +03:00
|
|
|
|
2019-10-18 14:59:24 +03:00
|
|
|
continue
|
|
|
|
|
2019-11-10 21:57:05 +03:00
|
|
|
commit_map[commit_data["node"]] = commit_data
|
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
with open(push_data_path, "r") as f:
|
2020-02-24 16:38:15 +03:00
|
|
|
push_data = json.load(f)
|
2019-11-10 21:57:05 +03:00
|
|
|
|
|
|
|
logger.info(f"push data nodes: {len(push_data)}")
|
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
if granularity == "label":
|
|
|
|
push_data = [
|
|
|
|
(
|
|
|
|
revisions,
|
|
|
|
rename_tasks(push_tasks),
|
|
|
|
rename_tasks(possible_regressions),
|
|
|
|
rename_tasks(likely_regressions),
|
|
|
|
)
|
|
|
|
for revisions, push_tasks, possible_regressions, likely_regressions in push_data
|
|
|
|
]
|
|
|
|
|
|
|
|
# In the last 28 pushes, we definitely run all possible runnables.
|
|
|
|
all_runnables_set = set(
|
|
|
|
sum((push_runnables for _, push_runnables, _, _ in push_data[-28:]), [])
|
2019-11-15 01:04:57 +03:00
|
|
|
)
|
2020-02-19 15:15:03 +03:00
|
|
|
# Filter runnables we don't need.
|
|
|
|
all_runnables = filter_runnables(
|
|
|
|
list(all_runnables_set), all_runnables_set, granularity
|
|
|
|
)
|
|
|
|
all_runnables_set = set(all_runnables_set)
|
|
|
|
logger.info(f"{len(all_runnables_set)} runnables run in the last 28 pushes")
|
|
|
|
|
|
|
|
# Store all runnables in the past_failures DB so it can be used in the evaluation phase.
|
|
|
|
past_failures["all_runnables"] = all_runnables
|
|
|
|
# XXX: Should we recreate the DB from scratch if the previous all_runnables are not the
|
2019-11-19 02:27:06 +03:00
|
|
|
# same as the current ones?
|
|
|
|
|
2019-11-18 20:33:44 +03:00
|
|
|
saved_nodes = set()
|
|
|
|
skipped_no_commits = 0
|
|
|
|
skipped_too_big_commits = 0
|
2020-02-19 15:15:03 +03:00
|
|
|
skipped_no_runnables = 0
|
2019-11-18 20:33:44 +03:00
|
|
|
|
2019-11-10 21:57:05 +03:00
|
|
|
# We can start once we get to the last revision we added in the previous run.
|
|
|
|
can_start = True if last_node is None else False
|
|
|
|
|
|
|
|
for i in tqdm(range(len(push_data))):
|
|
|
|
(
|
|
|
|
revisions,
|
2020-02-19 15:15:03 +03:00
|
|
|
push_runnables,
|
2019-11-10 21:57:05 +03:00
|
|
|
possible_regressions,
|
|
|
|
likely_regressions,
|
|
|
|
) = push_data.pop(0)
|
|
|
|
|
2019-10-18 14:59:24 +03:00
|
|
|
if not can_start:
|
2019-11-10 21:57:05 +03:00
|
|
|
if last_node == revisions[0]:
|
|
|
|
can_start = True
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
push_num += 1
|
|
|
|
|
|
|
|
# XXX: Some commits are skipped in the repository mining, e.g. merges and backouts. Maybe we should not skip them.
|
|
|
|
commits = tuple(
|
|
|
|
commit_map.pop(revision)
|
|
|
|
for revision in revisions
|
|
|
|
if revision in commit_map
|
|
|
|
)
|
|
|
|
if len(commits) == 0:
|
|
|
|
skipped_no_commits += 1
|
2019-10-18 14:59:24 +03:00
|
|
|
continue
|
|
|
|
|
2019-11-10 21:57:05 +03:00
|
|
|
merged_commits = commit_features.merge_commits(commits)
|
|
|
|
|
|
|
|
# XXX: For now, skip commits which are too large.
|
|
|
|
# In the future we can either:
|
|
|
|
# - Improve shelve perf and go back to consider all files;
|
|
|
|
# - Consider only files which appear with a given frequency, like the "files" feature in commit_features;
|
|
|
|
# - Keep a limit of number of files.
|
2019-11-21 14:53:55 +03:00
|
|
|
if len(merged_commits["files"]) > 50:
|
2019-11-10 21:57:05 +03:00
|
|
|
skipped_too_big_commits += 1
|
2019-09-18 14:10:22 +03:00
|
|
|
continue
|
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
# If we considered all_runnables, we'd generate a huge amount of data.
|
|
|
|
# So we consider only the runnables which run in this push, and the possible and likely regressions
|
2019-11-18 16:47:24 +03:00
|
|
|
# from this push.
|
2020-02-19 15:15:03 +03:00
|
|
|
runnables_to_consider = list(
|
|
|
|
set(push_runnables + possible_regressions + likely_regressions)
|
|
|
|
)
|
|
|
|
runnables_to_consider = filter_runnables(
|
|
|
|
runnables_to_consider, all_runnables_set, granularity
|
2019-11-18 16:47:24 +03:00
|
|
|
)
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
if len(runnables_to_consider) == 0:
|
|
|
|
skipped_no_runnables += 1
|
2019-11-10 21:57:05 +03:00
|
|
|
continue
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2019-11-18 16:49:34 +03:00
|
|
|
# Sync DB every 250 pushes, so we cleanup the shelve cache (we'd run OOM otherwise!).
|
|
|
|
if i % 250 == 0:
|
2019-11-10 21:57:05 +03:00
|
|
|
past_failures.sync()
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2019-11-10 21:57:05 +03:00
|
|
|
pushdate = dateutil.parser.parse(merged_commits["pushdate"])
|
2019-11-05 14:31:04 +03:00
|
|
|
|
2019-11-18 20:33:44 +03:00
|
|
|
for data in test_scheduling.generate_data(
|
|
|
|
past_failures,
|
|
|
|
merged_commits,
|
|
|
|
push_num,
|
2020-02-19 15:15:03 +03:00
|
|
|
runnables_to_consider,
|
2019-11-18 20:33:44 +03:00
|
|
|
possible_regressions,
|
|
|
|
likely_regressions,
|
|
|
|
):
|
2019-09-18 14:10:22 +03:00
|
|
|
if pushdate > HISTORY_DATE_START:
|
2019-11-10 21:57:05 +03:00
|
|
|
saved_nodes.add(i)
|
2019-11-20 12:37:26 +03:00
|
|
|
data["revs"] = revisions
|
2019-11-18 20:33:44 +03:00
|
|
|
yield data
|
2019-09-18 14:10:22 +03:00
|
|
|
|
|
|
|
logger.info(f"saved push data nodes: {len(saved_nodes)}")
|
2019-11-10 21:57:05 +03:00
|
|
|
logger.info(f"skipped {skipped_no_commits} (no commits in our DB)")
|
|
|
|
logger.info(f"skipped {skipped_too_big_commits} (too big commits)")
|
2020-02-19 15:15:03 +03:00
|
|
|
logger.info(f"skipped {skipped_no_runnables} (no interesting runnables)")
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2019-11-18 20:33:44 +03:00
|
|
|
past_failures["push_num"] = push_num
|
|
|
|
past_failures.close()
|
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
db.append(test_scheduling_db, generate_all_data())
|
2019-09-18 14:10:22 +03:00
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
zstd_compress(test_scheduling_db)
|
2019-09-11 21:17:02 +03:00
|
|
|
|
2020-02-19 15:15:03 +03:00
|
|
|
with open_tar_zst(past_failures_db) as tar:
|
|
|
|
tar.add(past_failures_db[: -len(".tar.zst")])
|
2019-10-18 14:59:24 +03:00
|
|
|
|
2019-09-11 21:17:02 +03:00
|
|
|
|
|
|
|
def main():
|
|
|
|
description = "Retrieve and extract the test scheduling history from ActiveData"
|
|
|
|
parser = argparse.ArgumentParser(description=description)
|
|
|
|
|
2019-10-18 15:33:53 +03:00
|
|
|
parser.add_argument(
|
|
|
|
"op", help="Which operation to perform.", choices=["retrieve", "generate"]
|
|
|
|
)
|
2020-02-19 15:15:03 +03:00
|
|
|
parser.add_argument(
|
|
|
|
"--granularity",
|
|
|
|
help="Which test granularity to use.",
|
|
|
|
choices=["label", "group"],
|
|
|
|
)
|
2019-10-18 15:33:53 +03:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
2019-09-11 21:17:02 +03:00
|
|
|
|
|
|
|
retriever = Retriever()
|
2019-10-18 15:33:53 +03:00
|
|
|
if args.op == "retrieve":
|
|
|
|
retriever.retrieve_push_data()
|
|
|
|
elif args.op == "generate":
|
2020-02-19 15:15:03 +03:00
|
|
|
assert args.granularity is not None
|
|
|
|
retriever.generate_test_scheduling_history(args.granularity)
|
2019-09-11 21:17:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|