async function processScalingRequest()

in src/scaler/scaler-core/index.js [345:462]


async function processScalingRequest(cluster, autoscalerState) {
  logger.info({
    message: `----- ${cluster.projectId}/${cluster.regionId}/${cluster.clusterId}: Scaling request received`,
    projectId: cluster.projectId,
    regionId: cluster.regionId,
    clusterId: cluster.clusterId,
    payload: cluster,
  });

  // Check for ongoing LRO
  const savedState = await readStateCheckOngoingLRO(cluster, autoscalerState);
  const suggestedSize = await getSuggestedSize(cluster);

  if (!savedState.scalingOperationId) {
    // no ongoing LRO, lets see if scaling is required.
    if (
      suggestedSize === cluster.currentSize &&
      cluster.currentSize === cluster.maxSize
    ) {
      logger.info({
        message: `----- ${cluster.projectId}/${cluster.regionId}/${cluster.clusterId}: has ${cluster.currentSize} ${cluster.units}, no scaling possible - at maxSize`,
        projectId: cluster.projectId,
        regionId: cluster.regionId,
        clusterId: cluster.clusterId,
        payload: cluster,
      });
      await Counters.incScalingDeniedCounter(
        cluster,
        suggestedSize,
        'MAX_SIZE',
      );
      return;
    } else if (suggestedSize == cluster.currentSize) {
      logger.info({
        message: `----- ${cluster.projectId}/${cluster.regionId}/${cluster.clusterId}: has ${cluster.currentSize} ${cluster.units}, no scaling needed at the moment`,
        projectId: cluster.projectId,
        regionId: cluster.regionId,
        clusterId: cluster.clusterId,
        payload: cluster,
      });
      await Counters.incScalingDeniedCounter(
        cluster,
        suggestedSize,
        'CURRENT_SIZE',
      );
      return;
    }

    if (
      !withinCooldownPeriod(
        cluster,
        suggestedSize,
        savedState,
        autoscalerState.now,
      )
    ) {
      let eventType;
      try {
        const operationId = await scaleMemorystoreCluster(
          cluster,
          suggestedSize,
        );
        await autoscalerState.updateState({
          ...savedState,
          scalingOperationId: operationId,
          scalingRequestedSize: suggestedSize,
          lastScalingTimestamp: autoscalerState.now,
          lastScalingCompleteTimestamp: null,
          scalingPreviousSize: cluster.currentSize,
          scalingMethod: cluster.scalingMethod,
        });
        eventType = 'SCALING';
      } catch (err) {
        logger.error({
          message: `----- ${cluster.projectId}/${cluster.regionId}/${cluster.clusterId}: Unsuccessful scaling attempt: ${err}`,
          projectId: cluster.projectId,
          regionId: cluster.regionId,
          clusterId: cluster.clusterId,
          payload: cluster,
          err: err,
        });
        eventType = 'SCALING_FAILURE';
        await Counters.incScalingFailedCounter(cluster, suggestedSize);
      }
      await publishDownstreamEvent(eventType, cluster, suggestedSize);
    } else {
      logger.info({
        message: `----- ${cluster.projectId}/${cluster.regionId}/${cluster.clusterId}: has ${cluster.currentSize} ${cluster.units}, no scaling possible - within cooldown period`,
        projectId: cluster.projectId,
        regionId: cluster.regionId,
        clusterId: cluster.clusterId,
        payload: cluster,
      });
      await Counters.incScalingDeniedCounter(
        cluster,
        suggestedSize,
        'WITHIN_COOLDOWN',
      );
    }
  } else {
    logger.info({
      message:
        `----- ${cluster.projectId}/${cluster.regionId}/${cluster.clusterId}: has ${cluster.currentSize} ${cluster.units}, no scaling possible ` +
        `- last scaling operation (${savedState.scalingMethod} to ${savedState.scalingRequestedSize}) is still in progress. Started: ${convertMillisecToHumanReadable(
          autoscalerState.now - savedState.lastScalingTimestamp,
        )} ago.`,
      projectId: cluster.projectId,
      regionId: cluster.regionId,
      clusterId: cluster.clusterId,
      payload: cluster,
    });
    await Counters.incScalingDeniedCounter(
      cluster,
      suggestedSize,
      'IN_PROGRESS',
    );
  }
}