fix: fix fair share algorithm implementation
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
index 359cfb948c6334dc507a3d0894149790aef584f9..114ae8c555141fd08bff0103fcf79d62a65b5f83 100644 (file)
@@ -1,5 +1,3 @@
-import type { AbstractPoolWorker } from '../abstract-pool-worker'
-
 /**
  * Enumeration of worker choice strategies.
  */
@@ -9,9 +7,17 @@ export const WorkerChoiceStrategies = Object.freeze({
    */
   ROUND_ROBIN: 'ROUND_ROBIN',
   /**
-   * Less recently used worker selection strategy.
+   * Less used worker selection strategy.
+   */
+  LESS_USED: 'LESS_USED',
+  /**
+   * Less busy worker selection strategy.
+   */
+  LESS_BUSY: 'LESS_BUSY',
+  /**
+   * Fair share worker selection strategy.
    */
-  LESS_RECENTLY_USED: 'LESS_RECENTLY_USED',
+  FAIR_SHARE: 'FAIR_SHARE',
   /**
    * Weighted round robin worker selection strategy.
    */
@@ -24,17 +30,81 @@ export const WorkerChoiceStrategies = Object.freeze({
 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
 
 /**
- * Worker choice strategy interface.
+ * Worker choice strategy options.
+ */
+export interface WorkerChoiceStrategyOptions {
+  /**
+   * Use tasks median run time instead of average run time.
+   *
+   * @defaultValue false
+   */
+  medRunTime?: boolean
+  /**
+   * Worker weights to use for weighted round robin worker selection strategy.
+   * Weight is the tasks maximum average or median runtime in milliseconds.
+   *
+   * @defaultValue Computed worker weights automatically given the CPU performance.
+   */
+  weights?: Record<number, number>
+}
+
+/**
+ * Pool worker tasks usage statistics requirements.
  *
- * @template Worker Type of worker which manages the strategy.
+ * @internal
  */
-export interface IWorkerChoiceStrategy<Worker extends AbstractPoolWorker> {
+export interface RequiredStatistics {
+  /**
+   * Require tasks run time.
+   */
+  runTime: boolean
+  /**
+   * Require tasks average run time.
+   */
+  avgRunTime: boolean
+  /**
+   * Require tasks median run time.
+   */
+  medRunTime: boolean
+}
+
+/**
+ * Worker choice strategy interface.
+ */
+export interface IWorkerChoiceStrategy {
+  /**
+   * Required tasks usage statistics.
+   */
+  readonly requiredStatistics: RequiredStatistics
+  /**
+   * Resets strategy internals.
+   *
+   * @returns `true` if the reset is successful, `false` otherwise.
+   */
+  reset: () => boolean
+  /**
+   * Updates worker node strategy internals.
+   *
+   * @returns `true` if the update is successful, `false` otherwise.
+   */
+  update: (workerNodeKey: number) => boolean
+  /**
+   * Chooses a worker node in the pool and returns its key.
+   *
+   * @returns The worker node key.
+   */
+  choose: () => number
   /**
-   * Is the pool attached to the strategy dynamic?.
+   * Removes a worker node key from strategy internals.
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns `true` if the worker node key is removed, `false` otherwise.
    */
-  isDynamicPool: boolean
+  remove: (workerNodeKey: number) => boolean
   /**
-   * Choose a worker in the pool.
+   * Sets the worker choice strategy options.
+   *
+   * @param opts - The worker choice strategy options.
    */
-  choose(): Worker
+  setOptions: (opts: WorkerChoiceStrategyOptions) => void
 }