|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { AuroraMySQLDatabaseDialect } from "./aurora_mysql_database_dialect"; |
| 17 | +import { GlobalAuroraTopologyDialect } from "../../../common/lib/database_dialect/topology_aware_database_dialect"; |
| 18 | +import { ClientWrapper } from "../../../common/lib/client_wrapper"; |
| 19 | +import { HostInfo } from "../../../common/lib"; |
| 20 | +import { HostListProvider } from "../../../common/lib/host_list_provider/host_list_provider"; |
| 21 | + |
| 22 | +export class GlobalAuroraMySQLDatabaseDialect extends AuroraMySQLDatabaseDialect implements GlobalAuroraTopologyDialect { |
| 23 | + private static readonly GLOBAL_STATUS_TABLE_EXISTS_QUERY = |
| 24 | + "SELECT 1 AS tmp FROM information_schema.tables WHERE" + |
| 25 | + " upper(table_schema) = 'INFORMATION_SCHEMA' AND upper(table_name) = 'AURORA_GLOBAL_DB_STATUS'"; |
| 26 | + |
| 27 | + private static readonly GLOBAL_INSTANCE_STATUS_EXISTS_QUERY = |
| 28 | + "SELECT 1 AS tmp FROM information_schema.tables WHERE" + |
| 29 | + " upper(table_schema) = 'INFORMATION_SCHEMA' AND upper(table_name) = 'AURORA_GLOBAL_DB_INSTANCE_STATUS'"; |
| 30 | + |
| 31 | + private static readonly GLOBAL_TOPOLOGY_QUERY = |
| 32 | + "SELECT SERVER_ID, CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END AS IS_WRITER, " + |
| 33 | + "VISIBILITY_LAG_IN_MSEC, AWS_REGION " + |
| 34 | + "FROM information_schema.aurora_global_db_instance_status"; |
| 35 | + |
| 36 | + private static readonly REGION_COUNT_QUERY = "SELECT count(1) FROM information_schema.aurora_global_db_status"; |
| 37 | + |
| 38 | + private static readonly REGION_BY_INSTANCE_ID_QUERY = |
| 39 | + "SELECT AWS_REGION FROM information_schema.aurora_global_db_instance_status WHERE SERVER_ID = ?"; |
| 40 | + |
| 41 | + async isDialect(targetClient: ClientWrapper): Promise<boolean> { |
| 42 | + try { |
| 43 | + // Check if both global status tables exist |
| 44 | + const [statusRows] = await targetClient.query(GlobalAuroraMySQLDatabaseDialect.GLOBAL_STATUS_TABLE_EXISTS_QUERY); |
| 45 | + if (!statusRows?.[0]) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + const [instanceStatusRows] = await targetClient.query(GlobalAuroraMySQLDatabaseDialect.GLOBAL_INSTANCE_STATUS_EXISTS_QUERY); |
| 50 | + if (!instanceStatusRows?.[0]) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + // Check if there are multiple regions |
| 55 | + const [regionCountRows] = await targetClient.query(GlobalAuroraMySQLDatabaseDialect.REGION_COUNT_QUERY); |
| 56 | + if (!regionCountRows?.[0]) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + const awsRegionCount = regionCountRows[0]["count(1)"]; |
| 61 | + return awsRegionCount > 1; |
| 62 | + } catch { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + getDialectUpdateCandidates(): string[] { |
| 68 | + return []; |
| 69 | + } |
| 70 | + |
| 71 | + async queryForTopology(targetClient: ClientWrapper, hostListProvider: HostListProvider): Promise<HostInfo[]> { |
| 72 | + const res = await targetClient.query(GlobalAuroraMySQLDatabaseDialect.GLOBAL_TOPOLOGY_QUERY); |
| 73 | + const hosts: HostInfo[] = []; |
| 74 | + const rows: any[] = res[0]; |
| 75 | + rows.forEach((row) => { |
| 76 | + // According to the topology query the result set |
| 77 | + // should contain 4 columns: node ID, 1/0 (writer/reader), CPU utilization, node lag in time. |
| 78 | + const hostName: string = row["server_id"]; |
| 79 | + const isWriter: boolean = row["is_writer"]; |
| 80 | + const hostLag: number = row["visibility_lag_in_msec"] ?? 0; // visibility_lag_in_msec is nullable. |
| 81 | + const awsRegion: string = row["aws_region"]; // TODO: update this after topologyUtils PR is merged. |
| 82 | + const host: HostInfo = hostListProvider.createHost(hostName, isWriter, Math.round(hostLag) * 100, Date.now() /* TODO: update this after topologyUtils PR is merged */); |
| 83 | + hosts.push(host); |
| 84 | + }); |
| 85 | + return hosts; |
| 86 | + } |
| 87 | + |
| 88 | + async getRegionByInstanceId(targetClient: ClientWrapper, instanceId: string): Promise<string | null> { |
| 89 | + try { |
| 90 | + const [rows] = await targetClient.query(GlobalAuroraMySQLDatabaseDialect.REGION_BY_INSTANCE_ID_QUERY, [instanceId]); |
| 91 | + if (!rows?.[0]) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + return rows[0]["AWS_REGION"] ?? null; |
| 95 | + } catch { |
| 96 | + return null; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments