Skip to content

Commit 2acff9c

Browse files
committed
chore: address review comments
1 parent 891b2b1 commit 2acff9c

4 files changed

Lines changed: 32 additions & 39 deletions

File tree

‎aws_advanced_python_wrapper/gdb_read_write_splitting_plugin.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737

3838
class GdbReadWriteSplittingPlugin(ReadWriteSplittingPlugin):
39-
"""Read/write splitting plugin for Aurora Global Database.
39+
"""Read/write splitting plugin for Aurora Global Databases.
4040
4141
Extends the topology-based :class:`ReadWriteSplittingPlugin` to keep
4242
reader and writer connections inside a configured home region. When
@@ -131,6 +131,7 @@ def _set_writer_connection(
131131
self, writer_conn: Connection, writer_host_info: HostInfo
132132
) -> None:
133133
if self._is_writer_outside_home_region(writer_host_info):
134+
self._close_connection(writer_conn)
134135
raise ReadWriteSplittingError(
135136
Messages.get_formatted(
136137
"GdbReadWriteSplittingPlugin.CantConnectWriterOutOfHomeRegion",

‎aws_advanced_python_wrapper/resources/aws_advanced_python_wrapper_messages.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ SimpleReadWriteSplittingPlugin.IncorrectConfiguration=[SimpleReadWriteSplittingP
427427

428428
GdbReadWriteSplittingPlugin.MissingHomeRegion=[GdbReadWriteSplittingPlugin] Unable to parse home region from endpoint '{}'. Please ensure you have set the 'gdb_rw_home_region' connection parameter.
429429
GdbReadWriteSplittingPlugin.CantConnectWriterOutOfHomeRegion=[GdbReadWriteSplittingPlugin] Writer connection to '{}' is not allowed since it is out of home region '{}'.
430-
GdbReadWriteSplittingPlugin.NoAvailableReadersInHomeRegion=[GdbReadWriteSplittingPlugin] No available reader nodes in home region '{}'.
430+
GdbReadWriteSplittingPlugin.NoAvailableReadersInHomeRegion=[GdbReadWriteSplittingPlugin] No available reader hosts in home region '{}'.
431431
GdbReadWriteSplittingPlugin.ParameterValue=[GdbReadWriteSplittingPlugin] {}={}
432432
GdbReadWriteSplittingPlugin.EnabledGwf=[GdbReadWriteSplittingPlugin] The current primary writer region is '{}' and is not within the home region. Keeping the current connection and letting Global Write Forwarding redirect writes to the primary region.
433433

‎aws_advanced_python_wrapper/utils/rds_utils.py‎

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _get_prepared_host(host: Optional[str]) -> Optional[str]:
143143
if func is None or host is None:
144144
return host
145145
prepared = func(host)
146-
return host if prepared is None else prepared
146+
return host.strip() if prepared is None else prepared.strip()
147147

148148
def is_rds_cluster_dns(self, host: str) -> bool:
149149
dns_group = self._get_dns_group(self._get_prepared_host(host))
@@ -155,7 +155,7 @@ def is_rds_custom_cluster_dns(self, host: str) -> bool:
155155

156156
def is_rds_dns(self, host: Optional[str]) -> bool:
157157
prepared_host = self._get_prepared_host(host)
158-
if not prepared_host or not prepared_host.strip():
158+
if not prepared_host:
159159
return False
160160

161161
pattern = self._find(prepared_host, [RdsUtils.AURORA_DNS_PATTERN,
@@ -179,7 +179,7 @@ def is_rds_proxy_dns(self, host: str) -> bool:
179179

180180
def get_rds_instance_host_pattern(self, host: str) -> str:
181181
prepared_host = self._get_prepared_host(host)
182-
if not prepared_host or not prepared_host.strip():
182+
if not prepared_host:
183183
return "?"
184184

185185
match = self._get_group(prepared_host, RdsUtils.DOMAIN_GROUP)
@@ -190,7 +190,7 @@ def get_rds_instance_host_pattern(self, host: str) -> str:
190190

191191
def get_rds_region(self, host: Optional[str]):
192192
prepared_host = self._get_prepared_host(host)
193-
if not prepared_host or not prepared_host.strip():
193+
if not prepared_host:
194194
return None
195195

196196
group = self._get_group(prepared_host, RdsUtils.REGION_GROUP)
@@ -220,7 +220,7 @@ def is_limitless_database_shard_group_dns(self, host: str) -> bool:
220220

221221
def get_rds_cluster_host_url(self, host: str):
222222
prepared_host = self._get_prepared_host(host)
223-
if not prepared_host or not prepared_host.strip():
223+
if not prepared_host:
224224
return None
225225

226226
for pattern in [RdsUtils.AURORA_CLUSTER_PATTERN,
@@ -241,7 +241,7 @@ def get_rds_cluster_host_url(self, host: str):
241241

242242
def get_cluster_id(self, host: str) -> Optional[str]:
243243
prepared_host = self._get_prepared_host(host)
244-
if prepared_host is None or not prepared_host.strip():
244+
if prepared_host is None:
245245
return None
246246

247247
if self._get_dns_group(prepared_host) is not None:
@@ -257,41 +257,34 @@ def get_instance_id(self, host: str) -> Optional[str]:
257257
return None
258258

259259
def is_ip(self, host: str) -> bool:
260-
return self.is_ipv4(host) or self.is_ipv6(host)
261-
262-
def is_ipv4(self, host: str) -> bool:
263-
if host is None or not host.strip():
264-
return False
265-
return search(RdsUtils.IP_V4, host) is not None
266-
267-
def is_ipv6(self, host: str) -> bool:
268-
if host is None or not host.strip():
260+
prepared_host = self._get_prepared_host(host)
261+
if prepared_host is None:
269262
return False
270-
return search(RdsUtils.IP_V6_COMPRESSED, host) is not None or search(RdsUtils.IP_V6, host) is not None
263+
return (search(RdsUtils.IP_V4, host) is not None or
264+
search(RdsUtils.IP_V6_COMPRESSED, host) is not None or
265+
search(RdsUtils.IP_V6, host) is not None)
271266

272267
def is_dns_pattern_valid(self, host: str) -> bool:
273268
return "?" in host
274269

275270
def identify_rds_type(self, host: Optional[str]) -> RdsUrlType:
276-
prepared_host = self._get_prepared_host(host)
277-
if prepared_host is None or not prepared_host.strip():
271+
if host is None or not host.strip():
278272
return RdsUrlType.OTHER
279-
280-
if self.is_ip(prepared_host):
273+
if self.is_ip(host):
281274
return RdsUrlType.IP_ADDRESS
282-
elif self.is_global_db_writer_cluster_dns(prepared_host):
275+
elif self.is_global_db_writer_cluster_dns(host):
283276
return RdsUrlType.RDS_GLOBAL_WRITER_CLUSTER
284-
elif self.is_writer_cluster_dns(prepared_host):
277+
elif self.is_writer_cluster_dns(host):
285278
return RdsUrlType.RDS_WRITER_CLUSTER
286-
elif self.is_reader_cluster_dns(prepared_host):
279+
elif self.is_reader_cluster_dns(host):
287280
return RdsUrlType.RDS_READER_CLUSTER
288-
elif self.is_limitless_database_shard_group_dns(prepared_host):
281+
elif self.is_limitless_database_shard_group_dns(host):
289282
return RdsUrlType.RDS_AURORA_LIMITLESS_DB_SHARD_GROUP
290-
elif self.is_rds_custom_cluster_dns(prepared_host):
283+
elif self.is_rds_custom_cluster_dns(host):
291284
return RdsUrlType.RDS_CUSTOM_CLUSTER
292-
elif self.is_rds_proxy_dns(prepared_host):
285+
elif self.is_rds_proxy_dns(host):
293286
return RdsUrlType.RDS_PROXY
294-
elif self.is_rds_instance(prepared_host):
287+
elif self.is_rds_instance(host):
295288
return RdsUrlType.RDS_INSTANCE
296289

297290
return RdsUrlType.OTHER
@@ -305,7 +298,7 @@ def is_green_instance(self, host: str) -> bool:
305298

306299
def is_not_old_instance(self, host: str) -> bool:
307300
prepared_host = self._get_prepared_host(host)
308-
if prepared_host is None or not prepared_host.strip():
301+
if prepared_host is None:
309302
return False
310303
return search(RdsUtils.BG_OLD_HOST_PATTERN, prepared_host) is None
311304

@@ -340,7 +333,7 @@ def remove_green_instance_prefix(self, host: str) -> str:
340333
return host.replace(f"{prefix}.", ".")
341334

342335
def _find(self, host: Optional[str], patterns: list):
343-
if not host or not host.strip():
336+
if not host:
344337
return None
345338

346339
for pattern in patterns:
@@ -361,7 +354,7 @@ def _get_regex_group(self, pattern: Match[str], group_name: str):
361354
return pattern.group(group_name)
362355

363356
def _get_group(self, host: Optional[str], group: str):
364-
if not host or not host.strip():
357+
if not host:
365358
return None
366359

367360
pattern = self._find(host, [RdsUtils.AURORA_DNS_PATTERN,

‎docs/using-the-python-wrapper/using-plugins/UsingTheGdbReadWriteSplittingPlugin.md‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ Unless otherwise stated, all recommendations, configurations, and code examples
88

99
## Loading the GDB Read/Write Splitting Plugin
1010

11-
The GDB Read/Write Splitting Plugin is not loaded by default. To load it, include `gdb_rw` in the [`plugins`](../UsingThePythonWrapper.md#connection-plugin-manager-parameters) connection parameter. If you want to load it alongside the failover and host monitoring plugins, the GDB Read/Write Splitting Plugin **must be listed before** these plugins in the plugin chain so that failover errors are processed correctly. The wrapper sorts plugins by default, but the relative ordering still matters when sorting is disabled.
12-
11+
The GDB Read/Write Splitting Plugin is not loaded by default. To load it, include `gdb_rw` in the [`plugins`](../UsingThePythonWrapper.md#connection-plugin-manager-parameters) connection parameter.
1312
```python
1413
params = {
1514
"plugins": "gdb_rw,failover_v2,host_monitoring_v2",
@@ -37,15 +36,15 @@ params = {
3736
3837
## Using the GDB Read/Write Splitting Plugin against non-GDB clusters
3938

40-
The GDB Read/Write Splitting Plugin can be used against Aurora and RDS clusters. However, since these cluster types are single-region, configuring a home region adds little value.
39+
The GDB Read/Write Splitting Plugin can be used against Aurora and RDS clusters. However, since these cluster types are single-region, configuring a home region adds little value. Instead, we recommend using the [Read/Write Splitting Plugin](./UsingTheReadWriteSplittingPlugin.md).
4140

4241
## Configuration Parameters
4342

44-
| Parameter | Value | Required | Description | Default Value |
45-
|----------------------------------------|:-------:|:------------------------------------------------------------------------------------------------------------------------------------------:|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
46-
| `reader_host_selector_strategy` | String | No | The name of the strategy used to select a new reader host. For more information on the available reader selection strategies, see the [Read/Write Splitting Plugin](./UsingTheReadWriteSplittingPlugin.md#connection-strategies) docs. | `random` |
43+
| Parameter | Value | Required | Description | Default Value |
44+
|----------------------------------------|:-------:|:------------------------------------------------------------------------------------------------------------------------------------------:|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
45+
| `reader_host_selector_strategy` | String | No | The name of the strategy used to select a new reader host. For more information on the available reader selection strategies, see the [Read/Write Splitting Plugin](./UsingTheReadWriteSplittingPlugin.md#connection-strategies) docs. | `random` |
4746
| `gdb_rw_home_region` | String | If connecting using an IP address, a custom domain URL, a Global Database endpoint, or any other endpoint without a region: Yes<br><br>Otherwise: No | Defines the home region.<br><br>Examples: `us-west-2`, `us-east-1`.<br><br>If this parameter is omitted, the value is parsed from the connection URL. For regional cluster endpoints and instance endpoints, it is set to the region of the provided endpoint. If the endpoint has no region (for example, a Global Database endpoint or an IP address), the configuration parameter is mandatory. | For regional cluster endpoints and instance endpoints, it is set to the region of the provided endpoint.<br><br>Otherwise: `None` |
48-
| `gdb_rw_restrict_writer_to_home_region`| Boolean | No | If set to `True`, prevents following or connecting to a writer node outside the defined home region. An exception will be raised when such a connection to a writer outside the home region is requested. | `True` |
47+
| `gdb_rw_restrict_writer_to_home_region`| Boolean | No | If set to `True`, prevents following or connecting to a writer node outside the defined home region. An exception will be raised when a connection to a writer outside the home region is requested. | `True` |
4948
| `gdb_rw_restrict_reader_to_home_region`| Boolean | No | If set to `True`, prevents connecting to a reader node outside the defined home region. If no reader nodes in the home region are available, an exception will be raised. | `True` |
5049
| `gdb_enable_global_write_forwarding` | Boolean | No | If set to `True`, allows connections in the secondary region to forward write queries to the primary global region. This is useful when your home region is the secondary global region. This functionality requires [Global Write Forwarding](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-global-database-write-forwarding.html) to be enabled on the cluster. | `False` |
5150

0 commit comments

Comments
 (0)