From 9200e5a5b305dce849551af99d79651e770208e5 Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Fri, 1 May 2026 15:29:22 -0700 Subject: [PATCH 1/2] PHOENIX-7817 Resolve Omid transaction manager via provider singleton view OmidTransactionContext now gets the current transaction manager from the provider on each call. A replaced manager then does not leave a stale reference in a cached context or client. The test connection services now share one test transaction service for each provider in the JVM. A server-side connection service no longer starts a second TSO and replaces the manager and commit table of the singleton provider while a transaction is open. Open regions keep the commit table they had when they opened, so such a swap let readers miss committed rows. The connection service that starts the shared service owns it and closes it. Co-Authored-By: Claude Opus 5.5 --- .../transaction/OmidTransactionContext.java | 53 ++++++++++++------- .../transaction/OmidTransactionProvider.java | 18 ++++--- .../ConnectionQueryServicesTestImpl.java | 40 ++++++++++---- 3 files changed, 76 insertions(+), 35 deletions(-) diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java index ab862423b66..6703e742e95 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java @@ -44,19 +44,21 @@ public class OmidTransactionContext implements PhoenixTransactionContext { private static final Logger LOGGER = LoggerFactory.getLogger(OmidTransactionContext.class); - private HBaseTransactionManager tm; + // The context does not keep the transaction manager. It gets the current manager from the + // provider on each call. Tests can replace that manager and close the old one, so a kept + // reference can become stale. In production the manager does not change. + private OmidTransactionProvider.OmidTransactionClient client; private HBaseTransaction tx; public OmidTransactionContext() { this.tx = null; - this.tm = null; + this.client = null; } public OmidTransactionContext(PhoenixConnection connection) throws SQLException { - PhoenixTransactionClient client = - connection.getQueryServices().initTransactionClient(getProvider()); - assert (client instanceof OmidTransactionProvider.OmidTransactionClient); - this.tm = ((OmidTransactionProvider.OmidTransactionClient) client).getTransactionClient(); + PhoenixTransactionClient c = connection.getQueryServices().initTransactionClient(getProvider()); + assert (c instanceof OmidTransactionProvider.OmidTransactionClient); + this.client = (OmidTransactionProvider.OmidTransactionClient) c; this.tx = null; } @@ -64,8 +66,10 @@ public OmidTransactionContext(byte[] txnBytes) throws InvalidProtocolBufferExcep this(); if (txnBytes != null && txnBytes.length > 0) { TSOProto.Transaction transaction = TSOProto.Transaction.parseFrom(txnBytes); + HBaseTransactionManager m = tm(); tx = new HBaseTransaction(transaction.getTimestamp(), transaction.getEpoch(), - new HashSet(), new HashSet(), null, tm.isLowLatency()); + new HashSet(), new HashSet(), null, + m != null && m.isLowLatency()); } else { tx = null; } @@ -75,33 +79,39 @@ public OmidTransactionContext(PhoenixTransactionContext ctx, boolean subTask) { assert (ctx instanceof OmidTransactionContext); OmidTransactionContext omidTransactionContext = (OmidTransactionContext) ctx; - this.tm = omidTransactionContext.tm; + this.client = omidTransactionContext.client; if (subTask) { if (omidTransactionContext.isTransactionRunning()) { Transaction transaction = omidTransactionContext.getTransaction(); + HBaseTransactionManager m = tm(); this.tx = new HBaseTransaction(transaction.getTransactionId(), transaction.getEpoch(), - new HashSet(), new HashSet(), this.tm, - transaction.getReadTimestamp(), transaction.getWriteTimestamp(), tm.isLowLatency()); + new HashSet(), new HashSet(), m, transaction.getReadTimestamp(), + transaction.getWriteTimestamp(), m != null && m.isLowLatency()); } else { this.tx = null; } - this.tm = null; + this.client = null; } else { this.tx = omidTransactionContext.getTransaction(); } } + private HBaseTransactionManager tm() { + return client == null ? null : client.getTransactionClient(); + } + @Override public void begin() throws SQLException { - if (tm == null) { + HBaseTransactionManager m = tm(); + if (m == null) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.NULL_TRANSACTION_CONTEXT).build() .buildException(); } try { - tx = (HBaseTransaction) tm.begin(); + tx = (HBaseTransaction) m.begin(); } catch (TransactionException e) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.TRANSACTION_FAILED) .setMessage(e.getMessage()).setRootCause(e).build().buildException(); @@ -110,10 +120,11 @@ public void begin() throws SQLException { @Override public void commit() throws SQLException { - if (tx == null || tm == null) return; + HBaseTransactionManager m = tm(); + if (tx == null || m == null) return; try { - tm.commit(tx); + m.commit(tx); } catch (TransactionException e) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.TRANSACTION_FAILED) .setMessage(e.getMessage()).setRootCause(e).build().buildException(); @@ -125,12 +136,13 @@ public void commit() throws SQLException { @Override public void abort() throws SQLException { - if (tx == null || tm == null || tx.getStatus() != Status.RUNNING) { + HBaseTransactionManager m = tm(); + if (tx == null || m == null || tx.getStatus() != Status.RUNNING) { return; } try { - tm.rollback(tx); + m.rollback(tx); } catch (TransactionException e) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.TRANSACTION_FAILED) .setMessage(e.getMessage()).setRootCause(e).build().buildException(); @@ -149,9 +161,14 @@ public void checkpoint(boolean hasUncommittedData) throws SQLException { @Override public void commitDDLFence(PTable dataTable) throws SQLException { + HBaseTransactionManager m = tm(); + if (m == null) { + throw new SQLExceptionInfo.Builder(SQLExceptionCode.NULL_TRANSACTION_CONTEXT).build() + .buildException(); + } try { - tx = (HBaseTransaction) tm.fence(dataTable.getName().getBytes()); + tx = (HBaseTransaction) m.fence(dataTable.getName().getBytes()); if (LOGGER.isInfoEnabled()) { LOGGER.info("Added write fence at ~" + tx.getReadTimestamp()); } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java index d71472752a6..6c3168b3615 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java @@ -36,7 +36,9 @@ public class OmidTransactionProvider implements PhoenixTransactionProvider { private static final OmidTransactionProvider INSTANCE = new OmidTransactionProvider(); - private HBaseTransactionManager transactionManager = null; + // Tests can replace the manager while other threads read it. The volatile field makes the + // new manager visible to those threads without a lock. + private volatile HBaseTransactionManager transactionManager = null; private volatile CommitTable.Client commitTableClient = null; public static final OmidTransactionProvider getInstance() { @@ -78,18 +80,22 @@ public PhoenixTransactionClient getTransactionClient(Configuration config, } } - return new OmidTransactionClient(transactionManager); + return new OmidTransactionClient(this); } + /** + * Gives the current {@link HBaseTransactionManager} of the provider. The client does not keep its + * own reference to the manager. + */ static class OmidTransactionClient implements PhoenixTransactionClient { - private final HBaseTransactionManager transactionManager; + private final OmidTransactionProvider provider; - public OmidTransactionClient(HBaseTransactionManager transactionManager) { - this.transactionManager = transactionManager; + public OmidTransactionClient(OmidTransactionProvider provider) { + this.provider = provider; } public HBaseTransactionManager getTransactionClient() { - return transactionManager; + return provider.transactionManager; } @Override diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConnectionQueryServicesTestImpl.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConnectionQueryServicesTestImpl.java index 1376b1acd4a..d4b73242888 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConnectionQueryServicesTestImpl.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConnectionQueryServicesTestImpl.java @@ -58,6 +58,12 @@ public class ConnectionQueryServicesTestImpl extends ConnectionQueryServicesImpl // indexed by ordinal. private final PhoenixTransactionService[] txServices = new PhoenixTransactionService[TransactionFactory.Provider.values().length]; + // All connection services in the JVM share one test transaction service for each provider. + // The provider is a singleton, and each region keeps the commit table client that is current + // when the region opens. A second service replaces that client, and readers can then miss + // commits. The connection service that starts a shared service owns it and closes it. + private static final PhoenixTransactionService[] SHARED_TX_SERVICES = + new PhoenixTransactionService[TransactionFactory.Provider.values().length]; public ConnectionQueryServicesTestImpl(QueryServices services, ConnectionInfo info, Properties props) throws SQLException { @@ -90,13 +96,20 @@ public void close() throws SQLException { connections = new ArrayList<>(this.connections); this.connections = Sets.newHashSet(); - // shut down the tx client service if we created one to support transactions - for (PhoenixTransactionService service : txServices) { + // Shut down the transaction services that this instance owns. Release the shared slot + // in the same step, so that the next connection service starts a new service. + for (int i = 0; i < txServices.length; i++) { + PhoenixTransactionService service = txServices[i]; if (service != null) { - try { - service.close(); - } catch (IOException e) { - LOGGER.warn(e.getMessage(), e); + synchronized (SHARED_TX_SERVICES) { + if (SHARED_TX_SERVICES[i] == service) { + SHARED_TX_SERVICES[i] = null; + } + try { + service.close(); + } catch (IOException e) { + LOGGER.warn(e.getMessage(), e); + } } } } @@ -113,11 +126,16 @@ public void close() throws SQLException { @Override public synchronized PhoenixTransactionClient initTransactionClient(Provider provider) throws SQLException { - PhoenixTransactionService txService = txServices[provider.ordinal()]; - if (txService == null) { - int port = TestUtil.getRandomPort(); - txService = txServices[provider.ordinal()] = - TransactionServiceManager.startTransactionService(provider, config, connectionInfo, port); + int i = provider.ordinal(); + if (txServices[i] == null) { + // Start a service only when no other connection service owns a live one. + synchronized (SHARED_TX_SERVICES) { + if (SHARED_TX_SERVICES[i] == null) { + int port = TestUtil.getRandomPort(); + txServices[i] = SHARED_TX_SERVICES[i] = TransactionServiceManager + .startTransactionService(provider, config, connectionInfo, port); + } + } } return super.initTransactionClient(provider); } From 7a1c2ebb8b82537758aa1afc89294f034dbf2889 Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Sun, 22 Nov 2026 23:30:15 -0800 Subject: [PATCH 2/2] PHOENIX-7817 Addendum: fix checkstyle warnings on edited lines The change edits three lines that carry existing checkstyle warnings, so precommit counts them as new. Remove the parentheses around an assert expression, add braces to a one-line if, and drop a redundant public modifier on a constructor of a package-private class. Co-Authored-By: Claude Opus 5.5 --- .../apache/phoenix/transaction/OmidTransactionContext.java | 6 ++++-- .../apache/phoenix/transaction/OmidTransactionProvider.java | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java index 6703e742e95..fe92124b940 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionContext.java @@ -57,7 +57,7 @@ public OmidTransactionContext() { public OmidTransactionContext(PhoenixConnection connection) throws SQLException { PhoenixTransactionClient c = connection.getQueryServices().initTransactionClient(getProvider()); - assert (c instanceof OmidTransactionProvider.OmidTransactionClient); + assert c instanceof OmidTransactionProvider.OmidTransactionClient; this.client = (OmidTransactionProvider.OmidTransactionClient) c; this.tx = null; } @@ -121,7 +121,9 @@ public void begin() throws SQLException { @Override public void commit() throws SQLException { HBaseTransactionManager m = tm(); - if (tx == null || m == null) return; + if (tx == null || m == null) { + return; + } try { m.commit(tx); diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java index 6c3168b3615..ab8a3dea294 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/transaction/OmidTransactionProvider.java @@ -90,7 +90,7 @@ public PhoenixTransactionClient getTransactionClient(Configuration config, static class OmidTransactionClient implements PhoenixTransactionClient { private final OmidTransactionProvider provider; - public OmidTransactionClient(OmidTransactionProvider provider) { + OmidTransactionClient(OmidTransactionProvider provider) { this.provider = provider; }