Class LoadBalancer

  • All Implemented Interfaces:
    AdditionalInformationHandler, CleanUpHandler, DeepCopyOperator, Destroyable, GlobalInfoSupporter, LoggingLevelHandler, LoggingSupporter, OptionHandler, Pausable, QuickInfoSupporter, ShallowCopySupporter<Actor>, SizeOfHandler, Stoppable, StoppableWithFeedback, VariablesInspectionHandler, FlowPauseStateListener, VariableChangeListener, Actor, ActorHandler, ControlActor, ErrorHandler, Flushable, InputConsumer, MutableActorHandler, SubFlowWrapUp, Serializable, Comparable

    public class LoadBalancer
    extends AbstractControlActor
    implements InputConsumer, MutableActorHandler, DeepCopyOperator
    Runs the specified 'load actor' in as many separate threads as specified with the 'num-threads' parameter.
    Always uses a copy of the variables.
    NB: no callable transformer or sink allowed.

    Input/output:
    - accepts:
       adams.flow.core.Unknown


    -logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel)
        The logging level for outputting errors and debugging output.
        default: WARNING
     
    -name <java.lang.String> (property: name)
        The name of the actor.
        default: LoadBalancer
     
    -annotation <adams.core.base.BaseAnnotation> (property: annotations)
        The annotations to attach to this actor.
        default: 
     
    -skip <boolean> (property: skip)
        If set to true, transformation is skipped and the input token is just forwarded 
        as it is.
        default: false
     
    -stop-flow-on-error <boolean> (property: stopFlowOnError)
        If set to true, the flow execution at this level gets stopped in case this 
        actor encounters an error; the error gets propagated; useful for critical 
        actors.
        default: false
     
    -silent <boolean> (property: silent)
        If enabled, then no errors are output in the console; Note: the enclosing 
        actor handler must have this enabled as well.
        default: false
     
    -load <adams.flow.core.Actor> [-load ...] (property: loadActors)
        The actors to 'load-balance'.
        default: adams.flow.sink.Null
     
    -num-threads <int> (property: numThreads)
        The number of threads to use for load-balancing (-1 means one for each core
        /cpu).
        default: 0
     
    -use-local-storage <boolean> (property: useLocalStorage)
        If enabled, then each thread will restrict the scope of storage to be local;
         initially, a shallow copy of the storage is taken at the thread's time 
        of creation.
        default: false
     
    -deep-copy <boolean> (property: deepCopy)
        If enabled, the local storage gets copied using a deep copy.
        default: false
     
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Detail

      • BACKUP_CURRENT

        public static final String BACKUP_CURRENT
        the key for storing the current token in the backup.
        See Also:
        Constant Field Values
      • m_Actors

        protected Sequence m_Actors
        the actors to "balance".
      • m_CurrentToken

        protected transient Token m_CurrentToken
        the input token.
      • m_NumThreads

        protected int m_NumThreads
        the number of threads to use for parallel execution.
      • m_ActualNumThreads

        protected int m_ActualNumThreads
        the actual number of threads to use.
      • m_ToCleanUp

        protected List<Actor> m_ToCleanUp
        the actors to clean up in the end.
      • m_ThreadsSpawned

        protected int m_ThreadsSpawned
        the count of threads spawned so far.
      • m_UseLocalStorage

        protected boolean m_UseLocalStorage
        whether to use local storage.
      • m_DeepCopy

        protected boolean m_DeepCopy
        whether to perform a deep copy of the storage.
    • Constructor Detail

      • LoadBalancer

        public LoadBalancer()
    • Method Detail

      • setSkip

        public void setSkip​(boolean value)
        Sets whether the transformation is skipped or not.
        Specified by:
        setSkip in interface Actor
        Overrides:
        setSkip in class AbstractActor
        Parameters:
        value - true if transformation is to be skipped
      • setLoadActors

        public void setLoadActors​(Actor[] value)
        Sets the load actors.
        Parameters:
        value - the actors
      • getLoadActors

        public Actor[] getLoadActors()
        Returns the load actors.
        Returns:
        the actors
      • loadActorsTipText

        public String loadActorsTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setNumThreads

        public void setNumThreads​(int value)
        Sets the number of threads to use.
        Parameters:
        value - the number of threads
      • getNumThreads

        public int getNumThreads()
        Returns the number of threads in use.
        Returns:
        the number of threads
      • numThreadsTipText

        public String numThreadsTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setUseLocalStorage

        public void setUseLocalStorage​(boolean value)
        Sets whether to use local storage scope.
        Parameters:
        value - if true local storage scope will be used
      • getUseLocalStorage

        public boolean getUseLocalStorage()
        Returns whether to use user local storage scope.
        Returns:
        true if local storage scope enabled
      • useLocalStorageTipText

        public String useLocalStorageTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setDeepCopy

        public void setDeepCopy​(boolean value)
        Sets whether to perform a deep copy for the local storage.
        Specified by:
        setDeepCopy in interface DeepCopyOperator
        Parameters:
        value - if true a deep copy for the local storage will be performed
      • getDeepCopy

        public boolean getDeepCopy()
        Returns whether to perform a deep copy for the local storage.
        Specified by:
        getDeepCopy in interface DeepCopyOperator
        Returns:
        true if a deep copy is performed
      • deepCopyTipText

        public String deepCopyTipText()
        Returns the tip text for this property.
        Specified by:
        deepCopyTipText in interface DeepCopyOperator
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • forceVariables

        protected void forceVariables​(Variables value)
        Updates the Variables instance in use.

        Use with caution!
        Overrides:
        forceVariables in class AbstractControlActor
        Parameters:
        value - the instance to use
      • get

        public Actor get​(int index)
        Returns the actor at the given position.
        Specified by:
        get in interface ActorHandler
        Specified by:
        get in class AbstractControlActor
        Parameters:
        index - the position
        Returns:
        the actor
      • set

        public String set​(int index,
                          Actor actor)
        Sets the actor at the given position.
        Specified by:
        set in interface ActorHandler
        Specified by:
        set in class AbstractControlActor
        Parameters:
        index - the position
        actor - the actor to set at this position
        Returns:
        null if successful, otherwise error message
      • indexOf

        public int indexOf​(String actor)
        Returns the index of the actor.
        Specified by:
        indexOf in interface ActorHandler
        Specified by:
        indexOf in class AbstractControlActor
        Parameters:
        actor - the name of the actor to look for
        Returns:
        the index of -1 if not found
      • add

        public String add​(Actor actor)
        Inserts the actor at the end.
        Specified by:
        add in interface MutableActorHandler
        Parameters:
        actor - the actor to insert
        Returns:
        null if successful, otherwise error message
      • add

        public String add​(int index,
                          Actor actor)
        Inserts the actor at the given position.
        Specified by:
        add in interface MutableActorHandler
        Parameters:
        index - the position
        actor - the actor to insert
        Returns:
        null if successful, otherwise error message
      • remove

        public Actor remove​(int index)
        Removes the actor at the given position and returns the removed object.
        Specified by:
        remove in interface MutableActorHandler
        Parameters:
        index - the position
        Returns:
        the removed actor
      • accepts

        public Class[] accepts()
        Returns the class that the consumer accepts.
        Specified by:
        accepts in interface InputConsumer
        Returns:
        adams.flow.core.Unknown.class
      • setUpLoadActors

        protected String setUpLoadActors()
        Gets called in the setUp() method. Returns null if load-actors are fine, otherwise error message.
        Returns:
        null if everything OK, otherwise error message
      • setUpSubActors

        protected String setUpSubActors()
        Performs the setUp of the sub-actors.
        Overrides:
        setUpSubActors in class AbstractControlActor
        Returns:
        null if everything is fine, otherwise error message
      • input

        public void input​(Token token)
        The method that accepts the input token and then processes it.
        Specified by:
        input in interface InputConsumer
        Parameters:
        token - the token to accept and process
      • hasInput

        public boolean hasInput()
        Returns whether an input token is currently present.
        Specified by:
        hasInput in interface InputConsumer
        Returns:
        true if input token present
      • currentInput

        public Token currentInput()
        Returns the current input token, if any.
        Specified by:
        currentInput in interface InputConsumer
        Returns:
        the input token, null if none present
      • doExecute

        protected String doExecute()
        Executes the flow item.
        Specified by:
        doExecute in class AbstractActor
        Returns:
        null if everything is fine, otherwise error message