fix(reliability): journal fail-safe serialization, per-line parse, review hardening
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
"""Mark VIII reliability event journal. JSONL on /mmc, rotated."""
|
"""Mark VIII reliability event journal. JSONL on /mmc, rotated."""
|
||||||
import json, os, threading
|
import json, os, threading, time
|
||||||
|
|
||||||
MK8_DIR = '/mmc/mk8'
|
MK8_DIR = '/mmc/mk8'
|
||||||
EVENTS_PATH = os.path.join(MK8_DIR, 'events.log')
|
EVENTS_PATH = os.path.join(MK8_DIR, 'events.log')
|
||||||
@@ -7,11 +7,10 @@ MAX_BYTES = 5 * 1024 * 1024
|
|||||||
KEEP = 4
|
KEEP = 4
|
||||||
_LOCK = threading.Lock()
|
_LOCK = threading.Lock()
|
||||||
|
|
||||||
COUNTER_KINDS = ('boot', 'unexpected_boot', 'rollback', 'restart',
|
|
||||||
'guard_fix')
|
|
||||||
_COUNTER_KEYS = {'boot': 'boots', 'unexpected_boot': 'unexpected_boots',
|
_COUNTER_KEYS = {'boot': 'boots', 'unexpected_boot': 'unexpected_boots',
|
||||||
'rollback': 'rollbacks', 'restart': 'restarts',
|
'rollback': 'rollbacks', 'restart': 'restarts',
|
||||||
'guard_fix': 'guard_fixes'}
|
'guard_fix': 'guard_fixes'}
|
||||||
|
COUNTER_KINDS = tuple(_COUNTER_KEYS)
|
||||||
|
|
||||||
|
|
||||||
def _ensure_dir():
|
def _ensure_dir():
|
||||||
@@ -22,11 +21,23 @@ def _ensure_dir():
|
|||||||
|
|
||||||
|
|
||||||
def log_event(kind, sev='info', msg='', meta=None):
|
def log_event(kind, sev='info', msg='', meta=None):
|
||||||
entry = {'ts': int(__import__('time').time()), 'kind': str(kind),
|
"""Append one journal entry. Never raises: a reliability journal that
|
||||||
'sev': sev, 'msg': msg[:500]}
|
can crash its caller would defeat its purpose. Single-writer per
|
||||||
if meta:
|
process is assumed; there is no inter-process lock."""
|
||||||
entry['meta'] = meta
|
try:
|
||||||
line = json.dumps(entry) + '\n'
|
entry = {'ts': int(time.time()), 'kind': str(kind), 'sev': sev,
|
||||||
|
'msg': str(msg)[:500]}
|
||||||
|
if meta is not None:
|
||||||
|
json.dumps(meta)
|
||||||
|
entry['meta'] = meta
|
||||||
|
line = json.dumps(entry) + '\n'
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
line = json.dumps({'ts': int(time.time()), 'kind': str(kind),
|
||||||
|
'sev': sev, 'msg': str(msg)[:500],
|
||||||
|
'meta_repr': repr(meta)[:500]}) + '\n'
|
||||||
|
except Exception:
|
||||||
|
return
|
||||||
with _LOCK:
|
with _LOCK:
|
||||||
_ensure_dir()
|
_ensure_dir()
|
||||||
try:
|
try:
|
||||||
@@ -52,8 +63,16 @@ def read_events(limit=100):
|
|||||||
for path in paths:
|
for path in paths:
|
||||||
try:
|
try:
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
out.extend(json.loads(l) for l in f if l.strip())
|
for l in f:
|
||||||
except (OSError, ValueError):
|
if not l.strip():
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
row = json.loads(l)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if isinstance(row, dict):
|
||||||
|
out.append(row)
|
||||||
|
except OSError:
|
||||||
continue
|
continue
|
||||||
out.sort(key=lambda r: r.get('ts', 0))
|
out.sort(key=lambda r: r.get('ts', 0))
|
||||||
return out[-limit:][::-1]
|
return out[-limit:][::-1]
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ class EventsTest(unittest.TestCase):
|
|||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
mk8_events.MK8_DIR = self.old
|
mk8_events.MK8_DIR = self.old
|
||||||
|
mk8_events.EVENTS_PATH = os.path.join(self.old, 'events.log')
|
||||||
|
mk8_events.MAX_BYTES = 5 * 1024 * 1024
|
||||||
|
|
||||||
def test_log_and_read_newest_first(self):
|
def test_log_and_read_newest_first(self):
|
||||||
mk8_events.log_event('boot', msg='first')
|
mk8_events.log_event('boot', msg='first')
|
||||||
|
|||||||
Reference in New Issue
Block a user