yuqi1129 commented on code in PR #7354:
URL: https://github.com/apache/gravitino/pull/7354#discussion_r2136841459
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -54,12 +58,28 @@ 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:
This is unused. Is it for future use?
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -137,6 +235,7 @@ public <R, E extends Exception> R
executeInTransaction(Executable<R, E> executab
public void close() throws IOException {
garbageCollector.close();
backend.close();
+ cache.clear();
Review Comment:
Close the cache first and then backend.
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -83,47 +103,125 @@ private static RelationalBackend
createRelationalEntityBackend(Config config) {
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType) throws
IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, false);
+ entities.forEach(cache::put);
+
+ return entities;
+ });
+ }
+
return backend.list(namespace, entityType, false);
}
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType,
boolean allFields)
throws IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, allFields);
+ entities.forEach(cache::put);
+
+ return entities;
+ });
+ }
+
return backend.list(namespace, entityType, allFields);
}
@Override
public boolean exists(NameIdentifier ident, Entity.EntityType entityType)
throws IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ if (cache.contains(ident, entityType)) {
+ return true;
+ }
+
+ return backend.exists(ident, entityType);
Review Comment:
I think this code can be moved out of the cache lock to reduce the race.
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -83,47 +103,125 @@ private static RelationalBackend
createRelationalEntityBackend(Config config) {
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType) throws
IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, false);
+ entities.forEach(cache::put);
+
+ return entities;
+ });
+ }
+
return backend.list(namespace, entityType, false);
}
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType,
boolean allFields)
throws IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, allFields);
Review Comment:
Will we store the entities every time `list` is called? I believe in most
cases, this is unnecessary except for the first one.
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -196,6 +314,15 @@ public void insertRelation(
Entity.EntityType dstType,
boolean override)
throws IOException {
- backend.insertRelation(relType, srcIdentifier, srcType, dstIdentifier,
dstType, true);
+ if (cacheEnabled) {
+ cache.withCacheLock(
+ () -> {
+ cache.invalidate(srcIdentifier, srcType, relType);
+ backend.insertRelation(
Review Comment:
ditto
##########
core/src/test/java/org/apache/gravitino/utils/TestUtil.java:
##########
@@ -0,0 +1,518 @@
+/*
+ * 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.utils;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.Catalog;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.authorization.Privileges;
+import org.apache.gravitino.authorization.SecurableObject;
+import org.apache.gravitino.file.Fileset;
+import org.apache.gravitino.meta.AuditInfo;
+import org.apache.gravitino.meta.BaseMetalake;
+import org.apache.gravitino.meta.CatalogEntity;
+import org.apache.gravitino.meta.ColumnEntity;
+import org.apache.gravitino.meta.FilesetEntity;
+import org.apache.gravitino.meta.GroupEntity;
+import org.apache.gravitino.meta.ModelEntity;
+import org.apache.gravitino.meta.ModelVersionEntity;
+import org.apache.gravitino.meta.RoleEntity;
+import org.apache.gravitino.meta.SchemaEntity;
+import org.apache.gravitino.meta.SchemaVersion;
+import org.apache.gravitino.meta.TableEntity;
+import org.apache.gravitino.meta.TagEntity;
+import org.apache.gravitino.meta.TopicEntity;
+import org.apache.gravitino.meta.UserEntity;
+import org.apache.gravitino.rel.types.Types;
+import org.apache.gravitino.storage.RandomIdGenerator;
+
+/**
+ * Utility class providing factory methods for creating mock and test
instances of various metadata
+ * entities used in unit tests.
+ *
+ * <p>This class includes shortcut methods for constructing entities such as
{@link BaseMetalake},
+ * {@link CatalogEntity}, {@link SchemaEntity}, {@link TableEntity}, {@link
ModelEntity}, {@link
+ * FilesetEntity}, {@link TopicEntity}, {@link TagEntity}, {@link UserEntity},
{@link GroupEntity},
+ * {@link RoleEntity}, and {@link ModelVersionEntity}. It also includes
methods to create mock
+ * {@link ColumnEntity} and {@link SecurableObject} instances for use in
mocking behavior-dependent
+ * components.
+ *
+ * <p>These utilities are intended for testing purposes only and rely on
in-memory mock objects or
+ * randomly generated IDs to simulate real entities.
+ *
+ * <p>Note: This class is not thread-safe.
+ */
+public class TestUtil {
+ // Random ID generator used to generate IDs for test entities.
+ private static RandomIdGenerator generator = new RandomIdGenerator();
+
+ /**
+ * Returns a list of given iterable instance.
+ *
+ * @param iterable The iterable instance to convert to a list.
+ * @return A list of the elements in the iterable.
+ * @param <T> The type of the elements in the iterable.
+ */
+ public static <T> List<T> toList(Iterable<T> iterable) {
Review Comment:
I recall that they are similar tools that can provide this function. please
refer to
```
org.apache.commons.collections4.collect(Lists.newArrayList("a"), a -> a);
```
--
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]