Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 254 additions & 0 deletions http_service/bugbug_http/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import itertools
import logging
import os
import re
import uuid
from dataclasses import dataclass, field
from datetime import datetime, timedelta, timezone
Expand All @@ -34,11 +35,13 @@
from bugbug.model import CommentModel
from bugbug.models import get_model_class
from bugbug_http.models import (
COMMIT_MODELS,
MODELS_NAMES,
PUSH_CLASSIFIERS,
classify_broken_site_report,
classify_bug,
classify_comment,
classify_commits,
classify_issue,
get_config_specific_groups,
schedule_tests,
Expand Down Expand Up @@ -83,10 +86,17 @@
ssl_cert_reqs=None,
)

# A full hg commit hash.
COMMIT_HASH_RE = re.compile(r"[0-9a-f]{40}")

# Kill jobs which don't finish within 12 minutes.
JOB_TIMEOUT = 12 * 60
# Kill Bugzilla jobs which don't finish within 5 minutes.
BUGZILLA_JOB_TIMEOUT = 5 * 60
# The maximum number of commits which can be classified in a single request.
MAX_COMMITS = 100
# The number of commits classified by a single job.
COMMITS_PER_JOB = 25
# Remove jobs from the queue if they haven't started within 7 minutes.
QUEUE_TIMEOUT = 7 * 60
# Store the information that a job failed for 3 minutes.
Expand Down Expand Up @@ -128,6 +138,17 @@ class PerformanceRegressionPrediction(Schema):
extra_data = fields.Dict()


class CommitPrediction(Schema):
node = fields.String()
bug_id = fields.Integer(allow_none=True)
prob = fields.List(fields.Float(), allow_none=True)
index = fields.Integer(allow_none=True)
predicted_class = fields.Integer(data_key="class", allow_none=True)
risk_band = fields.String(allow_none=True)
ignored_reason = fields.String(allow_none=True)
extra_data = fields.Dict()


class NotAvailableYet(Schema):
ready = fields.Boolean(metadata={"enum": [False]})

Expand All @@ -145,6 +166,10 @@ class PushModelName(Schema):
)


class CommitModelName(Schema):
model_name = fields.String(metadata={"enum": COMMIT_MODELS, "example": "regressor"})


class UnauthorizedError(Schema):
message = fields.String(dump_default="Error, missing X-API-KEY")

Expand All @@ -167,9 +192,11 @@ class Schedules(Schema):
PerformanceRegressionPrediction.__name__,
schema=PerformanceRegressionPrediction,
)
spec.components.schema(CommitPrediction.__name__, schema=CommitPrediction)
spec.components.schema(NotAvailableYet.__name__, schema=NotAvailableYet)
spec.components.schema(ModelName.__name__, schema=ModelName)
spec.components.schema(PushModelName.__name__, schema=PushModelName)
spec.components.schema(CommitModelName.__name__, schema=CommitModelName)
spec.components.schema(UnauthorizedError.__name__, schema=UnauthorizedError)
spec.components.schema(BranchName.__name__, schema=BranchName)
spec.components.schema(Schedules.__name__, schema=Schedules)
Expand Down Expand Up @@ -658,6 +685,233 @@ def model_prediction_push(model_name: str, branch: str, rev: str):
return compress_response(data, status_code)


def get_commit_predictions(
model_name: str, branch: str, revs: Sequence[str]
) -> tuple[dict[str, Any], int]:
"""Get the results of the classification of commits, queuing the missing ones.

Returns the result for each revision ({"ready": False} for the ones being
classified), and 200 if all of them are available or 202 otherwise.
"""
# Support the string 'autoland' for convenience.
if branch == "autoland":
branch = "integration/autoland"

status_code = 200
data: dict[str, Any] = {}
missing_revs = []
for rev in dict.fromkeys(revs):
job = JobInfo(classify_commits, model_name, branch, rev)
data[rev] = get_result(job)
if not data[rev]:
if not is_pending(job):
missing_revs.append(rev)
status_code = 202
data[rev] = {"ready": False}

queue_jobs: Queue = []
for batch in itertools.batched(missing_revs, COMMITS_PER_JOB, strict=False):
job_id = get_job_id()
# Set the mapping before queuing to avoid some race conditions.
redis_conn.mset(
{
JobInfo(classify_commits, model_name, branch, rev).mapping_key: job_id
for rev in batch
}
)
queue_jobs.append(
prepare_queue_job(
JobInfo(classify_commits, model_name, branch, list(batch)),
job_id=job_id,
)
)
if queue_jobs:
q.enqueue_many(queue_jobs)

return data, status_code


