function ensureMinFreeMemory()

in src/scaler/scaler-core/scaling-methods/base.js [256:286]


function ensureMinFreeMemory(cluster, suggestedSize, scalingDirection) {
  const currentUtilization = getMaxMemoryUtilization(cluster);
  const usedShards = cluster.currentSize * (currentUtilization / 100);
  const safeSize = Math.ceil(
    usedShards / (1 - cluster.minFreeMemoryPercent / 100),
  );
  const suggestedUsagePct = Math.round((usedShards / suggestedSize) * 100);
  const safeSizeUsagePct = Math.round((usedShards / safeSize) * 100);

  let size = suggestedSize;

  logger.debug({
    message: `\tCurrent memory utilization: ${currentUtilization.toFixed(2)}%; safe utilization is at ${safeSize} ${cluster.units}: ${safeSizeUsagePct}% (utilization at suggested ${suggestedSize} ${cluster.units}: ${suggestedUsagePct}%)`,
    projectId: cluster.projectId,
    regionId: cluster.regionId,
    clusterId: cluster.clusterId,
  });

  if (suggestedSize < safeSize) {
    size = safeSize;
    logger.debug({
      message:
        `\tModifying scale ${scalingDirection} to ${size} ${cluster.units} (from ${suggestedSize} ${cluster.units}) ` +
        `to ensure safe scaling (used ${usedShards.toFixed(2)} ${cluster.units}, minFreeMemoryPercent ${cluster.minFreeMemoryPercent}%)`,
      projectId: cluster.projectId,
      regionId: cluster.regionId,
      clusterId: cluster.clusterId,
    });
  }
  return size;
}