ChinmaySKulkarni commented on a change in pull request #771:
URL: https://github.com/apache/phoenix/pull/771#discussion_r416987516



##########
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);

Review comment:
       We should use the new API for the master branch. The 4.x patch can 
continue to use the old one.

##########
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 {

Review comment:
       That's fine. In that case, can you remove repeated tests in 
`PhoenixMetricsIT`?

##########
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:
       I believe there is a way to change the task handling interval (set it to 
Long.MAX_VALUE or something for the sake of your tests). See 
https://github.com/apache/phoenix/blob/master/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/TaskRegionObserver.java#L67
   FYI @kadirozde is there a cleaner way to do this?

##########
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,

Review comment:
       In that case, is setting this property necessary? 

##########
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());

Review comment:
       Ok, as I think I've mentioned elsewhere, we should have a separate 
exception for internal vs external conn throttling.

##########
File path: 
phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
##########
@@ -4957,12 +4964,22 @@ private void 
returnAllSequences(ConcurrentMap<SequenceKey,Sequence> sequenceMap)
     public void addConnection(PhoenixConnection connection) throws 
SQLException {
         if (returnSequenceValues || shouldThrottleNumConnections) {
             synchronized (connectionCountLock) {
-                if (shouldThrottleNumConnections && connectionCount + 1 > 
maxConnectionsAllowed){
-                    GLOBAL_PHOENIX_CONNECTIONS_THROTTLED_COUNTER.increment();
-                    throw new 
SQLExceptionInfo.Builder(SQLExceptionCode.NEW_CONNECTION_THROTTLED).
-                        build().buildException();
+
+                if(shouldThrottleNumConnections) {
+                    int futureConnections = 1 + ( 
connection.isInternalConnection() ? internalConnectionCount : connectionCount);
+                    int allowedConnections = connection.isInternalConnection() 
? maxInternalConnectionsAllowed : maxConnectionsAllowed;
+                    if(allowedConnections != 0 && futureConnections > 
allowedConnections) {
+                        
GLOBAL_PHOENIX_CONNECTIONS_THROTTLED_COUNTER.increment();
+                        throw new 
SQLExceptionInfo.Builder(SQLExceptionCode.NEW_CONNECTION_THROTTLED).

Review comment:
       @dbwong I don't see this change. Can you please check again?




----------------------------------------------------------------
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]


Reply via email to