예제 #1
0
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
예제 #2
0
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
예제 #3
0
파일: _loop.py 프로젝트: jaggerliu/flocker
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
예제 #4
0
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)),
    )
예제 #5
0
 def test_addTerminalState(self):
     """
     L{TransitionTable.addTerminalState} returns a L{TransitionTable} that
     includes the given state in its table with no transitions defined.
     """
     table = TransitionTable()
     more = table.addTerminalState("foo")
     self.assertEqual({"foo": {}}, more.table)
예제 #6
0
 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)
예제 #7
0
 def test_addTerminalState(self):
     """
     L{TransitionTable.addTerminalState} returns a L{TransitionTable} that
     includes the given state in its table with no transitions defined.
     """
     table = TransitionTable()
     more = table.addTerminalState("foo")
     self.assertEqual({"foo": {}}, more.table)
예제 #8
0
 def test_addTransition(self):
     """
     L{TransitionTable.addTransition} accepts a state, an input, an output,
     and a next state and adds the transition defined by those four values
     to a new L{TransitionTable} which it returns.
     """
     table = TransitionTable()
     more = table.addTransition("foo", "bar", "baz", "quux")
     self.assertEqual({"foo": {"bar": Transition("baz", "quux")}}, more.table)
예제 #9
0
 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)
예제 #10
0
 def test_addTransition(self):
     """
     L{TransitionTable.addTransition} accepts a state, an input, an output,
     and a next state and adds the transition defined by those four values
     to a new L{TransitionTable} which it returns.
     """
     table = TransitionTable()
     more = table.addTransition("foo", "bar", "baz", "quux")
     self.assertEqual({"foo": {
         "bar": Transition("baz", "quux")
     }}, more.table)
예제 #11
0
 def test_extraInputContext(self):
     """
     L{ExtraInputContext} is raised if there are keys in C{inputContext}
     which are not symbols in the output alphabet.
     """
     extra = object()
     transitions = TransitionTable()
     transitions = transitions.addTransition(State.amber, Input.apple,
                                             [Output.aardvark], State.amber)
     exc = self.assertRaises(ExtraInputContext, constructFiniteStateMachine,
                             Input, Output, State, transitions, State.amber,
                             [], {extra: None}, NULL_WORLD)
     self.assertEqual(({extra}, ), exc.args)
예제 #12
0
 def test_invalidInitialState(self):
     """
     L{InvalidInitialState} is raised if the value given for C{initial} is
     not defined by C{state}.
     """
     extra = object()
     transitions = TransitionTable()
     transitions = transitions.addTransition(State.amber, Input.apple,
                                             [Output.aardvark], State.amber)
     exc = self.assertRaises(InvalidInitialState,
                             constructFiniteStateMachine, Input, Output,
                             State, transitions, extra, [], {}, NULL_WORLD)
     self.assertEqual((extra, ), exc.args)
예제 #13
0
    def test_richInputInterface(self):
        """
        L{DoesNotImplement} is raised if a rich input type is given which does
        not implement the interface required by one of the outputs which can be
        produced when that input is received.
        """
        apple = trivialInput(Input.apple)
        transitions = TransitionTable()
        transitions = transitions.addTransition(State.amber, Input.apple,
                                                [Output.aardvark], State.amber)

        self.assertRaises(DoesNotImplement, constructFiniteStateMachine, Input,
                          Output, State, transitions, State.amber, [apple],
                          {Output.aardvark: IRequiredByAardvark}, NULL_WORLD)
예제 #14
0
    def test_nextStateNotMissingIfInitial(self):
        """
        L{MissingTransitionNextState} is not raised if a value defined by
        C{state} appears nowhere in C{transitions} as a next state but is given
        as C{initial}.
        """
        transitions = TransitionTable()
        transitions = transitions.addTransition(MoreState.amber, Input.apple,
                                                [Output.aardvark],
                                                MoreState.amber)
        transitions = transitions.addTerminalState(MoreState.blue)

        constructFiniteStateMachine(Input, Output, MoreState, transitions,
                                    MoreState.blue, [], {}, NULL_WORLD)
예제 #15
0
    def test_nextStateNotMissingIfInitial(self):
        """
        L{MissingTransitionNextState} is not raised if a value defined by
        C{state} appears nowhere in C{transitions} as a next state but is given
        as C{initial}.
        """
        transitions = TransitionTable()
        transitions = transitions.addTransition(
            MoreState.amber, Input.apple, [Output.aardvark], MoreState.amber)
        transitions = transitions.addTerminalState(MoreState.blue)

        constructFiniteStateMachine(
            Input, Output, MoreState, transitions,
            MoreState.blue, [], {}, NULL_WORLD)
