|
| 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 { TestEnvironment } from "./utils/test_environment"; |
| 18 | +import { DriverHelper } from "./utils/driver_helper"; |
| 19 | +import { AuroraTestUtility } from "./utils/aurora_test_utility"; |
| 20 | +import { FailoverSuccessError } from "../../../../common/lib/utils/errors"; |
| 21 | +import { ProxyHelper } from "./utils/proxy_helper"; |
| 22 | +import { logger } from "../../../../common/logutils"; |
| 23 | +import { TestEnvironmentFeatures } from "./utils/test_environment_features"; |
| 24 | +import { features, instanceCount } from "./config"; |
| 25 | +import { InternalPooledConnectionProvider } from "../../../../common/lib/internal_pooled_connection_provider"; |
| 26 | +import { PluginManager } from "../../../../common/lib"; |
| 27 | +import { RdsHostListProvider } from "../../../../common/lib/host_list_provider/rds_host_list_provider"; |
| 28 | +import { PluginService } from "../../../../common/lib/plugin_service"; |
| 29 | +import { AwsPoolConfig } from "../../../../common/lib/aws_pool_config"; |
| 30 | + |
| 31 | +const itIf = |
| 32 | + !features.includes(TestEnvironmentFeatures.PERFORMANCE) && |
| 33 | + features.includes(TestEnvironmentFeatures.IAM) && |
| 34 | + !features.includes(TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY) && |
| 35 | + instanceCount >= 2 |
| 36 | + ? it |
| 37 | + : it.skip; |
| 38 | +const itIfMinThreeInstance = instanceCount >= 3 ? itIf : it.skip; |
| 39 | + |
| 40 | +let env: TestEnvironment; |
| 41 | +let driver; |
| 42 | +let initClientFunc: (props: any) => any; |
| 43 | +let client: any; |
| 44 | +let secondaryClient: any; |
| 45 | +let auroraTestUtility: AuroraTestUtility; |
| 46 | +let provider: InternalPooledConnectionProvider | null; |
| 47 | + |
| 48 | +async function initConfig(host: string, port: number, connectToProxy: boolean, plugins: string): Promise<any> { |
| 49 | + let config: any = { |
| 50 | + user: env.databaseInfo.username, |
| 51 | + host: host, |
| 52 | + database: env.databaseInfo.defaultDbName, |
| 53 | + password: env.databaseInfo.password, |
| 54 | + port: port, |
| 55 | + plugins: plugins, |
| 56 | + enableTelemetry: true, |
| 57 | + telemetryTracesBackend: "OTLP", |
| 58 | + telemetryMetricsBackend: "OTLP", |
| 59 | + readerHostSelectorStrategy: "fastestResponse" |
| 60 | + }; |
| 61 | + |
| 62 | + if (connectToProxy) { |
| 63 | + config["clusterInstanceHostPattern"] = "?." + env.proxyDatabaseInfo.instanceEndpointSuffix; |
| 64 | + } |
| 65 | + config = DriverHelper.addDriverSpecificConfiguration(config, env.engine); |
| 66 | + return config; |
| 67 | +} |
| 68 | + |
| 69 | +async function initDefaultConfig(host: string, port: number, connectToProxy: boolean): Promise<any> { |
| 70 | + return await initConfig(host, port, connectToProxy, "readWriteSplitting,fastestResponseStrategy"); |
| 71 | +} |
| 72 | + |
| 73 | +async function initConfigWithFailover(host: string, port: number, connectToProxy: boolean): Promise<any> { |
| 74 | + const config: any = await initConfig(host, port, connectToProxy, "readWriteSplitting,failover,fastestResponseStrategy"); |
| 75 | + config["failoverTimeoutMs"] = 400000; |
| 76 | + return config; |
| 77 | +} |
| 78 | + |
| 79 | +describe("aurora fastest response strategy", () => { |
| 80 | + beforeEach(async () => { |
| 81 | + logger.info(`Test started: ${expect.getState().currentTestName}`); |
| 82 | + env = await TestEnvironment.getCurrent(); |
| 83 | + auroraTestUtility = new AuroraTestUtility(env.region); |
| 84 | + |
| 85 | + driver = DriverHelper.getDriverForDatabaseEngine(env.engine); |
| 86 | + initClientFunc = DriverHelper.getClient(driver); |
| 87 | + await ProxyHelper.enableAllConnectivity(); |
| 88 | + client = null; |
| 89 | + secondaryClient = null; |
| 90 | + provider = null; |
| 91 | + await TestEnvironment.verifyClusterStatus(); |
| 92 | + await TestEnvironment.verifyAllInstancesHasRightState("available"); |
| 93 | + await TestEnvironment.verifyAllInstancesUp(); |
| 94 | + |
| 95 | + RdsHostListProvider.clearAll(); |
| 96 | + PluginService.clearHostAvailabilityCache(); |
| 97 | + }, 1320000); |
| 98 | + |
| 99 | + afterEach(async () => { |
| 100 | + if (client !== null) { |
| 101 | + try { |
| 102 | + await client.end(); |
| 103 | + } catch (error) { |
| 104 | + // pass |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (secondaryClient !== null) { |
| 109 | + try { |
| 110 | + await secondaryClient.end(); |
| 111 | + } catch (error) { |
| 112 | + // pass |
| 113 | + } |
| 114 | + } |
| 115 | + await PluginManager.releaseResources(); |
| 116 | + logger.info(`Test finished: ${expect.getState().currentTestName}`); |
| 117 | + }, 1320000); |
| 118 | + |
| 119 | + itIfMinThreeInstance( |
| 120 | + "test failover to new reader use cached connection", |
| 121 | + async () => { |
| 122 | + // Connect to writer instance |
| 123 | + const writerConfig = await initConfigWithFailover( |
| 124 | + env.proxyDatabaseInfo.writerInstanceEndpoint, |
| 125 | + env.proxyDatabaseInfo.instanceEndpointPort, |
| 126 | + true |
| 127 | + ); |
| 128 | + writerConfig["failoverMode"] = "reader-or-writer"; |
| 129 | + client = initClientFunc(writerConfig); |
| 130 | + |
| 131 | + await client.connect(); |
| 132 | + const initialWriterId = await auroraTestUtility.queryInstanceId(client); |
| 133 | + expect(await auroraTestUtility.isDbInstanceWriter(initialWriterId)).toStrictEqual(true); |
| 134 | + |
| 135 | + await client.setReadOnly(true); |
| 136 | + |
| 137 | + const readerConnectionId = await auroraTestUtility.queryInstanceId(client); |
| 138 | + expect(readerConnectionId).not.toBe(initialWriterId); |
| 139 | + // Get a reader instance |
| 140 | + let otherReaderId; |
| 141 | + for (const host of env.proxyDatabaseInfo.instances) { |
| 142 | + if (host.instanceId && host.instanceId !== readerConnectionId && host.instanceId !== initialWriterId) { |
| 143 | + otherReaderId = host.instanceId; |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (!otherReaderId) { |
| 149 | + throw new Error("Could not find a reader instance"); |
| 150 | + } |
| 151 | + // Kill all instances except one other reader |
| 152 | + for (const host of env.proxyDatabaseInfo.instances) { |
| 153 | + if (host.instanceId && host.instanceId !== otherReaderId) { |
| 154 | + await ProxyHelper.disableConnectivity(env.engine, host.instanceId); |
| 155 | + } |
| 156 | + } |
| 157 | + await expect(async () => { |
| 158 | + await auroraTestUtility.queryInstanceId(client); |
| 159 | + }).rejects.toThrow(FailoverSuccessError); |
| 160 | + |
| 161 | + const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); |
| 162 | + |
| 163 | + expect(currentReaderId0).toStrictEqual(otherReaderId); |
| 164 | + expect(currentReaderId0).not.toBe(readerConnectionId); |
| 165 | + |
| 166 | + await ProxyHelper.enableAllConnectivity(); |
| 167 | + await client.setReadOnly(false); |
| 168 | + |
| 169 | + const currentId = await auroraTestUtility.queryInstanceId(client); |
| 170 | + expect(currentId).toStrictEqual(initialWriterId); |
| 171 | + // Connect using cached fastest connection. |
| 172 | + await client.setReadOnly(true); |
| 173 | + |
| 174 | + const currentReaderId2 = await auroraTestUtility.queryInstanceId(client); |
| 175 | + expect(currentReaderId2).toStrictEqual(otherReaderId); |
| 176 | + }, |
| 177 | + 1320000 |
| 178 | + ); |
| 179 | + |
| 180 | + itIf( |
| 181 | + "test secondary client use fastest connection", |
| 182 | + async () => { |
| 183 | + const config = await initDefaultConfig(env.databaseInfo.writerInstanceEndpoint, env.databaseInfo.instanceEndpointPort, false); |
| 184 | + |
| 185 | + client = initClientFunc(config); |
| 186 | + secondaryClient = initClientFunc(config); |
| 187 | + |
| 188 | + await client.connect(); |
| 189 | + await client.setReadOnly(true); |
| 190 | + const currentReaderId0 = await auroraTestUtility.queryInstanceId(client); |
| 191 | + await client.end(); |
| 192 | + await secondaryClient.connect(); |
| 193 | + |
| 194 | + // Connect using cached fastest connection. |
| 195 | + await secondaryClient.setReadOnly(true); |
| 196 | + const currentReaderId1 = await auroraTestUtility.queryInstanceId(secondaryClient); |
| 197 | + |
| 198 | + expect(currentReaderId1).toStrictEqual(currentReaderId0); |
| 199 | + expect(client).not.toBe(secondaryClient); |
| 200 | + }, |
| 201 | + 1000000 |
| 202 | + ); |
| 203 | +}); |
0 commit comments