yuqi1129 commented on code in PR #12374: URL: https://github.com/apache/gravitino/pull/12374#discussion_r3766493280
########## core/src/main/java/org/apache/gravitino/storage/relational/EntityCacheChangeLogListener.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.base.Preconditions; +import java.util.List; +import java.util.Locale; +import org.apache.gravitino.Entity.EntityType; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.cache.EntityCache; +import org.apache.gravitino.storage.relational.po.cache.EntityChangeRecord; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Keeps a per-node {@link EntityCache} coherent across a multi-node cluster by replaying {@code + * entity_change_log} rows written by other nodes. + * + * <p>Every ALTER/DROP row is replayed as a direct {@link EntityCache#invalidate(NameIdentifier, + * EntityType)} for exactly the changed entity key. Because the cache indexes its keys by identifier + * prefix, invalidating a container (for example a schema) cascades to its cached children on the + * local node through that forward prefix scan; no reverse index is involved. + * + * <p>This listener is registered only for a {@link + * org.apache.gravitino.cache.Coherence#LOCAL_PER_NODE} cache: a shared cache has a single + * cluster-wide copy and nothing per-node to invalidate. It is called <em>synchronously</em> on the + * poller thread, so it performs only fast, in-memory, idempotent invalidations. + */ +public class EntityCacheChangeLogListener implements EntityChangeLogListener { + + private static final Logger LOG = LoggerFactory.getLogger(EntityCacheChangeLogListener.class); + + private final EntityCache cache; + + /** + * Creates a listener that invalidates the given entity store cache. + * + * @param cache the per-node entity store cache to keep coherent + */ + public EntityCacheChangeLogListener(EntityCache cache) { + Preconditions.checkArgument(cache != null, "cache cannot be null"); + this.cache = cache; + } + + @Override + public void onEntityChange(List<EntityChangeRecord> changes) { + for (EntityChangeRecord change : changes) { + try { + EntityType type = entityType(change); + NameIdentifier ident = identifier(change); + if (type == null || ident == null) { + continue; + } + + LOG.debug("Invalidating entity cache due to entity change log: {} ({})", ident, type); + cache.invalidate(ident, type); + } catch (RuntimeException e) { Review Comment: Split the two failure modes as you suggested. The block actually mixed two independent sources of exceptions: `identifier()` calls `EntityChangeLogNameIdentifierCodec.decode()`, which throws `IllegalArgumentException` on a malformed encoding, and `cache.invalidate()`, which fails for entirely different reasons. The single broad catch treated both as "bad record, log and move on". Now parsing catches `IllegalArgumentException` where it happens and returns null, so a malformed row is logged and skipped without affecting the rest of the batch. A failed invalidation is handled separately: it would otherwise leave this node serving stale metadata, so the listener clears the whole cache — the cache is derived state, so the clear is a safe superset of the invalidation that failed, and it also covers the remaining records in the batch. If the clear itself fails, the exception propagates and the poller applies its retry / failure-action policy. Added three tests for these paths. -- 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]
