perf(healing): scan heal history from file tail instead of full read
- add tail-based HEAL line lookup to avoid O(file-size) reads in should_heal() - keep a safe fallback full scan when no HEAL marker is found in tail window - add regression test for large history files with recent HEAL marker
This commit is contained in:
1 parent
55cc29071e
commit
9edce6b31d
2 files changed
+55
-5
No files matched your search
+43
-5
@@ -4,6 +4,47 @@ import time
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
|
||||||
|
def _read_last_heal_line(history_file, chunk_size=4096, max_scan_bytes=262144):
|
||||||
|
"""
|
||||||
|
Returns the most recent line containing 'HEAL:' by scanning from file tail.
|
||||||
|
Falls back to a full forward scan if no marker is found in the tail window.
|
||||||
|
"""
|
||||||
|
file_size = os.path.getsize(history_file)
|
||||||
|
if file_size <= 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
with open(history_file, "rb") as f:
|
||||||
|
pos = file_size
|
||||||
|
scanned = 0
|
||||||
|
buffer = b""
|
||||||
|
|
||||||
|
while pos > 0 and scanned < max_scan_bytes:
|
||||||
|
to_read = min(chunk_size, pos, max_scan_bytes - scanned)
|
||||||
|
pos -= to_read
|
||||||
|
f.seek(pos)
|
||||||
|
chunk = f.read(to_read)
|
||||||
|
buffer = chunk + buffer
|
||||||
|
scanned += to_read
|
||||||
|
|
||||||
|
lines = buffer.splitlines()
|
||||||
|
if pos > 0 and lines:
|
||||||
|
# Drop potential partial line at the chunk boundary.
|
||||||
|
lines = lines[1:]
|
||||||
|
|
||||||
|
for raw_line in reversed(lines):
|
||||||
|
line = raw_line.decode("utf-8", errors="replace").strip()
|
||||||
|
if "HEAL:" in line:
|
||||||
|
return line
|
||||||
|
|
||||||
|
# Fallback for unusual files where recent tail has no HEAL marker.
|
||||||
|
last_heal_line = None
|
||||||
|
with open(history_file) as f:
|
||||||
|
for line in f:
|
||||||
|
if "HEAL:" in line:
|
||||||
|
last_heal_line = line.strip()
|
||||||
|
return last_heal_line
|
||||||
|
|
||||||
|
|
||||||
def should_heal(history_file, drift_fingerprint, min_interval=900):
|
def should_heal(history_file, drift_fingerprint, min_interval=900):
|
||||||
"""
|
"""
|
||||||
Prevents heal flapping by suppressing repeated no-op heals.
|
Prevents heal flapping by suppressing repeated no-op heals.
|
||||||
@@ -12,13 +53,10 @@ def should_heal(history_file, drift_fingerprint, min_interval=900):
|
|||||||
if not os.path.exists(history_file):
|
if not os.path.exists(history_file):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
with open(history_file) as f:
|
last = _read_last_heal_line(history_file)
|
||||||
lines = [l.strip() for l in f.readlines() if "HEAL:" in l]
|
if not last:
|
||||||
|
|
||||||
if not lines:
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
last = lines[-1]
|
|
||||||
ts_str, msg = last.split(" - ", 1)
|
ts_str, msg = last.split(" - ", 1)
|
||||||
last_time = datetime.fromisoformat(ts_str)
|
last_time = datetime.fromisoformat(ts_str)
|
||||||
last_fingerprint = ""
|
last_fingerprint = ""
|
||||||
|
|||||||
@@ -52,6 +52,18 @@ class HealGateTests(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertTrue(should_heal(path, "xyz999", min_interval=900))
|
self.assertTrue(should_heal(path, "xyz999", min_interval=900))
|
||||||
|
|
||||||
|
def test_should_heal_reads_recent_heal_from_tail(self):
|
||||||
|
now = datetime.now().isoformat()
|
||||||
|
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
|
||||||
|
for idx in range(4000):
|
||||||
|
tmp.write(f"noise line {idx}\n")
|
||||||
|
tmp.write(
|
||||||
|
f"{now} - HEAL: drift_fingerprint=tail123 heal_result=success post_drift_fingerprint=\n"
|
||||||
|
)
|
||||||
|
path = tmp.name
|
||||||
|
|
||||||
|
self.assertFalse(should_heal(path, "tail123", min_interval=900))
|
||||||
|
|
||||||
|
|
||||||
class PostHealVerificationTests(unittest.TestCase):
|
class PostHealVerificationTests(unittest.TestCase):
|
||||||
@patch("healing.time.sleep", return_value=None)
|
@patch("healing.time.sleep", return_value=None)
|
||||||
|
|||||||
Reference in new issue
Block a user