virajjasani commented on code in PR #2321:
URL: https://github.com/apache/phoenix/pull/2321#discussion_r2601162564


##########
phoenix-core-server/src/main/java/org/apache/phoenix/index/PhoenixIndexMetaDataBuilder.java:
##########
@@ -117,4 +116,101 @@ public int getClientVersion() {
     }
 
   }
+
+  /**
+   * Get IndexMetaDataCache by looking up PTable using table metadata 
attributes attached to the
+   * mutation.
+   * @param env        RegionCoprocessorEnvironment.
+   * @param attributes Mutation attributes.
+   * @return IndexMetaDataCache or null if table metadata not found in 
attributes.
+   */
+  private static IndexMetaDataCache getIndexMetaDataCacheFromPTable(
+    RegionCoprocessorEnvironment env, Map<String, byte[]> attributes) {
+    try {
+      byte[] schemaBytes =
+        
attributes.get(MutationState.MutationMetadataType.SCHEMA_NAME.toString());
+      byte[] tableBytes =
+        
attributes.get(MutationState.MutationMetadataType.LOGICAL_TABLE_NAME.toString());
+      if (schemaBytes == null || tableBytes == null) {
+        LOGGER.error("Table metadata for table name and schema name not found 
in mutation "
+          + "attributes, falling back to GlobalCache lookup");
+        return null;
+      }
+      byte[] tenantIdBytes =
+        
attributes.get(MutationState.MutationMetadataType.TENANT_ID.toString());
+      byte[] txState = 
attributes.get(BaseScannerRegionObserverConstants.TX_STATE);
+      byte[] clientVersionBytes = 
attributes.get(BaseScannerRegionObserverConstants.CLIENT_VERSION);
+
+      final int clientVersion = clientVersionBytes == null
+        ? ScanUtil.UNKNOWN_CLIENT_VERSION
+        : Bytes.toInt(clientVersionBytes);
+      final PhoenixTransactionContext txnContext =
+        TransactionFactory.getTransactionContext(txState, clientVersion);
+
+      String fullTableName = SchemaUtil.getTableName(schemaBytes, tableBytes);
+      String tenantId =
+        tenantIdBytes == null || tenantIdBytes.length == 0 ? null : 
Bytes.toString(tenantIdBytes);
+      Properties props = new Properties();
+      if (tenantId != null) {
+        props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
+      }
+      try (Connection conn = QueryUtil.getConnectionOnServer(props, 
env.getConfiguration())) {
+        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
+        PTable dataTable = pconn.getTable(tenantId, fullTableName);
+        final List<IndexMaintainer> indexMaintainers =
+          buildIndexMaintainersFromPTable(dataTable, pconn);

Review Comment:
   We already have index maintainer cache at PTable level:
   
   ```
     @Override
     public synchronized IndexMaintainer getIndexMaintainer(PTable dataTable, 
PTable cdcTable,
       PhoenixConnection connection) throws SQLException {
       if (indexMaintainer == null) {
         indexMaintainer = IndexMaintainer.create(dataTable, cdcTable, this, 
connection);
       }
       return indexMaintainer;
     }
   ```
   
   Therefore, if a caller calls getIndexMaintainer(), it will retrieve index 
maintainer for the first time and then cache it so that any subsequent call to 
getIndexMaintainer() will get the index maintainer from the cache. We only need 
to ensure this API is used.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to