From 81964183e8364b4ac9c4c0778741d668908fd518 Mon Sep 17 00:00:00 2001 From: pavel Date: Wed, 8 Apr 2026 13:47:31 +0300 Subject: [PATCH] 1347 --- files/nats_registration_listener.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/files/nats_registration_listener.py b/files/nats_registration_listener.py index f2f0852..2bd37e4 100644 --- a/files/nats_registration_listener.py +++ b/files/nats_registration_listener.py @@ -125,6 +125,10 @@ NB_PROBLEM_LOCK = asyncio.Lock() # Structured event log output (JSON Lines) EVENT_LOG_PATH = os.environ.get("EVENT_LOG_PATH", "/opt/containers/nats-registration-listener/logs/registration_events.jsonl") +EVENODD = (os.environ.get("EVENODD", "") or "").strip().lower() +if EVENODD not in ("", "even", "odd"): + raise SystemExit("ERROR: EVENODD must be unset, 'even', or 'odd'") + # ========================= # Arg parsing @@ -296,6 +300,18 @@ def normalize_mac(mac: str) -> Optional[str]: return None +def mac_belongs_to_this_shard(mac_norm: Optional[str]) -> bool: + if EVENODD == "": + return True + if not mac_norm: + return True + md5_hex = hashlib.md5(mac_norm.encode("utf-8")).hexdigest() + last_nibble = int(md5_hex[-1], 16) + if EVENODD == "even": + return (last_nibble % 2) == 0 + return (last_nibble % 2) == 1 + + IGNORE_MACS = {m for m in (normalize_mac(x) for x in IGNORE_MACS_RAW) if m} @@ -584,11 +600,11 @@ async def main(): recent_payloads.pop(k, None) product = mac = fw = "-" + obj = None try: text = payload.decode("utf-8", errors="replace") obj = json.loads(text) product, mac, fw = extract_fields(obj) - event["reg_age_s"] = extract_registration_age_s(obj) except Exception: pass @@ -597,7 +613,15 @@ async def main(): event["fw"] = fw mac_norm = normalize_mac(mac) + if not mac_belongs_to_this_shard(mac_norm): + return + event["mac_norm"] = mac_norm + if obj is not None: + try: + event["reg_age_s"] = extract_registration_age_s(obj) + except Exception: + pass if mac_norm in IGNORE_MACS: event["ignored"] = True event["ignore_reason"] = "mac_ignore_list"