diff --git a/chv-guest/chv-broker.py b/chv-guest/chv-broker.py index 0b3283f..0d55ab8 100644 --- a/chv-guest/chv-broker.py +++ b/chv-guest/chv-broker.py @@ -89,7 +89,12 @@ def peer_allowed_multi(peer_uid, peer_pid, cell_pids, proc_root="/proc"): return False, "uid %d 不在单元映射范围 [%d, %d)" % ( peer_uid, UID_BASE, UID_BASE + UID_COUNT) if not cell_pids: - return True, "uid 在范围内(无 cell pid 文件,未做 pid ns 对照)" + # part 5:reason 文案与单 cell 版 peer_allowed 对齐,保留 + # “cell.pid 缺失”字样——P2-9 驱动 grep 该串认降级日志; + # part 4 切到 multi 版时把这里改写成“无 cell pid 文件”, + # 该串自此再也没进过 journal,是 RETEST4 + # degraded_logged=False 的根因(不是时序、不是日志路径)。 + return True, "uid 在范围内(cell.pid 缺失,未做 pid ns 对照)" peer_ns = _read_ns_link(peer_pid, proc_root) if peer_ns is None: return False, "对端 pid ns 读不到,拒绝" @@ -131,6 +136,19 @@ def _read_cell_pids(cell_dir="/run/chv"): return pids +def _degraded_warning(reason, pid, uid): + """降级警告行:reason 原样进日志。serve() 在无 cell pid 文件 + 时打这条;P2-9 驱动 grep 'cell.pid 缺失' 认的就是它(part 5: + part 4 之前 serve 直接打印含该串的警告行,RETEST3 同项为 + True;part 4 切到 peer_allowed_multi 后警告行改写成 + “无 cell pid 文件”,该串再也没进过 journal——RETEST4 的 + degraded_logged=False 即由此来,不是时序、不是日志路径 + 变化。reason 与警告行分开写曾让两者悄悄漂移,今后改文案 + 只改 peer_allowed_multi 的 reason,警告行自动跟随)。""" + return ("chv-broker: 警告:%s (pid=%d uid=%d)" + % (reason, pid, uid)) + + def _resolve_repo(repo): """仓目录只允许 ~/workspace 下。repo 为空时返回 None(由 chv-sync 自己取唯一仓)。返回解析后的绝对路径或抛 ValueError。""" @@ -267,9 +285,8 @@ def serve(sock_path=BROKER_SOCK, browser_sock=BROWSER_SOCK): conn.close() continue if not cell_pids: - print("chv-broker: 警告:无 cell pid 文件,只做了 uid 检查 " - "(pid=%d uid=%d)" % (pid, uid), file=sys.stderr, - flush=True) + print(_degraded_warning(reason, pid, uid), + file=sys.stderr, flush=True) conn.settimeout(310) data = b"" while len(data) < MAX_REQUEST_BYTES: diff --git a/cloud/tests/test_chv_muse_p2.py b/cloud/tests/test_chv_muse_p2.py index c2daa17..41d7af6 100644 --- a/cloud/tests/test_chv_muse_p2.py +++ b/cloud/tests/test_chv_muse_p2.py @@ -1086,6 +1086,21 @@ class BrokerMultiTest(unittest.TestCase): ok, reason = BROKER.peer_allowed_multi(1000, 200, []) self.assertTrue(ok) self.assertIn("未做 pid ns 对照", reason) + # part 5:reason 须保留“cell.pid 缺失”字样——P2-9 驱动 + # grep 该串认降级日志;part 4 切到 multi 版时改写成 + # “无 cell pid 文件”,该串再也没进过 journal, + # RETEST4 的 degraded_logged 即因此为 False。 + self.assertIn("cell.pid 缺失", reason) + + def test_degraded_warning_carries_probe_string(self): + # serve() 打的警告行就是 _degraded_warning 的返回值: + # reason 原样进日志,journalctl grep 'cell.pid 缺失' + # 才能命中;pid/uid 也在行里(排障用)。 + _, reason = BROKER.peer_allowed_multi(1000, 200, []) + line = BROKER._degraded_warning(reason, 123, 1000) + self.assertIn("cell.pid 缺失", line) + self.assertIn("pid=123", line) + self.assertIn("uid=1000", line) def test_read_cell_pids_glob_and_legacy(self): with tempfile.TemporaryDirectory() as tmp: