xunliu commented on code in PR #7200: URL: https://github.com/apache/gravitino/pull/7200#discussion_r2103594106
########## core/src/main/java/org/apache/gravitino/cache/CacheConfig.java: ########## @@ -0,0 +1,187 @@ +/* + * 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; + +/* + * 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. + */ + +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 { + Review Comment: Please remote this blank line ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,179 @@ +/* + * 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 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.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<?> aClass = ENTITY_CLASS_MAP.get(type); Review Comment: Maybe the `clazz` is better than `aClass`? ########## core/src/main/java/org/apache/gravitino/cache/CacheConfig.java: ########## @@ -0,0 +1,187 @@ +/* + * 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; + +/* + * 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. + */ Review Comment: Please remove this duplicate license header. ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,179 @@ +/* + * 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 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.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<?> aClass = ENTITY_CLASS_MAP.get(type); + if (aClass == null) { + throw new IllegalArgumentException("Unsupported EntityType: " + type.getShortName()); + } + + return (Class<E>) aClass; + } + + /** + * 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: Please remove this blank line. ########## core/src/main/java/org/apache/gravitino/cache/CacheConfig.java: ########## @@ -0,0 +1,187 @@ +/* + * 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; + +/* + * 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. + */ + +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.size") + .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.expiration.time") + .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" + + " size.") + .version(ConfigConstants.VERSION_0_10_0) + .booleanConf() + .createWithDefault(true); + + // Maximum weight of cache entries + public static final ConfigEntry<Long> CACHE_MAX_WEIGHT = + new ConfigBuilder("gravitino.server.cache.max.weight") + .doc("The maximum weight of cache entries. default is 10000.") + .version(ConfigConstants.VERSION_0_10_0) + .longConf() + .checkValue(value -> value > 0, ConfigConstants.POSITIVE_NUMBER_ERROR_MSG) + .createWithDefault(EntityCacheWeigher.getMaxWeight()); Review Comment: I'm not sure we need this configuration. ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,179 @@ +/* + * 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 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.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<?> aClass = ENTITY_CLASS_MAP.get(type); + if (aClass == null) { + throw new IllegalArgumentException("Unsupported EntityType: " + type.getShortName()); + } + + return (Class<E>) aClass; + } + + /** + * 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) { + + if (metadata instanceof HasIdentifier) { + HasIdentifier hasIdentifier = (HasIdentifier) metadata; + return hasIdentifier.nameIdentifier(); + } + + throw new IllegalArgumentException("Unsupported EntityType: " + metadata.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> convertSafe(List<Entity> entities) { + for (Entity e : entities) { + if (!(e instanceof HasIdentifier)) { + throw new IllegalStateException( + "Cached entity " + e + " is not of expected type: " + HasIdentifier.class.getName()); + } + } + + 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 convertSafe(Entity entity) { + if (!(entity instanceof HasIdentifier)) { + throw new IllegalStateException( + "Cached entity " + entity + " is not of expected type: " + HasIdentifier.class.getName()); + } + + return (E) entity; + } Review Comment: I think maybe the `convertEntity` is better than `converSafe` ########## core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java: ########## @@ -0,0 +1,458 @@ +/* + * 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.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.List; +import java.util.Optional; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Supplier; +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(); + + @VisibleForTesting + public static void resetForTest() { + INSTANCE = null; + } + + /** + * Returns the instance of MetaCacheCaffeine based on the cache configuration and entity store. + * + * @param cacheConfig The cache configuration + * @param entityStore The entity store to load entities from the database + * @return The instance of {@link CaffeineEntityCache} + */ + public static CaffeineEntityCache getInstance(CacheConfig cacheConfig, EntityStore entityStore) { + if (INSTANCE == null) { + synchronized (CaffeineEntityCache.class) { + if (INSTANCE == null) { + INSTANCE = new CaffeineEntityCache(cacheConfig, entityStore); + } + } + } + return INSTANCE; + } + + /** + * Returns the instance of MetaCacheCaffeine, if it is initialized, otherwise throws an exception. + * + * @return If INSTANCE initialized, returns the instance, otherwise throws an exception. + */ + public static CaffeineEntityCache getInstance() { + if (INSTANCE == null) { + throw new RuntimeException("Illegal state: instance not initialized"); + } + + return INSTANCE; + } + + /** + * Constructs a new MetaCacheCaffeine. + * + * @param cacheConfig the cache configuration + * @param entityStore The entity store to load entities from the database + */ + private CaffeineEntityCache(CacheConfig cacheConfig, EntityStore entityStore) { + super(cacheConfig, entityStore); + cacheIndex = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory()); + + /** + * Executor for async cache cleanup, when a cache expires then use this executor to sync other + * cache and index trees + */ + ThreadPoolExecutor cleanupExec = + new ThreadPoolExecutor( + 1, + 1, + 0L, + TimeUnit.MILLISECONDS, + new ArrayBlockingQueue<>(100), + r -> { + Thread t = new Thread(r, "CaffeineMetaCache-Cleanup"); + t.setDaemon(true); + return t; + }, + new ThreadPoolExecutor.CallerRunsPolicy()); + + Caffeine<EntityCacheKey, List<Entity>> cacheDataBuilder = newBaseBuilder(cacheConfig); + + cacheDataBuilder + .executor(cleanupExec) + .removalListener( + (key, value, cause) -> { + if (cause != RemovalCause.EXPIRED) { + return; + } + try { + invalidateExpiredItem(key); + } catch (Throwable t) { + LOG.error("Async removal failed for {}", value, t); + } + }); + + this.cacheData = cacheDataBuilder.build(); + } + + /** {@inheritDoc} */ + @Override + public <E extends Entity & HasIdentifier> List<E> getOrLoad( + NameIdentifier ident, Entity.EntityType type, SupportsRelationOperations.Type relType) + throws IOException { + Preconditions.checkArgument(ident != null, "NameIdentifier cannot be null"); + Preconditions.checkArgument(type != null, "EntityType cannot be null"); + Preconditions.checkArgument(relType != null, "SupportsRelationOperations.Type cannot be null"); + + return withLockAndThrow( + () -> { + EntityCacheKey entityCacheKey = EntityCacheKey.of(ident, type, relType); + List<Entity> entitiesFromCache = cacheData.getIfPresent(entityCacheKey); + + if (entitiesFromCache != null) { + return convertSafe(entitiesFromCache); + } + + List<E> entities = entityStore.listEntitiesByRelation(relType, ident, type); + syncEntitiesToCache(entityCacheKey, toEntityList(entities)); + + return entities; + }); + } + + /** {@inheritDoc} */ + @Override + public <E extends Entity & HasIdentifier> E getOrLoad( + NameIdentifier ident, Entity.EntityType type) throws IOException { + Preconditions.checkArgument(ident != null, "NameIdentifier cannot be null"); + Preconditions.checkArgument(type != null, "EntityType cannot be null"); + + return withLockAndThrow( + () -> { + EntityCacheKey entityCacheKey = EntityCacheKey.of(ident, type); + List<Entity> entitiesFromCache = cacheData.getIfPresent(entityCacheKey); + + if (entitiesFromCache != null) { + return convertSafe(entitiesFromCache.get(0)); + } + + E entityFromDb = entityStore.get(ident, type, getEntityClass(type)); Review Comment: `entityFromStore` ########## core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java: ########## @@ -0,0 +1,458 @@ +/* + * 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.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.List; +import java.util.Optional; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Supplier; +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(); + + @VisibleForTesting + public static void resetForTest() { + INSTANCE = null; + } + + /** + * Returns the instance of MetaCacheCaffeine based on the cache configuration and entity store. + * + * @param cacheConfig The cache configuration + * @param entityStore The entity store to load entities from the database + * @return The instance of {@link CaffeineEntityCache} + */ + public static CaffeineEntityCache getInstance(CacheConfig cacheConfig, EntityStore entityStore) { + if (INSTANCE == null) { + synchronized (CaffeineEntityCache.class) { + if (INSTANCE == null) { + INSTANCE = new CaffeineEntityCache(cacheConfig, entityStore); + } + } + } + return INSTANCE; + } + + /** + * Returns the instance of MetaCacheCaffeine, if it is initialized, otherwise throws an exception. + * + * @return If INSTANCE initialized, returns the instance, otherwise throws an exception. + */ + public static CaffeineEntityCache getInstance() { + if (INSTANCE == null) { + throw new RuntimeException("Illegal state: instance not initialized"); + } + + return INSTANCE; + } + + /** + * Constructs a new MetaCacheCaffeine. + * + * @param cacheConfig the cache configuration + * @param entityStore The entity store to load entities from the database + */ + private CaffeineEntityCache(CacheConfig cacheConfig, EntityStore entityStore) { + super(cacheConfig, entityStore); + cacheIndex = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory()); + + /** + * Executor for async cache cleanup, when a cache expires then use this executor to sync other + * cache and index trees + */ + ThreadPoolExecutor cleanupExec = + new ThreadPoolExecutor( + 1, + 1, + 0L, + TimeUnit.MILLISECONDS, + new ArrayBlockingQueue<>(100), + r -> { + Thread t = new Thread(r, "CaffeineMetaCache-Cleanup"); + t.setDaemon(true); + return t; + }, + new ThreadPoolExecutor.CallerRunsPolicy()); + + Caffeine<EntityCacheKey, List<Entity>> cacheDataBuilder = newBaseBuilder(cacheConfig); + + cacheDataBuilder + .executor(cleanupExec) + .removalListener( + (key, value, cause) -> { + if (cause != RemovalCause.EXPIRED) { + return; + } + try { + invalidateExpiredItem(key); + } catch (Throwable t) { + LOG.error("Async removal failed for {}", value, t); Review Comment: This log message is too vague ########## core/src/main/java/org/apache/gravitino/cache/CacheConfig.java: ########## @@ -0,0 +1,187 @@ +/* + * 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; + +/* + * 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. + */ + +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.size") Review Comment: Maybe the `gravitino.server.cache.max.num` is better than `gravitino.server.cache.max.size`, because `size` lets us think of memory size? ########## core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java: ########## @@ -0,0 +1,179 @@ +/* + * 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 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.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<?> aClass = ENTITY_CLASS_MAP.get(type); + if (aClass == null) { + throw new IllegalArgumentException("Unsupported EntityType: " + type.getShortName()); + } + + return (Class<E>) aClass; + } + + /** + * 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) { + + if (metadata instanceof HasIdentifier) { + HasIdentifier hasIdentifier = (HasIdentifier) metadata; + return hasIdentifier.nameIdentifier(); + } + + throw new IllegalArgumentException("Unsupported EntityType: " + metadata.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> convertSafe(List<Entity> entities) { + for (Entity e : entities) { + if (!(e instanceof HasIdentifier)) { + throw new IllegalStateException( + "Cached entity " + e + " is not of expected type: " + HasIdentifier.class.getName()); + } + } + + 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 convertSafe(Entity entity) { + if (!(entity instanceof HasIdentifier)) { + throw new IllegalStateException( + "Cached entity " + entity + " is not of expected type: " + HasIdentifier.class.getName()); + } + + return (E) entity; + } + + /** + * Converts a list of entities to a list of entities. + * + * @param sourceList The original list of entities. + * @return A list of entities. + * @param <E> The type of the elements in the list. + */ + protected static <E extends Entity & HasIdentifier> List<Entity> toEntityList( + List<E> sourceList) { + return sourceList.stream().map(e -> (Entity) e).collect(Collectors.toList()); + } Review Comment: I find the `toEntityList()` only calls once, I think we didn't need to create a function, and this function is very simple ########## core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.java: ########## @@ -0,0 +1,103 @@ +/* + * 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.Weigher; +import java.util.List; +import lombok.NonNull; +import org.apache.gravitino.Entity; +import org.checkerframework.checker.index.qual.NonNegative; + +/** + * A {@link Weigher} implementation that calculates the weight of an entity based on its type. The + * weight is calculated as follows: + * + * <ul> + * <li>Metalake: 100 bytes + * <li>Catalog: 75 bytes + * <li>Schema: 50 bytes + * <li>Other: 15 bytes Review Comment: What does Metalake: 100` bytes`? You mean is percentage? ########## core/src/main/java/org/apache/gravitino/cache/CaffeineEntityCache.java: ########## @@ -0,0 +1,458 @@ +/* + * 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.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.List; +import java.util.Optional; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Supplier; +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(); + + @VisibleForTesting + public static void resetForTest() { + INSTANCE = null; + } + + /** + * Returns the instance of MetaCacheCaffeine based on the cache configuration and entity store. + * + * @param cacheConfig The cache configuration + * @param entityStore The entity store to load entities from the database + * @return The instance of {@link CaffeineEntityCache} + */ + public static CaffeineEntityCache getInstance(CacheConfig cacheConfig, EntityStore entityStore) { + if (INSTANCE == null) { + synchronized (CaffeineEntityCache.class) { + if (INSTANCE == null) { + INSTANCE = new CaffeineEntityCache(cacheConfig, entityStore); + } + } + } + return INSTANCE; + } + + /** + * Returns the instance of MetaCacheCaffeine, if it is initialized, otherwise throws an exception. + * + * @return If INSTANCE initialized, returns the instance, otherwise throws an exception. + */ + public static CaffeineEntityCache getInstance() { + if (INSTANCE == null) { + throw new RuntimeException("Illegal state: instance not initialized"); + } + + return INSTANCE; + } Review Comment: I think we didn't need this function. -- 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]
