Skip to content

Commit d936dfc

Browse files
committed
feat: gdb rw splitting plugin
1 parent 234e123 commit d936dfc

7 files changed

Lines changed: 171 additions & 4 deletions

common/lib/connection_plugin_chain_builder.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { HostMonitoring2PluginFactory } from "./plugins/efm2/host_monitoring2_pl
4545
import { BlueGreenPluginFactory } from "./plugins/bluegreen/blue_green_plugin_factory";
4646
import { GlobalDbFailoverPluginFactory } from "./plugins/gdb_failover/global_db_failover_plugin_factory";
4747
import { FullServicesContainer } from "./utils/full_services_container";
48+
import { GdbReadWriteSplittingPluginFactory } from "./plugins/read_write_splitting/gdb_read_write_splitting_plugin_factory";
4849

4950
/*
5051
Type alias used for plugin factory sorting. It holds a reference to a plugin
@@ -65,6 +66,7 @@ export class ConnectionPluginChainBuilder {
6566
["staleDns", { factory: StaleDnsPluginFactory, weight: 500 }],
6667
["bg", { factory: BlueGreenPluginFactory, weight: 550 }],
6768
["readWriteSplitting", { factory: ReadWriteSplittingPluginFactory, weight: 600 }],
69+
["gdbReadWriteSplitting", { factory: GdbReadWriteSplittingPluginFactory, weight: 610 }],
6870
["failover", { factory: FailoverPluginFactory, weight: 700 }],
6971
["failover2", { factory: Failover2PluginFactory, weight: 710 }],
7072
["gdbFailover", { factory: GlobalDbFailoverPluginFactory, weight: 720 }],
@@ -87,6 +89,7 @@ export class ConnectionPluginChainBuilder {
8789
[StaleDnsPluginFactory, 500],
8890
[BlueGreenPluginFactory, 550],
8991
[ReadWriteSplittingPluginFactory, 600],
92+
[GdbReadWriteSplittingPluginFactory, 610],
9093
[FailoverPluginFactory, 700],
9194
[Failover2PluginFactory, 710],
9295
[GlobalDbFailoverPluginFactory, 720],
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 { ConnectionPluginFactory } from "../../plugin_factory";
18+
import { PluginService } from "../../plugin_service";
19+
import { ConnectionPlugin } from "../../connection_plugin";
20+
import { AwsWrapperError } from "../../utils/errors";
21+
import { Messages } from "../../utils/messages";
22+
23+
export class GdbReadWriteSplittingPluginFactory extends ConnectionPluginFactory {
24+
private static gdbReadWriteSplittingPlugin: any;
25+
26+
async getInstance(pluginService: PluginService, properties: Map<string, any>): Promise<ConnectionPlugin> {
27+
try {
28+
if (!GdbReadWriteSplittingPluginFactory.gdbReadWriteSplittingPlugin) {
29+
GdbReadWriteSplittingPluginFactory.gdbReadWriteSplittingPlugin = await import("./gdb_read_writer_splitting_plugin");
30+
}
31+
return new GdbReadWriteSplittingPluginFactory.gdbReadWriteSplittingPlugin.GdbReadWriteSplittingPlugin(pluginService, properties);
32+
} catch (error: any) {
33+
throw new AwsWrapperError(Messages.get("ConnectionPluginChainBuilder.errorImportingPlugin", error.message, "gdbReadWriteSplittingPlugin"));
34+
}
35+
}
36+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

common/lib/plugins/read_write_splitting/read_write_splitting_plugin_factory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
3+
44
Licensed under the Apache License, Version 2.0 (the "License").
55
You may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
7-
7+
88
http://www.apache.org/licenses/LICENSE-2.0
9-
9+
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
1212
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

common/lib/utils/errors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export class FailoverFailedError extends FailoverError {}
4848

4949
export class TransactionResolutionUnknownError extends FailoverError {}
5050

51+
export class ReadWriteSplittingError extends AwsWrapperError {}
52+
5153
export class LoginError extends AwsWrapperError {}
5254

5355
export class AwsTimeoutError extends AwsWrapperError {}

common/lib/utils/messages.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,14 @@ const MESSAGES: Record<string, string> = {
416416
"GlobalDbFailoverPlugin.currentFailoverMode": "Current Global DB failover mode: %s",
417417
"GlobalDbFailoverPlugin.failoverElapsed": "Global DB failover elapsed time: %s ms",
418418
"GlobalDbFailoverPlugin.candidateNull": "Candidate host is null for role: %s",
419-
"GlobalDbFailoverPlugin.unableToConnect": "Unable to establish a connection during Global DB failover."
419+
"GlobalDbFailoverPlugin.unableToConnect": "Unable to establish a connection during Global DB failover.",
420+
"GlobalDbFailoverPlugin.unableToConnect": "Unable to establish a connection during failover.",
421+
"GdbReadWriteSplittingPlugin.missingHomeRegion":
422+
"Unable to parse home region from endpoint '%s'. Please ensure you have set the 'gdbRwHomeRegion' connection parameter.",
423+
"GdbReadWriteSplittingPlugin.cantConnectWriterOutOfHomeRegion":
424+
"Writer connection to '%s' is not allowed since it is out of home region '%s'.",
425+
"GdbReadWriteSplittingPlugin.noAvailableReadersInHomeRegion": "No available reader nodes in home region '%s'.",
426+
"GdbReadWriteSplittingPlugin.parameterValue": "%s=%s"
420427
};
421428

422429
export class Messages {

common/lib/wrapper_property.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,24 @@ export class WrapperProperties {
564564
["writer", "none"]
565565
);
566566

567+
static readonly GDB_RW_HOME_REGION = new WrapperProperty<string>(
568+
"gdbRwHomeRegion",
569+
"Specifies the home region for read/write splitting.",
570+
null
571+
);
572+
573+
static readonly GDB_RW_RESTRICT_WRITER_TO_HOME_REGION = new WrapperProperty<boolean>(
574+
"gdbRwRestrictWriterToHomeRegion",
575+
"Prevents connections to a writer node outside of the defined home region.",
576+
true
577+
);
578+
579+
static readonly GDB_RW_RESTRICT_READER_TO_HOME_REGION = new WrapperProperty<boolean>(
580+
"gdbRwRestrictReaderToHomeRegion",
581+
"Prevents connections to a reader node outside of the defined home region.",
582+
true
583+
);
584+
567585
private static readonly PREFIXES = [
568586
WrapperProperties.MONITORING_PROPERTY_PREFIX,
569587
ClusterTopologyMonitorImpl.MONITORING_PROPERTY_PREFIX,

0 commit comments

Comments
 (0)