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

tdsilva pushed a commit to branch 4.14-HBase-1.2
in repository https://gitbox.apache.org/repos/asf/phoenix.git

commit f1cdc1715000d800fd23a5c0401a7f51d7728bf1
Author: Chinmay Kulkarni <chinmayskulka...@gmail.com>
AuthorDate: Fri Nov 9 19:22:57 2018 -0800

    PHOENIX-5008: CQSI.init should not bubble up RetriableUpgradeException to 
client in case of an UpgradeRequiredException
---
 .../SystemCatalogCreationOnConnectionIT.java       | 122 +++++++++++++++++----
 .../phoenix/query/ConnectionQueryServicesImpl.java |   4 +-
 2 files changed, 101 insertions(+), 25 deletions(-)

diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SystemCatalogCreationOnConnectionIT.java
 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SystemCatalogCreationOnConnectionIT.java
index 689eb20..8fe3b69 100644
--- 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SystemCatalogCreationOnConnectionIT.java
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SystemCatalogCreationOnConnectionIT.java
@@ -17,6 +17,24 @@
  */
 package org.apache.phoenix.end2end;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.apache.phoenix.query.BaseTest.generateUniqueName;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.TimeoutException;
+
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HBaseTestingUtility;
 import org.apache.hadoop.hbase.HConstants;
@@ -26,6 +44,7 @@ import org.apache.phoenix.coprocessor.MetaDataProtocol;
 import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.exception.UpgradeRequiredException;
 import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixDriver;
 import org.apache.phoenix.jdbc.PhoenixEmbeddedDriver;
 import org.apache.phoenix.jdbc.PhoenixTestDriver;
 import org.apache.phoenix.query.*;
@@ -36,13 +55,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
-import java.io.IOException;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.*;
-import java.util.concurrent.TimeoutException;
-
-import static org.junit.Assert.*;
 
 @Category(NeedsOwnMiniClusterTest.class)
 public class SystemCatalogCreationOnConnectionIT {
@@ -57,6 +69,12 @@ public class SystemCatalogCreationOnConnectionIT {
     private static final String PHOENIX_SYSTEM_CATALOG = "SYSTEM.CATALOG";
     private static final String EXECUTE_UPGRADE_COMMAND = "EXECUTE UPGRADE";
     private static final String MODIFIED_MAX_VERSIONS ="5";
+    private static final String CREATE_TABLE_STMT = "CREATE TABLE %s"
+            + " (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY 
KEY(K1,K2))";
+    private static final String SELECT_STMT = "SELECT * FROM %s";
+    private static final String DELETE_STMT = "DELETE FROM %s";
+    private static final String CREATE_INDEX_STMT = "CREATE INDEX DUMMY_IDX ON 
%s (K1) INCLUDE (K2)";
+    private static final String UPSERT_STMT = "UPSERT INTO %s VALUES ('A', 
'B')";
 
     private static final Set<String> PHOENIX_SYSTEM_TABLES = new 
