This is an automated email from the ASF dual-hosted git repository. tdsilva pushed a commit to branch 4.14-HBase-1.3 in repository https://gitbox.apache.org/repos/asf/phoenix.git
commit 293e75ecb67158870b0c147b7844ade53c3faa02 Author: Kiran Kumar Maturi <maturi.ki...@gmail.com> AuthorDate: Thu Jan 17 10:32:49 2019 +0530 PHOENIX-4993 close cache connections when region server is going down --- .../java/org/apache/phoenix/util/ServerUtil.java | 12 ++- .../CoprocessorHConnectionTableFactoryTest.java | 119 +++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/ServerUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/ServerUtil.java index 09701c5..a8170ce 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/util/ServerUtil.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/util/ServerUtil.java @@ -316,7 +316,10 @@ public class ServerUtil { @Override public void shutdown() { - ConnectionFactory.shutdown(); + // close the connections when region server is going down + if (this.server.isStopping() || this.server.isStopped() || this.server.isAborted()) { + ConnectionFactory.shutdown(); + } } @Override @@ -383,6 +386,13 @@ public class ServerUtil { return conf; } } + + /** + * Added for testing + */ + public static int getConnectionsCount() { + return connections.size(); + } } public static Configuration getCompactionConfig(Configuration conf) { diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/CoprocessorHConnectionTableFactoryTest.java b/phoenix-core/src/test/java/org/apache/phoenix/util/CoprocessorHConnectionTableFactoryTest.java new file mode 100644 index 0000000..a757780 --- /dev/null +++ b/phoenix-core/src/test/java/org/apache/phoenix/util/CoprocessorHConnectionTableFactoryTest.java @@ -0,0 +1,119 @@ + +/* + * 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.util; + +import static org.junit.Assert.assertTrue; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HRegionInfo; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.end2end.BaseUniqueNamesOwnClusterIT; +import org.junit.BeforeClass; +import org.junit.Test; + +/* + * This test is wrt to https://issues.apache.org/jira/browse/PHOENIX-4993.Test checks 1. region + * close should not close the shared connections 2. region server close should close the shared + * connections + */ +public class CoprocessorHConnectionTableFactoryTest extends BaseUniqueNamesOwnClusterIT { + private static String ORG_PREFIX = "ORG"; + private static final Log LOG = LogFactory.getLog(CoprocessorHConnectionTableFactoryTest.class); + + @BeforeClass + public static final void doSetup() throws Exception { + + setUpTestDriver(ReadOnlyProps.EMPTY_PROPS); + + } + + static String getOrgId(long id) { + return ORG_PREFIX + "-" + id; + } + + static String getRandomOrgId(int maxOrgId) { + return getOrgId(Math.round(Math.random() * maxOrgId)); + } + + static void writeToTable(String tableName, Connection conn, int maxOrgId) throws SQLException { + try { + + String orgId = getRandomOrgId(maxOrgId); + Statement stmt = conn.createStatement(); + for (int i = 0; i < 10; i++) { + stmt.executeUpdate("UPSERT INTO " + tableName + " VALUES('" + orgId + "'," + i + "," + + (i + 1) + "," + (i + 2) + ")"); + + } + conn.commit(); + } catch (Exception e) { + LOG.error("Client side exception:" + e); + } + + } + + static int getActiveConnections(HRegionServer regionServer, Configuration conf) throws Exception { + return ServerUtil.ConnectionFactory.getConnectionsCount(); + } + + @Test + public void testCachedConnections() throws Exception { + final String tableName = generateUniqueName(); + final String index1Name = generateUniqueName(); + final Connection conn = DriverManager.getConnection(getUrl()); + + final HBaseAdmin admin = getUtility().getHBaseAdmin(); + final MiniHBaseCluster cluster = getUtility().getHBaseCluster(); + final HRegionServer regionServer = cluster.getRegionServer(0); + Configuration conf = admin.getConfiguration(); + final int noOfOrgs = 20; + final AtomicBoolean flag = new AtomicBoolean(); + flag.set(false); + // create table and indices + String createTableSql = "CREATE TABLE " + tableName + + "(org_id VARCHAR NOT NULL PRIMARY KEY, v1 INTEGER, v2 INTEGER, v3 INTEGER) VERSIONS=1 SPLIT ON ('" + + ORG_PREFIX + "-" + noOfOrgs / 2 + "')"; + conn.createStatement().execute(createTableSql); + conn.createStatement().execute("CREATE INDEX " + index1Name + " ON " + tableName + "(v1)"); + + List<HRegionInfo> regions = admin.getTableRegions(Bytes.toBytes(tableName)); + final HRegionInfo regionInfo = regions.get(0); + writeToTable(tableName, conn, noOfOrgs); + int beforeRegionCloseCount = getActiveConnections(regionServer, conf); + admin.unassign(regionInfo.getEncodedNameAsBytes(), true); + getUtility().waitUntilAllRegionsAssigned(TableName.valueOf(tableName)); + int afterRegionCloseCount = getActiveConnections(regionServer, conf); + assertTrue("Cached connections not closed when region closes: ", + afterRegionCloseCount == beforeRegionCloseCount && afterRegionCloseCount > 0); + admin.stopRegionServer(regionServer.getServerName().toShortString()); + cluster.waitOnRegionServer(0); + int afterRegionServerStopCount = getActiveConnections(regionServer, conf); + assertTrue("Cached connections closed when region server stops: ", + afterRegionServerStopCount == 0); + + } + +}