Class Worker

java.lang.Object
titan.scheduler.Worker

public class Worker extends Object
Represents a worker node in the scheduling system. A worker has a host, port, and a set of capabilities. It maintains its current load, maximum capacity, and last seen timestamp to facilitate job assignment and resource management. Workers can be permanent or temporary.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final List<String>
    A list of capabilities or features supported by this worker.
    The ID of the job currently assigned to this worker, if any.
    private int
    The current number of jobs or tasks actively being processed by this worker.
    private final String
    The hostname or IP address of the worker.
    private long
    The timestamp (in milliseconds) when the worker became idle (currentLoad dropped to 0).
    private boolean
    Indicates whether this worker is a permanent part of the cluster or a temporary, dynamically provisioned worker.
    private long
    The timestamp (in milliseconds) when this worker was last seen or updated.
    static final int
    The maximum number of concurrent job slots available on any worker.
    private int
    The maximum number of jobs or tasks this worker can handle concurrently.
    private final int
    The port number on which the worker is listening.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Worker(String host, int port, List<String> capabilities, boolean isPermanent)
    Constructs a new Worker instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns an unmodifiable list of capabilities supported by this worker.
    void
    Decrements the current load of the worker by one, if the load is greater than zero.
    int
    Returns the current load of the worker.
    long
    Calculates and returns the duration (in milliseconds) for which the worker has been idle.
    int
    Returns the maximum capacity of the worker.
    Returns the hostname or IP address of the worker.
    void
    Increments the current load of the worker by one.
    boolean
    Checks if this worker is designated as a permanent worker.
    boolean
    Checks if the worker is saturated, meaning its current load has reached or exceeded its maximum capacity.
    long
    Returns the timestamp (in milliseconds) when this worker was last seen or updated.
    int
    Returns the port number on which the worker is listening.
    void
    setCurrentLoad(int load)
    Sets the current load of the worker.
    void
    setMaxCap(int maxCap)
    Sets the maximum capacity of the worker.
    Returns a string representation of the Worker, including its host, port, and current load.
    void
    Updates the lastSeen timestamp to the current system time.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • host

      private final String host
      The hostname or IP address of the worker.
    • port

      private final int port
      The port number on which the worker is listening.
    • capabilities

      private final List<String> capabilities
      A list of capabilities or features supported by this worker. These capabilities can be used by the scheduler to match jobs to suitable workers.
    • lastSeen

      private long lastSeen
      The timestamp (in milliseconds) when this worker was last seen or updated. Used for liveness checks and identifying stale workers.
    • currentLoad

      private int currentLoad
      The current number of jobs or tasks actively being processed by this worker.
    • maxCap

      private int maxCap
      The maximum number of jobs or tasks this worker can handle concurrently. This field will eventually be replaced by MAX_SLOTS.
    • currentJobId

      public String currentJobId
      The ID of the job currently assigned to this worker, if any. This field is public for direct access by the scheduler.
    • MAX_SLOTS

      public static final int MAX_SLOTS
      The maximum number of concurrent job slots available on any worker. This is a static constant representing the default or system-wide maximum capacity. This will replace the instance-specific maxCap field.
      See Also:
    • idleStartTime

      private long idleStartTime
      The timestamp (in milliseconds) when the worker became idle (currentLoad dropped to 0). A value of -1 indicates the worker is currently busy.
    • isPermanent

      private boolean isPermanent
      Indicates whether this worker is a permanent part of the cluster or a temporary, dynamically provisioned worker. Permanent workers might have different lifecycle management.
  • Constructor Details

    • Worker

      public Worker(String host, int port, List<String> capabilities, boolean isPermanent)
      Constructs a new Worker instance. Initializes the worker with its network details, capabilities, and sets its initial state.
      Parameters:
      host - The hostname or IP address of the worker.
      port - The port number on which the worker is listening.
      capabilities - A list of capabilities supported by this worker. Can be null, in which case an empty list is used.
      isPermanent - True if this worker is a permanent part of the cluster, false otherwise.
  • Method Details

    • updateLastSeen

      public void updateLastSeen()
      Updates the lastSeen timestamp to the current system time. This method is synchronized to ensure thread-safe updates to the worker's liveness status.
    • isPermanent

      public boolean isPermanent()
      Checks if this worker is designated as a permanent worker.
      Returns:
      true if the worker is permanent, false otherwise.
    • setCurrentLoad

      public void setCurrentLoad(int load)
      Sets the current load of the worker. If the load becomes 0, it records the idleStartTime. If the load is greater than 0, it resets idleStartTime to -1. This method is synchronized to ensure thread-safe updates to the worker's load and idle state.
      Parameters:
      load - The new current load value. Must be non-negative.
    • setMaxCap

      public void setMaxCap(int maxCap)
      Sets the maximum capacity of the worker. This method is synchronized to ensure thread-safe updates to the worker's capacity.
      Parameters:
      maxCap - The new maximum capacity value.
    • getMaxCap

      public int getMaxCap()
      Returns the maximum capacity of the worker. This method is synchronized to ensure thread-safe access to the worker's capacity.
      Returns:
      The maximum number of jobs or tasks this worker can handle concurrently.
    • getIdleDuration

      public long getIdleDuration()
      Calculates and returns the duration (in milliseconds) for which the worker has been idle. If the worker is currently busy (idleStartTime is -1), it returns 0.
      Returns:
      The idle duration in milliseconds, or 0 if the worker is currently busy.
    • getCurrentLoad

      public int getCurrentLoad()
      Returns the current load of the worker. This method is synchronized to ensure thread-safe access to the worker's load.
      Returns:
      The current number of jobs or tasks actively being processed.
    • incrementCurrentLoad

      public void incrementCurrentLoad()
      Increments the current load of the worker by one. Resets the idleStartTime if it was previously set, as the worker is now busy. This method is synchronized to ensure thread-safe load updates.
    • decrementCurrentLoad

      public void decrementCurrentLoad()
      Decrements the current load of the worker by one, if the load is greater than zero. If the load drops to zero after decrementing, it records the idleStartTime. This method is synchronized to ensure thread-safe load updates and idle state management.
    • isSaturated

      public boolean isSaturated()
      Checks if the worker is saturated, meaning its current load has reached or exceeded its maximum capacity. This method is synchronized to ensure thread-safe access to load and capacity.
      Returns:
      true if the worker is saturated, false otherwise.
    • host

      public String host()
      Returns the hostname or IP address of the worker. This is a record-style accessor for the host field.
      Returns:
      The host string.
    • port

      public int port()
      Returns the port number on which the worker is listening. This is a record-style accessor for the port field.
      Returns:
      The port number.
    • lastSeen

      public long lastSeen()
      Returns the timestamp (in milliseconds) when this worker was last seen or updated. This is a record-style accessor for the lastSeen field.
      Returns:
      The last seen timestamp.
    • capabilities

      public List<String> capabilities()
      Returns an unmodifiable list of capabilities supported by this worker. This is a record-style accessor for the capabilities field.
      Returns:
      An unmodifiable list of capabilities.
    • toString

      public String toString()
      Returns a string representation of the Worker, including its host, port, and current load.
      Overrides:
      toString in class Object
      Returns:
      A formatted string representing the worker.