Class ProcessStateHolder
- java.lang.Object
-
- com.broadleafcommerce.search.index.core.ProcessStateHolder
-
- All Implemented Interfaces:
IndexableTypeAware
,Serializable
public class ProcessStateHolder extends Object implements Serializable, IndexableTypeAware
This component is a container for allowing thread-safe access to state across a global, in JVM process (e.g. a process that is multi-threaded). This should typically be accessed by control or flow components. You must start and end the process to prevent memory leaks and other side effects:ReindexStateHolder.startProcessState(IndexableType.PRODUCT); try { ... //Do a full re-index, likely across multiple threads. } finally { ReindexStateHolder.endProcessState(IndexableType.PRODUCT); }
This is useful for determining if a process has failed, or for causing a process to fail fast across multiple threads.
- Author:
- Kelly Tisdell (ktisdell)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ProcessStateHolder.ProcessFailure
-
Constructor Summary
Constructors Constructor Description ProcessStateHolder(@NonNull com.broadleafcommerce.search.api.type.IndexableType indexableType, boolean master)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
failFast()
Tells the system to arbitrarily fail without a specific exception.void
failFast(Throwable th)
Notifies that the process has failed and keeps track of the Throwables passed in.Map<String,Object>
getAdditionalProperties()
Returns an unmodifiable map of all additional properties.Object
getAdditionalProperty(String key)
Returns an arbitrary Object in a map, based on the provided key.List<ProcessStateHolder.ProcessFailure>
getAllFailures()
More than one thread may have reported an error before realizing that the process failed.String
getCorrelationId()
UUID that is assigned per process, per node.long
getExpectedIndexableItemsToProcess()
Get the number of expected indexable items to process.Throwable
getFirstFailure()
More than one thread can potentially report an error.long
getIndexableItemsProcessed()
Get the number of indexable items processed.com.broadleafcommerce.search.api.type.IndexableType
getIndexableType()
The indexable type for the reindex process this state holder belongs to.int
getNodeCount()
Get the number of nodes that workers are running on.long
getTimeStartedMillis()
The time the process state holder was created.int
getTotalThreadCount()
Get the number of worker threads across all nodes.void
incrementIndexableItemsProcessed()
Increment the number of indexable items processed.void
incrementIndexableItemsProcessed(long itemsProcessed)
Increment the number of indexable items processed by the given value.void
incrementNodeCount()
Increment the number of nodes that workers are running on.void
incrementOrDecrementIntPropertyVal(String key, int incrementBy)
Provides a thread-safe, atomic way to increment an arbitrary Integer value in the additional properties map.void
incrementOrDecrementLongPropertyVal(String key, long incrementBy)
Provides a thread-safe, atomic way to increment an arbitraryNumber
value in the additional properties map.void
incrementTotalThreadCount(int threads)
Increment the number of worker threads across all nodes.boolean
isFailed()
Indicates if the process has been notified as failing by one or more threads.boolean
isMaster()
Indicates if this JVM kicked off the process for this IndexableType.Object
removeAdditionalProperty(String key)
Allows callers to remove an arbitrary entry from a map.void
setAdditionalProperty(String key, Serializable value)
Allows callers to set arbitrary state with key/value pairs for a particular process.void
setExpectedIndexableItemsToProcess(long expectedIndexableItemsToProcess)
Set the number of expected indexable items to process.
-
-
-
Method Detail
-
failFast
public void failFast()
Tells the system to arbitrarily fail without a specific exception.
-
failFast
public void failFast(@Nullable Throwable th)
Notifies that the process has failed and keeps track of the Throwables passed in. This allows other threads to monitor this state and fail the entire process fast if other thread reported a failure.- Parameters:
th
- the error which caused the process failure
-
isFailed
public boolean isFailed()
Indicates if the process has been notified as failing by one or more threads.- Returns:
- whether or not the process is failed
-
getFirstFailure
@Nullable public Throwable getFirstFailure()
More than one thread can potentially report an error.- Returns:
- the first Throwable that was reported, which caused the process to fail.
-
getAllFailures
public List<ProcessStateHolder.ProcessFailure> getAllFailures()
More than one thread may have reported an error before realizing that the process failed. This keeps track of all exceptions in order, and returns the list. It is possible that the list is empty, if someone passed in a null Throwable when failing the process.- Returns:
- the errors causing the process to fail, in order
-
getAdditionalProperty
public Object getAdditionalProperty(String key)
Returns an arbitrary Object in a map, based on the provided key.- Parameters:
key
- the key to get the arbitrary additional property- Returns:
- the property for the key
-
getAdditionalProperties
public Map<String,Object> getAdditionalProperties()
Returns an unmodifiable map of all additional properties. This is an arbitrary map of key/value pairs, where the key is a String and value is an Object.- Returns:
- all of the stored arbitrary additional properties
-
setAdditionalProperty
public void setAdditionalProperty(String key, Serializable value)
Allows callers to set arbitrary state with key/value pairs for a particular process. This is thread safe and is managed via a map.- Parameters:
key
- the key for the property to setvalue
- the value to set for the property
-
incrementOrDecrementLongPropertyVal
public void incrementOrDecrementLongPropertyVal(String key, long incrementBy)
Provides a thread-safe, atomic way to increment an arbitraryNumber
value in the additional properties map. If the property does not exist, it will be incremented (or decremented) from zero. If the property already exists and is not a long value, a ClassCastException will be thrown. A negative number can be used to decrement.- Parameters:
key
- the key for the property to updateincrementBy
- the amount to update the value by
-
incrementOrDecrementIntPropertyVal
public void incrementOrDecrementIntPropertyVal(String key, int incrementBy)
Provides a thread-safe, atomic way to increment an arbitrary Integer value in the additional properties map. If the property does not exist, it will be incremented (or decremented) from zero. If the property already exists and is not a long value, a ClassCastException will be thrown. A negative number can be used to decrement.- Parameters:
key
- the key for the property to updateincrementBy
- the amount to update the value by
-
removeAdditionalProperty
public Object removeAdditionalProperty(String key)
Allows callers to remove an arbitrary entry from a map.- Parameters:
key
- the key of the property to remove- Returns:
- the value of the removed property
-
incrementIndexableItemsProcessed
public void incrementIndexableItemsProcessed()
Increment the number of indexable items processed.
-
incrementIndexableItemsProcessed
public void incrementIndexableItemsProcessed(long itemsProcessed)
Increment the number of indexable items processed by the given value.- Parameters:
itemsProcessed
- the amount to increment
-
getIndexableItemsProcessed
public long getIndexableItemsProcessed()
Get the number of indexable items processed.- Returns:
- the number of indexable items processed
-
setExpectedIndexableItemsToProcess
public void setExpectedIndexableItemsToProcess(long expectedIndexableItemsToProcess)
Set the number of expected indexable items to process.- Parameters:
expectedIndexableItemsToProcess
- the number of expected indexable items to process
-
getExpectedIndexableItemsToProcess
public long getExpectedIndexableItemsToProcess()
Get the number of expected indexable items to process.- Returns:
- the number of expected indexable items to process
-
incrementNodeCount
public void incrementNodeCount()
Increment the number of nodes that workers are running on.
-
getNodeCount
public int getNodeCount()
Get the number of nodes that workers are running on.- Returns:
- the number of nodes that workers are running on
-
incrementTotalThreadCount
public void incrementTotalThreadCount(int threads)
Increment the number of worker threads across all nodes.- Parameters:
threads
- the amount to increment
-
getTotalThreadCount
public int getTotalThreadCount()
Get the number of worker threads across all nodes.- Returns:
- the number of worker threads across all nodes
-
isMaster
public boolean isMaster()
Indicates if this JVM kicked off the process for this IndexableType.- Returns:
- whether this JVM kicked off the process for this IndexableType
-
getIndexableType
public com.broadleafcommerce.search.api.type.IndexableType getIndexableType()
The indexable type for the reindex process this state holder belongs to.- Specified by:
getIndexableType
in interfaceIndexableTypeAware
- Returns:
- the indexable type for the reindex process
-
getTimeStartedMillis
public long getTimeStartedMillis()
The time the process state holder was created.- Returns:
- the time the process state holder was created
-
getCorrelationId
public String getCorrelationId()
UUID that is assigned per process, per node. Each new process will receive a new UUID for each participating node.- Returns:
- UUID for this process on this node
-
-