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

vjasani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/phoenix-adapters.git


The following commit(s) were added to refs/heads/main by this push:
     new b22cbc3  Use index cron job to drop deleted indexes
b22cbc3 is described below

commit b22cbc3b7c37290747bfe0260e081e1681d47142
Author: Palash Chauhan <[email protected]>
AuthorDate: Thu Jan 8 17:55:04 2026 -0800

    Use index cron job to drop deleted indexes
---
 .../org/apache/phoenix/ddb/rest/RESTServer.java    |  9 +++--
 .../org/apache/phoenix/ddb/UpdateTable2IT.java     |  6 ++--
 .../java/org/apache/phoenix/ddb/UpdateTableIT.java | 13 ++++++--
 ...ildingActivator.java => AsyncIndexManager.java} | 38 +++++++++++++++++++---
 4 files changed, 52 insertions(+), 14 deletions(-)

diff --git 
a/phoenix-ddb-rest/src/main/java/org/apache/phoenix/ddb/rest/RESTServer.java 
b/phoenix-ddb-rest/src/main/java/org/apache/phoenix/ddb/rest/RESTServer.java
index b9b16ad..e357810 100644
--- a/phoenix-ddb-rest/src/main/java/org/apache/phoenix/ddb/rest/RESTServer.java
+++ b/phoenix-ddb-rest/src/main/java/org/apache/phoenix/ddb/rest/RESTServer.java
@@ -70,7 +70,7 @@ import org.apache.phoenix.ddb.rest.auth.CredentialStore;
 import org.apache.phoenix.ddb.rest.util.Constants;
 import org.apache.phoenix.ddb.TableOptionsConfig;
 import org.apache.phoenix.ddb.service.utils.SegmentScanUtil;
