kadirozde commented on a change in pull request #517: PHOENIX-5211 Consistent 
Immutable Global Indexes for Non-Transactiona…
URL: https://github.com/apache/phoenix/pull/517#discussion_r294400376
 
 

 ##########
 File path: 
phoenix-core/src/it/java/org/apache/phoenix/end2end/index/ImmutableIndexIT.java
 ##########
 @@ -259,7 +276,136 @@ private void assertIndexMutations(Connection conn) 
throws SQLException {
                 (transactionProvider != null && 
                  
transactionProvider.isUnsupported(Feature.MAINTAIN_LOCAL_INDEX_ON_SERVER)), 
iterator.hasNext());
     }
-    
+
+    private void createAndPopulateTableAndIndexForConsistentIndex(Connection 
conn, String tableName, String indexName, int numOfRowsToInsert)
+            throws Exception {
+        String ddl = "CREATE TABLE " + TABLE_NAME + TestUtil.TEST_TABLE_SCHEMA 
+ tableDDLOptions;
+        INDEX_DDL =
+                "CREATE " + " INDEX IF NOT EXISTS " + 
SchemaUtil.getTableNameFromFullName(indexName)
+                        + " ON " + tableName + " (long_pk, varchar_pk)"
+                        + " INCLUDE (long_col1, long_col2) ";
+
+        conn.createStatement().execute(ddl);
+        conn.createStatement().execute(INDEX_DDL);
+        upsertRows(conn, tableName, numOfRowsToInsert);
+        conn.commit();
+
+        TestUtil.waitForIndexState(conn, indexName, PIndexState.ACTIVE);
+    }
+
+    @Test
+    public void testGlobalImmutableIndexCreate() throws Exception {
+        if (localIndex || transactionProvider != null) {
+            return;
+        }
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        String tableName = "TBL_" + generateUniqueName();
+        String indexName = "IND_" + generateUniqueName();
+        String fullTableName = 
SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
+        String fullIndexName = 
SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, indexName);
+        TABLE_NAME = fullTableName;
+        try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+            conn.setAutoCommit(true);
+            int numRows = 1;
+            createAndPopulateTableAndIndexForConsistentIndex(conn, 
fullTableName, fullIndexName,
+                    numRows);
+
+            ResultSet rs;
+            rs = conn.createStatement().executeQuery("SELECT /*+ NO_INDEX */ 
COUNT(*) FROM " + TABLE_NAME);
+            assertTrue(rs.next());
+            assertEquals(numRows, rs.getInt(1));
+            rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + 
fullIndexName);
+            assertTrue(rs.next());
+            assertEquals(numRows, rs.getInt(1));
+            verifyRowsForEmptyColValue(conn, fullIndexName, 
IndexRegionObserver.VERIFIED_BYTES);
+
+            // Now try to fail Phase1 and observe that index state is not 
DISABLED
+            try (Admin admin = 
conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();) {
+                admin.disableTable(TableName.valueOf(fullIndexName));
+                boolean isWriteOnDisabledIndexFailed = false;
+                try {
+                    upsertRows(conn, fullTableName, numRows);
+                } catch (CommitException ex) {
+                    isWriteOnDisabledIndexFailed = true;
+                }
+                assertEquals(true, isWriteOnDisabledIndexFailed);
+                PIndexState indexState = TestUtil.getIndexState(conn, 
fullIndexName);
+                assertEquals(PIndexState.ACTIVE, indexState);
+            }
+        }
+    }
+
+    @Test
+    public void testGlobalImmutableIndexDelete() throws Exception {
+        if (localIndex || transactionProvider != null) {
+            return;
+        }
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        String tableName = "TBL_" + generateUniqueName();
+        String indexName = "IND_" + generateUniqueName();
+        String fullTableName = 
SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
+        String fullIndexName = 
SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, indexName);
+        TABLE_NAME = fullTableName;
+        try (Connection conn = DriverManager.getConnection(getUrl(), props);
+                Admin admin = 
conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();) {
+            conn.setAutoCommit(true);
+            int numRows = 2;
+            createAndPopulateTableAndIndexForConsistentIndex(conn, 
fullTableName, fullIndexName, numRows);
+
+            String dml = "DELETE from " + fullTableName + " WHERE 
varchar_pk='varchar1'";
+            conn.createStatement().execute(dml);
+            ResultSet rs;
+            rs = conn.createStatement().executeQuery("SELECT /*+ NO_INDEX */ 
COUNT(*) FROM " + TABLE_NAME);
+            assertTrue(rs.next());
+            assertEquals(numRows - 1, rs.getInt(1));
+            rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + 
fullIndexName);
+            assertTrue(rs.next());
+            assertEquals(numRows - 1, rs.getInt(1));
+
+            // Disable data table so that delete fails on data table but index 
table row remains as unverified
+            admin.disableTable(TableName.valueOf(TABLE_NAME));
+            dml = "DELETE from " + fullTableName + " WHERE 
varchar_pk='varchar2'";
+            boolean isDeleteFailed = false;
+            try {
+                conn.createStatement().execute(dml);
+            } catch (Exception ex) {
+                isDeleteFailed = true;
+            }
+            assertEquals(true, isDeleteFailed);
+            rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + 
fullIndexName);
+            assertTrue(rs.next());
+            assertEquals(numRows - 1, rs.getInt(1));
+            verifyRowsForEmptyColValue(conn, fullIndexName, 
IndexRegionObserver.UNVERIFIED_BYTES);
+
+            // Now read from index and see that we don't get any data
+            rs =
+                    conn.createStatement().executeQuery(
+                            "EXPLAIN SELECT long_pk, varchar_pk, long_col1 
FROM " + TABLE_NAME + " WHERE varchar_pk='varchar2' AND long_pk=2");
+            String actualExplainPlan = QueryUtil.getExplainPlan(rs);
+            assertExplainPlan(false, actualExplainPlan, fullTableName, 
fullIndexName);
+            assertFalse(rs.next());
 
 Review comment:
   The test has failed to delete the rows WHERE varchar_pk='varchar2'. What is 
the purpose of this select with WHERE varchar_pk='varchar2' AND long_pk=2? and 
why to expect assertFalse(rs.next())?

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


With regards,
Apache Git Services

Reply via email to