async scaleInstance()

in projects/alloydb-autoscaler/src/alloydb-autoscaler/scaler/alloydb-scaler-executor.ts [34:97]


  async scaleInstance(
    instance: AlloyDbScalableInstanceWithData,
    suggestedSize: number
  ): Promise<string | null> {
    if (!instance?.info?.resourcePath) {
      throw new Error(
        'No resource path specified for instance. Unable to scale'
      );
    }

    if (instance?.metadata?.instanceType !== 'READ_POOL') {
      throw new Error(
        'Only AlloyDB read pool instances are currently supported. ' +
          `Instance ${instance.info.resourcePath} is of type ` +
          `${instance?.metadata?.instanceType}`
      );
    }

    const payloadLogger = getPayloadLogger(this.baseLogger, instance, instance);
    payloadLogger.info({
      message:
        `----- ${instance.info.resourcePath}: ` +
        'Scaling AlloyDB read pool instance to ' +
        `${suggestedSize}${getUnitsText(instance)} -----`,
    });

    const scaleRequest = {
      instance: {
        name: instance.info.resourcePath,
        readPoolConfig: {nodeCount: suggestedSize},
      },
      updateMask: {paths: ['read_pool_config.node_count']},
    };

    const response = await this.alloyDbClient.updateInstance(scaleRequest);
    if (!response || !Array.isArray(response)) {
      throw new Error(
        `Executing scaling operation to ${suggestedSize} for ` +
          `${instance.info.resourcePath} failed for unknown reasons`
      );
    }

    const [operation] = response;
    if (!operation) {
      throw new Error(
        `Executing scaling operation to ${suggestedSize} for ` +
          `${instance.info.resourcePath} failed for unknown reasons`
      );
    }

    if (!operation?.name) {
      payloadLogger.error({
        message:
          `Scaling operation for ${instance.info.resourcePath} returned an ` +
          'empty operation name. Operation may not be running',
      });
    } else {
      payloadLogger.debug({
        message: `Started the scaling operation: ${operation.name}`,
      });
    }

    return operation?.name ?? null;
  }