Skip to content

Commit

Permalink
decouple prediction strategy from env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
cmelone committed Apr 25, 2024
1 parent e3b2602 commit 00821d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
3 changes: 0 additions & 3 deletions docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,3 @@ The following variables should be exposed to the container. Those **bolded** are
- **`GITLAB_API_TOKEN`** - this token should have API read access
- **`GITLAB_WEBHOOK_TOKEN`** - coordinate this value with the collection webhook
- **`DB_FILE`** - path where the application can access the SQLite file
- `PREDICT_STRATEGY` - optional mode for the prediction algorithm
- options:
- `ensure_higher`: if the predicted resource usage is below current levels, it will disregard the prediction and keep what would be allocated without Gantry's intervention
11 changes: 7 additions & 4 deletions gantry/routes/prediction/prediction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import os

import aiosqlite

Expand All @@ -23,13 +22,17 @@
}


async def predict(db: aiosqlite.Connection, spec: dict) -> dict:
async def predict(db: aiosqlite.Connection, spec: dict, strategy: str = None) -> dict:
"""
Predict the resource usage of a spec
args:
spec: dict that contains pkg_name, pkg_version, pkg_variants,
compiler_name, compiler_version
strategy (optional): custom prediction behavior
"ensure_higher": if the predicted resource usage is
below current levels, it will disregard the prediction and
keep what would be allocated without Gantry's intervention
returns:
dict of predicted resource usage: cpu_request, mem_request
CPU in millicore, mem in MB
Expand All @@ -51,7 +54,7 @@ async def predict(db: aiosqlite.Connection, spec: dict) -> dict:
"mem_request": sum([build[2] for build in sample]) / len(sample),
}

if os.environ.get("PREDICT_STRATEGY") == "ensure_higher":
if strategy == "ensure_higher":
ensure_higher_pred(predictions, spec["pkg_name"])

# warn if the prediction is below some thresholds
Expand Down Expand Up @@ -85,7 +88,7 @@ async def get_sample(db: aiosqlite.Connection, spec: dict) -> list:
Selects a sample of builds to use for prediction
args:
spec: see predict_single
spec: see predict
returns:
list of lists with cpu_mean, cpu_max, mem_mean, mem_max
"""
Expand Down
7 changes: 5 additions & 2 deletions gantry/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aiohttp import web

from gantry.routes.collection import fetch_job
from gantry.routes.prediction.prediction import predict_single
from gantry.routes.prediction.prediction import predict
from gantry.util.spec import parse_alloc_spec

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -68,4 +68,7 @@ async def allocation(request: web.Request) -> web.Response:
if not parsed_spec:
return web.Response(status=400, text="invalid spec")

return web.json_response(await predict_single(request.app["db"], parsed_spec))
# we want to keep predictions >= current levels (with ensure_higher strategy)
return web.json_response(
await predict(request.app["db"], parsed_spec, strategy="ensure_higher")
)

0 comments on commit 00821d4

Please sign in to comment.