Class CoordinatorHelper

java.lang.Object
org.codelibs.fess.helper.CoordinatorHelper

public class CoordinatorHelper extends Object
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.
  • 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

      public List<CoordinatorHelper.InstanceInfo> getActiveInstances()
      Returns a list of currently active instances based on non-expired heartbeat documents.
      Returns:
      the list of active instances.
    • isInstanceActive

      public boolean isInstanceActive(String targetInstanceId)
      Checks whether the specified instance is currently active.
      Parameters:
      targetInstanceId - the instance ID to check.
      Returns:
      true if the instance is active.
    • tryStartOperation

      public boolean tryStartOperation(String operationName)
      Attempts to acquire a distributed lock for the specified operation.
      Parameters:
      operationName - the operation name used as the lock document ID.
      Returns:
      true if the lock was acquired.
    • tryStartOperation

      public boolean tryStartOperation(String operationName, String data)
      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:
      true if the lock was acquired.
    • tryStartOperation

      protected boolean tryStartOperation(String operationName, String data, int remainingRetries)
      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:
      true if the lock was acquired.
    • tryCleanupAndRetry

      protected boolean tryCleanupAndRetry(String operationName, String data, int remainingRetries)
      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:
      true if the lock was acquired after cleanup.
    • completeOperation

      public void completeOperation(String operationName)
      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

      public boolean isOperationRunning(String operationName)
      Checks whether the specified operation is currently running (lock held by an active instance).
      Parameters:
      operationName - the operation name to check.
      Returns:
      true if the operation is running.
    • getOperationInfo

      public Optional<CoordinatorHelper.OperationInfo> getOperationInfo(String operationName)
      Retrieves information about the specified operation.
      Parameters:
      operationName - the operation name to look up.
      Returns:
      an Optional containing the operation info, or empty if not found.
    • publishEvent

      public void publishEvent(String eventName, String data)
      Publishes an event to all instances.
      Parameters:
      eventName - the event name.
      data - optional event data.
    • publishEvent

      public void publishEvent(String eventName, String targetInstanceId, String data)
      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

      public void addEventHandler(String eventName, Consumer<CoordinatorHelper.EventInfo> handler)
      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

      protected List<CoordinatorHelper.EventInfo> 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

      protected void dispatchEvent(CoordinatorHelper.EventInfo event)
      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

      protected String getIndexName()
      Returns the coordinator index name, adjusted for the configured index prefix.
      Returns:
      the coordinator index name.
    • toJson

      protected String toJson(Map<String,Object> map)
      Serializes a map to a JSON string.
      Parameters:
      map - the map to serialize.
      Returns:
      the JSON string, or "{}" on failure.
    • parseJson

      protected Map<String,Object> parseJson(String json)
      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

      protected Map<String,Object> getMapValue(Map<String,Object> map, String key)
      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 null if not found or not a map.
    • getListValue

      protected List<Map<String,Object>> getListValue(Map<String,Object> map, String key)
      Gets a list value from the given map.
      Parameters:
      map - the source map.
      key - the key to look up.
      Returns:
      the list value, or null if not found or not a list.
    • getStringValue

      protected String getStringValue(Map<String,Object> map, String key)
      Gets a string value from the given map.
      Parameters:
      map - the source map.
      key - the key to look up.
      Returns:
      the string value, or null if not found.
    • getLongValue

      protected long getLongValue(Map<String,Object> map, String key)
      Gets a long value from the given map.
      Parameters:
      map - the source map.
      key - the key to look up.
      Returns:
      the long value, or 0L if not found or not a number.