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
2 changes: 1 addition & 1 deletion pandajedi/jedicore/JediDBSchemaInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class JediDBSchemaInfo:
schema_version: str | None = None

def method(self) -> str:
schema_version = "0.1.1"
schema_version = "0.1.9"
_logger.debug(f"PanDA schema version required for JEDI is : {schema_version}")
return schema_version
7 changes: 6 additions & 1 deletion pandajedi/jedirefine/TaskRefinerBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ def extractCommon(
else:
self.setSplitRule(taskParamMap, "nFilesPerJob", JediTaskSpec.splitRuleToken["nFilesPerJob"])
self.setSplitRule(taskParamMap, "nEventsPerJob", JediTaskSpec.splitRuleToken["nEventsPerJob"])
self.setSplitRule(taskParamMap, "nGBPerJob", JediTaskSpec.splitRuleToken["nGBPerJob"])
# NG is read back as an integer number of GB, so a fractional value such as 0.5 from pbook
# would be read as 0; round it down to keep it a maximum, but not below 1
n_gb_per_job = taskParamMap.get("nGBPerJob")
if isinstance(n_gb_per_job, float):
n_gb_per_job = max(1, int(n_gb_per_job))
self.setSplitRule(None, n_gb_per_job, JediTaskSpec.splitRuleToken["nGBPerJob"])
self.setSplitRule(taskParamMap, "nMaxFilesPerJob", JediTaskSpec.splitRuleToken["nMaxFilesPerJob"])
self.setSplitRule(taskParamMap, "maxEventsPerJob", JediTaskSpec.splitRuleToken["maxEventsPerJob"])
self.setSplitRule(taskParamMap, "nEventsPerWorker", JediTaskSpec.splitRuleToken["nEventsPerWorker"])
Expand Down
217 changes: 216 additions & 1 deletion pandaserver/api/v1/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pandaserver.jobdispatcher.Protocol as Protocol
from pandaserver.config import panda_config
from pandaserver.dataservice.ddm import rucioAPI
from pandaserver.srvcore import CoreUtils
from pandaserver.srvcore import CoreUtils, task_command_log
from pandaserver.srvcore.CoreUtils import clean_user_id, shorten_for_log
from pandaserver.srvcore.panda_request import PandaRequest
from pandaserver.taskbuffer.db_proxy_mods.async_request_module import (
Expand All @@ -34,6 +34,16 @@
MESSAGE_TASK_ID = "jediTaskID must be an integer"
MESSAGE_DATABASE = "database error in the PanDA server"
MESSAGE_JSON = "failed to load JSON"
MESSAGE_COMMAND_LOG = "failed to record the command in the task command log, so it was not run"

# names of the parameters in which an endpoint takes the task, or the list of tasks, it acts on.
# A POST endpoint of a module registered with log_task_commands is recorded when it has one
COMMAND_TASK_ID_PARAMETERS = ("task_id", "jedi_task_id", "task_id_list")
# largest JSON size, in bytes, of an argument recorded in the task command log. A larger one is
# recorded as {"omitted_bytes": size}
COMMAND_ARGUMENT_SIZE_LIMIT = 64 * 1024
# longest message, in characters, and largest JSON size of the data, in bytes, recorded from a response
COMMAND_RESPONSE_FIELD_LIMIT = 1024


def get_endpoint(protocol: str) -> tuple[bool, str]:
Expand Down Expand Up @@ -323,6 +333,201 @@ def isinstance_types(expected_type: Any) -> tuple[Any, ...]:
return (origin or expected_type,)


# modules whose POST endpoints are recorded in the task command log, with what returns the
# TaskBuffer to record through
_command_logging_modules: dict[str, Callable[[], Any]] = {}


def log_task_commands(module_name: str, task_buffer: Callable[[], Any]) -> None:
"""
Record the commands the POST endpoints of an API module receive in the task command log, TASKS_COMMANDLOG.

request_validation records an endpoint of a registered module when the endpoint is called with
POST and takes the tasks it acts on in a parameter named in COMMAND_TASK_ID_PARAMETERS. So a new
endpoint is recorded without being listed anywhere.

Args:
module_name(str): __name__ of the API module
task_buffer(callable): returns the TaskBuffer to record through, e.g. ``lambda: global_task_buffer``,
so that it is resolved at request time rather than at import time when it may still be None
"""
_command_logging_modules[module_name] = task_buffer


def get_command_task_ids(arguments: dict[str, Any]) -> list[int]:
"""
Get the tasks a command acts on from the arguments of its endpoint.

Args:
arguments(dict): the bound arguments of the endpoint, by parameter name

Returns:
list: the jediTaskIDs from the first parameter named in COMMAND_TASK_ID_PARAMETERS, or an empty list when there is none
"""
for name in COMMAND_TASK_ID_PARAMETERS:
value = arguments.get(name)
if value is None:
continue
if isinstance(value, (list, tuple, set, frozenset)):
return [int(task_id) for task_id in value]
return [int(value)]
return []


def _encode_json_capped(value: Any, limit: int) -> Any:
# the value itself when its JSON fits in limit bytes, else {"omitted_bytes": size}. A value
# JSON cannot hold, e.g. NaN, which IS JSON (STRICT) rejects, is kept as its str
try:
encoded = json.dumps(value, default=str, allow_nan=False)
except ValueError:
value = str(value)
encoded = json.dumps(value)
size = len(encoded.encode())
if size > limit:
return {"omitted_bytes": size}
return value


def encode_command_parameters(arguments: dict[str, Any]) -> str:
"""
Encode the arguments of a task command for the task command log.

Args:
arguments(dict): the bound arguments of the endpoint, by parameter name

Returns:
str: JSON of the arguments other than req and the tasks, each replaced by
{"omitted_bytes": size} when its JSON is over COMMAND_ARGUMENT_SIZE_LIMIT bytes
"""
parameters = {
name: _encode_json_capped(value, COMMAND_ARGUMENT_SIZE_LIMIT)
for name, value in arguments.items()
if name != "req" and name not in COMMAND_TASK_ID_PARAMETERS
}
return json.dumps(parameters, default=str)


def encode_command_response(response: Any) -> str:
"""
Encode the response an endpoint returned for a task command for the task command log.

Args:
response(Any): what the endpoint returned, normally {"success", "message", "data"} from generate_response

Returns:
str: JSON of {"success", "message", "data"}, with the message cut to COMMAND_RESPONSE_FIELD_LIMIT
characters and the data replaced by {"omitted_bytes": size} when its JSON is over
COMMAND_RESPONSE_FIELD_LIMIT bytes. Anything else is kept as {"response": its str, cut}
"""
if isinstance(response, dict) and "success" in response:
message = response.get("message")
encoded = {
"success": response.get("success"),
"message": None if message is None else str(message)[:COMMAND_RESPONSE_FIELD_LIMIT],
"data": _encode_json_capped(response.get("data"), COMMAND_RESPONSE_FIELD_LIMIT),
}
else:
encoded = {"response": str(response)[:COMMAND_RESPONSE_FIELD_LIMIT]}
return json.dumps(encoded, default=str)


def encode_command_exception(exc: BaseException) -> str:
"""
Encode the exception an endpoint raised instead of responding to a task command, for the task command log.

Args:
exc(Exception): the exception

Returns:
str: JSON of {"exception": "<type>: <message>"}, cut to COMMAND_RESPONSE_FIELD_LIMIT characters
"""
return json.dumps({"exception": f"{type(exc).__name__}: {exc}"[:COMMAND_RESPONSE_FIELD_LIMIT]})


def get_client_address(req: PandaRequest) -> str | None:
"""
Get the address of the client that sent a request.

Args:
req(PandaRequest): the request

Returns:
str or None: the client's host name when httpd looked it up (HostnameLookups), else its IP
address, or None when neither is known. A proxy between the client and httpd gives its own
"""
return req.get_remote_host() or req.subprocess_env.get("REMOTE_ADDR")


def run_logged_task_command(
func: Callable[..., Any],
bound_args: inspect.BoundArguments,
req: PandaRequest,
task_ids: list[int],
task_buffer: Any,
tmp_logger: LogWrapper,
) -> Any:
"""
Run a task command and record it in the task command log, TASKS_COMMANDLOG.

The rows are written as received before the command runs, and the command is refused when they
cannot be written, so that every command that ran has its rows. While the command runs, the key
of the rows is in task_command_log.current_task_command, where TaskBuffer reads it to mark the
rows queued when the command is registered for JEDI. The response is recorded after; a failure
to record it is only logged, since the command has run by then.

Args:
func(callable): the endpoint
bound_args(inspect.BoundArguments): its arguments, validated and cast
req(PandaRequest): the request
task_ids(list): the tasks the command acts on
task_buffer(TaskBuffer): the TaskBuffer to record through
tmp_logger(LogWrapper): the logger of the endpoint

Returns:
Any: what the endpoint returned, or the response refusing the command
"""
dn = get_dn(req)
requester = clean_user_id(dn)
if requester in ["", "NULL", None]:
requester = dn
try:
command_key = task_buffer.insert_task_command(
task_ids,
func.__name__,
encode_command_parameters(bound_args.arguments),
requester,
has_production_role(req),
get_client_address(req),
)
except Exception as e:
tmp_logger.error(f"failed to record the command with {type(e).__name__}: {e}")
command_key = None
if command_key is None:
tmp_logger.error(MESSAGE_COMMAND_LOG)
return generate_response(False, message=MESSAGE_COMMAND_LOG)
tmp_logger.debug(f"recorded in the task command log as command_id={command_key['command_id']}")

def record_response(encoded: str, is_error: bool) -> None:
try:
is_ok = task_buffer.set_task_command_response(command_key, encoded, is_error)
except Exception as e:
tmp_logger.error(f"failed to record the response with {type(e).__name__}: {e}")
is_ok = False
if not is_ok:
tmp_logger.error(f"failed to record the response of command_id={command_key['command_id']} in the task command log")

token = task_command_log.current_task_command.set(command_key)
try:
response = func(*bound_args.args, **bound_args.kwargs)
except Exception as e:
record_response(encode_command_exception(e), True)
raise
finally:
task_command_log.current_task_command.reset(token)
record_response(encode_command_response(response), False)
return response


def request_validation(
logger: logging.Logger,
secure: bool = True,
Expand All @@ -335,6 +540,9 @@ def request_validation(
"""
Decorator that validates an incoming API request before the handler runs.

For a module registered with log_task_commands, a POST request that names the tasks it acts on
is also recorded in the task command log once it has passed validation; see run_logged_task_command.

Args:
logger: Logger instance passed to LogWrapper for the decorated function.
secure(bool): If True, requires an SSL connection without a limited proxy. Defaults to True.
Expand Down Expand Up @@ -502,6 +710,13 @@ def log_error_with_context(message: str) -> None:
tmp_logger.error(MESSAGE_TASK_OWNER)
return generate_response(False, message=MESSAGE_TASK_OWNER)

# record a task command in the task command log while running it
command_task_buffer = _command_logging_modules.get(func.__module__)
if command_task_buffer is not None and expected_request_method == "POST":
command_task_ids = get_command_task_ids(bound_args.arguments)
if command_task_ids:
return run_logged_task_command(func, bound_args, req, command_task_ids, command_task_buffer(), tmp_logger)

return func(*bound_args.args, **bound_args.kwargs)

return wrapper
Expand Down
74 changes: 73 additions & 1 deletion pandaserver/api/v1/task_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
get_email_address,
get_fqan,
has_production_role,
log_task_commands,
request_validation,
)
from pandaserver.srvcore.CoreUtils import clean_user_id, make_reassign_comment
Expand All @@ -41,6 +42,10 @@ def init_task_buffer(task_buffer: TaskBuffer) -> None:
global_task_buffer = task_buffer


# every POST endpoint that takes the task it acts on is recorded in the task command log
log_task_commands(__name__, lambda: global_task_buffer)


@request_validation(_logger, secure=True, request_method="POST", task_owner=True, task_buffer=lambda: global_task_buffer)
def retry(
req: PandaRequest,
Expand Down Expand Up @@ -632,7 +637,7 @@ def reload_input(req: PandaRequest, task_id: int, ignore_hard_exhausted: bool =
"""
Reload input

Request to reload the input for a given task. Requires a secure connection and production role.
Request to reload the input for a given task. Requires a secure connection.

API details:
HTTP Method: POST
Expand Down Expand Up @@ -1001,6 +1006,73 @@ def get_status_history(req: PandaRequest, task_id: int) -> dict[str, Any]:
return generate_response(True, data=data)


@request_validation(_logger, secure=True, request_method="GET")
def get_command_history(req: PandaRequest, task_id: int) -> dict[str, Any]:
"""
Get task command history

Gets the commands for a task that the POST endpoints of this API received, as recorded in the task command log
(TASKS_COMMANDLOG), oldest first. Requires a secure connection.

API details:
HTTP Method: GET
Path: /v1/task/get_command_history

Args:
req(PandaRequest): internally generated request object
task_id(int): JEDI Task ID

Returns:
dict: The system response `{"success": success, "message": message, "data": data}`.

Response data:
dict: The commands for the task, and what tells how complete they are.
{"rows": [...], "as_of": datetime, "retained_from": datetime or None}
- rows: one dict per command, with
- command_id: ID of the request. A request acting on several tasks has one row per task with the same ID
- jediTaskID, command (the endpoint, e.g. retry), parameters (dict of the other arguments; one over
64 KiB of JSON is {"omitted_bytes": size})
- requester (user name), prod_role (bool), client_addr (host name or IP address of the client, or of a
proxy in between), server_host (the server that received it)
- command_time: when it was received, in UTC
- status: where the command is, one of
- received: the server has not responded yet, or stopped before it did
- responded: the server responded and nothing further follows; whether the command was done or
refused is in response, which each endpoint shapes in its own way
- error: the server raised an exception instead of responding
- queued: registered for JEDI, which has not taken it yet
- accepted / rejected: JEDI took the command, or rejected it
- superseded: a later command for the task replaced it before JEDI took it
- status_time: when status last changed, in UTC
- status_reason: why status moved on from queued, e.g. "running -> toretry", "task in done" or "superseded by kill"
- response: {"success", "message", "data"} returned to the client, with message and data shortened,
or {"exception": str}; None while received
- response_time: when the server responded, in UTC
A task without rows got no command through these endpoints
- as_of: database time in UTC taken before the rows were read. Every command received by then is in rows
- retained_from: time from which the table holds every row, or None when it cannot be told. Old partitions are dropped,
so a task without rows before this time may still have got commands then
On failure, with the reason in the message: {"error": str} when the database failed, with the error code (ORA-xxxxx, or
the SQLSTATE on Postgres), and None otherwise.
"""
tmp_logger = LogWrapper(_logger, f"get_command_history < task_id={task_id} >")
tmp_logger.debug("Start")

try:
task_id = int(task_id)
except ValueError:
tmp_logger.error("Failed due to invalid task_id")
return generate_response(False, message=MESSAGE_TASK_ID)

success, message, data = global_task_buffer.get_task_command_history(task_id)
if not success:
tmp_logger.debug(f"Failed with {message}")
return generate_response(False, message=message, data=data)

tmp_logger.debug(f"""Done with {len(data["rows"])} rows""")
return generate_response(True, data=data)


@request_validation(_logger, request_method="GET", secure=True)
def get_details(req: PandaRequest, task_id: int, include_parameters: bool = False, include_status: bool = False) -> dict[str, Any]:
"""
Expand Down
16 changes: 16 additions & 0 deletions pandaserver/api/v1/tests/task_api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ def test_get_status_history(self) -> None:
for row in output["data"]["rows"]:
self.assertEqual(row["jediTaskID"], int(JEDI_TASK_ID))

def test_get_command_history(self) -> None:
url = f"{api_url_ssl}/task/get_command_history"
print(f"Testing URL: {url}")
data = {"task_id": JEDI_TASK_ID}
status, output = self.http_client.get(url, data)
print(output)
self.assertEqual(status, 0)
self.assertTrue(output["success"])
self.assertEqual(set(output["data"]), {"rows", "as_of", "retained_from"})
statuses = {"received", "responded", "error", "queued", "accepted", "rejected", "superseded"}
for row in output["data"]["rows"]:
# the other tests send commands for the fake task too, so it may have rows
self.assertEqual(row["jediTaskID"], int(JEDI_TASK_ID))
self.assertIn(row["status"], statuses)
self.assertIsInstance(row["parameters"], dict)

def test_get_status(self) -> None:
url = f"{api_url_ssl}/task/get_status"
print(f"Testing URL: {url}")
Expand Down
Loading
Loading