|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.espertech.esper.epl.variable.VariableServiceImpl
public class VariableServiceImpl
Variables service for reading and writing variables, and for setting a version number for the current thread to consider variables for.
Consider a statement as follows: select * from MyEvent as A where A.val > var1 and A.val2 > var1 and A.val3 > var2
Upon statement execution we need to guarantee that the same atomic value for all variables is applied for all variable reads (by expressions typically) within the statement.
Designed to support:
As an alternative to a version-based design, a read-lock for the variable space could also be used, with the following disadvantages: The write lock may just not be granted unless fair locks are used which are more expensive; And a read-lock is more expensive to acquire for multiple CPUs; A thread-local is still need to deal with "set var1=3, var2=var1+1" assignments where the new uncommitted value must be visible in the local evaluation.
Every new write to a variable creates a new version. Thus when reading variables, readers can ignore newer versions and a read lock is not required in most circumstances.
This algorithm works as follows:
A thread processing an event into the engine via sendEvent() calls the "setLocalVersion" method once before processing a statement that has variables. This places into a threadlocal variable the current version number, say version 570.
A statement that reads a variable has an ExprVariableNode
that has a VariableReader
handle
obtained during validation (example).
The VariableReader
takes the version from the threadlocal (570) and compares the version number with the
version numbers held for the variable.
If the current version is same or lower (520, as old or older) then the threadlocal version,
then use the current value.
If the current version is higher (571, newer) then the threadlocal version, then go to the prior value.
Use the prior value until a version is found that as old or older then the threadlocal version.
If no version can be found that is old enough, output a warning and return the newest version. This should not happen, unless a thread is executing for very long within a single statement such that lifetime-old-version time speriod passed before the thread asks for variable values.
As version numbers are counted up they may reach a boundary. Any write transaction after the boundary is reached performs a roll-over. In a roll-over, all variables version lists are newly created and any existing threads that read versions go against a (old) high-collection, while new threads reading the reset version go against a new low-collection.
The class also allows an optional state handler to be plugged in to handle persistence for variable state. The state handler gets invoked when a variable changes value, and when a variable gets created to obtain the current value from persistence, if any.
Field Summary | |
---|---|
protected static int |
HIGH_WATERMARK_VERSIONS
Applicable for each variable if more then the number of versions accumulated, check timestamps to determine if a version can be expired. |
protected static int |
ROLLOVER_READER_BOUNDARY
Sets the boundary above which a reader considers the high-version list of variable values. |
Constructor Summary | |
---|---|
protected |
VariableServiceImpl(int startVersion,
long millisecondLifetimeOldVersions,
TimeProvider timeProvider,
EventAdapterService eventAdapterService,
VariableStateHandler optionalStateHandler)
Ctor. |
|
VariableServiceImpl(long millisecondLifetimeOldVersions,
TimeProvider timeProvider,
EventAdapterService eventAdapterService,
VariableStateHandler optionalStateHandler)
Ctor. |
Method Summary | |
---|---|
void |
checkAndWrite(int variableNumber,
java.lang.Object newValue)
Check type of the value supplied and writes the new variable value. |
void |
commit()
Commits the variable outstanding changes. |
void |
createNewVariable(java.lang.String variableName,
java.lang.String variableType,
java.lang.Object value,
boolean constant,
boolean array,
StatementExtensionSvcContext extensionServicesContext,
EngineImportService engineImportService)
Creates a new variable. |
void |
destroy()
|
static java.lang.String |
getAssigmentExMessage(java.lang.String variableName,
java.lang.Class variableType,
java.lang.Class initValueClass)
|
VariableReader |
getReader(java.lang.String variableName)
Returns a reader that provides access to variable values. |
java.util.concurrent.locks.ReadWriteLock |
getReadWriteLock()
Lock for use in atomic writes to the variable space. |
java.lang.String |
getVariableName(int variableNum)
|
java.util.Map<java.lang.String,VariableReader> |
getVariables()
Returns a map of variable name and reader, for thread-safe iteration. |
void |
registerCallback(int variableNumber,
VariableChangeCallback variableChangeCallback)
Registers a callback invoked when the variable is written with a new value. |
void |
removeVariable(java.lang.String name)
Removes a variable. |
void |
rollback()
Rolls back the variable outstanding changes. |
void |
setLocalVersion()
Sets the variable version that subsequent reads consider. |
java.lang.String |
toString()
|
void |
unregisterCallback(int variableNumber,
VariableChangeCallback variableChangeCallback)
Removes a callback. |
void |
write(int variableNumber,
java.lang.Object newValue)
Writes a new variable value. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final int ROLLOVER_READER_BOUNDARY
protected static final int HIGH_WATERMARK_VERSIONS
Constructor Detail |
---|
public VariableServiceImpl(long millisecondLifetimeOldVersions, TimeProvider timeProvider, EventAdapterService eventAdapterService, VariableStateHandler optionalStateHandler)
millisecondLifetimeOldVersions
- number of milliseconds a version may hang around before expirytimeProvider
- provides the current timeoptionalStateHandler
- a optional plug-in that may store variable state and retrieve state upon creationeventAdapterService
- event adaptersprotected VariableServiceImpl(int startVersion, long millisecondLifetimeOldVersions, TimeProvider timeProvider, EventAdapterService eventAdapterService, VariableStateHandler optionalStateHandler)
startVersion
- the first version number to start frommillisecondLifetimeOldVersions
- number of milliseconds a version may hang around before expirytimeProvider
- provides the current timeoptionalStateHandler
- a optional plug-in that may store variable state and retrieve state upon creationeventAdapterService
- for finding event typesMethod Detail |
---|
public void destroy()
destroy
in interface VariableService
public void removeVariable(java.lang.String name)
VariableService
removeVariable
in interface VariableService
name
- to removepublic java.lang.String getVariableName(int variableNum)
getVariableName
in interface VariableService
public void setLocalVersion()
VariableService
setLocalVersion
in interface VariableService
public void registerCallback(int variableNumber, VariableChangeCallback variableChangeCallback)
VariableService
registerCallback
in interface VariableService
variableNumber
- the variable index numbervariableChangeCallback
- a callbackpublic void unregisterCallback(int variableNumber, VariableChangeCallback variableChangeCallback)
VariableService
unregisterCallback
in interface VariableService
variableNumber
- the variable index numbervariableChangeCallback
- a callbackpublic void createNewVariable(java.lang.String variableName, java.lang.String variableType, java.lang.Object value, boolean constant, boolean array, StatementExtensionSvcContext extensionServicesContext, EngineImportService engineImportService) throws VariableExistsException, VariableTypeException
VariableService
createNewVariable
in interface VariableService
variableName
- name of the variablevariableType
- variable typevalue
- initialization value; String values are allowed and parsed according to typeextensionServicesContext
- is extensions for implementing resilience attributes of variables
VariableExistsException
- if the variable name is already in use
VariableTypeException
- if the variable type cannot be recognizedpublic VariableReader getReader(java.lang.String variableName)
VariableService
getReader
in interface VariableService
variableName
- the variable that the reader should read
public void write(int variableNumber, java.lang.Object newValue)
VariableService
Must be followed by either a commit or rollback.
write
in interface VariableService
variableNumber
- the index number of the variable to write (from VariableReader)newValue
- the new valuepublic java.util.concurrent.locks.ReadWriteLock getReadWriteLock()
VariableService
getReadWriteLock
in interface VariableService
public void commit()
VariableService
commit
in interface VariableService
public void rollback()
VariableService
rollback
in interface VariableService
public void checkAndWrite(int variableNumber, java.lang.Object newValue) throws VariableValueException
VariableService
Must be followed by either a commit or rollback.
checkAndWrite
in interface VariableService
variableNumber
- the index number of the variable to write (from VariableReader)newValue
- the new value
VariableValueException
public java.lang.String toString()
toString
in class java.lang.Object
public java.util.Map<java.lang.String,VariableReader> getVariables()
VariableService
getVariables
in interface VariableService
public static java.lang.String getAssigmentExMessage(java.lang.String variableName, java.lang.Class variableType, java.lang.Class initValueClass)
|
© 2006-2014 EsperTech Inc. All rights reserved. Visit us at espertech.com |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |