Last active
April 2, 2021 19:26
-
-
Save samueleresca/b0da0754e3bb86c12c822c0c80a8a3f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import time | |
from atomos.atomic import AtomicReference | |
from src._heartbeat_history import _HeartbeatHistory | |
from src._state import _State | |
class PhiAccrualFailureDetector: | |
""" | |
The φ Accrual Failure Detector implementation. | |
See: | |
https://github.com/akka/akka/blob/master/akka-remote/src/main/scala/akka/remote/PhiAccrualFailureDetector.scala | |
""" | |
# ... | |
def heartbeat(self) -> None: | |
""" | |
Add an heartbeat to the state of the instance. | |
""" | |
timestamp = self._get_time() | |
old_state = self._state.get() | |
if old_state.timestamp is None: | |
new_history = self._first_heartbeat() | |
else: | |
latest_timestamp = old_state.timestamp | |
interval = timestamp - latest_timestamp | |
new_history = old_state.history | |
if self._is_available(timestamp): | |
new_history += interval | |
new_state = _State(history=new_history, timestamp=timestamp) | |
# Eventually it retry the heartbeat operation when a concurrent process modify the state | |
if not self._state.compare_and_set(old_state, new_state): | |
self.heartbeat() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment