Package org.codelibs.fess.helper
Class CoordinatorHelper
java.lang.Object
org.codelibs.fess.helper.CoordinatorHelper
Helper for inter-instance coordination via OpenSearch.
Provides heartbeat registration, distributed operation locking, and event notification
to prevent concurrent execution of maintenance operations across multiple Fess instances.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents an inter-instance event notification.static classRepresents an active Fess instance discovered via heartbeat.static classRepresents the state of a distributed operation lock. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEventHandler(String eventName, Consumer<CoordinatorHelper.EventInfo> handler) Registers an event handler for the specified event name.protected voidDeletes expired heartbeat, operation, and event documents from the coordinator index.voidcompleteOperation(String operationName) Releases the operation lock by deleting the operation document.voiddestroy()Stops the poll loop and removes the heartbeat document on shutdown.protected voidDispatches an event to all registered handlers for the event name.protected List<CoordinatorHelper.EventInfo> Fetches new events from OpenSearch that were created after the last check time.Returns a list of currently active instances based on non-expired heartbeat documents.protected StringReturns the coordinator index name, adjusted for the configured index prefix.getListValue(Map<String, Object> map, String key) Gets a list value from the given map.protected longgetLongValue(Map<String, Object> map, String key) Gets a long value from the given map.getMapValue(Map<String, Object> map, String key) Gets a nested map value from the given map.getOperationInfo(String operationName) Retrieves information about the specified operation.protected StringgetStringValue(Map<String, Object> map, String key) Gets a string value from the given map.voidinit()Initializes the coordinator by sending an initial heartbeat and starting the poll loop.booleanisInstanceActive(String targetInstanceId) Checks whether the specified instance is currently active.booleanisOperationRunning(String operationName) Checks whether the specified operation is currently running (lock held by an active instance).Parses a JSON string into a map.protected voidpoll()Periodic poll that sends a heartbeat, processes new events, and cleans up expired documents.voidpublishEvent(String eventName, String data) Publishes an event to all instances.voidpublishEvent(String eventName, String targetInstanceId, String data) Publishes an event to a specific instance or all instances.protected voidRemoves the heartbeat document for this instance from OpenSearch.voidSends a heartbeat document to OpenSearch to indicate this instance is active.protected StringSerializes a map to a JSON string.protected booleantryCleanupAndRetry(String operationName, String data, int remainingRetries) Checks if the existing operation lock is stale (expired or owner inactive) and retries acquisition.booleantryStartOperation(String operationName) Attempts to acquire a distributed lock for the specified operation.booleantryStartOperation(String operationName, String data) Attempts to acquire a distributed lock for the specified operation with optional data.protected booleantryStartOperation(String operationName, String data, int remainingRetries) Internal method to acquire a distributed lock with retry control.
-
Constructor Details
-
CoordinatorHelper
public CoordinatorHelper()Default constructor.
-
-
Method Details
-
init
@PostConstruct public void init()Initializes the coordinator by sending an initial heartbeat and starting the poll loop. -
destroy
@PreDestroy public void destroy()Stops the poll loop and removes the heartbeat document on shutdown. -
sendHeartbeat
public void sendHeartbeat()Sends a heartbeat document to OpenSearch to indicate this instance is active. -
removeHeartbeat
protected void removeHeartbeat()Removes the heartbeat document for this instance from OpenSearch. -
getActiveInstances
Returns a list of currently active instances based on non-expired heartbeat documents.- Returns:
- the list of active instances.
-
isInstanceActive
Checks whether the specified instance is currently active.- Parameters:
targetInstanceId- the instance ID to check.- Returns:
trueif the instance is active.
-
tryStartOperation
Attempts to acquire a distributed lock for the specified operation.- Parameters:
operationName- the operation name used as the lock document ID.- Returns:
trueif the lock was acquired.
-
tryStartOperation
Attempts to acquire a distributed lock for the specified operation with optional data.- Parameters:
operationName- the operation name used as the lock document ID.data- optional data to store with the operation document.- Returns:
trueif the lock was acquired.
-
tryStartOperation
Internal method to acquire a distributed lock with retry control.- Parameters:
operationName- the operation name used as the lock document ID.data- optional data to store with the operation document.remainingRetries- the number of remaining retry attempts for stale lock cleanup.- Returns:
trueif the lock was acquired.
-
tryCleanupAndRetry
Checks if the existing operation lock is stale (expired or owner inactive) and retries acquisition.- Parameters:
operationName- the operation name.data- optional data for the operation.remainingRetries- the number of remaining retry attempts.- Returns:
trueif the lock was acquired after cleanup.
-
completeOperation
Releases the operation lock by deleting the operation document. Safe to call multiple times; does nothing if the document is already deleted.- Parameters:
operationName- the operation name whose lock should be released.
-
isOperationRunning
Checks whether the specified operation is currently running (lock held by an active instance).- Parameters:
operationName- the operation name to check.- Returns:
trueif the operation is running.
-
getOperationInfo
Retrieves information about the specified operation.- Parameters:
operationName- the operation name to look up.- Returns:
- an
Optionalcontaining the operation info, or empty if not found.
-
publishEvent
Publishes an event to all instances.- Parameters:
eventName- the event name.data- optional event data.
-
publishEvent
Publishes an event to a specific instance or all instances.- Parameters:
eventName- the event name.targetInstanceId- the target instance ID, or"*"for all instances.data- optional event data.
-
addEventHandler
Registers an event handler for the specified event name.- Parameters:
eventName- the event name to handle.handler- the consumer to invoke when the event is received.
-
fetchNewEvents
Fetches new events from OpenSearch that were created after the last check time.- Returns:
- the list of new events.
-
poll
protected void poll()Periodic poll that sends a heartbeat, processes new events, and cleans up expired documents. -
dispatchEvent
Dispatches an event to all registered handlers for the event name.- Parameters:
event- the event to dispatch.
-
cleanupExpiredDocuments
protected void cleanupExpiredDocuments()Deletes expired heartbeat, operation, and event documents from the coordinator index. -
getIndexName
Returns the coordinator index name, adjusted for the configured index prefix.- Returns:
- the coordinator index name.
-
toJson
Serializes a map to a JSON string.- Parameters:
map- the map to serialize.- Returns:
- the JSON string, or
"{}"on failure.
-
parseJson
Parses a JSON string into a map.- Parameters:
json- the JSON string to parse.- Returns:
- the parsed map, or an empty map on failure.
-
getMapValue
Gets a nested map value from the given map.- Parameters:
map- the source map.key- the key to look up.- Returns:
- the map value, or
nullif not found or not a map.
-
getListValue
Gets a list value from the given map.- Parameters:
map- the source map.key- the key to look up.- Returns:
- the list value, or
nullif not found or not a list.
-
getStringValue
Gets a string value from the given map.- Parameters:
map- the source map.key- the key to look up.- Returns:
- the string value, or
nullif not found.
-
getLongValue
Gets a long value from the given map.- Parameters:
map- the source map.key- the key to look up.- Returns:
- the long value, or
0Lif not found or not a number.
-