diff --git a/content/dev-guide.yaml b/content/dev-guide.yaml index 93b7c05..45ee451 100644 --- a/content/dev-guide.yaml +++ b/content/dev-guide.yaml @@ -297,14 +297,15 @@ wrappers: background cleanup thread. Shared across the wrapper. offers: | - `register(name, ttl:)` — create a named cache partition. - - `set(name, key, value)` / `get(name, key)` — store and read items. - - `get_if_registered(name, key)` — read a partition that may not exist (e.g. a cache owned by an optional plugin). + - `set(name, key, value)` / `get(name, key, register_access: false)` — store and read items. + - `get_if_registered(name, key, register_access: false)` — read a partition that may not exist (e.g. a cache owned by an optional plugin). - `exists?(name, key)` / `remove(name, key)` / `clear(name)` / `clear_all`. whenToUse: | Use it to cache data reused across the wrapper — for example the - `RdsHostListProvider` caches cluster topology here. Reads publish a - `DataAccessEvent` (unless `register_access: false`), which keeps related - monitors alive. + `RdsHostListProvider` caches cluster topology here. A read publishes a + `DataAccessEvent` only when called with `register_access: true`, which + extends the expiration of any monitor that produces that cache's data. Pass + it only when the read should keep that monitor alive. - name: "Monitor Service" short: "Monitor Service" global: true @@ -334,7 +335,7 @@ wrappers: - `release_resources` — stop the background publishing thread. whenToUse: | Use it to decouple event producers and consumers. For example the Storage - Service publishes a `DataAccessEvent` on cache reads, and the Monitor Service + Service publishes a `DataAccessEvent` on cache reads made with `register_access: true`, and the Monitor Service subscribes to it to extend the matching monitor's expiration. - name: "Plugin Manager" short: "Plugin Manager" diff --git a/content/failover.yaml b/content/failover.yaml index cb4637d..f398db1 100644 --- a/content/failover.yaml +++ b/content/failover.yaml @@ -328,7 +328,7 @@ wrapperOnly: - | ### ActiveRecord integration - Through ActiveRecord (the `aws_postgresql` / `aws_mysql2` adapters), the adapter's + Through ActiveRecord (the `aws_postgresql` / `aws_mysql2` / `aws_trilogy` adapters), the adapter's `translate_exception` decides what your code rescues, and only one failover error is translated: @@ -471,7 +471,7 @@ enhanced: thirdPartyNote: ruby: | :::note Using the wrapper through ActiveRecord - When you use the wrapper through ActiveRecord (`aws_postgresql` or `aws_mysql2`), + When you use the wrapper through ActiveRecord (`aws_postgresql`, `aws_mysql2`, or `aws_trilogy`), reconnection is partially handled for you. On a successful failover the adapter re-runs `configure_connection` and reuses the pooled connection in place rather than discarding it, so you should not call `disconnect!` or discard the connection object @@ -480,10 +480,48 @@ enhanced: wrapperOnly: ruby: + - | + ## Timeouts and idle connections on MySQL + + On MySQL, the failover plugin treats a connection that timed out or was closed by + the server as lost, and fails over. This applies to `mysql2` (`WrapperMysql2Client` + and `aws_mysql2`) and to `trilogy` (`WrapperTrilogyClient` and `aws_trilogy`), and + includes: + + - A query that runs longer than the client's `read_timeout`. The driver closes the + socket before it raises the timeout, so the connection cannot be reused. + - A query on a connection that sat idle longer than the server's `wait_timeout`. + The server closes such a connection; MySQL 8.0.24 and later reports error 4031, + and earlier versions report a lost connection (2006 or 2013). + - A connection lost during a query, or a server that has gone away (with `mysql2`, + errors 2006, 2013, and 2055). + + Your code then sees `FailoverSuccessError` (or `TransactionStateUnknownError` inside a + transaction) instead of the driver's timeout or connection error. Through ActiveRecord + these errors are re-raised unchanged (see [ActiveRecord integration](#activerecord-integration)), + and ActiveRecord does not retry them for you. To avoid a failover that is caused by a + slow query or an idle connection rather than by the database: + + - Set `read_timeout` higher than your slowest expected query. + - Keep pooled connections from outliving `wait_timeout`: raise `wait_timeout` on the + server, or have the pool close idle connections sooner, for example with + ActiveRecord's `idle_timeout`. + + A lock wait timeout (error 1205) does not trigger failover. The connection is still + healthy, and the driver's error is raised as usual. + + :::note Changed in 1.1.0 + Before 1.1.0, `mysql2` did not fail over on a `read_timeout` or on errors 4031 and + 2055. A `read_timeout` raised `Mysql2::Error::TimeoutError` (`ActiveRecord::AdapterTimeout` + under `aws_mysql2`), and errors 4031 and 2055 were raised as is. Under `aws_mysql2`, + error 4031 became `ActiveRecord::ConnectionFailed`, which ActiveRecord answered by + reconnecting to the original endpoint and retrying the queries it considers + retryable, and error 2055 became `ActiveRecord::StatementInvalid`. + ::: - | ## ActiveRecord integration - When using the wrapper through ActiveRecord (`aws_postgresql` or `aws_mysql2`), the + When using the wrapper through ActiveRecord (`aws_postgresql`, `aws_mysql2`, or `aws_trilogy`), the adapter's `translate_exception` decides what your code actually rescues, and the outcome differs by error: diff --git a/content/plugins.yaml b/content/plugins.yaml index 4525b29..89447b0 100644 --- a/content/plugins.yaml +++ b/content/plugins.yaml @@ -2073,7 +2073,7 @@ plugins: - **Before the switchover**, it inventories the blue and green endpoints and their IP addresses. - **During the active switchover**, it temporarily suspends calls to blue nodes and substitutes hostnames with the corresponding IP addresses to avoid stale DNS. - - **After the switchover**, it monitors DNS until blue endpoints are reconfigured, then stops substituting. + - **After the switchover**, it monitors DNS until blue endpoints are reconfigured, then stops substituting. From this phase on, it also invalidates connections that were opened to the old blue environment before the switchover, so their next use raises an error instead of reading from retired blue nodes. See `bg_drop_blue_connections` below. - **Once switchover completes**, it rejects new connections to green nodes. - **If the switchover fails or rolls back**, it detects that and restores normal handling. code: @@ -2136,6 +2136,11 @@ plugins: description: "Maximum duration allowed for switchover completion. If the process stalls or exceeds this, the wrapper assumes completion and resumes normal operations." spellings: ruby: { name: bg_switchover_timeout_sec, type: float, required: "No", default: "180.0", unit: s, example: "180.0" } + - concept: dropBlueConnections + type: boolean + description: "Whether to invalidate connections that were opened to the old blue environment before a switchover. Once the switchover is confirmed (it reaches the Post or Completed phase), each such connection is closed the next time it is used. With the failover plugin enabled, that use triggers failover through Blue/Green routing and raises one of the [failover errors](../enhanced-failover#failover-errors): `FailoverSuccessError` or `TransactionStateUnknownError` (inside a transaction) when it reconnects, or `FailoverFailedError` when it cannot, for example during a network disconnect. Without it, the driver raises its closed-connection error; under ActiveRecord, a query it considers retryable may instead be reconnected and retried for you. In neither case is a stale read served from the old blue environment. Connections are invalidated only if the plugin saw the switchover while it was in progress, and none are invalidated if the switchover rolls back, or if `bg_switchover_timeout_sec` elapses before the switchover reaches the Post phase." + spellings: + ruby: { name: bg_drop_blue_connections, required: "No", default: "true", example: "false" } monitoringPrefix: ruby: "bg_monitoring_" monitoringSnippet: diff --git a/content/scenarios.yaml b/content/scenarios.yaml index 55e0b8f..aa2579d 100644 --- a/content/scenarios.yaml +++ b/content/scenarios.yaml @@ -492,7 +492,10 @@ addons: short: query retries requires: { integration: [active_record] } blocked: Active Record supplies this. Talking to the gem directly, retrying is your code's decision. - hint: Active Record retries safely retryable queries once by default. + hint: > + Active Record retries safely retryable queries once by default. It does not + retry `FailoverSuccessError` or `TransactionStateUnknownError`, which the + failover plugin raises after a successful failover. emit: pool: { connection_retries: 3, retry_deadline: 5 } links: [ar_retries] @@ -578,6 +581,7 @@ addons: - "A Blue/Green Deployment exists and the wrapper can read its status before the switchover starts." notes: - "The plugin holds traffic during the switchover instead of letting it fail, then resumes against the promoted deployment." + - "After the switchover, connections that were opened to the old blue environment are closed the next time they are used. If the failover plugin is enabled (list `failover` in `wrapper_plugins` alongside `bg`), that use triggers failover and raises one of the failover errors: `FailoverSuccessError` (or `TransactionStateUnknownError` inside a transaction) when the connection reconnects to the new environment, or `FailoverFailedError` when it cannot. Handle it as you would any other failover. Set `bg_drop_blue_connections: false` to keep using those connections instead, at the risk of reading from the retired blue environment." - "If you connect through an RDS Proxy, AWS requires the blue cluster to be registered with the proxy before the Blue/Green deployment is created — you cannot add the proxy afterward." links: [rds_bluegreen] diff --git a/content/templates/plugin-blue-green.md.j2 b/content/templates/plugin-blue-green.md.j2 index 757b1d6..16292c6 100644 --- a/content/templates/plugin-blue-green.md.j2 +++ b/content/templates/plugin-blue-green.md.j2 @@ -30,6 +30,7 @@ stateDiagram-v2 end note note right of Post Monitor DNS; stop substitution; + invalidate pre-switchover blue connections; reject new connections to green nodes end note ``` diff --git a/site/docs-ruby/development-guide/service-container.md b/site/docs-ruby/development-guide/service-container.md index f687f0f..4ca99ff 100644 --- a/site/docs-ruby/development-guide/service-container.md +++ b/site/docs-ruby/development-guide/service-container.md @@ -148,16 +148,17 @@ background cleanup thread. Shared across the wrapper. **What it offers** - `register(name, ttl:)` — create a named cache partition. -- `set(name, key, value)` / `get(name, key)` — store and read items. -- `get_if_registered(name, key)` — read a partition that may not exist (e.g. a cache owned by an optional plugin). +- `set(name, key, value)` / `get(name, key, register_access: false)` — store and read items. +- `get_if_registered(name, key, register_access: false)` — read a partition that may not exist (e.g. a cache owned by an optional plugin). - `exists?(name, key)` / `remove(name, key)` / `clear(name)` / `clear_all`. **When to use it** Use it to cache data reused across the wrapper — for example the -`RdsHostListProvider` caches cluster topology here. Reads publish a -`DataAccessEvent` (unless `register_access: false`), which keeps related -monitors alive. +`RdsHostListProvider` caches cluster topology here. A read publishes a +`DataAccessEvent` only when called with `register_access: true`, which +extends the expiration of any monitor that produces that cache's data. Pass +it only when the read should keep that monitor alive. ### Monitor Service @@ -195,7 +196,7 @@ events synchronously and batches the rest, deduplicating via `Set` semantics. **When to use it** Use it to decouple event producers and consumers. For example the Storage -Service publishes a `DataAccessEvent` on cache reads, and the Monitor Service +Service publishes a `DataAccessEvent` on cache reads made with `register_access: true`, and the Monitor Service subscribes to it to extend the matching monitor's expiration. ### Plugin Manager diff --git a/site/docs-ruby/enhanced-failover.md b/site/docs-ruby/enhanced-failover.md index d3e2b57..d52c778 100644 --- a/site/docs-ruby/enhanced-failover.md +++ b/site/docs-ruby/enhanced-failover.md @@ -224,16 +224,54 @@ first and reuse the connection when failover succeeded. ::: :::note Using the wrapper through ActiveRecord -When you use the wrapper through ActiveRecord (`aws_postgresql` or `aws_mysql2`), +When you use the wrapper through ActiveRecord (`aws_postgresql`, `aws_mysql2`, or `aws_trilogy`), reconnection is partially handled for you. On a successful failover the adapter re-runs `configure_connection` and reuses the pooled connection in place rather than discarding it, so you should not call `disconnect!` or discard the connection object yourself — the pool already holds the live, reconnected connection. ::: +## Timeouts and idle connections on MySQL + +On MySQL, the failover plugin treats a connection that timed out or was closed by +the server as lost, and fails over. This applies to `mysql2` (`WrapperMysql2Client` +and `aws_mysql2`) and to `trilogy` (`WrapperTrilogyClient` and `aws_trilogy`), and +includes: + +- A query that runs longer than the client's `read_timeout`. The driver closes the + socket before it raises the timeout, so the connection cannot be reused. +- A query on a connection that sat idle longer than the server's `wait_timeout`. + The server closes such a connection; MySQL 8.0.24 and later reports error 4031, + and earlier versions report a lost connection (2006 or 2013). +- A connection lost during a query, or a server that has gone away (with `mysql2`, + errors 2006, 2013, and 2055). + +Your code then sees `FailoverSuccessError` (or `TransactionStateUnknownError` inside a +transaction) instead of the driver's timeout or connection error. Through ActiveRecord +these errors are re-raised unchanged (see [ActiveRecord integration](#activerecord-integration)), +and ActiveRecord does not retry them for you. To avoid a failover that is caused by a +slow query or an idle connection rather than by the database: + +- Set `read_timeout` higher than your slowest expected query. +- Keep pooled connections from outliving `wait_timeout`: raise `wait_timeout` on the + server, or have the pool close idle connections sooner, for example with + ActiveRecord's `idle_timeout`. + +A lock wait timeout (error 1205) does not trigger failover. The connection is still +healthy, and the driver's error is raised as usual. + +:::note Changed in 1.1.0 +Before 1.1.0, `mysql2` did not fail over on a `read_timeout` or on errors 4031 and +2055. A `read_timeout` raised `Mysql2::Error::TimeoutError` (`ActiveRecord::AdapterTimeout` +under `aws_mysql2`), and errors 4031 and 2055 were raised as is. Under `aws_mysql2`, +error 4031 became `ActiveRecord::ConnectionFailed`, which ActiveRecord answered by +reconnecting to the original endpoint and retrying the queries it considers +retryable, and error 2055 became `ActiveRecord::StatementInvalid`. +::: + ## ActiveRecord integration -When using the wrapper through ActiveRecord (`aws_postgresql` or `aws_mysql2`), the +When using the wrapper through ActiveRecord (`aws_postgresql`, `aws_mysql2`, or `aws_trilogy`), the adapter's `translate_exception` decides what your code actually rescues, and the outcome differs by error: diff --git a/site/docs-ruby/using-plugins/blue-green.md b/site/docs-ruby/using-plugins/blue-green.md index 9c3b027..36e82ef 100644 --- a/site/docs-ruby/using-plugins/blue-green.md +++ b/site/docs-ruby/using-plugins/blue-green.md @@ -16,7 +16,7 @@ switchover status and adjusting connection handling at each phase: - **Before the switchover**, it inventories the blue and green endpoints and their IP addresses. - **During the active switchover**, it temporarily suspends calls to blue nodes and substitutes hostnames with the corresponding IP addresses to avoid stale DNS. -- **After the switchover**, it monitors DNS until blue endpoints are reconfigured, then stops substituting. +- **After the switchover**, it monitors DNS until blue endpoints are reconfigured, then stops substituting. From this phase on, it also invalidates connections that were opened to the old blue environment before the switchover, so their next use raises an error instead of reading from retired blue nodes. See `bg_drop_blue_connections` below. - **Once switchover completes**, it rejects new connections to green nodes. - **If the switchover fails or rolls back**, it detects that and restores normal handling. @@ -61,6 +61,7 @@ stateDiagram-v2 end note note right of Post Monitor DNS; stop substitution; + invalidate pre-switchover blue connections; reject new connections to green nodes end note ``` @@ -92,6 +93,7 @@ Verify plugin compatibility within your Ruby configuration using the [compatibil | `bg_increased_sec` | Float | No | `1.0` | Increased interval for checking Blue/Green Deployment status. Configure within 0.5–2 sec. *(sec)*

