StateMachine

StateMachine

This module contains the RSS implementation of StateMachine, using its own states map.

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

Bases: 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.

getLevelOfPolicyState(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.getLevelOfPolicyState( { 'Status' : 'Active', 'A' : 'A' } )
    5
>>> rsm0.getLevelOfPolicyState( { '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 )

getLevelOfState(state)

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

>>> sm0.getLevelOfState('Nirvana')
    100
>>> sm0.getLevelOfState('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>)

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)

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(candidateState, noWarn=False)
Makes sure the state is either None or known to the machine, and that it is a valid state to move into.

Final states are also checked.

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