StateMachine

StateMachine

This module contains the basic blocks to build a state machine ( State and StateMachine ). And the RSS implementation of it, using its own states map.

class DIRAC.ResourceStatusSystem.PolicySystem.StateMachine.RSSMachine(state)

Bases: DIRAC.ResourceStatusSystem.PolicySystem.StateMachine.StateMachine

RSS implementation of the State Machine. It defines six states, which ordered by level conform the following list ( higher level first ): Unknown, Active, Degraded, Probing, Banned, Error.

The StateMachine allows any transition except if the current state is Banned, which will force any transition to any state different of Error, Banned and Probing to Probing.

examples:
>>> rsm0 = RSSMachine( None )
>>> rsm1 = RSSMachine( 'Unknown' )
Parameters:state (None or str) – name of the current state of the StateMachine
__init__(state)

Constructor.

getNextState(candidateState)

Method that gets the next state, given the proposed transition to candidateState. If candidateState is not on the state map <self.states>, it is rejected. If it is not the case, we have two options: if <self.state> is None, then the next state will be <candidateState>. Otherwise, the current state is using its own transition rule to decide.

examples

>>> sm0.getNextState( None )
    S_OK( None )
>>> sm0.getNextState( 'NextState' )
    S_OK( 'NextState' )
Parameters:candidateState (str) – name of the next state
Returns:S_OK( nextState ) || S_ERROR
getStates()

Returns all possible states in the state map

examples

>>> sm0.getStates()
    [ 'Nirvana' ]
Returns:list( stateNames )
levelOfPolicyState(policyResult)

Returns the level of the state associated with the policy, -1 if something goes wrong. It is mostly used while sorting policies with method orderPolicyResults.

examples

>>> rsm0.levelOfPolicyState( { 'Status' : 'Active', 'A' : 'A' } )
    5
>>> rsm0.levelOfPolicyState( { 'Status' : 'Rubbish', 'R' : 'R' } )
    -1
Parameters:policyResult (dict) – dictionary that must have the Status key.
Returns:int || -1 ( if policyResult[ ‘Status’ ] is not known by the StateMachine )
levelOfState(state)

Given a state name, it returns its level ( integer ), which defines the hierarchy.

>>> sm0.levelOfState( 'Nirvana' )
    100
>>> sm0.levelOfState( 'AnotherState' )
    -1
Parameters:state (str) – name of the state, it should be on <self.states> key set
Returns:int || -1 ( if not in <self.states> )
orderPolicyResults(policyResults)

Method built specifically to interact with the policy results obtained on the PDP module. It sorts the input based on the level of their statuses, the lower the level state, the leftmost position in the list. Beware, if any of the statuses is not know to the StateMachine, it will be ordered first, as its level will be -1 !.

examples

>>> rsm0.orderPolicyResults( [ { 'Status' : 'Active', 'A' : 'A' },
                               { 'Status' : 'Banned', 'B' : 'B' } ] )
    [ { 'Status' : 'Banned', 'B' : 'B' }, { 'Status' : 'Active', 'A' : 'A' } ]
>>> rsm0.orderPolicyResults( [ { 'Status' : 'Active', 'A' : 'A' },
                               { 'Status' : 'Rubbish', 'R' : 'R' } ] )
    [ { 'Status' : 'Rubbish', 'R' : 'R' }, { 'Status' : 'Active', 'A' : 'A' } ]
Parameters:policyResults (list) – list of dictionaries to be ordered. The dictionary can have any key as far as the key Status is present.
Result:list( dict ), which is ordered
setState(state)
Makes sure the state is either None or known to the machine

examples

>>> sm0.setState( None )[ 'OK' ]
    True
>>> sm0.setState( 'Nirvana' )[ 'OK' ]
    True
>>> sm0.setState( 'AnotherState' )[ 'OK' ]
    False
Parameters:state (None or str) – state which will be set as current state of the StateMachine
Returns:S_OK || S_ERROR
class DIRAC.ResourceStatusSystem.PolicySystem.StateMachine.State(level, stateMap=[], defState=None)

Bases: object

State class that represents a single step on a StateMachine, with all the possible transitions, the default transition and an ordering level.

examples:
>>> s0 = State( 100 )
>>> s1 = State( 0, [ 'StateName1', 'StateName2' ], defState = 'StateName1' )
>>> s2 = State( 0, [ 'StateName1', 'StateName2' ] )
# this example is tricky. The transition rule says that will go to
# nextState, e.g. 'StateNext'. But, it is not on the stateMap, and there
# is no default defined, so it will end up going to StateNext anyway. You
# must be careful while defining states and their stateMaps and defaults.
Parameters:
  • level (int) – each state is mapped to an integer, which is used to sort the states according to that integer.
  • stateMap (list) – it is a list ( of strings ) with the reachable states from this particular status. If not defined, we assume there are no restrictions.
  • defState (None or str) – default state used in case the next state it is not stateMap ( not defined or simply not there ).
__init__(level, stateMap=[], defState=None)

Constructor.

transitionRule(nextState)

Method that selects next state, knowing the default and the transitions map, and the proposed next state. If <nextState> is in stateMap, goes there. If not, then goes to <self.default> if any. Otherwise, goes to <nextState> anyway.

examples

>>> s0.transitionRule( 'nextState' )
    'nextState'
>>> s1.transitionRule( 'StateName2' )
    'StateName2'
>>> s1.transitionRule( 'StateNameNotInMap' )
    'StateName1'
>>> s2.transitionRule( 'StateNameNotInMap' )
    'StateNameNotInMap'
Parameters:nextState (str) – name of the state in the stateMap
Returns:state name
Return type:str
class DIRAC.ResourceStatusSystem.PolicySystem.StateMachine.StateMachine(state=None)

Bases: object

StateMachine class that represents the whole state machine with all transitions.

examples

>>> sm0 = StateMachine()
>>> sm1 = StateMachine( state = 'Active' )
Parameters:state (None or str) – current state of the StateMachine, could be None if we do not use the StateMachine to calculate transitions. Beware, it is not checked if the state is on the states map !
__init__(state=None)

Constructor.

getNextState(candidateState)

Method that gets the next state, given the proposed transition to candidateState. If candidateState is not on the state map <self.states>, it is rejected. If it is not the case, we have two options: if <self.state> is None, then the next state will be <candidateState>. Otherwise, the current state is using its own transition rule to decide.

examples

>>> sm0.getNextState( None )
    S_OK( None )
>>> sm0.getNextState( 'NextState' )
    S_OK( 'NextState' )
Parameters:candidateState (str) – name of the next state
Returns:S_OK( nextState ) || S_ERROR
getStates()

Returns all possible states in the state map

examples

>>> sm0.getStates()
    [ 'Nirvana' ]
Returns:list( stateNames )
levelOfState(state)

Given a state name, it returns its level ( integer ), which defines the hierarchy.

>>> sm0.levelOfState( 'Nirvana' )
    100
>>> sm0.levelOfState( 'AnotherState' )
    -1
Parameters:state (str) – name of the state, it should be on <self.states> key set
Returns:int || -1 ( if not in <self.states> )
setState(state)
Makes sure the state is either None or known to the machine

examples

>>> sm0.setState( None )[ 'OK' ]
    True
>>> sm0.setState( 'Nirvana' )[ 'OK' ]
    True
>>> sm0.setState( 'AnotherState' )[ 'OK' ]
    False
Parameters:state (None or str) – state which will be set as current state of the StateMachine
Returns:S_OK || S_ERROR