You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by "kadirozde (via GitHub)" <gi...@apache.org> on 2025/12/09 00:22:34 UTC

Re: [PR] PHOENIX-7018 Server side index maintainer caching for read, write, and replication [phoenix]

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


##########
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 should not build index maintainers for every mutation batch. We need to cache them.  One way to cache them s to add a field to PTable for holding the index maintainer if the table type is index. 



##########
phoenix-core-client/src/main/java/org/apache/phoenix/index/IndexMetaDataCacheClient.java:
##########
@@ -120,10 +129,51 @@ public static ServerCache setMetaDataOnMutations(PhoenixConnection connection, P
       txState = connection.getMutationState().encodeTransaction();
     }
     boolean hasIndexMetaData = indexMetaDataPtr.getLength() > 0;
+    ReadOnlyProps props = connection.getQueryServices().getProps();
     if (hasIndexMetaData) {
-      if (
+      List<PTable> indexes = table.getIndexes();
+      boolean sendIndexMaintainers =
+        indexes != null && indexes.stream().anyMatch(IndexMaintainer::sendIndexMaintainer);

Review Comment:
   Can we have a simple loop here instead of setting up a stream from the list of a small number of objects?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org