HashSet<>(Arrays.asList(
       "SYSTEM.CATALOG", "SYSTEM.SEQUENCE", "SYSTEM.STATS", "SYSTEM.FUNCTION",
@@ -155,12 +173,8 @@ public class SystemCatalogCreationOnConnectionIT {
         UpgradeUtil.doNotUpgradeOnFirstConnection(propsDoNotUpgradePropSet);
         SystemCatalogCreationOnConnectionIT.PhoenixSysCatCreationTestingDriver 
driver =
           new 
SystemCatalogCreationOnConnectionIT.PhoenixSysCatCreationTestingDriver(ReadOnlyProps.EMPTY_PROPS);
-        try {
-            driver.getConnectionQueryServices(getJdbcUrl(), 
propsDoNotUpgradePropSet);
-            fail("Client should not be able to create SYSTEM.CATALOG since we 
set the doNotUpgrade property");
-        } catch (Exception e) {
-            assertTrue(e instanceof UpgradeRequiredException);
-        }
+
+        driver.getConnectionQueryServices(getJdbcUrl(), 
propsDoNotUpgradePropSet);
         hbaseTables = getHBaseTables();
         assertFalse(hbaseTables.contains(PHOENIX_SYSTEM_CATALOG) || 
hbaseTables.contains(PHOENIX_NAMESPACE_MAPPED_SYSTEM_CATALOG));
         assertTrue(hbaseTables.size() == 0);
@@ -416,6 +430,70 @@ public class SystemCatalogCreationOnConnectionIT {
         assertEquals(Integer.parseInt(MODIFIED_MAX_VERSIONS), 
verifyModificationTableMetadata(driver, PHOENIX_SYSTEM_CATALOG));
     }
 
+    // Test the case when an end-user uses the vanilla PhoenixDriver to create 
a connection and a
+    // requirement for upgrade is detected. In this case, the user should get 
a connection on which
+    // they are only able to run "EXECUTE UPGRADE"
+    @Test
+    public void testExecuteUpgradeSameConnWithPhoenixDriver() throws Exception 
{
+        // Register the vanilla PhoenixDriver
+        DriverManager.registerDriver(PhoenixDriver.INSTANCE);
+        startMiniClusterWithToggleNamespaceMapping(Boolean.FALSE.toString());
+        Properties propsDoNotUpgradePropSet = new Properties();
+        // Set doNotUpgradeProperty to true
+        UpgradeUtil.doNotUpgradeOnFirstConnection(propsDoNotUpgradePropSet);
+
+        Connection conn = DriverManager.getConnection(getJdbcUrl(), 
propsDoNotUpgradePropSet);
+        hbaseTables = getHBaseTables();
+        assertFalse(hbaseTables.contains(PHOENIX_SYSTEM_CATALOG)
+                || 
hbaseTables.contains(PHOENIX_NAMESPACE_MAPPED_SYSTEM_CATALOG));
+        assertTrue(hbaseTables.size() == 0);
+
+        // Test that we are unable to run any other queries using this 
connection until we upgrade
+        final String tableName = generateUniqueName();
+        try {
+            conn.createStatement().execute(String.format(CREATE_TABLE_STMT, 
tableName));
+            fail("CREATE TABLE should have failed with 
UpgradeRequiredException");
+        } catch (UpgradeRequiredException expected) {
+
+        }
+        try {
+            conn.createStatement().execute(String.format(SELECT_STMT, 
tableName));
+            fail("SELECT should have failed with UpgradeRequiredException");
+        } catch (UpgradeRequiredException expected) {
+
+        }
+        try {
+            conn.createStatement().execute(String.format(DELETE_STMT, 
tableName));
+            fail("DELETE should have failed with UpgradeRequiredException");
+        } catch (UpgradeRequiredException expected) {
+
+        }
+        try {
+            conn.createStatement().execute(String.format(CREATE_INDEX_STMT, 
tableName));
+            fail("CREATE INDEX should have failed with 
UpgradeRequiredException");
+        } catch (UpgradeRequiredException expected) {
+
+        }
+        try {
+            conn.createStatement().execute(String.format(UPSERT_STMT, 
tableName));
+            fail("UPSERT VALUES should have failed with 
UpgradeRequiredException");
+        } catch (UpgradeRequiredException expected) {
+
+        }
+
+        // Now run the upgrade command. All SYSTEM tables should be created
+        conn.createStatement().execute("EXECUTE UPGRADE");
+        hbaseTables = getHBaseTables();
+        assertEquals(PHOENIX_SYSTEM_TABLES, hbaseTables);
+
+        // Now we can run any other query/mutation using this connection object
+        conn.createStatement().execute(String.format(CREATE_TABLE_STMT, 
tableName));
+        conn.createStatement().execute(String.format(SELECT_STMT, tableName));
+        conn.createStatement().execute(String.format(DELETE_STMT, tableName));
+        conn.createStatement().execute(String.format(CREATE_INDEX_STMT, 
tableName));
+        conn.createStatement().execute(String.format(UPSERT_STMT, tableName));
+    }
+
     /**
      * Return all created HBase tables
      * @return Set of HBase table name strings
@@ -423,7 +501,7 @@ public class SystemCatalogCreationOnConnectionIT {
      */
     private Set<String> getHBaseTables() throws IOException {
         Set<String> tables = new HashSet<>();
-        for (TableName tn : testUtil.getHBaseAdmin().listTableNames()) {
+        for (TableName tn : testUtil.getAdmin().listTableNames()) {
             tables.add(tn.getNameAsString());
         }
         return tables;
@@ -508,19 +586,17 @@ public class SystemCatalogCreationOnConnectionIT {
         ReadOnlyProps readOnlyProps = new ReadOnlyProps(props);
         SystemCatalogCreationOnConnectionIT.PhoenixSysCatCreationTestingDriver 
driver =
           new 
SystemCatalogCreationOnConnectionIT.PhoenixSysCatCreationTestingDriver(readOnlyProps);
-        try {
-            driver.getConnectionQueryServices(getJdbcUrl(), new Properties());
-            fail("Client should not be able to create SYSTEM.CATALOG since we 
set the isAutoUpgradeEnabled property to false");
-        } catch (Exception e) {
-            assertTrue(e instanceof UpgradeRequiredException);
-        }
+
+        // We should be able to get a connection, however upgradeRequired 
should be set so that we
+        // are not allowed to run any query/mutation until "EXECUTE UPGRADE" 
has been run
+        Connection conn = driver.getConnectionQueryServices(getJdbcUrl(), new 
Properties())
+                .connect(getJdbcUrl(), new Properties());
         hbaseTables = getHBaseTables();
         assertFalse(hbaseTables.contains(PHOENIX_SYSTEM_CATALOG) || 
hbaseTables.contains(PHOENIX_NAMESPACE_MAPPED_SYSTEM_CATALOG));
         assertTrue(hbaseTables.size() == 0);
         assertEquals(1, countUpgradeAttempts);
 
-        // We use the same ConnectionQueryServices instance to run "EXECUTE 
UPGRADE"
-        Connection conn = driver.getConnectionQueryServices(getJdbcUrl(), new 
Properties()).connect(getJdbcUrl(), new Properties());
+        // We use the same connection to run "EXECUTE UPGRADE"
         try {
             conn.createStatement().execute(EXECUTE_UPGRADE_COMMAND);
         } finally {
@@ -623,4 +699,4 @@ public class SystemCatalogCreationOnConnectionIT {
         assertEquals(0, countUpgradeAttempts);
         return driver;
     }
-}
\ No newline at end of file
+}
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
index d3cad64..dff8894 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
@@ -2592,8 +2592,8 @@ public class ConnectionQueryServicesImpl extends 
DelegateQueryServices implement
                                     upgradeSystemTables(url, props);
                                 } else {
                                     // We expect the user to manually run the 
"EXECUTE UPGRADE" command first.
-                                    // This exception will get caught below as 
a RetriableUpgradeException
-                                    throw new UpgradeRequiredException();
+                                    logger.error("Upgrade is required. Must 
run 'EXECUTE UPGRADE' "
+                                            + "before any other command");
                                 }
                             }
                             scheduleRenewLeaseTasks();

Reply via email to