jerryshao commented on code in PR #7200: URL: https://github.com/apache/gravitino/pull/7200#discussion_r2113389246
########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +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.ModelEntity; +import org.apache.gravitino.meta.ModelVersionEntity; +import org.apache.gravitino.meta.RoleEntity; +import org.apache.gravitino.meta.SchemaEntity; +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.storage.relational.RelationalEntityStore; + +/** + * An abstract class that provides a basic implementation for the MetaCache interface. This class is + * abstract and cannot be instantiated directly, it is designed to be a base class for other meta + * cache implementations. + * + * <p>The purpose of the BaseMetaCache is to provide a unified way of accessing entity stores, + * allowing subclasses to focus on caching logic without having to deal with entity store + * management. + */ +public abstract class BaseEntityCache implements EntityCache { + private static final Map<Entity.EntityType, Class<?>> ENTITY_CLASS_MAP; + // The entity store used by the cache, initialized through the constructor. + protected final RelationalEntityStore entityStore; + protected final CacheConfig cacheConfig; + + static { + Map<Entity.EntityType, Class<?>> map = new EnumMap<>(Entity.EntityType.class); + map.put(Entity.EntityType.METALAKE, BaseMetalake.class); + map.put(Entity.EntityType.CATALOG, CatalogEntity.class); + map.put(Entity.EntityType.SCHEMA, SchemaEntity.class); + map.put(Entity.EntityType.TABLE, TableEntity.class); + map.put(Entity.EntityType.FILESET, FilesetEntity.class); + map.put(Entity.EntityType.MODEL, ModelEntity.class); + map.put(Entity.EntityType.TOPIC, TopicEntity.class); + map.put(Entity.EntityType.TAG, TagEntity.class); + map.put(Entity.EntityType.MODEL_VERSION, ModelVersionEntity.class); + map.put(Entity.EntityType.COLUMN, ColumnEntity.class); + map.put(Entity.EntityType.USER, UserEntity.class); + map.put(Entity.EntityType.GROUP, Entity.class); + map.put(Entity.EntityType.ROLE, RoleEntity.class); + ENTITY_CLASS_MAP = Collections.unmodifiableMap(map); + } + + /** + * Returns the class of the entity based on its type. + * + * @param type The entity type + * @return The class of the entity + * @throws IllegalArgumentException if the entity type is not supported + */ + @SuppressWarnings("unchecked") + public static <E extends Entity & HasIdentifier> Class<E> getEntityClass(Entity.EntityType type) { + Preconditions.checkNotNull(type, "EntityType must not be null"); + + Class<?> clazz = ENTITY_CLASS_MAP.get(type); + if (clazz == null) { + throw new IllegalArgumentException("Unsupported EntityType: " + type.getShortName()); + } + + return (Class<E>) clazz; + } + + /** + * Returns the {@link NameIdentifier} of the metadata based on its type. + * + * @param metadata The entity + * @return The {@link NameIdentifier} of the metadata + */ + protected static NameIdentifier getIdentFromMetadata(Entity metadata) { Review Comment: Change the variable name to `entity`, `entity` is an entity, `metadata` is just a concept. ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +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.ModelEntity; +import org.apache.gravitino.meta.ModelVersionEntity; +import org.apache.gravitino.meta.RoleEntity; +import org.apache.gravitino.meta.SchemaEntity; +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.storage.relational.RelationalEntityStore; + +/** + * An abstract class that provides a basic implementation for the MetaCache interface. This class is + * abstract and cannot be instantiated directly, it is designed to be a base class for other meta + * cache implementations. + * + * <p>The purpose of the BaseMetaCache is to provide a unified way of accessing entity stores, + * allowing subclasses to focus on caching logic without having to deal with entity store + * management. + */ +public abstract class BaseEntityCache implements EntityCache { + private static final Map<Entity.EntityType, Class<?>> ENTITY_CLASS_MAP; + // The entity store used by the cache, initialized through the constructor. + protected final RelationalEntityStore entityStore; + protected final CacheConfig cacheConfig; + + static { + Map<Entity.EntityType, Class<?>> map = new EnumMap<>(Entity.EntityType.class); + map.put(Entity.EntityType.METALAKE, BaseMetalake.class); + map.put(Entity.EntityType.CATALOG, CatalogEntity.class); + map.put(Entity.EntityType.SCHEMA, SchemaEntity.class); + map.put(Entity.EntityType.TABLE, TableEntity.class); + map.put(Entity.EntityType.FILESET, FilesetEntity.class); + map.put(Entity.EntityType.MODEL, ModelEntity.class); + map.put(Entity.EntityType.TOPIC, TopicEntity.class); + map.put(Entity.EntityType.TAG, TagEntity.class); + map.put(Entity.EntityType.MODEL_VERSION, ModelVersionEntity.class); + map.put(Entity.EntityType.COLUMN, ColumnEntity.class); + map.put(Entity.EntityType.USER, UserEntity.class); + map.put(Entity.EntityType.GROUP, Entity.class); + map.put(Entity.EntityType.ROLE, RoleEntity.class); + ENTITY_CLASS_MAP = Collections.unmodifiableMap(map); + } + + /** + * Returns the class of the entity based on its type. + * + * @param type The entity type + * @return The class of the entity + * @throws IllegalArgumentException if the entity type is not supported + */ + @SuppressWarnings("unchecked") + public static <E extends Entity & HasIdentifier> Class<E> getEntityClass(Entity.EntityType type) { + Preconditions.checkNotNull(type, "EntityType must not be null"); + + Class<?> clazz = ENTITY_CLASS_MAP.get(type); + if (clazz == null) { + throw new IllegalArgumentException("Unsupported EntityType: " + type.getShortName()); + } + + return (Class<E>) clazz; + } + + /** + * Returns the {@link NameIdentifier} of the metadata based on its type. + * + * @param metadata The entity + * @return The {@link NameIdentifier} of the metadata + */ + protected static NameIdentifier getIdentFromMetadata(Entity metadata) { + validateEntityHasIdentifier(metadata); + HasIdentifier hasIdentifier = (HasIdentifier) metadata; + + return hasIdentifier.nameIdentifier(); + } + + /** + * Checks if the entity is of type {@link HasIdentifier}. + * + * @param entity The entity to check. + */ + protected static void validateEntityHasIdentifier(Entity entity) { + if (!(entity instanceof HasIdentifier)) { + throw new IllegalArgumentException("Unsupported EntityType: " + entity.type().getShortName()); Review Comment: short name is meaningless, you'd use the type as a string directly. ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +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.ModelEntity; +import org.apache.gravitino.meta.ModelVersionEntity; +import org.apache.gravitino.meta.RoleEntity; +import org.apache.gravitino.meta.SchemaEntity; +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.storage.relational.RelationalEntityStore; + +/** + * An abstract class that provides a basic implementation for the MetaCache interface. This class is + * abstract and cannot be instantiated directly, it is designed to be a base class for other meta + * cache implementations. + * + * <p>The purpose of the BaseMetaCache is to provide a unified way of accessing entity stores, + * allowing subclasses to focus on caching logic without having to deal with entity store + * management. + */ +public abstract class BaseEntityCache implements EntityCache { + private static final Map<Entity.EntityType, Class<?>> ENTITY_CLASS_MAP; + // The entity store used by the cache, initialized through the constructor. + protected final RelationalEntityStore entityStore; + protected final CacheConfig cacheConfig; + + static { + Map<Entity.EntityType, Class<?>> map = new EnumMap<>(Entity.EntityType.class); + map.put(Entity.EntityType.METALAKE, BaseMetalake.class); + map.put(Entity.EntityType.CATALOG, CatalogEntity.class); + map.put(Entity.EntityType.SCHEMA, SchemaEntity.class); + map.put(Entity.EntityType.TABLE, TableEntity.class); + map.put(Entity.EntityType.FILESET, FilesetEntity.class); + map.put(Entity.EntityType.MODEL, ModelEntity.class); + map.put(Entity.EntityType.TOPIC, TopicEntity.class); + map.put(Entity.EntityType.TAG, TagEntity.class); + map.put(Entity.EntityType.MODEL_VERSION, ModelVersionEntity.class); + map.put(Entity.EntityType.COLUMN, ColumnEntity.class); + map.put(Entity.EntityType.USER, UserEntity.class); + map.put(Entity.EntityType.GROUP, Entity.class); + map.put(Entity.EntityType.ROLE, RoleEntity.class); + ENTITY_CLASS_MAP = Collections.unmodifiableMap(map); + } + + /** + * Returns the class of the entity based on its type. + * + * @param type The entity type + * @return The class of the entity + * @throws IllegalArgumentException if the entity type is not supported + */ + @SuppressWarnings("unchecked") + public static <E extends Entity & HasIdentifier> Class<E> getEntityClass(Entity.EntityType type) { + Preconditions.checkNotNull(type, "EntityType must not be null"); + + Class<?> clazz = ENTITY_CLASS_MAP.get(type); + if (clazz == null) { + throw new IllegalArgumentException("Unsupported EntityType: " + type.getShortName()); + } + + return (Class<E>) clazz; + } + + /** + * Returns the {@link NameIdentifier} of the metadata based on its type. + * + * @param metadata The entity + * @return The {@link NameIdentifier} of the metadata + */ + protected static NameIdentifier getIdentFromMetadata(Entity metadata) { + validateEntityHasIdentifier(metadata); + HasIdentifier hasIdentifier = (HasIdentifier) metadata; + + return hasIdentifier.nameIdentifier(); + } + + /** + * Checks if the entity is of type {@link HasIdentifier}. + * + * @param entity The entity to check. + */ + protected static void validateEntityHasIdentifier(Entity entity) { + if (!(entity instanceof HasIdentifier)) { + throw new IllegalArgumentException("Unsupported EntityType: " + entity.type().getShortName()); + } + } + + /** + * Converts a list of entities to a new list. + * + * @param entities Thr original list of entities. + * @return A list of converted entities. + * @param <E> The type of the entities in the list. + */ + @SuppressWarnings("unchecked") + protected static <E extends Entity & HasIdentifier> List<E> convertEntity(List<Entity> entities) { + entities.forEach(BaseEntityCache::validateEntityHasIdentifier); + + return (List<E>) (List<? extends Entity>) entities; + } + + /** + * Converts an entity to a new entity. + * + * @param entity The original entity. + * @return A new entity. + * @param <E> The type of the entity. + */ + @SuppressWarnings("unchecked") + protected static <E extends Entity & HasIdentifier> E convertEntity(Entity entity) { + validateEntityHasIdentifier(entity); + + return (E) entity; + } + + /** + * Constructs a new {@link BaseEntityCache} instance. If the provided entityStore is null, it will + * use the entity store configured in the Gravitino environment. + * + * @param entityStore The entity store to be used by the cache, can be null. + */ + public BaseEntityCache(CacheConfig cacheConfig, EntityStore entityStore) { + this.cacheConfig = cacheConfig; + this.entityStore = (RelationalEntityStore) entityStore; + } Review Comment: Move the public constructor to the top of the method. ########## core/src/main/java/org/apache/gravitino/cache/CacheConfig.java: ########## @@ -0,0 +1,158 @@ +/* + * 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.concurrent.TimeUnit; +import org.apache.gravitino.Config; +import org.apache.gravitino.config.ConfigBuilder; +import org.apache.gravitino.config.ConfigConstants; +import org.apache.gravitino.config.ConfigEntry; + +/** + * Cache configuration class, inheriting from Config. This class defines configuration entries + * related to caching, including whether to use a weighted cache, cache size limits, and cache + * expiration settings. + */ +public class CacheConfig extends Config { + // Maximum number of entries in the cache + public static final ConfigEntry<Integer> CACHE_MAX_SIZE = + new ConfigBuilder("gravitino.server.cache.max.num") + .doc("The max size of the cache in number of entries.") + .version(ConfigConstants.VERSION_0_10_0) + .intConf() + .checkValue(value -> value > 0, ConfigConstants.POSITIVE_NUMBER_ERROR_MSG) + .createWithDefault(10_000); + + // Whether to enable cache expiration + public static final ConfigEntry<Boolean> CACHE_EXPIRATION_ENABLED = + new ConfigBuilder("gravitino.server.cache.expiration.enabled") + .doc("Whether to enable cache expiration.") + .version(ConfigConstants.VERSION_0_10_0) + .booleanConf() + .createWithDefault(true); + + // Cache entry expiration time + public static final ConfigEntry<Long> CACHE_EXPIRATION_TIME = + new ConfigBuilder("gravitino.server.cache.expirationTimeMin") + .doc("The time after which cache entries expire. default is 60 minutes.") + .version(ConfigConstants.VERSION_0_10_0) + .longConf() + .checkValue(value -> value > 0, ConfigConstants.POSITIVE_NUMBER_ERROR_MSG) + .createWithDefault(60L); + + // Whether to enable cache status + public static final ConfigEntry<Boolean> CACHE_STATUS_ENABLED = + new ConfigBuilder("gravitino.server.cache.status.log.enabled") + .doc( + "Whether to collect and log cache status. if enabled, cache status will be collected and logged.") + .version(ConfigConstants.VERSION_0_10_0) + .booleanConf() + .createWithDefault(false); + + // Whether to enable weighted cache + public static final ConfigEntry<Boolean> CACHE_WEIGHER_ENABLED = + new ConfigBuilder("gravitino.server.cache.weigher.enabled") + .doc( + "Whether to enable weighted cache. if enabled, cache entries will be weighed based on their weight, not" Review Comment: I guess this line exceeds 100 characters, can you check and break the line. ########## core/src/main/java/org/apache/gravitino/storage/relational/CachedEntityStore.java: ########## @@ -0,0 +1,254 @@ +/* + * 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; + +/** Cached Entity store, which caches metadata in memory. */ +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.function.Function; +import org.apache.gravitino.Config; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityAlreadyExistsException; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.SupportsRelationOperations; +import org.apache.gravitino.cache.EntityCache; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.meta.TagEntity; +import org.apache.gravitino.tag.SupportsTagOperations; +import org.apache.gravitino.utils.Executable; + +public class CachedEntityStore Review Comment: I think this part can be moved to another PR to decrease the size of this PR. Also, it is unrelated to the cache implementation in this PR. ########## core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java: ########## @@ -0,0 +1,481 @@ +/* + * 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 com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.RemovalCause; +import com.github.benmanes.caffeine.cache.stats.CacheStats; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree; +import com.googlecode.concurrenttrees.radix.RadixTree; +import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFactory; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.SupportsRelationOperations; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** This class implements a meta cache using Caffeine cache. */ +public class CaffeineEntityCache extends BaseEntityCache { + private static final Logger LOG = LoggerFactory.getLogger(CaffeineEntityCache.class.getName()); + + /** Singleton instance */ + private static volatile CaffeineEntityCache INSTANCE = null; + + /** Cache part */ + private final Cache<EntityCacheKey, List<Entity>> cacheData; + + /** Index part */ + private RadixTree<EntityCacheKey> cacheIndex; + + private final ReentrantLock opLock = new ReentrantLock(); + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); + + @VisibleForTesting + public static void resetForTest() { Review Comment: If it is only for test, you can make it package public. ########## core/src/main/java/org/apache/gravitino/cache/CacheConfig.java: ########## @@ -0,0 +1,158 @@ +/* + * 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.concurrent.TimeUnit; +import org.apache.gravitino.Config; +import org.apache.gravitino.config.ConfigBuilder; +import org.apache.gravitino.config.ConfigConstants; +import org.apache.gravitino.config.ConfigEntry; + +/** + * Cache configuration class, inheriting from Config. This class defines configuration entries + * related to caching, including whether to use a weighted cache, cache size limits, and cache + * expiration settings. + */ +public class CacheConfig extends Config { + // Maximum number of entries in the cache + public static final ConfigEntry<Integer> CACHE_MAX_SIZE = + new ConfigBuilder("gravitino.server.cache.max.num") + .doc("The max size of the cache in number of entries.") + .version(ConfigConstants.VERSION_0_10_0) + .intConf() + .checkValue(value -> value > 0, ConfigConstants.POSITIVE_NUMBER_ERROR_MSG) + .createWithDefault(10_000); + + // Whether to enable cache expiration + public static final ConfigEntry<Boolean> CACHE_EXPIRATION_ENABLED = + new ConfigBuilder("gravitino.server.cache.expiration.enabled") + .doc("Whether to enable cache expiration.") + .version(ConfigConstants.VERSION_0_10_0) + .booleanConf() + .createWithDefault(true); + + // Cache entry expiration time + public static final ConfigEntry<Long> CACHE_EXPIRATION_TIME = + new ConfigBuilder("gravitino.server.cache.expirationTimeMin") Review Comment: expireTimeInMs, better to use MS so you can easily trigger the test. ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +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.ModelEntity; +import org.apache.gravitino.meta.ModelVersionEntity; +import org.apache.gravitino.meta.RoleEntity; +import org.apache.gravitino.meta.SchemaEntity; +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.storage.relational.RelationalEntityStore; + +/** + * An abstract class that provides a basic implementation for the MetaCache interface. This class is + * abstract and cannot be instantiated directly, it is designed to be a base class for other meta + * cache implementations. + * + * <p>The purpose of the BaseMetaCache is to provide a unified way of accessing entity stores, + * allowing subclasses to focus on caching logic without having to deal with entity store + * management. + */ +public abstract class BaseEntityCache implements EntityCache { + private static final Map<Entity.EntityType, Class<?>> ENTITY_CLASS_MAP; + // The entity store used by the cache, initialized through the constructor. + protected final RelationalEntityStore entityStore; + protected final CacheConfig cacheConfig; + + static { + Map<Entity.EntityType, Class<?>> map = new EnumMap<>(Entity.EntityType.class); + map.put(Entity.EntityType.METALAKE, BaseMetalake.class); + map.put(Entity.EntityType.CATALOG, CatalogEntity.class); + map.put(Entity.EntityType.SCHEMA, SchemaEntity.class); + map.put(Entity.EntityType.TABLE, TableEntity.class); + map.put(Entity.EntityType.FILESET, FilesetEntity.class); + map.put(Entity.EntityType.MODEL, ModelEntity.class); + map.put(Entity.EntityType.TOPIC, TopicEntity.class); + map.put(Entity.EntityType.TAG, TagEntity.class); + map.put(Entity.EntityType.MODEL_VERSION, ModelVersionEntity.class); + map.put(Entity.EntityType.COLUMN, ColumnEntity.class); + map.put(Entity.EntityType.USER, UserEntity.class); + map.put(Entity.EntityType.GROUP, Entity.class); + map.put(Entity.EntityType.ROLE, RoleEntity.class); + ENTITY_CLASS_MAP = Collections.unmodifiableMap(map); + } + + /** + * Returns the class of the entity based on its type. + * + * @param type The entity type + * @return The class of the entity + * @throws IllegalArgumentException if the entity type is not supported + */ + @SuppressWarnings("unchecked") + public static <E extends Entity & HasIdentifier> Class<E> getEntityClass(Entity.EntityType type) { + Preconditions.checkNotNull(type, "EntityType must not be null"); + + Class<?> clazz = ENTITY_CLASS_MAP.get(type); + if (clazz == null) { + throw new IllegalArgumentException("Unsupported EntityType: " + type.getShortName()); + } + + return (Class<E>) clazz; + } + + /** + * Returns the {@link NameIdentifier} of the metadata based on its type. + * + * @param metadata The entity + * @return The {@link NameIdentifier} of the metadata + */ + protected static NameIdentifier getIdentFromMetadata(Entity metadata) { + validateEntityHasIdentifier(metadata); + HasIdentifier hasIdentifier = (HasIdentifier) metadata; + + return hasIdentifier.nameIdentifier(); + } + + /** + * Checks if the entity is of type {@link HasIdentifier}. + * + * @param entity The entity to check. + */ + protected static void validateEntityHasIdentifier(Entity entity) { + if (!(entity instanceof HasIdentifier)) { + throw new IllegalArgumentException("Unsupported EntityType: " + entity.type().getShortName()); Review Comment: Also you can simplify with `Preconditions.xxx`. ########## core/src/main/java/org/apache/gravitino/cache/CacheConfig.java: ########## @@ -0,0 +1,158 @@ +/* + * 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.concurrent.TimeUnit; +import org.apache.gravitino.Config; +import org.apache.gravitino.config.ConfigBuilder; +import org.apache.gravitino.config.ConfigConstants; +import org.apache.gravitino.config.ConfigEntry; + +/** + * Cache configuration class, inheriting from Config. This class defines configuration entries + * related to caching, including whether to use a weighted cache, cache size limits, and cache + * expiration settings. + */ +public class CacheConfig extends Config { + // Maximum number of entries in the cache + public static final ConfigEntry<Integer> CACHE_MAX_SIZE = + new ConfigBuilder("gravitino.server.cache.max.num") Review Comment: We don't need 'server' in the configure name, also we typically use 3 level name, like "gravitino.cache.maxEntries", besides, it is better to put all the configs to `Configs` class, no need to create a class specifically. ########## core/src/test/java/org/apache/gravitino/cache/TestCacheConfig.java: ########## @@ -0,0 +1,39 @@ +/* + * 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.concurrent.TimeUnit; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestCacheConfig { + @Test + void testDefaultCacheConfig() { + CacheConfig cacheConfig = new CacheConfig(); + + Assertions.assertEquals(10_000, cacheConfig.getMaxSize()); + Assertions.assertTrue(cacheConfig.isExpirationEnabled()); + Assertions.assertEquals(60, cacheConfig.getExpirationTime()); + Assertions.assertEquals(TimeUnit.MINUTES, cacheConfig.getExpirationTimeUnit()); + Assertions.assertFalse(cacheConfig.isCacheStatusEnabled()); + Assertions.assertTrue(cacheConfig.isWeigherEnabled()); + Assertions.assertEquals(200_302_000, cacheConfig.getMaxWeight()); + } +} Review Comment: You don't have tests for caffeine cache, only add the trivial tests for configuration, the test coverage is not enough. ########## gradle/libs.versions.toml: ########## @@ -282,6 +284,9 @@ aliyun-credentials-sdk = { group='com.aliyun', name='credentials-java', version. flinkjdbc = {group='org.apache.flink',name='flink-connector-jdbc', version.ref='flinkjdbc'} openlineage-java= { group = "io.openlineage", name = "openlineage-java", version.ref = "openlineage" } +concurrent-trees = {group="com.googlecode.concurrent-trees", name="concurrent-trees", version.ref="concurrent-trees"} +jcstress = {group="org.openjdk.jcstress", name="jcstress-core", version.ref="jcstress"} Review Comment: Leave the whitespace before and after like above. ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,166 @@ +/* + * 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 com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.NameIdentifier; +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.ModelEntity; +import org.apache.gravitino.meta.ModelVersionEntity; +import org.apache.gravitino.meta.RoleEntity; +import org.apache.gravitino.meta.SchemaEntity; +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.storage.relational.RelationalEntityStore; + +/** + * An abstract class that provides a basic implementation for the MetaCache interface. This class is + * abstract and cannot be instantiated directly, it is designed to be a base class for other meta + * cache implementations. + * + * <p>The purpose of the BaseMetaCache is to provide a unified way of accessing entity stores, + * allowing subclasses to focus on caching logic without having to deal with entity store + * management. + */ +public abstract class BaseEntityCache implements EntityCache { + private static final Map<Entity.EntityType, Class<?>> ENTITY_CLASS_MAP; + // The entity store used by the cache, initialized through the constructor. + protected final RelationalEntityStore entityStore; + protected final CacheConfig cacheConfig; + + static { + Map<Entity.EntityType, Class<?>> map = new EnumMap<>(Entity.EntityType.class); + map.put(Entity.EntityType.METALAKE, BaseMetalake.class); + map.put(Entity.EntityType.CATALOG, CatalogEntity.class); + map.put(Entity.EntityType.SCHEMA, SchemaEntity.class); + map.put(Entity.EntityType.TABLE, TableEntity.class); + map.put(Entity.EntityType.FILESET, FilesetEntity.class); + map.put(Entity.EntityType.MODEL, ModelEntity.class); + map.put(Entity.EntityType.TOPIC, TopicEntity.class); + map.put(Entity.EntityType.TAG, TagEntity.class); + map.put(Entity.EntityType.MODEL_VERSION, ModelVersionEntity.class); + map.put(Entity.EntityType.COLUMN, ColumnEntity.class); + map.put(Entity.EntityType.USER, UserEntity.class); + map.put(Entity.EntityType.GROUP, Entity.class); + map.put(Entity.EntityType.ROLE, RoleEntity.class); + ENTITY_CLASS_MAP = Collections.unmodifiableMap(map); + } + + /** + * Returns the class of the entity based on its type. + * + * @param type The entity type + * @return The class of the entity + * @throws IllegalArgumentException if the entity type is not supported + */ + @SuppressWarnings("unchecked") + public static <E extends Entity & HasIdentifier> Class<E> getEntityClass(Entity.EntityType type) { + Preconditions.checkNotNull(type, "EntityType must not be null"); Review Comment: Don't use `checkNotNull`, it will throw NPE, I think I mentioned in different PR before. -- 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]
