net.i2p.router

## Class JobQueueScaler

- java.lang.Object
- - net.i2p.router.JobQueueScaler

- All Implemented Interfaces:
    Runnable

      class JobQueueScaler
      extends Object
      implements Runnable

  Dynamic job runner scaling controller for the JobQueue. Monitors queue metrics (ready job count, lag) and automatically scales the number of JobQueueRunner threads up or down based on load conditions. Scaling Algorithm:
  - Scale UP when: readyJobs > activeRunners * scaleUpJobsRatio OR maxLag > scaleUpLagThreshold
  - Scale DOWN when: readyJobs < activeRunners AND maxLag < scaleDownLagThreshold (sustained)
  - Bounds: min runners = max(4, cores), max runners = min(2 * router.maxJobRunners, RAM-based limit)
  Feedback Mechanism:
  - Captures pre-scale metrics before adding runners
  - Compares post-scale metrics after 3 check intervals
  - If lag INCREASED after scaling, automatically rolls back added runners
  - After 2 failed scale attempts, stops scaling up until router restart
  - Tracks memory usage to prevent OOM conditions
  Sub-millisecond Lag Target: With very low lag targets (sub-ms), even small degradations are significant. The feedback system uses tight thresholds (20% increase = failure) to ensure we don't add runners that worsen performance.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `JobQueueScaler(RouterContext context, JobQueue jobQueue)` Create a new JobQueueScaler.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `getCurrentMaxRunners()` Get the current maximum runner limit (may be up to 2x configured or RAM-limited).
    `boolean` | `isAlive()` Check if the scaler is currently running.
    `void` | `run()` Main scaling loop.
    `void` | `shutdown()` Shutdown the scaler gracefully.
    `void` | `startup()` Start the scaler thread.
    `void` | `updateMaxRunners(int configuredMax)` Update the maximum runner limit (called when configuration changes).

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### JobQueueScaler

          public JobQueueScaler(RouterContext context,
                                JobQueue jobQueue)

      Create a new JobQueueScaler.
      Parameters:
        `context` - the router context
        `jobQueue` - the job queue to scale

  - ### Method Detail

    - #### getCurrentMaxRunners

          public int getCurrentMaxRunners()

      Get the current maximum runner limit (may be up to 2x configured or RAM-limited).

    - #### isAlive

          public boolean isAlive()

      Check if the scaler is currently running.

    - #### run

          public void run()

      Main scaling loop.
      Specified by:
        `run` in interface `Runnable`

    - #### shutdown

          public void shutdown()

      Shutdown the scaler gracefully.

    - #### startup

          public void startup()

      Start the scaler thread.

    - #### updateMaxRunners

          public void updateMaxRunners(int configuredMax)

      Update the maximum runner limit (called when configuration changes).