-import org.apache.phoenix.ddb.utils.IndexBuildingActivator;
+import org.apache.phoenix.ddb.utils.AsyncIndexManager;
 import org.apache.phoenix.ddb.utils.PhoenixUtils;
 
 /**
@@ -168,7 +168,7 @@ public class RESTServer {
         try {
             String url = PhoenixUtils.URL_ZK_PREFIX + 
conf.get(Constants.PHOENIX_DDB_ZK_QUORUM);
             validateConnection(url);
-            indexBuildingActivator = startIndexBuildingActivator(url);
+            indexBuildingActivator = startAsyncIndexManager(url);
             TableOptionsConfig.initialize();
             SegmentScanUtil.createSegmentScanMetadataTable(url);
         } catch (Exception e) {
@@ -294,12 +294,11 @@ public class RESTServer {
         }
     }
 
-    private ExecutorService startIndexBuildingActivator(String jdbcUrl) {
+    private ExecutorService startAsyncIndexManager(String jdbcUrl) {
         ScheduledExecutorService scheduler = 
Executors.newSingleThreadScheduledExecutor();
         scheduler.scheduleAtFixedRate(() -> {
             try (Connection connection = 
ConnectionUtil.getConnection(jdbcUrl)) {
-                IndexBuildingActivator.activateIndexesForBuilding(connection, 
1800010);
-                IndexBuildingActivator.runIndexTool(connection, 1860000);
+                AsyncIndexManager.run(connection);
             } catch (SQLException e) {
                 LOG.info("Error while running IndexBuildingActivator. ", e);
             }
diff --git 
a/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTable2IT.java 
b/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTable2IT.java
index 7c2ce5d..cdc8e84 100644
--- a/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTable2IT.java
+++ b/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTable2IT.java
@@ -21,7 +21,7 @@ import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.HBaseTestingUtility;
 import org.apache.phoenix.ddb.rest.RESTServer;
-import org.apache.phoenix.ddb.utils.IndexBuildingActivator;
+import org.apache.phoenix.ddb.utils.AsyncIndexManager;
 import org.apache.phoenix.end2end.ServerMetadataCacheTestImpl;
 import org.apache.phoenix.jdbc.PhoenixDriver;
 import org.apache.phoenix.jdbc.PhoenixTestDriver;
@@ -177,9 +177,9 @@ public class UpdateTable2IT {
 
         try (Connection connection = DriverManager.getConnection(url)) {
             // background thread activates index to BUILDING state
-            IndexBuildingActivator.activateIndexesForBuilding(connection, 0);
+            AsyncIndexManager.activateIndexesForBuilding(connection, 0);
             // run MR tool to build index and set state to ACTIVE
-            IndexBuildingActivator.runIndexTool(connection, 0);
+            AsyncIndexManager.runIndexTool(connection, 0);
         }
 
         // make sure Index is active
diff --git 
a/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTableIT.java 
b/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTableIT.java
index d29cc3c..af08f28 100644
--- a/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTableIT.java
+++ b/phoenix-ddb-rest/src/test/java/org/apache/phoenix/ddb/UpdateTableIT.java
@@ -21,7 +21,7 @@ import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.HBaseTestingUtility;
 import org.apache.phoenix.ddb.rest.RESTServer;
-import org.apache.phoenix.ddb.utils.IndexBuildingActivator;
+import org.apache.phoenix.ddb.utils.AsyncIndexManager;
 import org.apache.phoenix.ddb.utils.PhoenixUtils;
 import org.apache.phoenix.end2end.ServerMetadataCacheTestImpl;
 import org.apache.phoenix.jdbc.PhoenixConnection;
@@ -162,6 +162,15 @@ public class UpdateTableIT {
         utre = dynamoDbClient.updateTable(utr.build());
         Assert.assertEquals("DELETING",
                 
utre.tableDescription().globalSecondaryIndexes().get(0).indexStatus().toString());
+
+        // AsyncIndexManager should drop the index
+        try (Connection connection = DriverManager.getConnection(url)) {
+            AsyncIndexManager.dropDisabledIndexes(connection, 0);
+        }
+
+        //describe table shows no index
+        describeTableResponse = 
phoenixDBClientV2.describeTable(describeTableRequest);
+        
Assert.assertTrue(describeTableResponse.table().globalSecondaryIndexes().isEmpty());
     }
 
     @Test(timeout = 120000)
@@ -221,7 +230,7 @@ public class UpdateTableIT {
 
         //change index state
         try (Connection connection = DriverManager.getConnection(url)) {
-            IndexBuildingActivator.activateIndexesForBuilding(connection, 0);
+            AsyncIndexManager.activateIndexesForBuilding(connection, 0);
             PTable pTable =
                     
connection.unwrap(PhoenixConnection.class).getTableNoCache(PhoenixUtils.getFullTableName(tableName,
 false));
             Assert.assertEquals(PIndexState.BUILDING, 
pTable.getIndexes().get(0).getIndexState());
diff --git 
a/phoenix-ddb-utils/src/main/java/org/apache/phoenix/ddb/utils/IndexBuildingActivator.java
 
b/phoenix-ddb-utils/src/main/java/org/apache/phoenix/ddb/utils/AsyncIndexManager.java
similarity index 66%
rename from 
phoenix-ddb-utils/src/main/java/org/apache/phoenix/ddb/utils/IndexBuildingActivator.java
rename to 
phoenix-ddb-utils/src/main/java/org/apache/phoenix/ddb/utils/AsyncIndexManager.java
index ce4403c..9fc1cc9 100644
--- 
a/phoenix-ddb-utils/src/main/java/org/apache/phoenix/ddb/utils/IndexBuildingActivator.java
+++ 
b/phoenix-ddb-utils/src/main/java/org/apache/phoenix/ddb/utils/AsyncIndexManager.java
@@ -15,9 +15,9 @@ import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 
-public class IndexBuildingActivator {
+public class AsyncIndexManager {
 
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(IndexBuildingActivator.class);
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(AsyncIndexManager.class);
 
     private static final String SELECT_CREATE_DISABLE_INDEX = "SELECT DISTINCT 
TABLE_SCHEM, TABLE_NAME "
             + "FROM SYSTEM.CATALOG "
@@ -39,6 +39,22 @@ public class IndexBuildingActivator {
             + "AND TENANT_ID IS NULL "
             + "AND (TO_NUMBER(CURRENT_TIME()) - LAST_DDL_TIMESTAMP) > %d";
 
+    private static final String SELECT_DISABLED_INDEX = "SELECT DISTINCT 
TABLE_SCHEM, TABLE_NAME "
+            + "FROM SYSTEM.CATALOG "
+            + "WHERE TABLE_SCHEM='DDB'"
+            + "AND COLUMN_NAME IS NULL "
+            + "AND COLUMN_FAMILY IS NULL "
+            + "AND TABLE_TYPE = 'i'"
+            + "AND INDEX_STATE = 'x' "
+            + "AND TENANT_ID IS NULL "
+            + "AND (TO_NUMBER(CURRENT_TIME()) - LAST_DDL_TIMESTAMP) > %d";
+
+    public static void run(Connection connection) throws SQLException {
+        activateIndexesForBuilding(connection, 1800010);
+        dropDisabledIndexes(connection, 1800010);
+        runIndexTool(connection, 1860000);
+    }
+
     public static void activateIndexesForBuilding(Connection conn, int 
minAgeMs) throws SQLException {
         ResultSet rs = 
conn.createStatement().executeQuery(String.format(SELECT_CREATE_DISABLE_INDEX, 
minAgeMs));
         while (rs.next()) {
@@ -69,9 +85,23 @@ public class IndexBuildingActivator {
                         null, IndexTool.IndexVerifyType.NONE);
             }
         } catch (Exception e) {
-            LOGGER.error("Error in building indexes", e);
-            throw new SQLException(e);
+            LOGGER.error("Error in building indexes using IndexTool", e);
         }
     }
 
+    public static void dropDisabledIndexes(Connection conn, int minAgeMs) 
throws SQLException {
+        try {
+            ResultSet rs = 
conn.createStatement().executeQuery(String.format(SELECT_DISABLED_INDEX, 
minAgeMs));
+            while (rs.next()) {
+                String schemaName = rs.getString(1);
+                String indexName = rs.getString(2);
+                String fullIndexName = SchemaUtil.getTableName(schemaName, 
indexName);
+                PTable ptable = 
conn.unwrap(PhoenixConnection.class).getTable(fullIndexName);
+                String parentTable = 
PhoenixUtils.getFullTableName(ptable.getParentTableName().getString(), true);
+                conn.createStatement().execute("DROP INDEX \"" + indexName + 
"\" ON " + parentTable);
+            }
+        } catch (Exception e) {
+            LOGGER.error("Error in dropping disabled indexes.", e);
+        }
+    }
 }

Reply via email to