apurtell commented on code in PR #2658:
URL: https://github.com/apache/phoenix/pull/2658#discussion_r4508530737


##########
phoenix-core-server/src/main/java/org/apache/phoenix/hbase/index/IndexCDCConsumer.java:
##########
@@ -319,8 +344,86 @@ private PTable getDataTable(PhoenixConnection conn) throws 
SQLException {
     return dataTable;
   }
 
+  /**
+   * Reads the data table definition from SYSTEM.CATALOG. The metadata cache 
of the region server
+   * can keep an old definition after an ALTER, for example when the region 
moves to this server.
+   */
   private void refreshDataTableCache(PhoenixConnection conn) throws 
SQLException {
-    cachedDataTable = conn.getTable(dataTableName);
+    long readTime = EnvironmentEdgeManager.currentTimeMillis();
+    cachedDataTable = conn.getTableNoCache(conn.getTenantId(), dataTableName);
+    cachedDataTableReadTime = readTime;
+  }
+
+  /**
+   * Removes from the batch the events that the cached data table definition 
does not cover. The
+   * definition covers an event when the consumer read it from SYSTEM.CATALOG 
after the event time.
+   * A writer that sees a consistency change resolves its metadata after the 
DDL commit. Thus, when
+   * a read does not show the DDL, the events of that writer are newer than 
the read. The consumer
+   * reads the definition again when the batch has newer events and the 
refresh interval has
+   * expired. The UPDATE_CACHE_FREQUENCY of the data table sets that interval, 
within fixed limits.
+   * Removed events stay in the CDC for a later batch. The event timestamps 
and the read time come
+   * from the clock of the region server that hosts the region.
+   * @return the timestamp of the last event that the batch keeps, or {@code 
lastProcessedTimestamp}
+   *         when the batch keeps no events.
+   */
+  private long keepEventsCoveredByMetadata(PhoenixConnection conn,
+    List<Pair<Long, IndexMutationsProtos.DataRowStates>> batchStates, long 
lastProcessedTimestamp,
+    long lastTimestamp) throws SQLException {
+    long maxEventTimestamp = Long.MIN_VALUE;
+    for (Pair<Long, IndexMutationsProtos.DataRowStates> entry : batchStates) {
+      maxEventTimestamp = Math.max(maxEventTimestamp, entry.getFirst());
+    }
+    if (maxEventTimestamp <= cachedDataTableReadTime) {
+      return lastTimestamp;
+    }
+    long refreshInterval = Math.max(MIN_METADATA_REFRESH_INTERVAL_MS,
+      Math.min(getDataTable(conn).getUpdateCacheFrequency(), 
MAX_METADATA_REFRESH_INTERVAL_MS));
+    if (EnvironmentEdgeManager.currentTimeMillis() - cachedDataTableReadTime 
>= refreshInterval) {
+      refreshDataTableCache(conn);
+      if (maxEventTimestamp <= cachedDataTableReadTime) {
+        return lastTimestamp;
+      }
+    }
+    final long readTime = cachedDataTableReadTime;
+    batchStates.removeIf(entry -> entry.getFirst() > readTime);
+    long keptTimestamp = lastProcessedTimestamp;
+    for (Pair<Long, IndexMutationsProtos.DataRowStates> entry : batchStates) {
+      keptTimestamp = Math.max(keptTimestamp, entry.getFirst());
+    }
+    LOG.debug("Deferred CDC events after {} for table {} region {} until the 
next metadata refresh",
+      readTime, dataTableName, encodedRegionName);
+    return keptTimestamp;

Review Comment:
   Addressed.



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