예제 #16
0
 def test_extraInputContext(self):
     """
     L{ExtraInputContext} is raised if there are keys in C{inputContext}
     which are not symbols in the output alphabet.
     """
     extra = object()
     transitions = TransitionTable()
     transitions = transitions.addTransition(
         State.amber, Input.apple, [Output.aardvark], State.amber)
     exc = self.assertRaises(
         ExtraInputContext,
         constructFiniteStateMachine,
         Input, Output, State, transitions,
         State.amber, [], {extra: None}, NULL_WORLD)
     self.assertEqual(({extra},), exc.args)
예제 #17
0
 def test_invalidInitialState(self):
     """
     L{InvalidInitialState} is raised if the value given for C{initial} is
     not defined by C{state}.
     """
     extra = object()
     transitions = TransitionTable()
     transitions = transitions.addTransition(
         State.amber, Input.apple, [Output.aardvark], State.amber)
     exc = self.assertRaises(
         InvalidInitialState,
         constructFiniteStateMachine,
         Input, Output, State, transitions,
         extra, [], {}, NULL_WORLD)
     self.assertEqual((extra,), exc.args)
예제 #18
0
 def test_empty(self):
     """
     When constructed with no arguments, L{TransitionTable} contains a table
     with no states or transitions.
     """
     table = TransitionTable()
     self.assertEqual({}, table.table)
예제 #19
0
    def test_missingTransitionNextState(self):
        """
        L{MissingTransitionNextState} is raised if any of the values defined by
        C{state} appears nowhere in C{transitions} as a next state.
        """
        transitions = TransitionTable()
        transitions = transitions.addTransition(
            MoreState.amber, Input.apple, [Output.aardvark], MoreState.amber)
        transitions = transitions.addTerminalState(MoreState.blue)

        exc = self.assertRaises(
            MissingTransitionNextState,
            constructFiniteStateMachine,
            Input, Output, MoreState, transitions,
            MoreState.amber, [], {}, NULL_WORLD)
        self.assertEqual(({MoreState.blue},), exc.args)
예제 #20
0
 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)
예제 #21
0
    def test_missingTransitionNextState(self):
        """
        L{MissingTransitionNextState} is raised if any of the values defined by
        C{state} appears nowhere in C{transitions} as a next state.
        """
        transitions = TransitionTable()
        transitions = transitions.addTransition(MoreState.amber, Input.apple,
                                                [Output.aardvark],
                                                MoreState.amber)
        transitions = transitions.addTerminalState(MoreState.blue)

        exc = self.assertRaises(MissingTransitionNextState,
                                constructFiniteStateMachine, Input, Output,
                                MoreState, transitions, MoreState.amber, [],
                                {}, NULL_WORLD)
        self.assertEqual(({MoreState.blue}, ), exc.args)
예제 #22
0
 def test_initial(self):
     """
     When constructed with a transition table as an argument,
     L{TransitionTable} contains exactly that table.
     """
     expected = {"foo": {"bar": Transition("baz", "quux")}}
     table = TransitionTable(expected)
     self.assertIs(expected, table.table)
예제 #23
0
    def test_richInputInterface(self):
        """
        L{DoesNotImplement} is raised if a rich input type is given which does
        not implement the interface required by one of the outputs which can be
        produced when that input is received.
        """
        apple = trivialInput(Input.apple)
        transitions = TransitionTable()
        transitions = transitions.addTransition(
            State.amber, Input.apple, [Output.aardvark], State.amber)

        self.assertRaises(
            DoesNotImplement,
            constructFiniteStateMachine,
            Input, Output, State, transitions,
            State.amber, [apple], {Output.aardvark: IRequiredByAardvark},
            NULL_WORLD)
예제 #24
0
 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)
예제 #25
0
 def test_missingTransitionState(self):
     """
     L{MissingTransitionState} is raised if there are any keys in
     C{transitions} that are not defined by C{state}.
     """
     exc = self.assertRaises(MissingTransitionState,
                             constructFiniteStateMachine, Input, Output,
                             State, TransitionTable({}), State.amber, [],
                             {}, NULL_WORLD)
     self.assertEqual(({State.amber}, ), exc.args)