Example: `1.0` | | `bg_high_sec` | Float | No | `0.1` | High-frequency interval for checking Blue/Green Deployment status. Configure within 0.05–0.5 sec. *(sec)*

Example: `0.1` | | `bg_switchover_timeout_sec` | Float | No | `180.0` | Maximum duration allowed for switchover completion. If the process stalls or exceeds this, the wrapper assumes completion and resumes normal operations. *(sec)*

Example: `180.0` | +| `bg_drop_blue_connections` | Boolean | No | `true` | Whether to invalidate connections that were opened to the old blue environment before a switchover. Once the switchover is confirmed (it reaches the Post or Completed phase), each such connection is closed the next time it is used. With the failover plugin enabled, that use triggers failover through Blue/Green routing and raises one of the [failover errors](../enhanced-failover#failover-errors): `FailoverSuccessError` or `TransactionStateUnknownError` (inside a transaction) when it reconnects, or `FailoverFailedError` when it cannot, for example during a network disconnect. Without it, the driver raises its closed-connection error; under ActiveRecord, a query it considers retryable may instead be reconnected and retried for you. In neither case is a stale read served from the old blue environment. Connections are invalidated only if the plugin saw the switchover while it was in progress, and none are invalidated if the switchover rolls back, or if `bg_switchover_timeout_sec` elapses before the switchover reaches the Post phase.

Example: `false` | ## Configuring monitoring connections diff --git a/site/src/generated/scenarios-ruby.json b/site/src/generated/scenarios-ruby.json index 461dd33..5b72d1e 100644 --- a/site/src/generated/scenarios-ruby.json +++ b/site/src/generated/scenarios-ruby.json @@ -1003,7 +1003,7 @@ ] }, "blocked": "Active Record supplies this. Talking to the gem directly, retrying is your code's decision.", - "hint": "Active Record retries safely retryable queries once by default.", + "hint": "Active Record retries safely retryable queries once by default. It does not retry `FailoverSuccessError` or `TransactionStateUnknownError`, which the failover plugin raises after a successful failover.\n", "emit": { "pool": { "connection_retries": 3, @@ -1165,6 +1165,7 @@ ], "notes": [ "The plugin holds traffic during the switchover instead of letting it fail, then resumes against the promoted deployment.", + "After the switchover, connections that were opened to the old blue environment are closed the next time they are used. If the failover plugin is enabled (list `failover` in `wrapper_plugins` alongside `bg`), that use triggers failover and raises one of the failover errors: `FailoverSuccessError` (or `TransactionStateUnknownError` inside a transaction) when the connection reconnects to the new environment, or `FailoverFailedError` when it cannot. Handle it as you would any other failover. Set `bg_drop_blue_connections: false` to keep using those connections instead, at the risk of reading from the retired blue environment.", "If you connect through an RDS Proxy, AWS requires the blue cluster to be registered with the proxy before the Blue/Green deployment is created \u2014 you cannot add the proxy afterward." ], "links": [