Skip to content

Commit

Permalink
Raise RetryError if Zino1 is flaky on getattrs response
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf authored Dec 5, 2023
1 parent 5630054 commit 9e45585
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/zinolib/controllers/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class Zino1Error(ZinoError):
pass


class RetryError(Zino1Error):
pass


def convert_timestamp(timestamp: int) -> datetime:
return datetime.fromtimestamp(timestamp, timezone.utc)

Expand Down Expand Up @@ -248,6 +252,14 @@ class EventAdapter:
def get_attrlist(request, event_id: int):
return request.get_raw_attributes(event_id)

@staticmethod
def validate_raw_attrlist(attrlist):
for item in attrlist:
if ':' not in item:
LOG.error('"getattrs" is spewing garbage: %s', attrlist)
return False
return True

@classmethod
@log_exception_with_params(LOG)
def attrlist_to_attrdict(cls, attrlist: Iterable[str]) -> Dict[str, str]:
Expand Down Expand Up @@ -464,6 +476,8 @@ def get_events(self):
def create_event_from_id(self, event_id: int):
self._verify_session()
attrlist = self.rename_exception(self._event_adapter.get_attrlist, self.session.request, event_id)
if not self._event_adapter.validate_raw_attrlist(attrlist):
raise RetryError('Zino 1 is flaking out, retry')
attrdict = self._event_adapter.attrlist_to_attrdict(attrlist)
attrdict = self._event_adapter.convert_values(attrdict)
return Event.create(attrdict)
Expand Down
17 changes: 16 additions & 1 deletion tests/test_zinolib_controllers_zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime, timedelta, timezone

from zinolib.event_types import AdmState, Event, HistoryEntry, LogEntry
from zinolib.controllers.zino1 import EventAdapter, HistoryAdapter, LogAdapter, SessionAdapter, Zino1EventManager, UpdateHandler
from zinolib.controllers.zino1 import EventAdapter, HistoryAdapter, LogAdapter, SessionAdapter, Zino1EventManager, UpdateHandler,RetryError
from zinolib.ritz import NotifierResponse

raw_event_id = 139110
Expand Down Expand Up @@ -49,6 +49,10 @@ def get_attrlist(request, event_id: int):
def attrlist_to_attrdict(cls, attrlist):
return EventAdapter.attrlist_to_attrdict(attrlist)

@staticmethod
def validate_raw_attrlist(attrlist):
return True

@classmethod
def convert_values(cls, attrdict):
return EventAdapter.convert_values(attrdict)
Expand Down Expand Up @@ -102,6 +106,17 @@ def init_manager(self):
zino1 = FakeZino1EventManager.configure(None)
return zino1

def test_create_event_from_id_may_get_garabage_data(self):
def falsey(_):
return False

zino1 = self.init_manager()
old = zino1._event_adapter.validate_raw_attrlist
zino1._event_adapter.validate_raw_attrlist = staticmethod(falsey)
with self.assertRaises(RetryError):
zino1.create_event_from_id(139110)
zino1._event_adapter.validate_raw_attrlist = old

def test_get_events(self):
zino1 = self.init_manager()
self.assertEqual(len(zino1.events), 0)
Expand Down

0 comments on commit 9e45585

Please sign in to comment.