lokiore opened a new pull request, #2673:
URL: https://github.com/apache/phoenix/pull/2673

   JIRA: https://issues.apache.org/jira/browse/PHOENIX-8068
   
   ### What changes were proposed in this pull request?
   
   With autocommit on, statement-level DML on an HA connection commits through 
the wrapped `PhoenixConnection`. This path does not go through 
`FailoverPhoenixConnection`. Thus a `StaleClusterRoleRecordException` from the 
server does not cause a cluster role record (CRR) refresh. This PR maps the 
error at the statement level.
   
   - `HighAvailabilityUtil.toFailoverInProgressIfStale(conn, e)` (new):
     - It applies only to an HA connection (`conn.getHAGroup() != null`) whose 
error chain has a `StaleClusterRoleRecordException`.
     - It increments `HA_STALE_CRR_DETECTED_COUNT` (global and per HA group).
     - It calls `refreshClusterRoleRecord(true)`. When the cluster is no longer 
active, the refresh closes the connections.
     - It returns `FAILOVER_IN_PROGRESS` with the original error as the root 
cause.
     - For all other errors, it returns the error unchanged.
   - `PhoenixStatement`: calls the helper in the outer catch of `executeQuery` 
and `executeMutation` (after the audit/query logging), and in `executeBatch` 
(the commit in the mutation-limit path, and the `BatchUpdateException` cause). 
The `finally` block of `executeBatch` skips `setAutoCommit` when the connection 
is closed, so that the original error is not hidden.
   - `FailoverPhoenixConnection.wrapActionDuringFailover`: throws 
`FAILOVER_IN_PROGRESS` again unchanged. This prevents a second refresh and a 
double count.
   - `isStaleClusterRoleRecordExceptionExistsInThrowable` is now null-safe.
   
   ### Why are the changes needed?
   
   Code path before this change (autocommit on):
   
   1. `FailoverPhoenixConnection.prepareStatement` / `createStatement` wrap 
only the create step. They return a raw `PhoenixStatement` on the wrapped 
`PhoenixConnection`.
   2. `executeUpdate()` → `executeMutation` → `PhoenixConnection.commit()` 
directly.
   3. The server rejects the batch with `StaleClusterRoleRecordException`, 
because the cluster is no longer ACTIVE.
   4. `executeMutation` throws the error again, unchanged. Nothing refreshes 
the CRR.
   5. The other refresh triggers do not apply:
      - The interval refresh runs only before non-point-lookup queries.
      - The poller runs only when no role is active.
      - The stale-CRR catch in `TableResultIterator` covers only scans.
   
   Result: a write-only autocommit application stays on the old active cluster 
after a failover. Every write fails in a few milliseconds with the same error, 
and the application gets no `FAILOVER_IN_PROGRESS` signal to fail over or 
reconnect. Testing found this behavior. An explicit `conn.commit()` goes 
through the wrapper and already worked.
   
   `Statement.executeUpdate(String)`, `execute()`, `executeBatch()` and the 
autocommit commit after a query have the same gap.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, for HA connections only. An autocommit statement-level DML (or query) 
rejected with a stale CRR now fails with `FAILOVER_IN_PROGRESS` (error code 
1990), and the CRR is refreshed. Before, it failed with the raw commit error, 
and the CRR was not refreshed. For `executeBatch`, the `BatchUpdateException` 
cause is now `FAILOVER_IN_PROGRESS`. Non-HA connections do not change.
   
   ### How was this patch tested?
   
   New tests in `FailoverPhoenixConnection2IT`:
   
   - `testStaleCrrWithAutoCommitPreparedStatementUpdate`
   - `testStaleCrrWithAutoCommitStatementUpdate`
   - `testStaleCrrWithAutoCommitExecuteBatch`
   
   Each test does these steps:
   1. Writes once with autocommit.
   2. Changes the server role ACTIVE → ACTIVE_TO_STANDBY → STANDBY.
   3. Expects `FAILOVER_IN_PROGRESS`.
   4. Expects the client CRR to show STANDBY and the wrapped connection to be 
closed.
   5. Expects a new connection to fail while no cluster is active.
   6. Makes the peer ACTIVE, calls `FailoverPhoenixConnection.failover`, and 
writes again.
   
   ```
   mvn verify -pl phoenix-core -am -Dtest=none 
'-Dit.test=FailoverPhoenixConnection2IT#testStaleCrr*'
   ```
   
   The 3 new tests and the existing `testStaleCrrVersionDetection`, 
`testStaleCrrVersionWithReadDetection` and 
`testStaleCrrVersionWithPointLookUpReadDetection` pass locally on an earlier 
base. On this base, `phoenix-core-client` compiles; CI runs the ITs.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Opus 5.5)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to