From ea51ac8cbd64a904b73216cb510dd88a56440267 Mon Sep 17 00:00:00 2001 From: Jing Yu Date: Wed, 15 Oct 2025 21:56:02 -0700 Subject: [PATCH 1/2] PHOENIX-7711 Make Phoenix test jar available outside for consumption by applications --- .../jdbc/FailoverPhoenixConnection.java | 2 +- .../phoenix/jdbc/HighAvailabilityGroup.java | 16 ++- .../jdbc/ParallelPhoenixConnection.java | 2 +- .../phoenix/jdbc/ParallelPhoenixContext.java | 7 +- .../jdbc/HighAvailabilityTestingUtility.java | 114 ++++++++++++++++++ 5 files changed, 131 insertions(+), 10 deletions(-) diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/FailoverPhoenixConnection.java b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/FailoverPhoenixConnection.java index aebd21586da..cac17115b7c 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/FailoverPhoenixConnection.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/FailoverPhoenixConnection.java @@ -606,7 +606,7 @@ public int getNetworkTimeout() throws SQLException { /** Returns the currently wrapped connection. */ @VisibleForTesting - PhoenixConnection getWrappedConnection() { + public PhoenixConnection getWrappedConnection() { return connection; } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java index e45e9e99b05..e2e22446900 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java @@ -136,15 +136,17 @@ public class HighAvailabilityGroup { */ @VisibleForTesting static final Map GROUPS = new ConcurrentHashMap<>(); - static final Map> URLS = new ConcurrentHashMap<>(); @VisibleForTesting - static final Cache MISSING_CRR_GROUPS_CACHE = CacheBuilder.newBuilder() + public static final Map> URLS = new ConcurrentHashMap<>(); + @VisibleForTesting + public static final Cache MISSING_CRR_GROUPS_CACHE = + CacheBuilder.newBuilder() .expireAfterWrite(PHOENIX_HA_TRANSITION_TIMEOUT_MS_DEFAULT, TimeUnit.MILLISECONDS).build(); /** * The Curator client cache, one client instance per cluster. */ @VisibleForTesting - static final Cache CURATOR_CACHE = CacheBuilder.newBuilder() .expireAfterAccess(DEFAULT_CLIENT_CONNECTION_CACHE_MAX_DURATION, TimeUnit.MILLISECONDS) .removalListener( @@ -644,7 +646,8 @@ PhoenixConnection connectActive(final Properties properties, final HAURLInfo hau } /** Returns true if the given phoenix connection points to ACTIVE cluster, else false */ - boolean isActive(PhoenixConnection connection) { + @VisibleForTesting + public boolean isActive(PhoenixConnection connection) { if (state != State.READY || connection == null) { return false; } @@ -687,11 +690,12 @@ PhoenixConnection connectToOneCluster(String url, Properties properties, HAURLIn } @VisibleForTesting - HAGroupInfo getGroupInfo() { + public HAGroupInfo getGroupInfo() { return info; } - Properties getProperties() { + @VisibleForTesting + public Properties getProperties() { return properties; } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixConnection.java b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixConnection.java index 1cfcee63470..5562b552a24 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixConnection.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixConnection.java @@ -118,7 +118,7 @@ public CompletableFuture getFutureConnection2() { } @VisibleForTesting - ParallelPhoenixContext getContext() { + public ParallelPhoenixContext getContext() { return this.context; } diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java index 896582cc69e..f35de29d561 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java @@ -35,6 +35,7 @@ import org.apache.phoenix.monitoring.MetricType; import org.apache.phoenix.query.QueryServices; import org.apache.phoenix.query.QueryServicesOptions; +import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -219,11 +220,13 @@ public long getOperationTimeout() { return this.operationTimeoutMs; } - CompletableFuture getChainOnConn1() { + @VisibleForTesting + public CompletableFuture getChainOnConn1() { return this.cluster1Context.getChainOnConn(); } - CompletableFuture getChainOnConn2() { + @VisibleForTesting + public CompletableFuture getChainOnConn2() { return this.cluster2Context.getChainOnConn(); } diff --git a/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java b/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java index 1a50e4f4aad..f2c6f775426 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java @@ -784,4 +784,118 @@ public static Properties getHATestProperties() { properties.setProperty(HConstants.THREAD_WAKE_FREQUENCY, "100"); return properties; } + + /** + * Helper method to close HighAvailabilityGroup from external packages. + * This is needed because HighAvailabilityGroup.close() is package-private. + * + * @param haGroup the HighAvailabilityGroup to close + */ + @VisibleForTesting + public static void closeHighAvailabilityGroup(HighAvailabilityGroup haGroup) { + if (haGroup != null) { + try { + haGroup.close(); + } catch (Exception e) { + LOG.warn("Failed to close HighAvailabilityGroup", e); + } + } + } + + /** + * Helper method to get wrapped connection from FailoverPhoenixConnection. + * This is needed because FailoverPhoenixConnection.getWrappedConnection() is package-private. + * + * @param failoverConnection the FailoverPhoenixConnection + * @return the wrapped PhoenixConnection + */ + public static PhoenixConnection getWrappedConnection(FailoverPhoenixConnection failoverConnection) { + if (failoverConnection != null) { + return failoverConnection.getWrappedConnection(); + } + return null; + } + + /** + * Helper method to get ConnectionQueryServices from PhoenixDriver. + * This is needed because PhoenixDriver.getConnectionQueryServices() has protected access. + * + * @param url the JDBC URL + * @param properties the connection properties + * @return the ConnectionQueryServices + * @throws SQLException if connection cannot be established + */ + public static org.apache.phoenix.query.ConnectionQueryServices getConnectionQueryServices(String url, Properties properties) throws SQLException { + return PhoenixDriver.INSTANCE.getConnectionQueryServices(url, properties); + } + + /** + * Helper method to get the PRINCIPAL constant from HBaseTestingUtilityPair. + * This is needed because HBaseTestingUtilityPair.PRINCIPAL is package-private. + * + * @return the PRINCIPAL constant value + */ + public static String getPrincipal() { + return HBaseTestingUtilityPair.PRINCIPAL; + } + + /** + * Helper method to check if ConnectionInfo is in PhoenixDriver cache. + * This is needed because PhoenixDriver.checkIfCQSIIsInCache() has protected access. + * + * @param connectionInfo the ConnectionInfo to check + * @return true if the ConnectionInfo is in cache, false otherwise + */ + public static boolean checkIfCQSIIsInCache(ConnectionInfo connectionInfo) { + return PhoenixDriver.INSTANCE.checkIfCQSIIsInCache(connectionInfo); + } + + /** + * Helper method to get HA group name from HighAvailabilityGroup. + * This is needed because HAGroupInfo.getName() is defined in an inaccessible class. + * + * @param haGroup the HighAvailabilityGroup + * @return the HA group name + */ + public static String getHAGroupName(HighAvailabilityGroup haGroup) { + if (haGroup != null && haGroup.getGroupInfo() != null) { + return haGroup.getGroupInfo().getName(); + } + return null; + } + + /** + * Helper method to connect to the active cluster in an HA group. + * This is needed because HighAvailabilityGroup.connectActive() is package-private. + * + * @param haGroup the HighAvailabilityGroup + * @param properties the connection properties + * @param haurlInfo the HA URL info + * @return the PhoenixConnection to the active cluster + * @throws SQLException if connection cannot be established + */ + public static PhoenixConnection connectActiveCluster(HighAvailabilityGroup haGroup, Properties properties, HAURLInfo haurlInfo) throws SQLException { + if (haGroup != null) { + return haGroup.connectActive(properties, haurlInfo); + } + return null; + } + + /** + * Helper method to connect to a specific cluster in an HA group. + * This is needed because HighAvailabilityGroup.connectToOneCluster() is package-private. + * + * @param haGroup the HighAvailabilityGroup + * @param url the cluster URL to connect to + * @param properties the connection properties + * @param haurlInfo the HA URL info + * @return the PhoenixConnection to the specified cluster + * @throws SQLException if connection cannot be established + */ + public static PhoenixConnection connectToOneCluster(HighAvailabilityGroup haGroup, String url, Properties properties, HAURLInfo haurlInfo) throws SQLException { + if (haGroup != null) { + return haGroup.connectToOneCluster(url, properties, haurlInfo); + } + return null; + } } From d1c43014132bba24a9bcc522396e47e7760c968b Mon Sep 17 00:00:00 2001 From: Jing Yu Date: Fri, 17 Oct 2025 11:10:46 -0700 Subject: [PATCH 2/2] run mvn spotless:apply --- .../phoenix/jdbc/HighAvailabilityGroup.java | 4 +- .../phoenix/jdbc/ParallelPhoenixContext.java | 2 +- .../jdbc/HighAvailabilityTestingUtility.java | 64 +++++++++---------- 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java index e2e22446900..5e522db88a5 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/HighAvailabilityGroup.java @@ -140,8 +140,8 @@ public class HighAvailabilityGroup { public static final Map> URLS = new ConcurrentHashMap<>(); @VisibleForTesting public static final Cache MISSING_CRR_GROUPS_CACHE = - CacheBuilder.newBuilder() - .expireAfterWrite(PHOENIX_HA_TRANSITION_TIMEOUT_MS_DEFAULT, TimeUnit.MILLISECONDS).build(); + CacheBuilder.newBuilder() + .expireAfterWrite(PHOENIX_HA_TRANSITION_TIMEOUT_MS_DEFAULT, TimeUnit.MILLISECONDS).build(); /** * The Curator client cache, one client instance per cluster. */ diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java index f35de29d561..928ad2923bd 100644 --- a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java +++ b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixContext.java @@ -35,10 +35,10 @@ import org.apache.phoenix.monitoring.MetricType; import org.apache.phoenix.query.QueryServices; import org.apache.phoenix.query.QueryServicesOptions; -import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions; /** diff --git a/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java b/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java index f2c6f775426..f87d57cbb92 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/jdbc/HighAvailabilityTestingUtility.java @@ -786,9 +786,8 @@ public static Properties getHATestProperties() { } /** - * Helper method to close HighAvailabilityGroup from external packages. - * This is needed because HighAvailabilityGroup.close() is package-private. - * + * Helper method to close HighAvailabilityGroup from external packages. This is needed because + * HighAvailabilityGroup.close() is package-private. * @param haGroup the HighAvailabilityGroup to close */ @VisibleForTesting @@ -803,13 +802,13 @@ public static void closeHighAvailabilityGroup(HighAvailabilityGroup haGroup) { } /** - * Helper method to get wrapped connection from FailoverPhoenixConnection. - * This is needed because FailoverPhoenixConnection.getWrappedConnection() is package-private. - * + * Helper method to get wrapped connection from FailoverPhoenixConnection. This is needed because + * FailoverPhoenixConnection.getWrappedConnection() is package-private. * @param failoverConnection the FailoverPhoenixConnection * @return the wrapped PhoenixConnection */ - public static PhoenixConnection getWrappedConnection(FailoverPhoenixConnection failoverConnection) { + public static PhoenixConnection + getWrappedConnection(FailoverPhoenixConnection failoverConnection) { if (failoverConnection != null) { return failoverConnection.getWrappedConnection(); } @@ -817,22 +816,21 @@ public static PhoenixConnection getWrappedConnection(FailoverPhoenixConnection f } /** - * Helper method to get ConnectionQueryServices from PhoenixDriver. - * This is needed because PhoenixDriver.getConnectionQueryServices() has protected access. - * - * @param url the JDBC URL + * Helper method to get ConnectionQueryServices from PhoenixDriver. This is needed because + * PhoenixDriver.getConnectionQueryServices() has protected access. + * @param url the JDBC URL * @param properties the connection properties * @return the ConnectionQueryServices * @throws SQLException if connection cannot be established */ - public static org.apache.phoenix.query.ConnectionQueryServices getConnectionQueryServices(String url, Properties properties) throws SQLException { + public static org.apache.phoenix.query.ConnectionQueryServices + getConnectionQueryServices(String url, Properties properties) throws SQLException { return PhoenixDriver.INSTANCE.getConnectionQueryServices(url, properties); } /** - * Helper method to get the PRINCIPAL constant from HBaseTestingUtilityPair. - * This is needed because HBaseTestingUtilityPair.PRINCIPAL is package-private. - * + * Helper method to get the PRINCIPAL constant from HBaseTestingUtilityPair. This is needed + * because HBaseTestingUtilityPair.PRINCIPAL is package-private. * @return the PRINCIPAL constant value */ public static String getPrincipal() { @@ -840,9 +838,8 @@ public static String getPrincipal() { } /** - * Helper method to check if ConnectionInfo is in PhoenixDriver cache. - * This is needed because PhoenixDriver.checkIfCQSIIsInCache() has protected access. - * + * Helper method to check if ConnectionInfo is in PhoenixDriver cache. This is needed because + * PhoenixDriver.checkIfCQSIIsInCache() has protected access. * @param connectionInfo the ConnectionInfo to check * @return true if the ConnectionInfo is in cache, false otherwise */ @@ -851,9 +848,8 @@ public static boolean checkIfCQSIIsInCache(ConnectionInfo connectionInfo) { } /** - * Helper method to get HA group name from HighAvailabilityGroup. - * This is needed because HAGroupInfo.getName() is defined in an inaccessible class. - * + * Helper method to get HA group name from HighAvailabilityGroup. This is needed because + * HAGroupInfo.getName() is defined in an inaccessible class. * @param haGroup the HighAvailabilityGroup * @return the HA group name */ @@ -865,16 +861,16 @@ public static String getHAGroupName(HighAvailabilityGroup haGroup) { } /** - * Helper method to connect to the active cluster in an HA group. - * This is needed because HighAvailabilityGroup.connectActive() is package-private. - * - * @param haGroup the HighAvailabilityGroup + * Helper method to connect to the active cluster in an HA group. This is needed because + * HighAvailabilityGroup.connectActive() is package-private. + * @param haGroup the HighAvailabilityGroup * @param properties the connection properties - * @param haurlInfo the HA URL info + * @param haurlInfo the HA URL info * @return the PhoenixConnection to the active cluster * @throws SQLException if connection cannot be established */ - public static PhoenixConnection connectActiveCluster(HighAvailabilityGroup haGroup, Properties properties, HAURLInfo haurlInfo) throws SQLException { + public static PhoenixConnection connectActiveCluster(HighAvailabilityGroup haGroup, + Properties properties, HAURLInfo haurlInfo) throws SQLException { if (haGroup != null) { return haGroup.connectActive(properties, haurlInfo); } @@ -882,17 +878,17 @@ public static PhoenixConnection connectActiveCluster(HighAvailabilityGroup haGro } /** - * Helper method to connect to a specific cluster in an HA group. - * This is needed because HighAvailabilityGroup.connectToOneCluster() is package-private. - * - * @param haGroup the HighAvailabilityGroup - * @param url the cluster URL to connect to + * Helper method to connect to a specific cluster in an HA group. This is needed because + * HighAvailabilityGroup.connectToOneCluster() is package-private. + * @param haGroup the HighAvailabilityGroup + * @param url the cluster URL to connect to * @param properties the connection properties - * @param haurlInfo the HA URL info + * @param haurlInfo the HA URL info * @return the PhoenixConnection to the specified cluster * @throws SQLException if connection cannot be established */ - public static PhoenixConnection connectToOneCluster(HighAvailabilityGroup haGroup, String url, Properties properties, HAURLInfo haurlInfo) throws SQLException { + public static PhoenixConnection connectToOneCluster(HighAvailabilityGroup haGroup, String url, + Properties properties, HAURLInfo haurlInfo) throws SQLException { if (haGroup != null) { return haGroup.connectToOneCluster(url, properties, haurlInfo); }