Class WorkerRegistry

java.lang.Object
titan.scheduler.WorkerRegistry

public class WorkerRegistry extends Object
A central registry for managing Worker instances within the Titan scheduler system. This class provides mechanisms to add, retrieve, update, and remove workers, facilitating the discovery and management of available processing capabilities. It uses a ConcurrentHashMap to ensure thread-safe operations.
  • Field Details

    • workerMap

      private final Map<String,Worker> workerMap
      A thread-safe map storing Worker instances, keyed by a unique string generated from the worker's host and port (e.g., "host:port").
  • Constructor Details

    • WorkerRegistry

      public WorkerRegistry()
      Constructs a new WorkerRegistry and initializes the internal worker map.
  • Method Details

    • getWorkerMap

      public Map<String,Worker> getWorkerMap()
      Returns the underlying map of workers. This map is a live view of the registry's state. Modifications to the returned map will directly affect the registry.
      Returns:
      A Map where keys are worker identifiers (host:port) and values are Worker objects.
    • getWorkerKey

      public String getWorkerKey(String host, int port)
      Generates a unique key for a worker based on its host and port. This key is used internally to identify workers in the registry.
      Parameters:
      host - The hostname or IP address of the worker.
      port - The port number the worker is listening on.
      Returns:
      A string representing the unique key for the worker (e.g., "localhost:8080").
    • addWorker

      public void addWorker(String host, int port, String capability, boolean isPermanent)
      Adds a new worker to the registry or updates an existing worker's capabilities and permanence status. If a worker with the given host and port already exists, its capabilities list will be updated to include the new capability if it's not already present. The `isPermanent` status will be set to true if the worker was previously marked permanent, ensuring permanence is sticky.
      Parameters:
      host - The hostname or IP address of the worker.
      port - The port number the worker is listening on.
      capability - A specific skill or capability that the worker possesses.
      isPermanent - true if the worker should be considered permanent and not removed by cleanup processes; false otherwise. Once a worker is marked permanent, it remains permanent.
    • getWorkers

      public Collection<Worker> getWorkers()
      Retrieves a collection of all currently registered workers.
      Returns:
      A Collection of Worker objects currently in the registry.
    • generateKey

      private String generateKey(String host, int port)
      A private helper method to generate a consistent key for a worker.
      Parameters:
      host - The hostname or IP address of the worker.
      port - The port number the worker is listening on.
      Returns:
      A string key in the format "host:port".
    • updateLastSeen

      public void updateLastSeen(String host, int port)
      Updates the 'last seen' timestamp for a specific worker. This method is typically called when a heartbeat or check-in is received from a worker, indicating it is still active.
      Parameters:
      host - The hostname or IP address of the worker.
      port - The port number the worker is listening on.
    • markWorkerDead

      public void markWorkerDead(String host, int port)
      Marks a worker as dead by removing it from the registry. This method effectively removes the worker entry, making it unavailable for task assignment.
      Parameters:
      host - The hostname or IP address of the worker to mark as dead.
      port - The port number of the worker to mark as dead.
    • getWorkersByCapability

      public List<Worker> getWorkersByCapability(String requiredSkill)
      Retrieves a list of workers that possess a specific capability.
      Parameters:
      requiredSkill - The capability string to filter workers by.
      Returns:
      A List of Worker objects that have the specified skill. Returns an empty list if no workers with the capability are found.
    • removeWorker

      private void removeWorker(String host, int port)
      A private helper method to remove a worker from the registry based on its host and port.
      Parameters:
      host - The hostname or IP address of the worker to remove.
      port - The port number of the worker to remove.