This is an automated email from the ASF dual-hosted git repository.

vincentpoon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/master by this push:
     new aa834a0  PHOENIX-4993 added test to check cached connections are not 
closed when region closes
aa834a0 is described below

commit aa834a060ce9330e77ca81ad26f6a1aa74ff1427
Author: Kiran Kumar Maturi <[email protected]>
AuthorDate: Tue Jan 29 15:57:03 2019 +0530

    PHOENIX-4993 added test to check cached connections are not closed when 
region closes
---
 .../java/org/apache/phoenix/util/ServerUtil.java   |   5 +
 .../CoprocessorHConnectionTableFactoryTest.java    | 111 +++++++++++++++++++++
 2 files changed, 116 insertions(+)

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 4d2d44b..322e461 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
@@ -348,6 +348,11 @@ public class ServerUtil {
                 connections.clear();
             }
         }
+
+        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..3997fca
--- /dev/null
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/util/CoprocessorHConnectionTableFactoryTest.java
@@ -0,0 +1,111 @@
+
+/*
+ * 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.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 region
+ * close should not 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(TableName.valueOf(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);
+
+  }
+
+}

Reply via email to