File indexing completed on 2026-12-02 10:28:06
0001 """
0002 Offline check of how analysis brokerage orders site candidates before the maxNumSites cut.
0003
0004 The ordering decides which sites survive truncation, so a site missing from the front cannot be
0005 chosen however good its weight. It used to be dead code: the membership test compared
0006 SiteCandidate objects against a collection of site-name strings, so the data-holding sites were
0007 never moved to the front. Fixing that changes placement for every analysis task, workflow or not,
0008 which is why the non-workflow behaviour is pinned here too.
0009
0010 Run from the repository root: python3 pandajedi/jedibrokerage/tests/workflow_affinity_test.py
0011 """
0012
0013 import os
0014 import random
0015 import sys
0016
0017 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")))
0018
0019 from pandajedi.jedibrokerage.workflow_affinity import (
0020 DEFAULT_WORKFLOW_AFFINITY_FACTOR,
0021 build_anchor_weights,
0022 order_site_candidates,
0023 workflow_affinity_weight,
0024 )
0025
0026
0027 class SiteCandidate:
0028 """Just enough of the real one: the ordering only ever reads these two names"""
0029
0030 def __init__(self, site_name, unified_name):
0031 self.siteName = site_name
0032 self.unifiedName = unified_name
0033
0034
0035 def make_candidate(name):
0036 """A candidate as the broker builds it: pseudo name first, unified name second"""
0037 return SiteCandidate(name, name.split("/")[0])
0038
0039
0040 def names(candidates):
0041 return [c.siteName for c in candidates]
0042
0043
0044 def check(label, condition, detail=""):
0045 print(f" {'PASS' if condition else 'FAIL'} {label}{' ' + str(detail) if detail and not condition else ''}")
0046 return condition
0047
0048
0049 def main():
0050 failures = 0
0051
0052 print("\n=== data-holding sites lead the list ===")
0053
0054 with_data = make_candidate("SITE_A")
0055 without_data = make_candidate("SITE_B")
0056 ordered = order_site_candidates({1.0: [without_data], 2.0: [with_data]}, {"SITE_A"}, [])
0057 failures += not check("the data-holding site is first", names(ordered)[0] == "SITE_A", names(ordered))
0058
0059 ordered = order_site_candidates({5.0: [without_data], 0.1: [with_data]}, {"SITE_A"}, [])
0060 failures += not check("...even when it weighs less than one without data", names(ordered)[0] == "SITE_A", names(ordered))
0061
0062 print("\n=== the membership test names the pseudo site ===")
0063
0064
0065
0066
0067 pseudo = SiteCandidate("SITE_C/pseudo", "SITE_C")
0068 marker = make_candidate("USED")
0069 ordered = order_site_candidates({1.0: [pseudo]}, {"SITE_C/pseudo"}, [marker])
0070 failures += not check("matched by pseudo name", names(ordered) == ["SITE_C/pseudo", "USED"], names(ordered))
0071 ordered = order_site_candidates({1.0: [pseudo]}, {"SITE_C"}, [marker])
0072 failures += not check("not matched by unified name", names(ordered) == ["USED", "SITE_C/pseudo"], names(ordered))
0073
0074 print("\n=== weight still orders within each group ===")
0075 low, mid, high = make_candidate("LOW"), make_candidate("MID"), make_candidate("HIGH")
0076 ordered = order_site_candidates({1.0: [low], 2.0: [mid], 3.0: [high]}, set(), [])
0077 failures += not check("highest weight first", names(ordered) == ["HIGH", "MID", "LOW"], names(ordered))
0078
0079 print("\n=== sites already used by the task keep their place ===")
0080 used = make_candidate("USED")
0081 ordered = order_site_candidates({1.0: [make_candidate("PLAIN")]}, set(), [used])
0082 failures += not check("ahead of sites with no data", names(ordered) == ["USED", "PLAIN"], names(ordered))
0083 ordered = order_site_candidates({1.0: [with_data]}, {"SITE_A"}, [used])
0084 failures += not check("behind sites with data", names(ordered) == ["SITE_A", "USED"], names(ordered))
0085
0086 print("\n=== equal weights are still randomised ===")
0087
0088
0089
0090 everything = [f"SITE_{i:02d}" for i in range(12)]
0091 orders = set()
0092 for _ in range(200):
0093 candidates = [make_candidate(n) for n in everything]
0094 ordered = order_site_candidates({1.0: list(candidates)}, set(everything), [])
0095 orders.add(tuple(names(ordered)))
0096 failures += not check("ties vary across runs when all sites hold data", len(orders) > 1, f"{len(orders)} distinct orders")
0097 failures += not check("...and nothing is lost or duplicated", all(sorted(o) == sorted(everything) for o in orders))
0098
0099 print("\n=== nothing is dropped ===")
0100 candidates = [make_candidate(f"S{i}") for i in range(6)]
0101 ordered = order_site_candidates({1.0: candidates[:3], 2.0: candidates[3:5]}, {"S0", "S3"}, [candidates[5]])
0102 failures += not check("every candidate comes back exactly once", sorted(names(ordered)) == sorted(names(candidates)), names(ordered))
0103
0104 print("\n=== an empty input is not an error ===")
0105 failures += not check("no weights, no used sites", order_site_candidates({}, set(), []) == [])
0106 failures += not check("no weights but used sites", names(order_site_candidates({}, set(), [used])) == ["USED"])
0107
0108 print("\n=== the caller's list is not mutated ===")
0109
0110 weight_map = {1.0: [make_candidate("X"), make_candidate("Y")]}
0111 used_list = [used]
0112 order_site_candidates(weight_map, {"X"}, used_list)
0113 failures += not check("the weight map still holds its candidates", len(weight_map[1.0]) == 2, len(weight_map[1.0]))
0114 failures += not check("the used list is unchanged", len(used_list) == 1, len(used_list))
0115
0116 print("\n=== workflow anchor sites also survive the cut ===")
0117
0118 anchored = SiteCandidate("SITE_W/pseudo", "SITE_W")
0119 plain = make_candidate("SITE_X")
0120 ordered = order_site_candidates({2.0: [plain], 1.0: [anchored]}, set(), [], {"SITE_W"})
0121 failures += not check("an anchor site leads despite a lower weight", names(ordered)[0] == "SITE_W/pseudo", names(ordered))
0122 ordered = order_site_candidates({2.0: [plain], 1.0: [anchored]}, set(), [], None)
0123 failures += not check("without an anchor the weight decides", names(ordered)[0] == "SITE_X", names(ordered))
0124 ordered = order_site_candidates({2.0: [plain], 1.0: [anchored]}, set(), [], set())
0125 failures += not check("an empty anchor changes nothing", names(ordered)[0] == "SITE_X", names(ordered))
0126
0127 print("\n=== the affinity weight is a bounded preference ===")
0128 weights = {"BEST": 10.0, "MIDDLE": 5.0, "NONE": 0.0}
0129 best = workflow_affinity_weight("BEST", weights)
0130 middle = workflow_affinity_weight("MIDDLE", weights)
0131 failures += not check("the strongest anchor earns the full factor", best == DEFAULT_WORKFLOW_AFFINITY_FACTOR, best)
0132 failures += not check("a weaker anchor earns less", 1 < middle < best, middle)
0133
0134 failures += not check("an unknown site is unchanged", workflow_affinity_weight("ELSEWHERE", weights) == 1.0)
0135 failures += not check("a zero-weight site is unchanged", workflow_affinity_weight("NONE", weights) == 1.0)
0136 failures += not check("no anchor at all is unchanged", workflow_affinity_weight("ANY", None) == 1.0)
0137 failures += not check("an empty anchor is unchanged", workflow_affinity_weight("ANY", {}) == 1.0)
0138 failures += not check("all-zero weights are unchanged", workflow_affinity_weight("A", {"A": 0.0}) == 1.0)
0139 failures += not check("the factor is honoured", workflow_affinity_weight("BEST", weights, factor=3.0) == 3.0)
0140
0141 print("\n=== the anchor weighs sites against each other ===")
0142
0143
0144 anchor = build_anchor_weights(input_sites=["SITE_A", "SITE_B", "SITE_B"])
0145 failures += not check("the site more of the workflow points at wins", anchor["SITE_B"] > anchor["SITE_A"], anchor)
0146 failures += not check("...and so earns the larger boost", workflow_affinity_weight("SITE_B", anchor) > workflow_affinity_weight("SITE_A", anchor))
0147
0148 print("\n=== firmer knowledge outweighs prediction ===")
0149 anchor = build_anchor_weights(pinned_sites=["PINNED"], input_sites=["GUESS", "GUESS", "GUESS"])
0150 failures += not check("a pinned sibling beats three predictions", anchor["PINNED"] > anchor["GUESS"], anchor)
0151 anchor = build_anchor_weights(output_sites=["RAN"], input_sites=["GUESS"])
0152 failures += not check("output already written beats a prediction", anchor["RAN"] > anchor["GUESS"], anchor)
0153 anchor = build_anchor_weights(output_sites=["RAN"], input_sites=["GUESS", "GUESS", "GUESS"])
0154 failures += not check("but enough predictions still outweigh one finished step", anchor["GUESS"] > anchor["RAN"], anchor)
0155
0156 print("\n=== a site some step cannot use is no anchor ===")
0157 anchor = build_anchor_weights(input_sites=["GOOD", "BANNED", "BANNED"], vetoed_sites={"BANNED"})
0158 failures += not check("the vetoed site is absent, not merely lighter", "BANNED" not in anchor, anchor)
0159 failures += not check("the rest survives", anchor.get("GOOD") == 1.0, anchor)
0160 failures += not check("nothing at all yields an empty anchor", build_anchor_weights() == {})
0161
0162 print(f"\n{'ALL CHECKS PASSED' if not failures else f'{failures} CHECK(S) FAILED'}")
0163 return 1 if failures else 0
0164
0165
0166 if __name__ == "__main__":
0167 random.seed()
0168 sys.exit(main())