yuqi1129 commented on code in PR #7200:
URL: https://github.com/apache/gravitino/pull/7200#discussion_r2106685751
##########
core/build.gradle.kts:
##########
@@ -57,6 +59,8 @@ dependencies {
testImplementation(libs.mysql.driver)
testImplementation(libs.postgresql.driver)
testImplementation(libs.testcontainers)
+ testImplementation(libs.jcstress)
+ testAnnotationProcessor(libs.jcstress)
Review Comment:
Can you move this line to L49?
##########
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.expiration.time")
Review Comment:
I mean you can add the unit information in the key like
`gravitino.lock.cleanIntervalInSecs`,
`gravitino.catalog.cache.evictionIntervalMs`, `s3-token-expire-in-secs` and so
on.
##########
core/src/main/java/org/apache/gravitino/cache/BaseEntityCache.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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) {
+ if (metadata instanceof HasIdentifier) {
+ HasIdentifier hasIdentifier = (HasIdentifier) metadata;
+ return hasIdentifier.nameIdentifier();
+ }
+
+ throw new IllegalArgumentException("Unsupported EntityType: " +
metadata.type().getShortName());
Review Comment:
You can also reuse method `validateEntityHasIdentifier` here.
##########
core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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
+ * <li>Catalog: 75
+ * <li>Schema: 50
+ * <li>Other: 15
+ * </ul>
+ */
+public class EntityCacheWeigher implements Weigher<EntityCacheKey,
List<Entity>> {
+ private static final EntityCacheWeigher INSTANCE = new EntityCacheWeigher();
+
+ private EntityCacheWeigher() {}
+
+ public static final int METALAKE_WEIGHT = 100;
+ public static final int CATALOG_WEIGHT = 75;
+ public static final int SCHEMA_WEIGHT = 50;
+ public static final int OTHER_WEIGHT = 15;
+
+ /**
+ * Returns the maximum weight that can be stored in the cache.
+ *
+ * <p>The total weight is estimated based on the expected number of metadata
entities:
+ *
+ * <ul>
+ * <li>~10 Metalakes per Gravitino instance
+ * <li>~200 Catalogs per Metalake
+ * <li>~1000 Schemas per Catalog
+ * </ul>
+ *
+ * <p>The total estimated entity count is:
+ *
+ * <pre>
+ * 10 * METALAKE_WEIGHT
+ * + (10 * 200) * CATALOG_WEIGHT
+ * + (10 * 200 * 1000) * SCHEMA_WEIGHT
+ * </pre>
+ *
+ * <p>To provide headroom and avoid early eviction, the result is multiplied
by 2:
+ *
+ * <pre>
+ * total = 2 * (10 * METALAKE_WEIGHT + 2000 * CATALOG_WEIGHT + 2_000_000 *
SCHEMA_WEIGHT)
+ * </pre>
+ *
+ * @return The maximum weight that can be stored in the cache.
+ */
+ public static long getMaxWeight() {
+ return 2
+ * (METALAKE_WEIGHT * 10 + CATALOG_WEIGHT * (10 * 200) + SCHEMA_WEIGHT
* (10 * 200 * 1000));
+ }
+
+ /**
+ * Returns the singleton instance of the {@link EntityCacheWeigher}.
+ *
+ * @return the singleton instance of the {@link EntityCacheWeigher}.
+ */
+ public static EntityCacheWeigher getInstance() {
+ return INSTANCE;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public @NonNegative int weigh(
+ @NonNull EntityCacheKey storeEntityCacheKey, @NonNull List<Entity>
entities) {
+ int weight = 0;
+ for (Entity entity : entities) {
+ weight += calculateWeight(entity.type());
+ }
+ return weight;
+ }
+
+ private int calculateWeight(Entity.EntityType tp) {
Review Comment:
What the `tp` stands for? **T**y**p**e?
##########
core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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
+ * <li>Catalog: 75
+ * <li>Schema: 50
+ * <li>Other: 15
+ * </ul>
+ */
+public class EntityCacheWeigher implements Weigher<EntityCacheKey,
List<Entity>> {
+ private static final EntityCacheWeigher INSTANCE = new EntityCacheWeigher();
+
+ private EntityCacheWeigher() {}
+
+ public static final int METALAKE_WEIGHT = 100;
+ public static final int CATALOG_WEIGHT = 75;
+ public static final int SCHEMA_WEIGHT = 50;
+ public static final int OTHER_WEIGHT = 15;
+
+ /**
+ * Returns the maximum weight that can be stored in the cache.
+ *
+ * <p>The total weight is estimated based on the expected number of metadata
entities:
+ *
+ * <ul>
+ * <li>~10 Metalakes per Gravitino instance
+ * <li>~200 Catalogs per Metalake
+ * <li>~1000 Schemas per Catalog
+ * </ul>
+ *
+ * <p>The total estimated entity count is:
+ *
+ * <pre>
+ * 10 * METALAKE_WEIGHT
+ * + (10 * 200) * CATALOG_WEIGHT
+ * + (10 * 200 * 1000) * SCHEMA_WEIGHT
+ * </pre>
+ *
+ * <p>To provide headroom and avoid early eviction, the result is multiplied
by 2:
+ *
+ * <pre>
+ * total = 2 * (10 * METALAKE_WEIGHT + 2000 * CATALOG_WEIGHT + 2_000_000 *
SCHEMA_WEIGHT)
+ * </pre>
+ *
+ * @return The maximum weight that can be stored in the cache.
+ */
+ public static long getMaxWeight() {
+ return 2
Review Comment:
Why don't you just make this a constant value?
##########
core/src/main/java/org/apache/gravitino/cache/EntityCacheWeigher.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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
+ * <li>Catalog: 75
+ * <li>Schema: 50
+ * <li>Other: 15
+ * </ul>
+ */
+public class EntityCacheWeigher implements Weigher<EntityCacheKey,
List<Entity>> {
+ private static final EntityCacheWeigher INSTANCE = new EntityCacheWeigher();
+
+ private EntityCacheWeigher() {}
+
+ public static final int METALAKE_WEIGHT = 100;
+ public static final int CATALOG_WEIGHT = 75;
+ public static final int SCHEMA_WEIGHT = 50;
+ public static final int OTHER_WEIGHT = 15;
+
+ /**
+ * Returns the maximum weight that can be stored in the cache.
+ *
+ * <p>The total weight is estimated based on the expected number of metadata
entities:
+ *
+ * <ul>
+ * <li>~10 Metalakes per Gravitino instance
+ * <li>~200 Catalogs per Metalake
+ * <li>~1000 Schemas per Catalog
+ * </ul>
+ *
+ * <p>The total estimated entity count is:
+ *
+ * <pre>
+ * 10 * METALAKE_WEIGHT
+ * + (10 * 200) * CATALOG_WEIGHT
+ * + (10 * 200 * 1000) * SCHEMA_WEIGHT
+ * </pre>
+ *
+ * <p>To provide headroom and avoid early eviction, the result is multiplied
by 2:
+ *
+ * <pre>
+ * total = 2 * (10 * METALAKE_WEIGHT + 2000 * CATALOG_WEIGHT + 2_000_000 *
SCHEMA_WEIGHT)
+ * </pre>
+ *
+ * @return The maximum weight that can be stored in the cache.
+ */
+ public static long getMaxWeight() {
+ return 2
+ * (METALAKE_WEIGHT * 10 + CATALOG_WEIGHT * (10 * 200) + SCHEMA_WEIGHT
* (10 * 200 * 1000));
+ }
+
+ /**
+ * Returns the singleton instance of the {@link EntityCacheWeigher}.
+ *
+ * @return the singleton instance of the {@link EntityCacheWeigher}.
+ */
+ public static EntityCacheWeigher getInstance() {
+ return INSTANCE;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public @NonNegative int weigh(
+ @NonNull EntityCacheKey storeEntityCacheKey, @NonNull List<Entity>
entities) {
+ int weight = 0;
+ for (Entity entity : entities) {
+ weight += calculateWeight(entity.type());
+ }
+ return weight;
Review Comment:
What if the value `weight` is greater than `getMaxWeight`?
--
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]