dbwong commented on code in PR #1430: URL: https://github.com/apache/phoenix/pull/1430#discussion_r910641830
########## phoenix-core/src/it/java/org/apache/phoenix/jdbc/FailoverPhoenixConnectionIT.java: ########## @@ -0,0 +1,879 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.phoenix.jdbc; + +import static org.apache.hadoop.hbase.HConstants.ZK_SESSION_TIMEOUT; +import static org.apache.hadoop.test.GenericTestUtils.waitFor; +import static org.apache.phoenix.exception.SQLExceptionCode.CANNOT_ESTABLISH_CONNECTION; +import static org.apache.phoenix.jdbc.FailoverPhoenixConnection.FAILOVER_TIMEOUT_MS_ATTR; +import static org.apache.phoenix.jdbc.HighAvailabilityGroup.PHOENIX_HA_TRANSITION_TIMEOUT_MS_KEY; +import static org.apache.phoenix.jdbc.HighAvailabilityGroup.PHOENIX_HA_ZK_RETRY_MAX_KEY; +import static org.apache.phoenix.jdbc.HighAvailabilityGroup.PHOENIX_HA_ZK_RETRY_MAX_SLEEP_MS_KEY; +import static org.apache.phoenix.jdbc.HighAvailabilityTestingUtility.HBaseTestingUtilityPair.doTestWhenOneZKDown; +import static org.apache.phoenix.jdbc.HighAvailabilityTestingUtility.doTestBasicOperationsWithConnection; +import static org.apache.phoenix.jdbc.HighAvailabilityTestingUtility.HBaseTestingUtilityPair; +import static org.apache.phoenix.jdbc.HighAvailabilityGroup.PHOENIX_HA_GROUP_ATTR; +import static org.apache.phoenix.jdbc.HighAvailabilityTestingUtility.doTestBasicOperationsWithStatement; +import static org.apache.phoenix.jdbc.HighAvailabilityTestingUtility.getHighAvailibilityGroup; +import static org.apache.phoenix.query.QueryServices.COLLECT_REQUEST_LEVEL_METRICS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.phoenix.end2end.NeedsOwnMiniClusterTest; +import org.apache.phoenix.exception.FailoverSQLException; +import org.apache.phoenix.jdbc.ClusterRoleRecord.ClusterRole; +import org.apache.phoenix.monitoring.MetricType; +import org.apache.phoenix.query.ConnectionQueryServices; +import org.apache.phoenix.query.ConnectionQueryServicesImpl; +import org.apache.phoenix.util.PhoenixRuntime; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; +import org.junit.rules.Timeout; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test failover basics for {@link FailoverPhoenixConnection}. + */ +@Category(NeedsOwnMiniClusterTest.class) +public class FailoverPhoenixConnectionIT { + private static final Logger LOG = LoggerFactory.getLogger(FailoverPhoenixConnectionIT.class); + private static final HBaseTestingUtilityPair CLUSTERS = new HBaseTestingUtilityPair(); + + @Rule + public final TestName testName = new TestName(); + @Rule + public final Timeout globalTimeout= new Timeout(300, TimeUnit.SECONDS); + + /** Client properties to create a connection per test. */ + private Properties clientProperties; + /** HA group for this test. */ + private HighAvailabilityGroup haGroup; + /** Table name per test case. */ + private String tableName; + /** HA Group name for this test. */ + private String haGroupName; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + CLUSTERS.start(); + DriverManager.registerDriver(PhoenixDriver.INSTANCE); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + DriverManager.deregisterDriver(PhoenixDriver.INSTANCE); + CLUSTERS.close(); + } + + @Before + public void setup() throws Exception { + haGroupName = testName.getMethodName(); + clientProperties = new Properties(); + clientProperties.setProperty(PHOENIX_HA_GROUP_ATTR, haGroupName); + // Set some client configurations to make test run faster + clientProperties.setProperty(COLLECT_REQUEST_LEVEL_METRICS, String.valueOf(true)); + clientProperties.setProperty(FAILOVER_TIMEOUT_MS_ATTR, "30000"); + clientProperties.setProperty(PHOENIX_HA_ZK_RETRY_MAX_KEY, "3"); + clientProperties.setProperty(PHOENIX_HA_ZK_RETRY_MAX_SLEEP_MS_KEY, "1000"); + clientProperties.setProperty(ZK_SESSION_TIMEOUT, "3000"); + clientProperties.setProperty(PHOENIX_HA_TRANSITION_TIMEOUT_MS_KEY, "3000"); + clientProperties.setProperty("zookeeper.recovery.retry.maxsleeptime", "1000"); + + // Make first cluster ACTIVE + CLUSTERS.initClusterRole(haGroupName, HighAvailabilityPolicy.FAILOVER); + + haGroup = getHighAvailibilityGroup(CLUSTERS.getJdbcUrl(), clientProperties); + LOG.info("Initialized haGroup {} with URL {}", haGroup, CLUSTERS.getJdbcUrl()); + tableName = testName.getMethodName().toUpperCase(); + CLUSTERS.createTableOnClusterPair(tableName); + } + + @After + public void tearDown() throws Exception { + try { + haGroup.close(); + PhoenixDriver.INSTANCE + .getConnectionQueryServices(CLUSTERS.getUrl1(), haGroup.getProperties()) + .close(); + PhoenixDriver.INSTANCE + .getConnectionQueryServices(CLUSTERS.getUrl2(), haGroup.getProperties()) + .close(); + } catch (Exception e) { + LOG.error("Fail to tear down the HA group and the CQS. Will ignore", e); + } + } + + /** + * Test Phoenix connection creation and basic operations with HBase cluster pair. + */ + @Test + public void testOperationUsingConnection() throws Exception { + try (Connection conn = createFailoverConnection()) { + doTestBasicOperationsWithConnection(conn, tableName, haGroupName); + } + } + + /** + * Test close() once more should not fail, as the second close should be a no-op. + */ + @Test + public void testCloseConnectionOnceMore() throws Exception { + Connection conn = createFailoverConnection(); + doTestBasicOperationsWithConnection(conn, tableName, haGroupName); + conn.close(); + conn.close(); // this is NOT duplicate code, but instead this is essential for this test. + } + + /** + * Tests that new Phoenix connections are not created during failover. + */ + @Test + public void testConnectionCreationFailsIfNoActiveCluster() throws Exception { + try (Connection conn = createFailoverConnection()) { + doTestBasicOperationsWithConnection(conn, tableName, haGroupName); + } + + CLUSTERS.transitClusterRole(haGroup, ClusterRole.STANDBY, ClusterRole.STANDBY); + + try { + createFailoverConnection(); + fail("Should have failed because neither cluster is ACTIVE"); + } catch (SQLException e) { + LOG.info("Got expected exception when creating new connection", e); + assertEquals(CANNOT_ESTABLISH_CONNECTION.getErrorCode(), e.getErrorCode()); + } // all other type of exception will fail this test. + } + + /** + * Tests new Phoenix connections are created if one cluster is OFFLINE and the other ACTIVE. + */ + @Test + public void testConnectionOneOfflineOneActive() throws Exception { + CLUSTERS.transitClusterRole(haGroup, ClusterRole.OFFLINE, ClusterRole.ACTIVE); + + try (Connection conn = createFailoverConnection()) { + doTestBasicOperationsWithConnection(conn, tableName, haGroupName); + } + } + + /** + * Test that failover can finish according to the policy. + * + * In this test case, there is no existing CQS or open connections to close. + * + * @see #testFailoverCanFinishWhenOneZKDownWithCQS + */ + @Test + public void testFailoverCanFinishWhenOneZKDown() throws Exception { Review Comment: I did fix some flapping tests awhile ago where there was a race condition on who was cluster 1. Will try and close out any known flappers. -- 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]
