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


##########
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:
   What we need to achieve:
   - If user has not changed any index consistency from S->E, we are not going 
to defer any writes just because we have not called `refreshDataTableCache()` 
yet
   - If user has changed index consistency from S->E and we get to know about 
it after refresh period, we need to replay the change data from the moment when 
user updated any index consistency on the given data table
   
   As a solution, we need to skip deferrals in this method. This method only 
needs to perform `refreshDataTableCache()` call based on the conditions it is 
already using. However, it should not perform any mutation removal from the 
batch.
   
   When the region starts, LAST_DDL_TIMESTAMP for each index should be cached 
in memory. When a user updates the consistency of any indexes from S->E or 
E->S, LAST_DDL_TIMESTAMP for the indexes are updated. As part of 
`refreshDataTableCache()`, if updated LAST_DDL_TIMESTAMP is reported for any 
index in comparison to the in-memory value, it means the consistency mode for 
some index(es) was recently updated, and therefore we need to set the region's 
IDX_CDC_TRACKER progress back to the updated LAST_DDL_TIMESTAMP value of the 
index.
   
   The goal here should be to move the region's last updated timestamp back to 
the timestamp when a given index had a change in its consistency value. Though 
the LAST_DDL_TIMESTAMP might change for other index property changes, it is 
fair trade-off I think.



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