@application.route("/<model_name>/predict/commit/<path:branch>/<rev>")
@cross_origin()
def model_prediction_commit(model_name: str, branch: str, rev: str):
"""
---
get:
description: >
Classify a commit using the given model, answer either 200 if the commit
is processed or 202 if the commit is being processed.
<br/><br/>
The answer is {"available": False} when the commit can't be found on
the branch (or is a merge commit). Backouts and commits ignored by the
model (e.g. reformatting commits) are not scored, and have their
ignored_reason set.
summary: Classify a single commit
parameters:
- name: model_name
in: path
schema: CommitModelName
- name: branch
in: path
required: true
schema:
BranchName
- name: rev
in: path
required: true
schema:
type: str
example: 8ba995b74e18334ab3707f27e9eb8f4e37ba3d29
responses:
200:
description: A single commit prediction
content:
application/json:
schema: CommitPrediction
202:
description: A temporary answer for the commit being processed
content:
application/json:
schema: NotAvailableYet
400:
description: The revision is not a full commit hash
401:
description: API key is missing
content:
application/json:
schema: UnauthorizedError
"""
if not request.headers.get(API_TOKEN):
return jsonify(UnauthorizedError().dump({})), 401

if model_name not in COMMIT_MODELS:
return (
jsonify(
{"error": f"Model {model_name} doesn't support commit predictions"}
),
404,
)

if not COMMIT_HASH_RE.fullmatch(rev):
return jsonify({"error": f"{rev} is not a full commit hash"}), 400

LOGGER.info(
"Received %s commit prediction request for %s @ %s", model_name, branch, rev
)

data, status_code = get_commit_predictions(model_name, branch, [rev])
return compress_response(data[rev], status_code)


@application.route("/<model_name>/predict/commits/<path:branch>", methods=["POST"])
@cross_origin()
def model_prediction_commits(model_name: str, branch: str):
"""
---
post:
description: >
Post a batch of commit hashes to classify, answer either 200 if all
commits are processed or 202 if at least one commit is not processed.
<br/><br/>
Send the full hashes of commits on the given branch like this:<br/>
```
{"commits": ["8ba995b74e18334ab3707f27e9eb8f4e37ba3d29", "1f2e3d4c5b6a79880f1e2d3c4b5a69788f9e0d1c"]}
```<br/><br/>

and call back the same endpoint with the same commits until all of the
results are available. The answer for each commit is the same as the
single commit prediction endpoint, or {"ready": False} while it is
being processed:<br/>
```
{"commits": {"8ba995b74e18334ab3707f27e9eb8f4e37ba3d29": {"ready": False}, "1f2e3d4c5b6a79880f1e2d3c4b5a69788f9e0d1c": {"node": "1f2e3d4c5b6a79880f1e2d3c4b5a69788f9e0d1c", "prob": [0.9, 0.1], ...}}}
```
summary: Classify a batch of commits
parameters:
- name: model_name
in: path
schema: CommitModelName
- name: branch
in: path
required: true
schema:
BranchName
requestBody:
description: The list of commits to classify
content:
application/json:
schema:
type: object
properties:
commits:
type: array
items:
type: string
examples:
commits:
summary: An example of payload
value:
commits:
["8ba995b74e18334ab3707f27e9eb8f4e37ba3d29", "1f2e3d4c5b6a79880f1e2d3c4b5a69788f9e0d1c"]
responses:
200:
description: The result for each commit
content:
application/json:
schema:
type: object
properties:
commits:
type: object
additionalProperties: CommitPrediction
202:
description: A temporary answer for commits being processed
400:
description: The request is not valid
401:
description: API key is missing
content:
application/json:
schema: UnauthorizedError
"""
if not request.headers.get(API_TOKEN):
return jsonify(UnauthorizedError().dump({})), 401

if model_name not in COMMIT_MODELS:
return (
jsonify(
{"error": f"Model {model_name} doesn't support commit predictions"}
),
404,
)

try:
body = orjson.loads(request.data)
except orjson.JSONDecodeError:
return jsonify({"error": "The body is not valid JSON"}), 400

schema = {
"commits": {
"type": "list",
"required": True,
"minlength": 1,
"maxlength": MAX_COMMITS,
"schema": {"type": "string", "regex": COMMIT_HASH_RE.pattern},
}
}
validator = Validator()
if not isinstance(body, dict) or not validator.validate(body, schema):
return jsonify({"errors": validator.errors}), 400

LOGGER.info(
"Received %s prediction request for %d commits @ %s",
model_name,
len(body["commits"]),
branch,
)

data, status_code = get_commit_predictions(model_name, branch, body["commits"])
return compress_response({"commits": data}, status_code)


@application.route(
"/<model_name>/predict/github/<string:owner>/<string:repo>/<int:issue_num>"
)
Expand Down
Loading