|
| 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 { ReadWriteSplittingPlugin } from "./read_write_splitting_plugin"; |
| 18 | +import { PluginService } from "../../plugin_service"; |
| 19 | +import { WrapperProperties } from "../../wrapper_property"; |
| 20 | +import { HostInfo } from "../../host_info"; |
| 21 | +import { RdsUtils } from "../../utils/rds_utils"; |
| 22 | +import { ReadWriteSplittingError } from "../../utils/errors"; |
| 23 | +import { Messages } from "../../utils/messages"; |
| 24 | +import { logger } from "../../../logutils"; |
| 25 | +import { ClientWrapper } from "../../client_wrapper"; |
| 26 | +import { equalsIgnoreCase } from "../../utils/utils"; |
| 27 | + |
| 28 | +export class GdbReadWriteSplittingPlugin extends ReadWriteSplittingPlugin { |
| 29 | + protected readonly rdsUtils: RdsUtils = new RdsUtils(); |
| 30 | + |
| 31 | + protected readonly restrictWriterToHomeRegion: boolean; |
| 32 | + protected readonly restrictReaderToHomeRegion: boolean; |
| 33 | + |
| 34 | + protected isInitialized: boolean = false; |
| 35 | + protected homeRegion: string; |
| 36 | + |
| 37 | + constructor(pluginService: PluginService, properties: Map<string, any>) { |
| 38 | + super(pluginService, properties); |
| 39 | + this.restrictWriterToHomeRegion = WrapperProperties.GDB_RW_RESTRICT_WRITER_TO_HOME_REGION.get(properties); |
| 40 | + this.restrictReaderToHomeRegion = WrapperProperties.GDB_RW_RESTRICT_READER_TO_HOME_REGION.get(properties); |
| 41 | + } |
| 42 | + |
| 43 | + protected initSettings(initHostInfo: HostInfo, properties: Map<string, any>): void { |
| 44 | + if (this.isInitialized) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + this.isInitialized = true; |
| 49 | + |
| 50 | + this.homeRegion = WrapperProperties.GDB_RW_HOME_REGION.get(properties); |
| 51 | + if (!this.homeRegion) { |
| 52 | + const rdsUrlType = this.rdsUtils.identifyRdsType(initHostInfo.host); |
| 53 | + if (rdsUrlType.hasRegion) { |
| 54 | + this.homeRegion = this.rdsUtils.getRdsRegion(initHostInfo.host); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (!this.homeRegion) { |
| 59 | + throw new ReadWriteSplittingError(Messages.get("GdbReadWriteSplittingPlugin.missingHomeRegion", initHostInfo.host)); |
| 60 | + } |
| 61 | + |
| 62 | + logger.debug(Messages.get("GdbReadWriteSplittingPlugin.parameterValue", "gdbRwHomeRegion", this.homeRegion)); |
| 63 | + } |
| 64 | + |
| 65 | + override async connect( |
| 66 | + hostInfo: HostInfo, |
| 67 | + props: Map<string, any>, |
| 68 | + isInitialConnection: boolean, |
| 69 | + connectFunc: () => Promise<ClientWrapper> |
| 70 | + ): Promise<ClientWrapper> { |
| 71 | + this.initSettings(hostInfo, props); |
| 72 | + return super.connect(hostInfo, props, isInitialConnection, connectFunc); |
| 73 | + } |
| 74 | + |
| 75 | + override setWriterClient(writerTargetClient: ClientWrapper | undefined, writerHostInfo: HostInfo) { |
| 76 | + if ( |
| 77 | + this.restrictWriterToHomeRegion && |
| 78 | + this.writerHostInfo != null && |
| 79 | + !equalsIgnoreCase(this.rdsUtils.getRdsRegion(this.writerHostInfo.host), this.homeRegion) |
| 80 | + ) { |
| 81 | + throw new ReadWriteSplittingError( |
| 82 | + Messages.get("GdbReadWriteSplittingPlugin.cantConnectWriterOutOfHomeRegion", writerHostInfo.host, this.homeRegion) |
| 83 | + ); |
| 84 | + } |
| 85 | + super.setWriterClient(writerTargetClient, writerHostInfo); |
| 86 | + } |
| 87 | + |
| 88 | + protected getReaderHostCandidates(): HostInfo[] { |
| 89 | + if (this.restrictReaderToHomeRegion) { |
| 90 | + const hostsInRegion: HostInfo[] = this.pluginService |
| 91 | + .getHosts() |
| 92 | + .filter((x) => equalsIgnoreCase(this.rdsUtils.getRdsRegion(x.host), this.homeRegion)); |
| 93 | + |
| 94 | + if (hostsInRegion.length === 0) { |
| 95 | + throw new ReadWriteSplittingError(Messages.get("GdbReadWriteSplittingPlugin.noAvailableReadersInHomeRegion", this.homeRegion)); |
| 96 | + } |
| 97 | + return hostsInRegion; |
| 98 | + } |
| 99 | + return super.getReaderHostCandidates(); |
| 100 | + } |
| 101 | +} |
0 commit comments