|
| 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 | + |
| 17 | +import { ClusterTopologyMonitorImpl } from "./cluster_topology_monitor"; |
| 18 | +import { GdbTopologyUtils, GlobalTopologyUtils } from "../global_topology_utils"; |
| 19 | +import { FullServicesContainer } from "../../utils/full_services_container"; |
| 20 | +import { HostInfo } from "../../host_info"; |
| 21 | +import { ClientWrapper } from "../../client_wrapper"; |
| 22 | +import { AwsWrapperError } from "../../utils/errors"; |
| 23 | +import { Messages } from "../../utils/messages"; |
| 24 | +import { TopologyUtils } from "../topology_utils"; |
| 25 | + |
| 26 | +function isGdbTopologyUtils(utils: TopologyUtils): utils is TopologyUtils & GdbTopologyUtils { |
| 27 | + return "getRegion" in utils && typeof (utils as unknown as GdbTopologyUtils).getRegion === "function"; |
| 28 | +} |
| 29 | + |
| 30 | +export class GlobalAuroraTopologyMonitor extends ClusterTopologyMonitorImpl { |
| 31 | + protected readonly instanceTemplatesByRegion: Map<string, HostInfo>; |
| 32 | + declare public readonly topologyUtils: TopologyUtils; |
| 33 | + |
| 34 | + constructor( |
| 35 | + servicesContainer: FullServicesContainer, |
| 36 | + topologyUtils: TopologyUtils, |
| 37 | + clusterId: string, |
| 38 | + initialHostInfo: HostInfo, |
| 39 | + properties: Map<string, any>, |
| 40 | + instanceTemplate: HostInfo, |
| 41 | + refreshRateNano: number, |
| 42 | + highRefreshRateNano: number, |
| 43 | + instanceTemplatesByRegion: Map<string, HostInfo> |
| 44 | + ) { |
| 45 | + super(servicesContainer, topologyUtils, clusterId, initialHostInfo, properties, instanceTemplate, refreshRateNano, highRefreshRateNano); |
| 46 | + |
| 47 | + this.instanceTemplatesByRegion = instanceTemplatesByRegion; |
| 48 | + this.topologyUtils = topologyUtils; |
| 49 | + } |
| 50 | + |
| 51 | + protected override async getInstanceTemplate(hostId: string, targetClient: ClientWrapper): Promise<HostInfo> { |
| 52 | + if (!isGdbTopologyUtils(this.topologyUtils)) { |
| 53 | + throw new AwsWrapperError(Messages.get("GlobalAuroraTopologyMonitor.invalidTopologyUtils")); |
| 54 | + } |
| 55 | + |
| 56 | + const dialect = this.hostListProviderService.getDialect(); |
| 57 | + const region = await this.topologyUtils.getRegion(hostId, targetClient, dialect); |
| 58 | + |
| 59 | + if (region) { |
| 60 | + const instanceTemplate = this.instanceTemplatesByRegion.get(region); |
| 61 | + if (!instanceTemplate) { |
| 62 | + throw new AwsWrapperError(Messages.get("GlobalAuroraTopologyMonitor.cannotFindRegionTemplate", region)); |
| 63 | + } |
| 64 | + return instanceTemplate; |
| 65 | + } |
| 66 | + |
| 67 | + return this.instanceTemplate; |
| 68 | + } |
| 69 | +} |
0 commit comments