async call()

in common/lib/plugins/failover/writer_failover_handler.ts [208:274]


  async call(): Promise<WriterFailoverResult> {
    let success = false;

    let latestTopology: HostInfo[] = [];

    if (!this.originalWriterHost) {
      return new WriterFailoverResult(
        success,
        false,
        latestTopology,
        ClusterAwareWriterFailoverHandler.RECONNECT_WRITER_TASK,
        success ? this.currentClient : null
      );
    }

    logger.info(
      Messages.get(
        "ClusterAwareWriterFailoverHandler.taskAAttemptReconnectToWriterInstance",
        this.originalWriterHost.url,
        JSON.stringify(Object.fromEntries(maskProperties(this.initialConnectionProps)))
      )
    );

    try {
      while (latestTopology.length === 0 && Date.now() < this.endTime && !this.failoverCompleted) {
        await this.pluginService.abortTargetClient(this.currentClient);

        try {
          const props = new Map(this.initialConnectionProps);
          props.set(WrapperProperties.HOST.name, this.originalWriterHost.host);
          this.currentClient = await this.pluginService.forceConnect(this.originalWriterHost, props);
          await this.pluginService.forceRefreshHostList(this.currentClient);
          latestTopology = this.pluginService.getAllHosts();
        } catch (error) {
          // Propagate errors that are not caused by network errors.
          if (error instanceof AwsWrapperError && !this.pluginService.isNetworkError(error)) {
            logger.info(Messages.get("ClusterAwareWriterFailoverHandler.taskAEncounteredError", error.message));
            return new WriterFailoverResult(false, false, [], ClusterAwareWriterFailoverHandler.RECONNECT_WRITER_TASK, null, error);
          }
        }

        if (!latestTopology || latestTopology.length === 0) {
          await new Promise((resolve) => {
            this.timeoutId = setTimeout(resolve, this.reconnectionWriterIntervalMs);
          });
        }
      }
      success = isCurrentHostWriter(latestTopology, this.originalWriterHost);

      this.pluginService.setAvailability(this.originalWriterHost.allAliases, HostAvailability.AVAILABLE);
      return new WriterFailoverResult(
        success,
        false,
        latestTopology,
        ClusterAwareWriterFailoverHandler.RECONNECT_WRITER_TASK,
        success ? this.currentClient : null
      );
    } catch (error: any) {
      logger.error(error.message);
      return new WriterFailoverResult(false, false, [], ClusterAwareWriterFailoverHandler.RECONNECT_WRITER_TASK, null);
    } finally {
      if (this.currentClient && (this.failoverCompletedDueToError || !success)) {
        await this.pluginService.abortTargetClient(this.currentClient);
      }
      logger.info(Messages.get("ClusterAwareWriterFailoverHandler.taskAFinished"));
    }
  }