예제 #26
0
 def test_missingTransitionInput(self):
     """
     L{MissingTransitionInput} is raised if any of the values defined by
     C{input} appears in none of the values of C{transitions}.
     """
     exc = self.assertRaises(MissingTransitionInput,
                             constructFiniteStateMachine, Input, Output,
                             State, TransitionTable({State.amber: {}}),
                             State.amber, [], {}, NULL_WORLD)
     self.assertEqual(({Input.apple}, ), exc.args)
예제 #27
0
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
예제 #28
0
 def test_missingTransitionOutput(self):
     """
     L{MissingTransitionOutput} is raised if any of the values defined by
     C{output} does not appear as an output value defined by C{transitions}.
     """
     exc = self.assertRaises(
         MissingTransitionOutput, constructFiniteStateMachine, Input,
         Output, State,
         TransitionTable({State.amber: {
             Input.apple: Transition([], None)
         }}), State.amber, [], {}, NULL_WORLD)
     self.assertEqual(({Output.aardvark}, ), exc.args)
예제 #29
0
파일: _loop.py 프로젝트: AlexRRR/flocker
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
예제 #30
0
 def test_extraTransitionState(self):
     """
     L{ExtraTransitionState} is raised if there are any keys in
     C{transitions} that are not defined by C{state}.
     """
     extra = object()
     exc = self.assertRaises(ExtraTransitionState,
                             constructFiniteStateMachine, Input, Output,
                             State,
                             TransitionTable({
                                 State.amber: {},
                                 extra: {}
                             }), State.amber, [], {}, NULL_WORLD)
     self.assertEqual(({extra}, ), exc.args)
예제 #31
0
 def test_extraTransitionOutput(self):
     """
     L{ExtraTransitionInput} is raised if there are any output values
     defined by C{transitions} that are not defined by C{output}.
     """
     extra = object()
     exc = self.assertRaises(
         ExtraTransitionOutput, constructFiniteStateMachine, Input, Output,
         State,
         TransitionTable(
             {State.amber: {
                 Input.apple: Transition([extra], None)
             }}), State.amber, [], {}, NULL_WORLD)
     self.assertEqual(({extra}, ), exc.args)
예제 #32
0
파일: _loop.py 프로젝트: jongiddy/flocker
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)))
예제 #33
0
 def test_extraTransitionNextState(self):
     """
     L{ExtraTransitionNextState} is raised if any of the next state
     definitions in C{transitions} is not defined by C{state}.
     """
     extra = object()
     exc = self.assertRaises(
         ExtraTransitionNextState, constructFiniteStateMachine, MoreInput,
         Output, State,
         TransitionTable().addTransitions(
             State.amber, {
                 MoreInput.apple: ([Output.aardvark], State.amber),
                 MoreInput.banana: ([Output.aardvark], extra)
             }), State.amber, [], {}, NULL_WORLD)
     self.assertEqual(({extra}, ), exc.args)
예제 #34
0
파일: _loop.py 프로젝트: AlexRRR/flocker
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
예제 #35
0
from __future__ import print_function   # python3 print function
from builtins import *

from twisted.python.constants import Names, NamedConstant

from machinist import TransitionTable, MethodSuffixOutputer, constructFiniteStateMachine

table = TransitionTable()

class States(Names):
   S_CONFIG_W = NamedConstant()
   S_POR_W = NamedConstant()
   S_PWR_UP_W = NamedConstant()
   S_RX_ACTIVE = NamedConstant()
   S_RX_ON = NamedConstant()
   S_SDN = NamedConstant()
   S_STANDBY = NamedConstant()
   S_TX_ACTIVE = NamedConstant()
   S_DEFAULT = NamedConstant()

class Events(Names):
   E_0NOP = NamedConstant()
   E_CONFIG_DONE = NamedConstant()
   E_CRC_ERROR = NamedConstant()
   E_INVALID_SYNC = NamedConstant()
   E_PACKET_RX = NamedConstant()
   E_PACKET_SENT = NamedConstant()
   E_PREAMBLE_DETECT = NamedConstant()
   E_RX_THRESH = NamedConstant()
   E_STANDBY = NamedConstant()
   E_SYNC_DETECT = NamedConstant()
