ablog

不器用で落着きのない技術者のメモ

MariaDB Connector/J で Aurora MySQL に接続すると INFORMATION_SCHEMA.REPLICA_HOST_STATUS からエンドポイントを取得する?

Amazon Aurora DB クラスターエンドポイントは、DNS レコードの更新を自動的に伝播しますが、処理は即座に行われるわけではありません。これにより、データベースで発生したイベントへの応答が遅れる可能性があり、イベントがアプリケーションによって処理される可能性があります。スマートドライバは、ほぼリアルタイムの INFORMATION_SCHEMA.REPLICA_HOST_STATUS メタデータテーブルを通じて DB クラスターのトポグラフィーを使用します。これにより、接続を適切なロールにルーティングし、既存のレプリカ間で負荷分散を行うのに役立ちます。MariaDB コネクタ/J は、Aurora MySQLをネイティブサポートしているサードパーティーのスマートドライバの例です。

Aurora DB クラスターがフェイルオーバーした後の読み取り専用エラーのトラブルシューティング

In versions before 1.5.1, cluster endpoint use was discouraged, since when a failover occurs, this cluster endpoint can point for a limited time to a host that isn't the current master any more. The old recommendation was to list all specific end-points. This kind of url string will still work, but now, recommended url string has to use only cluster endpoint.

Driver will automatically discover master and slaves of this cluster from current cluster end-point during connection time. This permits adding new replicas to the cluster instance which will be discovered without changing driver configuration.

Failover and High availability with MariaDB Connector/J - MariaDB Knowledge Base
  /**
   * Retrieves the information necessary to add a new endpoint. Calls the methods that retrieves the
   * instance identifiers and sets urlParser accordingly.
   *
   * @param protocol current protocol connected to
   * @throws SQLException if connection error occur
   */
  public void retrieveAllEndpointsAndSet(Protocol protocol) throws SQLException {
    // For a given cluster, same port for all endpoints and same end host address
    if (clusterDnsSuffix != null) {
      List<String> endpoints = getCurrentEndpointIdentifiers(protocol);
      setUrlParserFromEndpoints(endpoints, protocol.getPort());
    }
  }

/**
   * Retrieves all endpoints of a cluster from the appropriate database table.
   *
   * @param protocol current protocol connected to
   * @return instance endpoints of the cluster
   * @throws SQLException if connection error occur
   */
  private List<String> getCurrentEndpointIdentifiers(Protocol protocol) throws SQLException {
    List<String> endpoints = new ArrayList<>();
    try {
      proxy.lock.lock();
      try {
        // Deleted instance may remain in db for 24 hours so ignoring instances that have had no
        // change
        // for 3 minutes
        Results results = new Results();
        protocol.executeQuery(
            false,
            results,
            "select server_id, session_id from information_schema.replica_host_status " ★
                + "where last_update_timestamp > now() - INTERVAL 3 MINUTE");
        results.commandEnd();
        ResultSet resultSet = results.getResultSet();

        while (resultSet.next()) {
          endpoints.add(resultSet.getString(1) + "." + clusterDnsSuffix);
        }

        // randomize order for distributed load-balancing
        Collections.shuffle(endpoints);
  • INFORMATION_SCHEMA.REPLICA_HOST_STATUS
$ mysql -h aurora-mysql-5712-cluster.cluster-*******.ap-northeast-1.rds.amazonaws.com -u awsuser  -p
mysql> select * from INFORMATION_SCHEMA.REPLICA_HOST_STATUS\G
*************************** 1. row ***************************
                             SERVER_ID: aurora-mysql-5712
                            SESSION_ID: 5aead66f-1174-4f49-9431-2a1e5bce7020
                                  IOPS: 0
                              READ_IOS: 0
                      PENDING_READ_IOS: 0
                                   CPU: 6.324110507965088
                           DURABLE_LSN: 18446744073709551363
                            ACTIVE_LSN: 0
                  LAST_TRANSPORT_ERROR: 0
                  LAST_ERROR_TIMESTAMP: 1970-01-01 00:00:01.000000
                 LAST_UPDATE_TIMESTAMP: 2021-01-27 06:14:51.447303
  MASTER_SLAVE_LATENCY_IN_MICROSECONDS: 0
           REPLICA_LAG_IN_MILLISECONDS: 20.229000091552734
    LOG_STREAM_SPEED_IN_KiB_PER_SECOND: 2.102880738213399
            LOG_BUFFER_SEQUENCE_NUMBER: 7509
                            IS_CURRENT: 0
               OLDEST_READ_VIEW_TRX_ID: 2439276
                  OLDEST_READ_VIEW_LSN: 124843572
                  HIGHEST_LSN_RECEIVED: 124843574
                    CURRENT_READ_POINT: 124843572
CURRENT_REPLAY_LATENCY_IN_MICROSECONDS: 382690
AVERAGE_REPLAY_LATENCY_IN_MICROSECONDS: 466222
    MAX_REPLAY_LATENCY_IN_MICROSECONDS: 1026895
*************************** 2. row ***************************
                             SERVER_ID: aurora-mysql-5712-ap-northeast-1c
                            SESSION_ID: MASTER_SESSION_ID
                                  IOPS: 0
                              READ_IOS: 0
                      PENDING_READ_IOS: 0
                                   CPU: 4.332129955291748
                           DURABLE_LSN: 124843582
                            ACTIVE_LSN: 0
                  LAST_TRANSPORT_ERROR: 0
                  LAST_ERROR_TIMESTAMP: 1970-01-01 00:00:01.000000
                 LAST_UPDATE_TIMESTAMP: 2021-01-27 06:14:51.426614
  MASTER_SLAVE_LATENCY_IN_MICROSECONDS: 0
           REPLICA_LAG_IN_MILLISECONDS: 0
    LOG_STREAM_SPEED_IN_KiB_PER_SECOND: 2.102880738213399
            LOG_BUFFER_SEQUENCE_NUMBER: 7508
                            IS_CURRENT: 0
               OLDEST_READ_VIEW_TRX_ID: 0
                  OLDEST_READ_VIEW_LSN: 0
                  HIGHEST_LSN_RECEIVED: 0
                    CURRENT_READ_POINT: 0
CURRENT_REPLAY_LATENCY_IN_MICROSECONDS: 0
AVERAGE_REPLAY_LATENCY_IN_MICROSECONDS: 0
    MAX_REPLAY_LATENCY_IN_MICROSECONDS: 0
2 rows in set (0.03 sec)

mysql>