dbwong commented on a change in pull request #771: URL: https://github.com/apache/phoenix/pull/771#discussion_r416102576
########## File path: phoenix-core/src/it/java/org/apache/phoenix/query/MaxConcurrentConnectionsIT.java ########## @@ -0,0 +1,152 @@ +/* + * 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.query; + +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.phoenix.end2end.BaseUniqueNamesOwnClusterIT; +import org.apache.phoenix.jdbc.PhoenixDriver; +import org.apache.phoenix.util.DelayedRegionServer; +import org.apache.phoenix.util.PhoenixRuntime; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +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.Properties; + +import static org.apache.phoenix.exception.SQLExceptionCode.NEW_CONNECTION_THROTTLED; +import static org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_OPEN_INTERNAL_PHOENIX_CONNECTIONS; +import static org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_OPEN_PHOENIX_CONNECTIONS; +import static org.apache.phoenix.query.QueryServices.CLIENT_CONNECTION_MAX_ALLOWED_CONNECTIONS; +import static org.apache.phoenix.query.QueryServices.INTERNAL_CONNECTION_MAX_ALLOWED_CONNECTIONS; +import static org.apache.phoenix.query.QueryServices.RENEW_LEASE_ENABLED; +import static org.apache.phoenix.util.PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class MaxConcurrentConnectionsIT extends BaseUniqueNamesOwnClusterIT { + + private static HBaseTestingUtility hbaseTestUtil; + + @BeforeClass + public static void setUp() throws Exception { + hbaseTestUtil = new HBaseTestingUtility(); + hbaseTestUtil.startMiniCluster(1,1,null,null,DelayedRegionServer.class); + // establish url and quorum. Need to use PhoenixDriver and not PhoenixTestDriver + String zkQuorum = "localhost:" + hbaseTestUtil.getZkCluster().getClientPort(); + url = PhoenixRuntime.JDBC_PROTOCOL + JDBC_PROTOCOL_SEPARATOR + zkQuorum + + JDBC_PROTOCOL_SEPARATOR + "uniqueConn=A"; + DriverManager.registerDriver(PhoenixDriver.INSTANCE); + } + + private String getUniqueUrl() { + return url + generateUniqueName(); + } + + //Have to shutdown our special delayed region server + @AfterClass + public static void tearDown() throws Exception { + hbaseTestUtil.shutdownMiniCluster(); + } + + @Test + public void testCannotCreateOverMaximumConnections() throws Exception { + int maxConnections = 3; + Properties props = new Properties(); + props.setProperty(CLIENT_CONNECTION_MAX_ALLOWED_CONNECTIONS,String.valueOf(maxConnections)); + props.setProperty(RENEW_LEASE_ENABLED,String.valueOf(false)); + assertEquals(0,GLOBAL_OPEN_PHOENIX_CONNECTIONS.getMetric().getValue()); + + List<Connection> connections = new ArrayList<>(); + String url = getUniqueUrl(); + + try{ + for(int i = 0; i < maxConnections; i++) { + connections.add(DriverManager.getConnection(url, props)); + } + + try { + DriverManager.getConnection(url, props); + fail(); + } catch(SQLException e) { + assertEquals(NEW_CONNECTION_THROTTLED.getErrorCode(),e.getErrorCode()); + assertEquals(NEW_CONNECTION_THROTTLED.getSQLState(),e.getSQLState()); + } + + } finally { + for(Connection conn : connections) { + conn.close(); + } + } + } + + /** + * This tests the delete path which creates a internal phoenix connection per region + * @throws Exception + */ + @Test + public void testDeleteRuntimeFailureClosesConnections() throws Exception { + String tableName = generateUniqueName(); + + //table with lots of regions + String ddl = "create table " + tableName + " (i integer not null primary key, j integer) SALT_BUCKETS=256 "; + + Properties props = new Properties(); + props.setProperty(QueryServices.ENABLE_SERVER_SIDE_DELETE_MUTATIONS, + String.valueOf(false)); + props.setProperty(CLIENT_CONNECTION_MAX_ALLOWED_CONNECTIONS,String.valueOf(10)); + props.setProperty(INTERNAL_CONNECTION_MAX_ALLOWED_CONNECTIONS,String.valueOf(10)); + + String deleteStmt = "DELETE FROM " + tableName + " WHERE 20 = j"; + + assertEquals(0,GLOBAL_OPEN_PHOENIX_CONNECTIONS.getMetric().getValue()); + assertEquals(0,GLOBAL_OPEN_INTERNAL_PHOENIX_CONNECTIONS.getMetric().getValue()); + Connection conn = null; + try{ + conn = DriverManager.getConnection(getUniqueUrl(), props); + try(Statement statement = conn.createStatement()) { + statement.execute(ddl); + } + //Enable delay for the delete + DelayedRegionServer.setDelayEnabled(true); + try(Statement statement = conn.createStatement()) { + statement.execute(deleteStmt); + } + fail(); + } catch (SQLException e) { + assertEquals(NEW_CONNECTION_THROTTLED.getErrorCode(),e.getErrorCode()); + assertEquals(NEW_CONNECTION_THROTTLED.getSQLState(),e.getSQLState()); + } finally { + DelayedRegionServer.setDelayEnabled(false); + if(conn != null) { + conn.close(); + } + long connections = GLOBAL_OPEN_PHOENIX_CONNECTIONS.getMetric().getValue(); + assertTrue(String.format("Found %d connections still open.",connections),2 >= connections); + connections = GLOBAL_OPEN_INTERNAL_PHOENIX_CONNECTIONS.getMetric().getValue(); Review comment: No we are closing most of the connections now. The 2 was initially chosen as there were occasionally background tasks in phoenix making the assert inconsistent. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
