You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gravitino.apache.org by "Copilot (via GitHub)" <gi...@apache.org> on 2026/06/01 02:06:35 UTC

Re: [PR] [#11232][#11233] improvement(core): Global EntityChangeLogPoller with CatalogManager cache invalidation and automatic retention cleanup [gravitino]

Copilot commented on code in PR #11254:
URL: https://github.com/apache/gravitino/pull/11254#discussion_r3331340607


##########
core/src/main/java/org/apache/gravitino/storage/relational/EntityChangeLogPoller.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.storage.relational;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.function.LongSupplier;
+import org.apache.gravitino.storage.relational.mapper.EntityChangeLogMapper;
+import org.apache.gravitino.storage.relational.po.cache.EntityChangeRecord;
+import org.apache.gravitino.storage.relational.utils.SessionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Global poller for {@code entity_change_log}.
+ *
+ * <p>The poller owns the single high-water mark for a Gravitino server process and dispatches each
+ * consumed batch to registered listeners. Listeners should only perform idempotent local cache
+ * invalidation. The cursor always advances after dispatch regardless of individual listener
+ * failures, so a faulty listener cannot block other listeners or prevent pruning.
+ */
+public class EntityChangeLogPoller implements AutoCloseable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(EntityChangeLogPoller.class);
+
+  /** Max entity-change rows to fetch per poller cycle. */
+  private static final int ENTITY_CHANGE_POLLER_MAX_ROWS = 500;
+
+  private final List<EntityChangeLogListener> listeners = new CopyOnWriteArrayList<>();
+  private final long pollIntervalSecs;
+  private final long retentionMs;
+  private final long cleanupIntervalMs;
+  private final LongSupplier clockMs;
+
+  private ScheduledExecutorService scheduler;
+  private volatile long entityPollHighWaterId = 0;
+  private volatile long lastCleanupMs = Long.MIN_VALUE;
+
+  /**
+   * Creates an {@link EntityChangeLogPoller}.
+   *
+   * @param pollIntervalSecs interval between successive polling cycles
+   */
+  public EntityChangeLogPoller(long pollIntervalSecs) {
+    this(
+        pollIntervalSecs,
+        TimeUnit.DAYS.toMillis(1),
+        TimeUnit.HOURS.toMillis(1),
+        System::currentTimeMillis);
+  }
+
+  /**
+   * Creates an {@link EntityChangeLogPoller}.
+   *
+   * @param pollIntervalSecs interval between successive polling cycles
+   * @param retentionMs entity change retention in milliseconds, or 0 to disable cleanup
+   * @param cleanupIntervalMs interval between successive cleanup attempts in milliseconds
+   */
+  public EntityChangeLogPoller(long pollIntervalSecs, long retentionMs, long cleanupIntervalMs) {
+    this(pollIntervalSecs, retentionMs, cleanupIntervalMs, System::currentTimeMillis);
+  }
+
+  @VisibleForTesting
+  EntityChangeLogPoller(
+      long pollIntervalSecs, long retentionMs, long cleanupIntervalMs, LongSupplier clockMs) {
+    Preconditions.checkArgument(pollIntervalSecs > 0, "pollIntervalSecs must be positive");
+    Preconditions.checkArgument(retentionMs >= 0, "retentionMs must be non-negative");
+    Preconditions.checkArgument(cleanupIntervalMs > 0, "cleanupIntervalMs must be positive");
+    this.pollIntervalSecs = pollIntervalSecs;
+    this.retentionMs = retentionMs;
+    this.cleanupIntervalMs = cleanupIntervalMs;
+    this.clockMs = clockMs;
+  }
+
+  /**
+   * Registers a listener to receive future entity change batches.
+   *
+   * @param listener the listener to register
+   */
+  public void registerListener(EntityChangeLogListener listener) {
+    listeners.add(listener);
+  }
+
+  /**
+   * Unregisters a previously registered listener.
+   *
+   * @param listener the listener to unregister
+   */
+  public void unregisterListener(EntityChangeLogListener listener) {
+    listeners.remove(listener);
+  }
+
+  /**
+   * Initializes the high-water cursor to the current DB tail and schedules periodic polling.
+   *
+   * <p>Startup does not scan historical changes because this process has no warm local cache yet.
+   */
+  public void start() {
+    entityPollHighWaterId =
+        getOrDefault(
+            SessionUtils.getWithoutCommit(
+                EntityChangeLogMapper.class, EntityChangeLogMapper::selectMaxChangeId));
+
+    scheduler =
+        Executors.newSingleThreadScheduledExecutor(
+            r -> {
+              Thread t = new Thread(r);
+              t.setName("Gravitino-EntityChangeLogPoller");
+              t.setDaemon(true);
+              return t;
+            });
+    scheduler.scheduleWithFixedDelay(
+        this::pollChanges, pollIntervalSecs, pollIntervalSecs, TimeUnit.SECONDS);
+  }
+
+  @VisibleForTesting
+  void pollChanges() {
+    try {
+      doPollChanges();
+    } catch (Exception e) {
+      if (handleInterruptIfAny(e, "Entity change poll")) {
+        return;
+      }
+      LOG.warn("Entity change poll failed", e);
+    }
+  }
+
+  private synchronized void doPollChanges() {
+    List<EntityChangeRecord> changes = fetchEntityChanges();
+    if (changes.isEmpty()) {
+      pruneExpiredChangesIfNeeded();
+      return;
+    }
+
+    long maxSeenId = entityPollHighWaterId;
+    for (EntityChangeRecord change : changes) {
+      if (change.getId() > maxSeenId) {
+        maxSeenId = change.getId();
+      }
+    }
+
+    List<EntityChangeRecord> dispatchedChanges = Collections.unmodifiableList(changes);
+    for (EntityChangeLogListener listener : listeners) {
+      try {
+        listener.onEntityChange(dispatchedChanges);
+      } catch (Exception e) {
+        LOG.warn("Entity change listener {} failed", listener.getClass().getName(), e);
+      }
+    }
+
+    entityPollHighWaterId = maxSeenId;
+    pruneExpiredChangesIfNeeded();
+  }
+
+  private List<EntityChangeRecord> fetchEntityChanges() {
+    return SessionUtils.getWithoutCommit(
+        EntityChangeLogMapper.class,
+        m -> m.selectEntityChanges(entityPollHighWaterId, ENTITY_CHANGE_POLLER_MAX_ROWS));
+  }
+
+  private static boolean handleInterruptIfAny(Throwable e, String context) {
+    Throwable t = e;
+    while (t != null) {
+      if (t instanceof InterruptedException) {
+        Thread.currentThread().interrupt();
+        LOG.debug("{} interrupted, stopping poll cycle", context);
+        return true;
+      }
+      t = t.getCause();
+    }
+    if (Thread.currentThread().isInterrupted()) {
+      LOG.debug("{} ran while thread was interrupted, stopping poll cycle", context);
+      return true;
+    }
+    return false;
+  }
+
+  private void pruneExpiredChangesIfNeeded() {
+    if (retentionMs <= 0) {
+      return;
+    }
+
+    long now = clockMs.getAsLong();
+    if (lastCleanupMs != Long.MIN_VALUE && now - lastCleanupMs < cleanupIntervalMs) {
+      return;
+    }
+
+    long before = now - retentionMs;
+    try {
+      SessionUtils.doWithoutCommit(
+          EntityChangeLogMapper.class, mapper -> mapper.pruneOldEntityChanges(before));
+      lastCleanupMs = now;
+    } catch (Exception e) {
+      LOG.warn("Failed to prune expired entity change logs before {}", before, e);
+    }

Review Comment:
   Retention pruning is retried on every poll cycle if a prune attempt throws, because lastCleanupMs is only updated on success. With the default 3s poll interval and 1h cleanup interval, a transient DB issue would cause repeated prune attempts and warn logs every few seconds until one succeeds.



##########
core/src/main/java/org/apache/gravitino/catalog/CatalogChangeLogListener.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.catalog;
+
+import java.util.List;
+import java.util.Locale;
+import org.apache.gravitino.Entity.EntityType;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.storage.relational.EntityChangeLogListener;
+import org.apache.gravitino.storage.relational.po.cache.EntityChangeRecord;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Invalidates {@link CatalogManager}'s local catalog cache from {@code entity_change_log}. */
+public class CatalogChangeLogListener implements EntityChangeLogListener {
+
+  private static final Logger LOG = LoggerFactory.getLogger(CatalogChangeLogListener.class);
+
+  private final CatalogManager catalogManager;
+
+  /**
+   * Creates a listener for a catalog manager.
+   *
+   * @param catalogManager the catalog manager whose local cache should be invalidated
+   */
+  public CatalogChangeLogListener(CatalogManager catalogManager) {
+    this.catalogManager = catalogManager;
+  }
+
+  @Override
+  public void onEntityChange(List<EntityChangeRecord> changes) {
+    for (EntityChangeRecord change : changes) {
+      if (!EntityType.CATALOG.name().equals(change.getEntityType().toUpperCase(Locale.ROOT))) {
+        continue;
+      }
+
+      NameIdentifier ident = catalogIdentifier(change);
+      if (ident == null) {
+        continue;
+      }
+
+      if (catalogManager.consumeLocalMutation(ident)) {
+        LOG.debug("Skipping catalog cache invalidation for local mutation: {}", ident);
+        continue;
+      }
+
+      LOG.debug("Invalidating catalog cache due to entity change log: {}", ident);
+      catalogManager.getCatalogCache().invalidate(ident);
+    }
+  }
+
+  private NameIdentifier catalogIdentifier(EntityChangeRecord change) {
+    String[] names = change.getFullName().split("\\.");
+    if (names.length != 2) {
+      LOG.warn("Invalid catalog full name in entity change log: {}", change.getFullName());
+      return null;
+    }
+    return NameIdentifier.of(names[0], names[1]);
+  }

Review Comment:
   catalogIdentifier() can throw (e.g., IllegalNameIdentifierException / NPE) for malformed change log rows (empty segments, null fullName), which aborts this listener for the entire batch. Since the global poller catches listener exceptions per-listener, this would skip cache invalidation and also leave local-mutation markers unconsumed. Make parsing defensive and return null on parse failures.



-- 
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: commits-unsubscribe@gravitino.apache.org

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