ceph/qa/tasks/thrasher.py
Nitzan Mordechai a035b5a22f thrashers: standardize stop and join method names
Thrashers that do not inherit from ThrasherGreenlet previously used a
method called do_join, which combined stop and join functionality. To
ensure consistency and clarity, we want all thrashers to use separate
stop, join, and stop_and_join methods.

This commit renames methods and implements missing stop and stop_and_join
methods in thrashers that did not inherit from ThrasherGreenlet.

Fixes: https://tracker.ceph.com/issues/66698
Signed-off-by: Nitzan Mordechai <nmordech@redhat.com>
2024-08-06 06:57:40 +00:00

56 lines
1.3 KiB
Python

"""
Thrasher base class
"""
from gevent.greenlet import Greenlet
from gevent.event import Event
class Thrasher(object):
def __init__(self):
super(Thrasher, self).__init__()
self._exception = None
@property
def exception(self):
return self._exception
def set_thrasher_exception(self, e):
self._exception = e
def stop(self):
raise NotImplementedError("Subclasses didn't implement this method.")
class ThrasherGreenlet(Thrasher, Greenlet):
class Stopped(Exception): ...
def __init__(self):
super(ThrasherGreenlet, self).__init__()
self._should_stop_event = Event()
@property
def is_stopped(self):
return self._should_stop_event.is_set()
def stop(self):
self._should_stop_event.set()
def set_thrasher_exception(self, e):
if not isinstance(e, self.Stopped):
super(ThrasherGreenlet, self).set_thrasher_exception(e)
def proceed_unless_stopped(self):
self.sleep_unless_stopped(0, raise_stopped=True)
def sleep_unless_stopped(self, seconds, raise_stopped = True):
self._should_stop_event.wait(seconds)
if self.is_stopped and raise_stopped:
raise self.Stopped()
return not self.is_stopped
def stop_and_join(self):
self.stop()
return self.join()