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
4 changes: 3 additions & 1 deletion pandajedi/jedibrokerage/AtlasAnalJobBroker.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_workflow_anchor_weights(self, taskSpec: JediTaskSpec, sitePreAssigned: b
self.set_task_common(cache_key, anchor)
weights = (anchor or {}).get("weights") or None
if weights:
tmpLog.info(f"workflow_id={workflow_id} anchor: {workflow_affinity.summarize_anchor(anchor)}")
tmpLog.info(f"workflow_id={workflow_id} anchor: {workflow_affinity.summarize_anchor(anchor)} computed_at={(anchor or {}).get('computed_at')}")
else:
tmpLog.info(f"workflow_id={workflow_id} has no anchor yet")
return weights
Expand Down Expand Up @@ -2374,6 +2374,8 @@ def doBrokerage(
f"""nFailed={bw_map["nFailed"]} nClosed={bw_map["nClosed"]} nFinished={bw_map["nFinished"]} """
f"""dataW={tmpDataWeight} totalInGB={totalSize} localInGB={localSize} nFiles={totalNumFiles} """
)
if anchor_weights:
tmpStr += f"""wfAffW={tmpAffinityWeight:.2f} """
weightStr[tmpPseudoSiteName] = tmpStr
# append
if tmpSiteName in sitesUsedByTask:
Expand Down
33 changes: 10 additions & 23 deletions pandajedi/jedibrokerage/AtlasBrokerUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,45 +697,32 @@ def getSiteToRunRateStats(


# anchor of a native workflow, as the workflow manager recorded it on the workflow
CACHE_WorkflowAnchor: dict[int, dict[str, Any]] = {}
CACHE_WorkflowAnchor_MAX = 1000


def get_workflow_anchor(tbIF: JediTaskBufferInterface, workflow_id: int, cache_lifetime: int = 120) -> dict[str, Any] | None:
def get_workflow_anchor(tbIF: JediTaskBufferInterface, workflow_id: int) -> dict[str, Any] | None:
"""
Read a native workflow's brokerage anchor, cached in this process for a short while
Read a native workflow's brokerage anchor

The anchor is already persisted on the workflow, so this needs no shared cache of its own --
only enough memory to keep brokerage from re-reading the same row for every input chunk of
every task of the workflow, several times a minute.
Read afresh on every call, not cached in the process: the tasks a workflow held back are
released the moment a new anchor is stored, and a cached copy would broker them against the
one from before their siblings were placed. The caller shares one read among the chunks of a
task, so the cost is one primary-key select per workflow task per generation pass.

Args:
tbIF: task buffer interface
workflow_id: the native workflow to read
cache_lifetime: seconds before the local copy is read again

Returns:
dict | None: the anchor as the workflow manager stored it, or None if there is none yet
"""
current_time = naive_utcnow()
cached = CACHE_WorkflowAnchor.get(workflow_id)
if cached is not None and current_time <= cached["exp"]:
cached_anchor: dict[str, Any] | None = cached["data"]
return cached_anchor
anchor: dict[str, Any] | None = None
try:
workflow_spec = tbIF.get_workflow(workflow_id)
if workflow_spec is not None:
anchor = workflow_spec.get_parameter(workflow_affinity.WORKFLOW_ANCHOR_PARAM)
anchor: dict[str, Any] | None = workflow_spec.get_parameter(workflow_affinity.WORKFLOW_ANCHOR_PARAM)
return anchor
except Exception:
# A preference that cannot be read is not a reason to fail brokerage; the task is simply
# brokered as any other would be.
anchor = None
if len(CACHE_WorkflowAnchor) >= CACHE_WorkflowAnchor_MAX:
for expired_key in [key for key, value in CACHE_WorkflowAnchor.items() if current_time > value["exp"]]:
del CACHE_WorkflowAnchor[expired_key]
CACHE_WorkflowAnchor[workflow_id] = {"exp": current_time + datetime.timedelta(seconds=cache_lifetime), "data": anchor}
return anchor
pass
return None


# get users jobs stats from various resources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
never moved to the front. Fixing that changes placement for every analysis task, workflow or not,
which is why the non-workflow behaviour is pinned here too.

Run from the repository root: python3 pandajedi/jedibrokerage/tests/site_ordering_test.py
Run from the repository root: python3 pandajedi/jedibrokerage/tests/workflow_affinity_test.py
"""

import os
Expand Down
25 changes: 25 additions & 0 deletions pandajedi/jedibrokerage/workflow_affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from __future__ import annotations

import datetime
import random
from typing import Any, Collection, Iterable, Protocol, TypeVar

Expand Down Expand Up @@ -167,3 +168,27 @@ def summarize_anchor(anchor: dict[str, Any] | None, max_sites: int = 5) -> str:
return "no workflow anchor"
ranked = sorted(weights.items(), key=lambda item: item[1], reverse=True)[:max_sites]
return ", ".join(f"{site}={weight:g}" for site, weight in ranked)


def brokerage_holdup_expired(creation_time: datetime.datetime | None, now: datetime.datetime, wait_minutes: float) -> bool:
"""
Whether a task held for its workflow's brokerage has waited long enough to go on regardless

The hold is a preference, so it must never strand a step whose siblings are stuck. Rather than
a timer of its own, it rides the regular pending cycle: a held task sits in pending, and the
watchdog reactivates pending tasks after waitForPending minutes, sending it back through the
contents feeder. The first feed happens within minutes of creation, so a task older than that
interval can only be on a feed after reactivation -- the point at which to stop holding.

Args:
creation_time: when the task was created
now: current time
wait_minutes: the pending reactivation interval

Returns:
bool: True if the hold should be dropped
"""
if creation_time is None:
# nothing to measure against, and holding without bound is the worse failure
return True
return now - creation_time >= datetime.timedelta(minutes=wait_minutes)
Loading
Loading