jerryshao commented on code in PR #7354:
URL: https://github.com/apache/gravitino/pull/7354#discussion_r2145436128


##########
core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java:
##########
@@ -133,7 +76,7 @@ protected static void validateEntityHasIdentifier(Entity 
entity) {
    * @param <E> The type of the entities in the new list.
    */
   @SuppressWarnings("unchecked")
-  protected static <E extends Entity & HasIdentifier> List<E> 
convertEntity(List<Entity> entities) {
+  public static <E extends Entity & HasIdentifier> List<E> 
convertEntity(List<Entity> entities) {

Review Comment:
   Please change to `convertEntities`.



##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -184,7 +217,20 @@ public List<TagEntity> associateTagsWithMetadataObject(
   public <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
       Type relType, NameIdentifier nameIdentifier, Entity.EntityType 
identType, boolean allFields)
       throws IOException {
-    return backend.listEntitiesByRelation(relType, nameIdentifier, identType, 
allFields);
+    return cache.withCacheLock(
+        () -> {
+          Optional<List<E>> entities = cache.getIfPresent(relType, 
nameIdentifier, identType);
+          if (entities.isPresent()) {
+            return entities.get();
+          }
+
+          List<E> backendEntities =
+              backend.listEntitiesByRelation(relType, nameIdentifier, 
identType, allFields);
+
+          cache.put(nameIdentifier, identType, relType, backendEntities);

Review Comment:
   My concern is that the current cache may not be balanced. For some keys the 
value list may be large (like here). So the size  of cache may not be accurate, 
and potentially lead to OOM. For example, if the relation list is very large.



##########
core/src/main/java/org/apache/gravitino/cache/NoOpsCache.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.cache;
+
+import java.util.List;
+import java.util.Optional;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.HasIdentifier;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.SupportsRelationOperations;
+
+/** A cache implementation that does not cache anything. */
+public class NoOpsCache extends BaseEntityCache {
+  /**
+   * Constructs a new {@link BaseEntityCache} instance.
+   *
+   * @param config The cache configuration
+   */
+  public NoOpsCache(Config config) {
+    super(config);
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  protected void invalidateExpiredItem(EntityCacheKey key) {
+    // do nothing
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public void clear() {
+    // do nothing
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public long size() {
+    return 0;
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public <E extends Exception> void withCacheLock(ThrowingRunnable<E> action) 
throws E {
+    action.run();
+  }
+
+  /** {@inheritDoc} */
+  @Override
+  public <T, E extends Exception> T withCacheLock(ThrowingSupplier<T, E> 
action) throws E {
+    return action.get();
+  }

Review Comment:
   If user they want to use cache lock to guarantee the atomicity, I think we 
still need a lock here even for `NoOpsCache`, what do you think?



##########
core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java:
##########
@@ -306,12 +207,23 @@ public <E extends Entity & HasIdentifier> void put(E 
entity) {
 
     withLock(
         () -> {
+          invalidateOnKeyChange(entity);
           NameIdentifier identifier = getIdentFromEntity(entity);
           EntityCacheKey entityCacheKey = EntityCacheKey.of(identifier, 
entity.type());
+
           syncEntitiesToCache(entityCacheKey, Lists.newArrayList(entity));
         });
   }
 
+  /** {@inheritDoc} */
+  @Override
+  public <E extends Entity & HasIdentifier> void invalidateOnKeyChange(E 
entity) {
+    if (Objects.requireNonNull(entity.type()) == 
Entity.EntityType.MODEL_VERSION) {
+      NameIdentifier modelIdent = ((ModelVersionEntity) 
entity).modelIdentifier();
+      invalidate(modelIdent, Entity.EntityType.MODEL);
+    }
+  }

Review Comment:
   Please add comment to the code to explain why do you need this.



##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -54,12 +58,26 @@ public class RelationalEntityStore
           Configs.DEFAULT_ENTITY_RELATIONAL_STORE, 
JDBCBackend.class.getCanonicalName());
   private RelationalBackend backend;
   private RelationalGarbageCollector garbageCollector;
+  private EntityCache cache;
+  private boolean cacheEnabled;
+
+  /**
+   * Return whether the cache is enabled or not.
+   *
+   * @return {@code true} if cache is enable, otherwise {@code false}
+   */
+  public boolean cacheEnabled() {

Review Comment:
   Do you still need this method?



##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -184,7 +217,20 @@ public List<TagEntity> associateTagsWithMetadataObject(
   public <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
       Type relType, NameIdentifier nameIdentifier, Entity.EntityType 
identType, boolean allFields)
       throws IOException {
-    return backend.listEntitiesByRelation(relType, nameIdentifier, identType, 
allFields);
+    return cache.withCacheLock(
+        () -> {
+          Optional<List<E>> entities = cache.getIfPresent(relType, 
nameIdentifier, identType);
+          if (entities.isPresent()) {
+            return entities.get();
+          }
+
+          List<E> backendEntities =
+              backend.listEntitiesByRelation(relType, nameIdentifier, 
identType, allFields);
+
+          cache.put(nameIdentifier, identType, relType, backendEntities);

Review Comment:
   Also you only cache the list relations, not the list operation, what is your 
consideration?



##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -184,7 +217,20 @@ public List<TagEntity> associateTagsWithMetadataObject(
   public <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
       Type relType, NameIdentifier nameIdentifier, Entity.EntityType 
identType, boolean allFields)
       throws IOException {
-    return backend.listEntitiesByRelation(relType, nameIdentifier, identType, 
allFields);
+    return cache.withCacheLock(
+        () -> {
+          Optional<List<E>> entities = cache.getIfPresent(relType, 
nameIdentifier, identType);
+          if (entities.isPresent()) {
+            return entities.get();
+          }
+
+          List<E> backendEntities =
+              backend.listEntitiesByRelation(relType, nameIdentifier, 
identType, allFields);
+
+          cache.put(nameIdentifier, identType, relType, backendEntities);

Review Comment:
   If we want to manage the cache very well, to avoid the problem I mentioned 
above, my feeling is that current kv map is not enough. You need to design your 
own data structure, like linked hashmap, or something else.



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