def _build_convergence_loop_table(): """ Create the ``TransitionTable`` needed by the convergence loop FSM. :return TransitionTable: The transition table for the state machine for converging on the cluster configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition(S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ([O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) return table
def build_convergence_loop_fsm(reactor, deployer): """ Create a convergence loop FSM. :param IReactorTime reactor: Used to schedule delays in the loop. :param IDeployer deployer: Used to discover local state and calcualte necessary changes to match desired configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition( S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.ITERATION_DONE: ([O.CONVERGE], S.CONVERGING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.ITERATION_DONE: ([], S.STOPPED), }) loop = ConvergenceLoop(reactor, deployer) fsm = constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.STOPPED, table=table, richInputs=[_ClientStatusUpdate], inputContext={}, world=MethodSuffixOutputer(loop)) loop.fsm = fsm return fsm
def _build_convergence_loop_table(): """ Create the ``TransitionTable`` needed by the convergence loop FSM. :return TransitionTable: The transition table for the state machine for converging on the cluster configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition( S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ( [O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) return table
def build_cluster_status_fsm(convergence_loop_fsm): """ Create a new cluster status FSM. The automatic reconnection logic is handled by the ``AgentLoopService``; the world object here just gets notified of disconnects, it need schedule the reconnect itself. :param convergence_loop_fsm: A convergence loop FSM as output by ``build_convergence_loop_fsm``. """ S = ClusterStatusStates I = ClusterStatusInputs O = ClusterStatusOutputs table = TransitionTable() # We may be shut down in any state, in which case we disconnect if # necessary. table = table.addTransitions( S.DISCONNECTED, { # Store the client, then wait for cluster status to be sent # over AMP: I.CONNECTED_TO_CONTROL_SERVICE: ([O.STORE_CLIENT], S.IGNORANT), I.SHUTDOWN: ([], S.SHUTDOWN), }, ) table = table.addTransitions( S.IGNORANT, { # We never told agent to start, so no need to tell it to stop: I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.DISCONNECTED), # Tell agent latest cluster status, implicitly starting it: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.SHUTDOWN: ([O.DISCONNECT], S.SHUTDOWN), }, ) table = table.addTransitions( S.KNOWLEDGEABLE, { # Tell agent latest cluster status: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.DISCONNECTED_FROM_CONTROL_SERVICE: ([O.STOP], S.DISCONNECTED), I.SHUTDOWN: ([O.STOP, O.DISCONNECT], S.SHUTDOWN), }, ) table = table.addTransitions( S.SHUTDOWN, {I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.SHUTDOWN), I.STATUS_UPDATE: ([], S.SHUTDOWN)} ) return constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.DISCONNECTED, table=table, richInputs=[_ConnectedToControlService, _StatusUpdate], inputContext={}, world=MethodSuffixOutputer(ClusterStatus(convergence_loop_fsm)), )
def test_addTransitionsDoesNotMutate(self): """ L{TransitionTable.addTransitions} does not change the L{TransitionTable} it is called on. """ table = TransitionTable({"foo": {"bar": Transition("baz", "quux")}}) table.addTransitions("apple", {"banana": ("clementine", "date")}) self.assertEqual({"foo": {"bar": Transition("baz", "quux")}}, table.table)
def build_cluster_status_fsm(convergence_loop_fsm): """ Create a new cluster status FSM. The automatic reconnection logic is handled by the ``AgentLoopService``; the world object here just gets notified of disconnects, it need schedule the reconnect itself. :param convergence_loop_fsm: A convergence loop FSM as output by ``build_convergence_loop_fsm``. """ S = ClusterStatusStates I = ClusterStatusInputs O = ClusterStatusOutputs table = TransitionTable() # We may be shut down in any state, in which case we disconnect if # necessary. table = table.addTransitions( S.DISCONNECTED, { # Store the client, then wait for cluster status to be sent # over AMP: I.CONNECTED_TO_CONTROL_SERVICE: ([O.STORE_CLIENT], S.IGNORANT), I.SHUTDOWN: ([], S.SHUTDOWN), }) table = table.addTransitions( S.IGNORANT, { # We never told agent to start, so no need to tell it to stop: I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.DISCONNECTED), # Tell agent latest cluster status, implicitly starting it: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.SHUTDOWN: ([O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.KNOWLEDGEABLE, { # Tell agent latest cluster status: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.DISCONNECTED_FROM_CONTROL_SERVICE: ([O.STOP], S.DISCONNECTED), I.SHUTDOWN: ([O.STOP, O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.SHUTDOWN, { I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.SHUTDOWN), I.STATUS_UPDATE: ([], S.SHUTDOWN), }) return constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.DISCONNECTED, table=table, richInputs=[_ConnectedToControlService, _StatusUpdate], inputContext={}, world=MethodSuffixOutputer(ClusterStatus(convergence_loop_fsm)))
def test_addTransitionsDoesNotMutate(self): """ L{TransitionTable.addTransitions} does not change the L{TransitionTable} it is called on. """ table = TransitionTable({"foo": {"bar": Transition("baz", "quux")}}) table.addTransitions("apple", {"banana": ("clementine", "date")}) self.assertEqual({"foo": { "bar": Transition("baz", "quux") }}, table.table)
def _build_cluster_status_fsm_table(): """ Create the ``TransitionTable`` needed by the cluster status FSM. :return TransitionTable: The transition table for the state machine for keeping track of cluster state and configuration. """ S = ClusterStatusStates I = ClusterStatusInputs O = ClusterStatusOutputs table = TransitionTable() # We may be shut down in any state, in which case we disconnect if # necessary. table = table.addTransitions( S.DISCONNECTED, { # Store the client, then wait for cluster status to be sent # over AMP: I.CONNECTED_TO_CONTROL_SERVICE: ([O.STORE_CLIENT], S.IGNORANT), I.SHUTDOWN: ([], S.SHUTDOWN), }) table = table.addTransitions( S.IGNORANT, { # We never told agent to start, so no need to tell it to stop: I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.DISCONNECTED), # Tell agent latest cluster status, implicitly starting it: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.SHUTDOWN: ([O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.KNOWLEDGEABLE, { # Tell agent latest cluster status: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.DISCONNECTED_FROM_CONTROL_SERVICE: ([O.STOP], S.DISCONNECTED), I.SHUTDOWN: ([O.STOP, O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.SHUTDOWN, { I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.SHUTDOWN), I.STATUS_UPDATE: ([], S.SHUTDOWN), }) return table
def test_addTransitions(self): """ L{TransitionTable.addTransitions} accepts a state and a mapping from inputs to output, next state pairs and adds all of those transitions to the given state to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransitions( "apple", { "banana": ("clementine", "date"), "eggplant": ("fig", "grape")}) self.assertEqual( {"apple": { "banana": Transition("clementine", "date"), "eggplant": Transition("fig", "grape")}}, more.table)
def test_addTransitions(self): """ L{TransitionTable.addTransitions} accepts a state and a mapping from inputs to output, next state pairs and adds all of those transitions to the given state to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransitions("apple", { "banana": ("clementine", "date"), "eggplant": ("fig", "grape") }) self.assertEqual( { "apple": { "banana": Transition("clementine", "date"), "eggplant": Transition("fig", "grape") } }, more.table)
# end table def # begin first transition table = table.addTransition( State.LOCKED, Input.FARE_PAID, [Output.DISENGAGE_LOCK], State.ACTIVE) # end first transition # begin second transition table = table.addTransition( State.UNLOCKED, Input.ARM_TURNED, [Output.ENGAGE_LOCK], State.ACTIVE) # end second transition # begin last transitions table = table.addTransitions( State.ACTIVE, { Input.ARM_UNLOCKED: ([], State.UNLOCKED), Input.ARM_LOCKED: ([], State.LOCKED), }) # end last transitions # begin outputer from machinist import MethodSuffixOutputer class Outputer(object): def output_ENGAGE_LOCK(self, engage): print("Engaging the lock.") def output_DISENGAGE_LOCK(self, disengage): print("Disengaging the lock.") outputer = MethodSuffixOutputer(Outputer())
# end table def # begin first transition table = table.addTransition(State.LOCKED, Input.FARE_PAID, [Output.DISENGAGE_LOCK], State.ACTIVE) # end first transition # begin second transition table = table.addTransition(State.UNLOCKED, Input.ARM_TURNED, [Output.ENGAGE_LOCK], State.ACTIVE) # end second transition # begin last transitions table = table.addTransitions( State.ACTIVE, { Input.ARM_UNLOCKED: ([], State.UNLOCKED), Input.ARM_LOCKED: ([], State.LOCKED), }) # end last transitions # begin outputer from machinist import MethodSuffixOutputer class Outputer(object): def output_ENGAGE_LOCK(self, engage): print("Engaging the lock.") def output_DISENGAGE_LOCK(self, disengage): print("Disengaging the lock.")
def build_convergence_loop_fsm(reactor, deployer): """ Create a convergence loop FSM. Once cluster config+cluster state updates from control service are received the basic loop is: 1. Discover local state. 2. Calculate ``IStateChanges`` based on local state and cluster configuration and cluster state we received from control service. 3. Execute the change. 4. Sleep. However, if an update is received during sleep then we calculate based on that updated config+state whether a ``IStateChange`` needs to happen. If it does that means this change will have impact on what we do, so we interrupt the sleep. If calculation suggests a no-op then we keep sleeping. Notably we do **not** do a discovery of local state when an update is received while sleeping, since that is an expensive operation that can involve talking to external resources. Moreover an external update only implies external state/config changed, so we're not interested in the latest local state in trying to decide if this update requires us to do something; a recently cached version should suffice. :param IReactorTime reactor: Used to schedule delays in the loop. :param IDeployer deployer: Used to discover local state and calcualte necessary changes to match desired configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition( S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ( [O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) loop = ConvergenceLoop(reactor, deployer) fsm = constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.STOPPED, table=table, richInputs=[_ClientStatusUpdate, _Sleep], inputContext={}, world=MethodSuffixOutputer(loop)) loop.fsm = fsm return fsm
def build_convergence_loop_fsm(reactor, deployer): """ Create a convergence loop FSM. Once cluster config+cluster state updates from control service are received the basic loop is: 1. Discover local state. 2. Calculate ``IStateChanges`` based on local state and cluster configuration and cluster state we received from control service. 3. Execute the change. 4. Sleep. However, if an update is received during sleep then we calculate based on that updated config+state whether a ``IStateChange`` needs to happen. If it does that means this change will have impact on what we do, so we interrupt the sleep. If calculation suggests a no-op then we keep sleeping. Notably we do **not** do a discovery of local state when an update is received while sleeping, since that is an expensive operation that can involve talking to external resources. Moreover an external update only implies external state/config changed, so we're not interested in the latest local state in trying to decide if this update requires us to do something; a recently cached version should suffice. :param IReactorTime reactor: Used to schedule delays in the loop. :param IDeployer deployer: Used to discover local state and calcualte necessary changes to match desired configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition(S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ([O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) loop = ConvergenceLoop(reactor, deployer) fsm = constructFiniteStateMachine(inputs=I, outputs=O, states=S, initial=S.STOPPED, table=table, richInputs=[_ClientStatusUpdate, _Sleep], inputContext={}, world=MethodSuffixOutputer(loop)) loop.fsm = fsm return fsm
ARM_TURNED = NamedConstant() ARM_LOCKED = NamedConstant() class TurnstileOutput(Names): ENGAGE_LOCK = NamedConstant() DISENGAGE_LOCK = NamedConstant() class TurnstileState(Names): LOCKED = NamedConstant() UNLOCKED = NamedConstant() ACTIVE = NamedConstant() table = TransitionTable() table = table.addTransitions( TurnstileState.UNLOCKED, { TurnstileInput.ARM_TURNED: ([TurnstileOutput.ENGAGE_LOCK], TurnstileState.ACTIVE), }) table = table.addTransitions( TurnstileState.ACTIVE, { TurnstileInput.ARM_LOCKED: ([], TurnstileState.LOCKED), TurnstileInput.ARM_UNLOCKED: ([], TurnstileState.UNLOCKED), }) table = table.addTransitions( TurnstileState.LOCKED, { TurnstileInput.FARE_PAID: ([TurnstileOutput.DISENGAGE_LOCK], TurnstileState.ACTIVE), }) class Turnstile(object): def __init__(self, hardware):