Skip to content
Open
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
19 changes: 15 additions & 4 deletions MC/bin/o2dpg_sim_workflow_anchored.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
raise EnvironmentError("O2DPG_ROOT is not set in the environment.")
mc_prodinfo_path = os.path.abspath(os.path.join(o2dpg_root, "MC", "prodinfo"))
sys.path.append(mc_prodinfo_path)
from mcprodinfo_ccdb_upload import MCProdInfo, publish_MCProdInfo
from mcprodinfo_ccdb_upload import MCProdInfo, publish_MCProdInfo, production_output_dir, publishing_disabled
import dataclasses

# Creates a time anchored MC workflow; positioned within a given run-number (as function of production size etc)
Expand Down Expand Up @@ -799,7 +799,9 @@ def main():
print (output)

# when we get here, we can publish info about the production (optionally)
if args.publish_mcprodinfo == True or os.getenv("PUBLISH_MCPRODINFO") != None:
if publishing_disabled():
print("MCProdInfo publishing disabled by ALIEN_JDL_DISABLE_MCPRODINFO")
elif args.publish_mcprodinfo == True or os.getenv("PUBLISH_MCPRODINFO") != None:
prod_tag = os.getenv("ALIEN_JDL_LPMPRODUCTIONTAG")
grid_user_name = os.getenv("JALIEN_USER")
mcprod_ccdb_server = os.getenv("PUBLISH_MCPRODINFO_CCDBSERVER")
Expand All @@ -812,8 +814,17 @@ def main():
RunNumber = args.run_number,
OrbitsPerTF = GLOparams["OrbitsPerTF"],
McTag = os.getenv("ALIEN_JDL_PACKAGES"),
FirstOrbitMC = effective_first_orbit)
publish_MCProdInfo(info, username = grid_user_name, ccdb_url = mcprod_ccdb_server)
RecoTag = os.getenv("ALIEN_JDL_O2DPG_ASYNC_RECO_TAG") or None,
FirstOrbitMC = effective_first_orbit,
OutputDir = production_output_dir(os.getenv("ALIEN_JDL_OUTPUTDIR"), args.run_number),
MasterJobID = int(os.getenv("ALIEN_MASTERJOB_ID")) if (os.getenv("ALIEN_MASTERJOB_ID") or "").isdigit() else None)
# a failure to publish is reported but does not stop the job
try:
r = publish_MCProdInfo(info, username = grid_user_name, ccdb_url = mcprod_ccdb_server, complete = True)
if r is not None and not 200 <= r.status_code < 300:
print(f"WARNING: MCProdInfo was not published (HTTP {r.status_code})")
except Exception as e:
print(f"WARNING: MCProdInfo was not published: {e}")
else:
print("No production tag or GRID user name known. Not publishing MCProdInfo")

Expand Down
227 changes: 197 additions & 30 deletions MC/prodinfo/mcprodinfo_ccdb_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class MCProdInfo:
McTag: Optional[str] = None # main software tag used
RecoTag: Optional[str] = None # RecoTag (if any)
FirstOrbitMC: Optional[int] = None # first orbit used by the MC; its TFs start at FirstOrbitMC + k * OrbitsPerTF
OutputDir: Optional[str] = None # output directory of the production (without run and subjob parts)
MasterJobID: Optional[int] = None # GRID masterjob of the writer; orders versions written by different cycles
Hash: Optional[str] = field(default=None)

def __post_init__(self):
Expand All @@ -41,7 +43,7 @@ def __post_init__(self):

import re

def extract_metadata_blocks_from_CCDB(text: str):
def extract_metadata_blocks_from_CCDB(text: str, convert=True):
blocks = []
# Split on 'Metadata:\n' and iterate over each block
sections = text.split('Metadata:\n')
Expand All @@ -53,9 +55,13 @@ def extract_metadata_blocks_from_CCDB(text: str):
match = re.match(r'\s*(\w+)\s*=\s*(.+)', line)
if match:
key, val = match.groups()
# CCDB keeps metadata URL-encoded as uploaded, e.g. %2F for a slash
val = urllib.parse.unquote(val)
# Type conversion
if val == "None":
val = None
elif not convert:
val = val.strip()
elif val.isdigit() or (val.startswith('-') and val[1:].isdigit()):
val = int(val)
else:
Expand All @@ -69,10 +75,42 @@ def extract_metadata_blocks_from_CCDB(text: str):
return blocks


def extract_newest_metadata_from_CCDB(text: str, convert=True):
"""
Metadata of the most recently created object in a CCDB browse listing, or None
"""
newest, newest_key = None, None
for section in re.split(r'\n(?=ID: )', text):
blocks = extract_metadata_blocks_from_CCDB(section, convert)
if not blocks:
continue
m = re.search(r'^Created:\s*(\d+)', section, re.M)
i = re.search(r'^ID:\s*(\S+)', section, re.M)
# equal creation times are ordered by ID
key = (int(m.group(1)) if m else -1, i.group(1) if i else "")
if newest == None or key > newest_key:
newest, newest_key = blocks[0], key
return newest


def production_output_dir(job_output_dir, run_number):
"""
Output directory of a production from a job's OutputDir, e.g.
/alice/sim/2026/LHC26h3_dev2/0/572196/001 -> /alice/sim/2026/LHC26h3_dev2/0 . None if the run is not part of it.
"""
if not job_output_dir:
return None
parts = job_output_dir.rstrip('/').split('/')
if str(run_number) not in parts:
return None
return '/'.join(parts[:parts.index(str(run_number))]) or None



def query_mcprodinfo(base_url, user, run_number, lpm_prod_tag, cert_dir="/tmp"):
def query_mcprodinfo(base_url, user, run_number, lpm_prod_tag, cert_dir="/tmp", raw=False):
"""
Queries MCProdInfo from CCDB. Returns object or None
Queries MCProdInfo from CCDB. Returns object or None.
With raw, returns (object, metadata strings as stored) or (None, None).
"""
# check if the tokenfiles are there
key_path = os.environ.get("JALIEN_TOKEN_KEY")
Expand All @@ -92,19 +130,24 @@ def query_mcprodinfo(base_url, user, run_number, lpm_prod_tag, cert_dir="/tmp"):
if response.status_code not in (200, 404):
raise RuntimeError(f"CCDB query {url} failed with HTTP {response.status_code}")
if response.status_code == 200:
meta = extract_metadata_blocks_from_CCDB(response.content.decode('utf-8'))
if (len(meta) > 0):
# several versions can exist; the newest one is the current one
text = response.content.decode('utf-8')
newest = extract_newest_metadata_from_CCDB(text)
if newest != None:
def filter_known_fields(cls, data: dict) -> dict:
valid_keys = {f.name for f in fields(cls)}
return {k: v for k, v in data.items() if k in valid_keys}

clean_meta = filter_known_fields(MCProdInfo, meta[0])
clean_meta = filter_known_fields(MCProdInfo, newest)
# an object may lack fields that are required here (not every writer sets all of them)
for f in fields(MCProdInfo):
clean_meta.setdefault(f.name, None)
return MCProdInfo(**clean_meta)

return None
info = MCProdInfo(**clean_meta)
if raw:
return info, filter_known_fields(MCProdInfo, extract_newest_metadata_from_CCDB(text, convert=False))
return info

return (None, None) if raw else None


