Package adams.flow.control
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 Summary
Fields Modifier and Type Field Description static StringBACKUP_CURRENTthe key for storing the current token in the backup.protected Sequencem_Actorsthe actors to "balance".protected intm_ActualNumThreadsthe actual number of threads to use.protected Tokenm_CurrentTokenthe input token.protected booleanm_DeepCopywhether to perform a deep copy of the storage.protected PausableFixedThreadPoolExecutorm_Executorthe executor service to use for parallel execution.protected intm_NumThreadsthe number of threads to use for parallel execution.protected intm_ThreadsSpawnedthe count of threads spawned so far.protected List<Actor>m_ToCleanUpthe actors to clean up in the end.protected booleanm_UseLocalStoragewhether to use local storage.-
Fields inherited from class adams.flow.control.AbstractControlActor
m_Compatibility, m_PauseStateManager, m_SetUpSubActors
-
Fields inherited from class adams.flow.core.AbstractActor
m_Annotations, m_BackupState, m_DetectedObjectVariables, m_DetectedVariables, m_ErrorHandler, m_Executed, m_Executing, m_ExecutionListeningSupporter, m_FullName, m_LoggingPrefix, m_Name, m_Parent, m_ScopeHandler, m_Self, m_Silent, m_Skip, m_StopFlowOnError, m_StopMessage, m_Stopped, m_StorageHandler, m_VariablesUpdated
-
Fields inherited from class adams.core.option.AbstractOptionHandler
m_OptionManager
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
Fields inherited from interface adams.flow.core.Actor
FILE_EXTENSION, FILE_EXTENSION_GZ
-
-
Constructor Summary
Constructors Constructor Description LoadBalancer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class[]accepts()Returns the class that the consumer accepts.Stringadd(int index, Actor actor)Inserts the actor at the given position.Stringadd(Actor actor)Inserts the actor at the end.protected Hashtable<String,Object>backupState()Backs up the current state of the actor before update the variables.voidcleanUp()Cleans up after the execution has finished.TokencurrentInput()Returns the current input token, if any.StringdeepCopyTipText()Returns the tip text for this property.voiddefineOptions()Adds options to the internal list of options.protected StringdoExecute()Executes the flow item.voidflushExecution()Stops the processing of tokens without stopping the flow.protected voidforceVariables(Variables value)Updates the Variables instance in use.Actorget(int index)Returns the actor at the given position.ActorHandlerInfogetActorHandlerInfo()Returns some information about the actor handler, e.g., whether it can contain standalones and the actor execution.booleangetDeepCopy()Returns whether to perform a deep copy for the local storage.Actor[]getLoadActors()Returns the load actors.intgetNumThreads()Returns the number of threads in use.StringgetQuickInfo()Returns a quick info about the actor, which will be displayed in the GUI.booleangetUseLocalStorage()Returns whether to use user local storage scope.StringglobalInfo()Returns a string describing the object.booleanhasInput()Returns whether an input token is currently present.intindexOf(String actor)Returns the index of the actor.protected voidinitialize()Initializes the members.voidinput(Token token)The method that accepts the input token and then processes it.StringloadActorsTipText()Returns the tip text for this property.StringnumThreadsTipText()Returns the tip text for this property.Actorremove(int index)Removes the actor at the given position and returns the removed object.voidremoveAll()Removes all actors.protected voidrestoreState(Hashtable<String,Object> state)Restores the state of the actor before the variables got updated.Stringset(int index, Actor actor)Sets the actor at the given position.voidsetDeepCopy(boolean value)Sets whether to perform a deep copy for the local storage.voidsetLoadActors(Actor[] value)Sets the load actors.voidsetLoggingLevel(LoggingLevel value)Sets the logging level.voidsetNumThreads(int value)Sets the number of threads to use.voidsetSkip(boolean value)Sets whether the transformation is skipped or not.StringsetUp()Initializes the sub-actors for flow execution.protected StringsetUpLoadActors()Gets called in the setUp() method.protected StringsetUpSubActors()Performs the setUp of the sub-actors.voidsetUseLocalStorage(boolean value)Sets whether to use local storage scope.intsize()Returns the size of the group.voidstopExecution()Stops the execution.protected voidupdateParent()Updates the parent of all actors in this group.StringuseLocalStorageTipText()Returns the tip text for this property.voidwrapUp()Finishes up the execution.-
Methods inherited from class adams.flow.control.AbstractControlActor
active, canInspectOptions, check, destroy, firstActive, firstInputConsumer, flowPauseStateChanged, isPaused, isSubFlowWrappedUp, lastActive, pauseExecution, preExecute, reset, resumeExecution, setParent, wrapUpSubFlow
-
Methods inherited from class adams.flow.core.AbstractActor
annotationsTipText, canPerformSetUpCheck, compareTo, configureLogger, equals, execute, finalUpdateVariables, findVariables, findVariables, forCommandLine, forName, forName, getAdditionalInformation, getAnnotations, getDefaultName, getDetectedVariables, getErrorHandler, getFlowActors, getFlowExecutionListeningSupporter, getFullName, getName, getNextSibling, getParent, getParentComponent, getPreviousSibling, getRoot, getScopeHandler, getSilent, getSkip, getStopFlowOnError, getStopMessage, getStorageHandler, getVariables, handleError, handleException, hasErrorHandler, hasStopMessage, index, isBackedUp, isExecuted, isExecuting, isFinished, isHeadless, isStopped, nameTipText, performSetUpChecks, performVariableChecks, postExecute, pruneBackup, pruneBackup, setAnnotations, setErrorHandler, setName, setSilent, setStopFlowOnError, setVariables, shallowCopy, shallowCopy, silentTipText, sizeOf, skipTipText, stopExecution, stopFlowOnErrorTipText, updateDetectedVariables, updatePrefix, updateVariables, variableChanged
-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, finishInit, getDefaultLoggingLevel, getOptionManager, loggingLevelTipText, newOptionManager, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.flow.core.Actor
compareTo, destroy, equals, execute, findVariables, getAnnotations, getDefaultName, getDetectedVariables, getErrorHandler, getFlowExecutionListeningSupporter, getFullName, getName, getNextSibling, getParent, getParentComponent, getPreviousSibling, getRoot, getScopeHandler, getSilent, getSkip, getStopFlowOnError, getStopMessage, getStorageHandler, getVariables, handleError, hasErrorHandler, hasStopMessage, index, isExecuted, isFinished, isHeadless, isStopped, setAnnotations, setErrorHandler, setName, setParent, setSilent, setStopFlowOnError, setVariables, shallowCopy, shallowCopy, sizeOf, stopExecution, toCommandLine, variableChanged
-
Methods inherited from interface adams.flow.core.ActorHandler
check, firstActive, lastActive
-
Methods inherited from interface adams.core.AdditionalInformationHandler
getAdditionalInformation
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel
-
Methods inherited from interface adams.core.logging.LoggingSupporter
getLogger, isLoggingEnabled
-
Methods inherited from interface adams.core.option.OptionHandler
cleanUpOptions, getOptionManager
-
Methods inherited from interface adams.core.VariablesInspectionHandler
canInspectOptions
-
-
-
-
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_Executor
protected PausableFixedThreadPoolExecutor m_Executor
the executor service to use for parallel execution.
-
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.
-
-
Method Detail
-
globalInfo
public String globalInfo()
Returns a string describing the object.- Specified by:
globalInfoin interfaceGlobalInfoSupporter- Specified by:
globalInfoin classAbstractOptionHandler- Returns:
- a description suitable for displaying in the gui
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptionsin interfaceOptionHandler- Overrides:
defineOptionsin classAbstractActor
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initializein classAbstractControlActor
-
updateParent
protected void updateParent()
Updates the parent of all actors in this group.- Overrides:
updateParentin classAbstractControlActor
-
setLoggingLevel
public void setLoggingLevel(LoggingLevel value)
Sets the logging level.- Specified by:
setLoggingLevelin interfaceLoggingLevelHandler- Overrides:
setLoggingLevelin classAbstractOptionHandler- Parameters:
value- the level
-
setSkip
public void setSkip(boolean value)
Sets whether the transformation is skipped or not.- Specified by:
setSkipin interfaceActor- Overrides:
setSkipin classAbstractActor- 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:
setDeepCopyin interfaceDeepCopyOperator- 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:
getDeepCopyin interfaceDeepCopyOperator- Returns:
- true if a deep copy is performed
-
deepCopyTipText
public String deepCopyTipText()
Returns the tip text for this property.- Specified by:
deepCopyTipTextin interfaceDeepCopyOperator- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
getQuickInfo
public String getQuickInfo()
Returns a quick info about the actor, which will be displayed in the GUI.- Specified by:
getQuickInfoin interfaceActor- Specified by:
getQuickInfoin interfaceQuickInfoSupporter- Overrides:
getQuickInfoin classAbstractActor- Returns:
- null if no info available, otherwise short string
-
forceVariables
protected void forceVariables(Variables value)
Updates the Variables instance in use.
Use with caution!- Overrides:
forceVariablesin classAbstractControlActor- Parameters:
value- the instance to use
-
size
public int size()
Returns the size of the group.- Specified by:
sizein interfaceActorHandler- Specified by:
sizein classAbstractControlActor- Returns:
- the number of actors
-
get
public Actor get(int index)
Returns the actor at the given position.- Specified by:
getin interfaceActorHandler- Specified by:
getin classAbstractControlActor- Parameters:
index- the position- Returns:
- the actor
-
set
public String set(int index, Actor actor)
Sets the actor at the given position.- Specified by:
setin interfaceActorHandler- Specified by:
setin classAbstractControlActor- Parameters:
index- the positionactor- 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:
indexOfin interfaceActorHandler- Specified by:
indexOfin classAbstractControlActor- 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:
addin interfaceMutableActorHandler- 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:
addin interfaceMutableActorHandler- Parameters:
index- the positionactor- 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:
removein interfaceMutableActorHandler- Parameters:
index- the position- Returns:
- the removed actor
-
removeAll
public void removeAll()
Removes all actors.- Specified by:
removeAllin interfaceMutableActorHandler
-
getActorHandlerInfo
public ActorHandlerInfo getActorHandlerInfo()
Returns some information about the actor handler, e.g., whether it can contain standalones and the actor execution.- Specified by:
getActorHandlerInfoin interfaceActorHandler- Specified by:
getActorHandlerInfoin classAbstractControlActor- Returns:
- the info
-
backupState
protected Hashtable<String,Object> backupState()
Backs up the current state of the actor before update the variables.- Overrides:
backupStatein classAbstractActor- Returns:
- the backup
- See Also:
AbstractActor.updateVariables(),AbstractActor.restoreState(Hashtable)
-
restoreState
protected void restoreState(Hashtable<String,Object> state)
Restores the state of the actor before the variables got updated.- Overrides:
restoreStatein classAbstractActor- Parameters:
state- the backup of the state to restore from- See Also:
AbstractActor.updateVariables(),AbstractActor.backupState()
-
accepts
public Class[] accepts()
Returns the class that the consumer accepts.- Specified by:
acceptsin interfaceInputConsumer- 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:
setUpSubActorsin classAbstractControlActor- Returns:
- null if everything is fine, otherwise error message
-
setUp
public String setUp()
Initializes the sub-actors for flow execution.- Specified by:
setUpin interfaceActor- Overrides:
setUpin classAbstractControlActor- Returns:
- null if everything is fine, otherwise error message
- See Also:
AbstractActor.reset()
-
input
public void input(Token token)
The method that accepts the input token and then processes it.- Specified by:
inputin interfaceInputConsumer- Parameters:
token- the token to accept and process
-
hasInput
public boolean hasInput()
Returns whether an input token is currently present.- Specified by:
hasInputin interfaceInputConsumer- Returns:
- true if input token present
-
currentInput
public Token currentInput()
Returns the current input token, if any.- Specified by:
currentInputin interfaceInputConsumer- Returns:
- the input token, null if none present
-
doExecute
protected String doExecute()
Executes the flow item.- Specified by:
doExecutein classAbstractActor- Returns:
- null if everything is fine, otherwise error message
-
wrapUp
public void wrapUp()
Finishes up the execution.- Specified by:
wrapUpin interfaceActor- Overrides:
wrapUpin classAbstractControlActor
-
flushExecution
public void flushExecution()
Stops the processing of tokens without stopping the flow.- Specified by:
flushExecutionin interfaceActorHandler- Specified by:
flushExecutionin interfaceFlushable- Overrides:
flushExecutionin classAbstractControlActor
-
stopExecution
public void stopExecution()
Stops the execution. No message set.- Specified by:
stopExecutionin interfaceActor- Specified by:
stopExecutionin interfaceStoppable- Overrides:
stopExecutionin classAbstractControlActor
-
cleanUp
public void cleanUp()
Cleans up after the execution has finished. Also removes graphical components.- Specified by:
cleanUpin interfaceActor- Specified by:
cleanUpin interfaceCleanUpHandler- Overrides:
cleanUpin classAbstractControlActor
-
-