private async isScalingWithinCooldownPeriod()

in projects/alloydb-autoscaler/src/autoscaler-core/scaler/scaler-evaluator.ts [155:214]


  private async isScalingWithinCooldownPeriod(
    instance: ScalableInstanceWithData,
    suggestedSize: number,
    autoscalerState: StateData
  ): Promise<boolean> {
    this.payloadLogger.debug({
      message:
        `-----  ${instance.info.resourcePath}: Verifying if scaling is ` +
        'allowed -----',
    });

    const lastScalingMilliseconds = autoscalerState.lastScalingCompleteTimestamp
      ? autoscalerState.lastScalingCompleteTimestamp
      : autoscalerState.lastScalingTimestamp;

    const suggestedDirection = getScalingDirection(instance, suggestedSize);
    let coolingMilliseconds;
    switch (suggestedDirection) {
      case ScalingDirection.SCALE_UP:
        coolingMilliseconds =
          instance.scalingConfig.scaleOutCoolingMinutes * MS_IN_1_MIN;
        break;
      case ScalingDirection.SCALE_DOWN:
        coolingMilliseconds =
          instance.scalingConfig.scaleInCoolingMinutes * MS_IN_1_MIN;
        break;
      default:
      case ScalingDirection.SCALE_SAME:
        // This should not happen. SCALE_SAME is and should be checked before
        // checking isScalingWithinCooldownPeriod.
        throw new Error(
          'Checking cooldown operation, but no operation should have been ' +
            'running'
        );
    }

    let cooldownPeriodOver;
    if (!lastScalingMilliseconds) {
      cooldownPeriodOver = true;
      this.payloadLogger.debug({
        message: '\tNo previous scaling operation found for this cluster',
      });
    } else {
      const elapsedMilliseconds = Date.now() - lastScalingMilliseconds;
      cooldownPeriodOver = elapsedMilliseconds >= coolingMilliseconds;
      this.payloadLogger.debug({
        message:
          '\tLast scaling operation was ' +
          `${convertMillisecondsToHumanReadable(elapsedMilliseconds)} ago.`,
      });
    }

    if (cooldownPeriodOver) {
      this.payloadLogger.info({message: '\t=> Autoscale allowed'});
      return false;
    } else {
      this.payloadLogger.info({message: '\t=> Autoscale NOT allowed yet'});
      return true;
    }
  }