def upload_mcprodinfo_meta(base_url, user, run_number, lpm_prod_tag, keys, cert_dir="/tmp"):
Expand Down Expand Up @@ -147,42 +190,166 @@ def upload_mcprodinfo_meta(base_url, user, run_number, lpm_prod_tag, keys, cert_
with open(empty_file, 'rb') as f:
files = {'blob': f}
response = requests.post(url, files=files, cert=(cert_path, key_path), verify=False)
if not 200 <= response.status_code < 300:
print(f"WARNING: upload of MCProdInfo to {url} failed with HTTP {response.status_code}: {response.text[:300]}")

# Optional: remove the temporary file
os.remove(empty_file)

return response

def publish_MCProdInfo(mc_prod_info, ccdb_url = "https://alice-ccdb.cern.ch", username = "aliprod", force_overwrite=False, include_meta_into_aod=False):
# fields that describe what was run; IntRate varies per job, Comment describes one upload, Hash is derived,
# OutputDir only says where the data is (a production may have several) and MasterJobID orders the writers
CONTENT_FIELDS = ("Col", "OrbitsPerTF", "McTag", "RecoTag", "FirstOrbitMC")

def publishing_disabled():
"""
True if the job environment opts out of publishing MCProdInfo (e.g. validation jobs reusing a production tag)
"""
return os.getenv("ALIEN_JDL_DISABLE_MCPRODINFO", "") not in ("", "0")

def _normalised(value):
# values read back from CCDB may differ in type (e.g. 27020420 vs 27020420.0) or be empty
if value == None or value == "":
return None
try:
return float(value)
except (TypeError, ValueError):
return str(value)

def _masterjob(info):
# MasterJobID as int; a non-numeric value counts as absent
value = info.MasterJobID
if value == None or value == "":
return None
try:
return int(value)
except (TypeError, ValueError):
print(f"WARNING: ignoring non-numeric MasterJobID {value} of MCProdInfo {info.LPMProductionTag} run {info.RunNumber}")
return None

def changed_content(stored, new, complete=False):
"""
Content fields where the new object differs from the stored one, as {field: (old, new)}.
Unless complete, a field the new object leaves empty is unknown and not a change.
"""
return {f: (getattr(stored, f), getattr(new, f)) for f in CONTENT_FIELDS
if (complete or _normalised(getattr(new, f)) != None)
and _normalised(getattr(new, f)) != _normalised(getattr(stored, f))}

def is_stale_writer(stored, new):
"""
True if the new object may not replace the stored one because it comes from an older (or unknown) masterjob.
Objects without MasterJobID (old ones) can be replaced by any writer.
"""
stored_key, new_key = _masterjob(stored), _masterjob(new)
if stored_key == None:
return False
return new_key == None or new_key < stored_key

def publish_MCProdInfo(mc_prod_info, ccdb_url = "https://alice-ccdb.cern.ch", username = "aliprod", force_overwrite=False,
complete=False, include_meta_into_aod=False):
"""
Publishes mc_prod_info for its tag and run.
- no stored object: uploads it.
- complete=True (a GRID job that knows all fields): an empty field counts as a value; a differing content
is uploaded as a new version with a warning, unless the stored one comes from a newer masterjob or this
job has no production OutputDir. A FirstOrbitMC that differs while all else is equal keeps the stored value.
- complete=False (offline tools): only fills fields that are empty in the stored object, and nothing at all
if a content field conflicts.
- force_overwrite: uploads in any case; fields left empty keep their stored values.
Returns the response of the upload or None.
"""
print("Publishing MCProdInfo")

if mc_prod_info.LPMProductionTag == None or len(mc_prod_info.LPMProductionTag) == 0:
print ("No LPM production tag found; Not publishing")
return

# see if this already has meta-data uploaded, otherwise do nothing
try:
mc_prod_info_q = query_mcprodinfo(ccdb_url, username, mc_prod_info.RunNumber, mc_prod_info.LPMProductionTag)
mc_prod_info_q, stored_raw = query_mcprodinfo(ccdb_url, username, mc_prod_info.RunNumber, mc_prod_info.LPMProductionTag, raw=True)
except (RuntimeError, requests.RequestException) as e:
print(f"Not publishing MCProdInfo, the existing one could not be read: {e}")
print(f"WARNING: Not publishing MCProdInfo, the existing one could not be read: {e}")
return None
# optional fields the writer leaves empty (e.g. FirstOrbitMC for the harvester) keep their stored values
if mc_prod_info_q != None:

def upload(info, raw = {}, retry = True):
# raw: stored metadata strings that are uploaded unchanged
def post(info, raw):
keys = dataclasses.asdict(info)
keys.update({k: v for k, v in raw.items() if k in keys and k != "Hash"})
try:
return upload_mcprodinfo_meta(ccdb_url, username, info.RunNumber, info.LPMProductionTag, keys)
except requests.RequestException as e:
print(f"WARNING: upload of MCProdInfo failed: {e}")
return None
r = post(info, raw)
if retry and (r == None or not 200 <= r.status_code < 300) and info.OutputDir != None:
# a rejected OutputDir must not lose the whole object
print("WARNING: retrying the upload of MCProdInfo without OutputDir")
r = post(dataclasses.replace(info, OutputDir = None, Hash = None), {k: v for k, v in raw.items() if k != "OutputDir"})
return r

def summary(changes):
return ", ".join(f"{f}: {old} -> {new}" for f, (old, new) in changes.items())

def merged(info):
# optional fields this writer leaves empty keep their stored values
kept = {f.name: getattr(mc_prod_info_q, f.name) for f in fields(MCProdInfo)
if f.name != "Hash" and f.default is not dataclasses.MISSING
and getattr(mc_prod_info, f.name) == None and getattr(mc_prod_info_q, f.name) != None}
if kept:
mc_prod_info = dataclasses.replace(mc_prod_info, Hash = None, **kept)
# a stored first orbit is never replaced, since it describes the data already produced for this run;
# only force_overwrite (e.g. mcprodinfo_firstorbit.py apply) replaces it
stored_first_orbit = mc_prod_info_q.FirstOrbitMC if mc_prod_info_q != None else None
if stored_first_orbit != None and mc_prod_info.FirstOrbitMC != None and stored_first_orbit != mc_prod_info.FirstOrbitMC:
action = "overwriting it (forced)" if force_overwrite else "keeping the stored value"
print(f"WARNING: MCProdInfo of {mc_prod_info.LPMProductionTag} run {mc_prod_info.RunNumber} has FirstOrbitMC {stored_first_orbit}, "
f"this job uses {mc_prod_info.FirstOrbitMC}; its timeframes will be misaligned in analysis. {action}")
# publish when there is no object yet, or when the stored one lacks the first orbit
adds_first_orbit = mc_prod_info_q != None and stored_first_orbit == None and mc_prod_info.FirstOrbitMC != None
if mc_prod_info_q == None or force_overwrite == True or adds_first_orbit:
# could make this depend on hash values in future
return upload_mcprodinfo_meta(ccdb_url, username, mc_prod_info.RunNumber, mc_prod_info.LPMProductionTag, dataclasses.asdict(mc_prod_info))
and getattr(info, f.name) == None and getattr(mc_prod_info_q, f.name) != None}
return dataclasses.replace(info, Hash = None, **kept) if kept else info