예제 #36
0
table = TransitionTable({
    State.IDLE: {
        Input.REQUEST_START: Transition([Output.START], State.STARTING),
        Input.REQUEST_STOP: Transition([], State.IDLE),
    },
    State.STARTING: {
        Input.REQUEST_START: Transition([], State.STARTING),
        Input.REQUEST_STOP: Transition([], State.START_CANCELLED),
        Input.INSTANCE_STARTED: Transition([], State.ACTIVE),
        Input.START_FAILED: Transition([Output.START], State.STARTING),
    },
    State.START_CANCELLED: {
        Input.REQUEST_START: Transition([], State.STARTING),
        Input.REQUEST_STOP: Transition([], State.START_CANCELLED),
        Input.INSTANCE_STARTED: Transition([Output.STOP], State.STOPPING),
        Input.START_FAILED: Transition([], State.IDLE),
    },
    State.ACTIVE: {
        Input.REQUEST_START: Transition([], State.ACTIVE),
        Input.REQUEST_STOP: Transition([Output.STOP], State.STOPPING),
    },
    State.STOPPING: {
        Input.REQUEST_START: Transition([], State.STOP_CANCELLED),
        Input.REQUEST_STOP: Transition([], State.STOPPING),
        Input.INSTANCE_STOPPED: Transition([], State.IDLE),
        Input.STOP_FAILED: Transition([Output.STOP], State.STOPPING),
    },
    State.STOP_CANCELLED: {
        Input.REQUEST_START: Transition([], State.STOP_CANCELLED),
        Input.REQUEST_STOP: Transition([], State.STOPPING),
        Input.INSTANCE_STOPPED: Transition([Output.START], State.STARTING),
        Input.STOP_FAILED: Transition([], State.ACTIVE),
    },
})
예제 #37
0
    ENGAGE_LOCK = NamedConstant()
    DISENGAGE_LOCK = NamedConstant()


class State(Names):
    LOCKED = NamedConstant()
    UNLOCKED = NamedConstant()
    ACTIVE = NamedConstant()


# end setup

# begin table def
from machinist import TransitionTable

table = TransitionTable()
# 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, {
예제 #38
0
class TurnstileInput(Names):
    FARE_PAID = NamedConstant()
    ARM_UNLOCKED = NamedConstant()
    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),
      })
예제 #39
0
        world = object()
        self.assertEqual("<Output / %s>" % (world, ),
                         repr(MethodSuffixOutputer(world)))


class IFood(Interface):
    radius = Attribute("The radius of the food (all food is spherical)")


@implementer(IFood)
class Gravenstein(trivialInput(Input.apple)):
    # Conveniently, apples are approximately spherical.
    radius = 3


TRANSITIONS = TransitionTable()
TRANSITIONS = TRANSITIONS.addTransition(MoreState.amber, Input.apple,
                                        [Output.aardvark], MoreState.blue)
TRANSITIONS = TRANSITIONS.addTerminalState(MoreState.blue)


class FiniteStateMachineTests(TestCase):
    """
    Tests for the L{IFiniteStateMachine} provider returned by
    L{constructFiniteStateMachine}.
    """
    def setUp(self):
        self.animals = []
        self.initial = MoreState.amber

        self.world = AnimalWorld(self.animals)
예제 #40
0
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
예제 #41
0
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
예제 #42
0


class IFood(Interface):
    radius = Attribute("The radius of the food (all food is spherical)")



@implementer(IFood)
class Gravenstein(trivialInput(Input.apple)):
    # Conveniently, apples are approximately spherical.
    radius = 3



TRANSITIONS = TransitionTable()
TRANSITIONS = TRANSITIONS.addTransition(
    MoreState.amber, Input.apple, [Output.aardvark], MoreState.blue)
TRANSITIONS = TRANSITIONS.addTerminalState(MoreState.blue)


class FiniteStateMachineTests(TestCase):
    """
    Tests for the L{IFiniteStateMachine} provider returned by
    L{constructFiniteStateMachine}.
    """
    def setUp(self):
        self.animals = []
        self.initial = MoreState.amber

        self.world = AnimalWorld(self.animals)
예제 #43
0
    ARM_LOCKED = NamedConstant()

class Output(Names):
    ENGAGE_LOCK = NamedConstant()
    DISENGAGE_LOCK = NamedConstant()

class State(Names):
    LOCKED = NamedConstant()
    UNLOCKED = NamedConstant()
    ACTIVE = NamedConstant()
# end setup

# begin table def
from machinist import TransitionTable

table = TransitionTable()
# 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, {