if mc_prod_info_q == None:
return upload(mc_prod_info)

where = f"MCProdInfo of {mc_prod_info.LPMProductionTag} run {mc_prod_info.RunNumber}"
if force_overwrite:
print(f"{where}: uploading (forced)")
return upload(merged(mc_prod_info))

changes = changed_content(mc_prod_info_q, mc_prod_info, complete)
# values that replace stored ones; filling an empty field is not a replacement
replaced = {f: c for f, c in changes.items() if c[0] != None}
added = {f: c for f, c in changes.items() if c[0] == None}

if replaced and not complete:
print(f"WARNING: {where} differs from the stored one; not changing it without force_overwrite ({summary(replaced)})")
return None

if set(replaced) == {"FirstOrbitMC"}:
# a stored first orbit describes the data already produced by this software
old, new = replaced["FirstOrbitMC"]
print(f"WARNING: {where} has FirstOrbitMC {old}, this job uses {new}; "
f"its timeframes will be misaligned in analysis. Keeping the stored value")
replaced = {}

if replaced:
if mc_prod_info.OutputDir == None:
print(f"WARNING: {where} differs from the stored one, but this job writes outside of a production directory; "
f"not replacing it ({summary(replaced)})")
return None
# a writer with a production directory may replace an object written outside of one (e.g. by a validation job)
if mc_prod_info_q.OutputDir != None and is_stale_writer(mc_prod_info_q, mc_prod_info):
print(f"WARNING: {where} was written by masterjob {mc_prod_info_q.MasterJobID}; this job's masterjob "
f"{mc_prod_info.MasterJobID} is older or unknown, so not replacing it ({summary(replaced)})")
return None
print(f"WARNING: {where} differs from the stored one; uploading a new version ({summary(replaced)})")
return upload(mc_prod_info)

# same production: fill the empty fields and keep all stored values
fills = dict(added)
if mc_prod_info_q.OutputDir == None and mc_prod_info.OutputDir != None:
fills["OutputDir"] = (None, mc_prod_info.OutputDir)
stored_key, new_key = _masterjob(mc_prod_info_q), _masterjob(mc_prod_info)
if new_key != None and (stored_key == None or (complete and fills and new_key > stored_key)):
fills["MasterJobID"] = (mc_prod_info_q.MasterJobID, new_key)
if not fills:
print(f"{where} is up to date; not publishing")
return None
if stored_key != None and new_key != None and new_key < stored_key:
print(f"WARNING: {where} was written by a newer masterjob {stored_key}; not filling {summary(fills)}")
return None
print(f"{where}: filling {summary(fills)}")
filled = dataclasses.replace(mc_prod_info_q, Hash = None, **{f: new for f, (_, new) in fills.items()})
# without anything else to fill, a rejected OutputDir is not retried; it would only duplicate the stored version
return upload(filled, {k: v for k, v in stored_raw.items() if k not in fills}, retry = set(fills) != {"OutputDir"})
Loading
Loading