dimas-b commented on code in PR #490:
URL: https://github.com/apache/polaris/pull/490#discussion_r2033225171


##########
polaris-core/src/test/java/org/apache/polaris/core/persistence/cache/EntityCacheTest.java:
##########
@@ -478,4 +481,26 @@ void testRenameAndCacheDestinationBeforeLoadingSource() {
     // now the loading by the old name should return null
     Assertions.assertThat(cache.getOrLoadEntityByName(callCtx, 
T4_name)).isNull();
   }
+
+  /* Helper for `testEntityWeigher` */
+  private int getEntityWeight(PolarisEntity entity) {
+    return EntityWeigher.getInstance()
+        .weigh(-1L, new ResolvedPolarisEntity(diagServices, entity, List.of(), 
1));
+  }
+
+  @Test
+  void testEntityWeigher() {
+    var smallEntity = new 
IcebergTableLikeEntity.Builder(TableIdentifier.of("ns.t1"), "").build();
+    var mediumEntity =
+        new IcebergTableLikeEntity.Builder(TableIdentifier.of("ns.t1"), "")
+            .setMetadataLocation("a".repeat(10000))
+            .build();
+    var largeEntity =
+        new IcebergTableLikeEntity.Builder(TableIdentifier.of("ns.t1"), "")
+            .setMetadataLocation("a".repeat(1000 * 1000))
+            .build();
+
+    
Assertions.assertThat(getEntityWeight(smallEntity)).isLessThan(getEntityWeight(mediumEntity));
+    
Assertions.assertThat(getEntityWeight(mediumEntity)).isLessThan(getEntityWeight(largeEntity));

Review Comment:
   IMHO this is not a very useful test. If we want to make sure the weigher 
takes specific entity fields into account we should probably do that directly 
in a weigher-specific unit test, and using exact comparisons (white-box test).



##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/EntityWeigher.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.polaris.core.persistence.cache;
+
+import com.github.benmanes.caffeine.cache.Weigher;
+import org.apache.polaris.core.persistence.ResolvedPolarisEntity;
+import org.checkerframework.checker.index.qual.NonNegative;
+
+/**
+ * A {@link Weigher} implementation that weighs {@link ResolvedPolarisEntity} 
objects by the
+ * approximate size of the entity object.
+ */
+public class EntityWeigher implements Weigher<Long, ResolvedPolarisEntity> {
+
+  /** The amount of weight that is expected to roughly equate to 1MB of memory 
usage */
+  public static final long WEIGHT_PER_MB = 1024 * 1024;

Review Comment:
   Do we need this constant anymore? Why not inline its value in the config 
default?



##########
polaris-core/src/main/java/org/apache/polaris/core/config/FeatureConfiguration.java:
##########
@@ -183,4 +184,11 @@ protected FeatureConfiguration(
               "How many times to retry refreshing metadata when the previous 
error was retryable")
           .defaultValue(2)
           .buildFeatureConfiguration();
+
+  public static final FeatureConfiguration<Long> ENTITY_CACHE_WEIGHER_TARGET =
+      PolarisConfiguration.<Long>builder()
+          .key("ENTITY_CACHE_WEIGHER_TARGET")
+          .description("The target weight for the entity cache.")

Review Comment:
   Given the current weigher impl., how about this message: `The maximum weight 
for the entity cache. This is a heuristic value without any particular unit of 
measurement. It roughly correlates with the total heap size of cached values. 
Fine-tuning requires experimentation in the specific deployment environment`.



##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/EntityWeigher.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.polaris.core.persistence.cache;
+
+import com.github.benmanes.caffeine.cache.Weigher;
+import org.apache.polaris.core.persistence.ResolvedPolarisEntity;
+import org.checkerframework.checker.index.qual.NonNegative;
+
+/**
+ * A {@link Weigher} implementation that weighs {@link ResolvedPolarisEntity} 
objects by the
+ * approximate size of the entity object.
+ */
+public class EntityWeigher implements Weigher<Long, ResolvedPolarisEntity> {
+
+  /** The amount of weight that is expected to roughly equate to 1MB of memory 
usage */
+  public static final long WEIGHT_PER_MB = 1024 * 1024;
+
+  /* Represents the approximate size of an entity beyond the properties */
+  private static final int APPROXIMATE_ENTITY_OVERHEAD = 1000;
+
+  /* Represents the amount of bytes that a character is expected to take up */
+  private static final int APPROXIMATE_BYTES_PER_CHAR = 3;
+
+  /** Singleton instance */
+  private static final EntityWeigher instance = new EntityWeigher();
+
+  private EntityWeigher() {}
+
+  /** Gets the singleton {@link EntityWeigher} */
+  public static EntityWeigher getInstance() {
+    return instance;
+  }
+
+  /**
+   * Computes the weight of a given entity. The unit here is not exactly 
bytes, but it's close.
+   *
+   * @param key The entity's key; not used
+   * @param value The entity to be cached
+   * @return The weight of the entity
+   */
+  @Override
+  public @NonNegative int weigh(Long key, ResolvedPolarisEntity value) {
+    return APPROXIMATE_ENTITY_OVERHEAD
+        + (value.getEntity().getName().length() * APPROXIMATE_BYTES_PER_CHAR)
+        + (value.getEntity().getProperties().length() * 
APPROXIMATE_BYTES_PER_CHAR)
+        + (value.getEntity().getInternalProperties().length() * 
APPROXIMATE_BYTES_PER_CHAR);

Review Comment:
   I think we can leave this "as is" for now. Putting a more general comment in 
the main thread.



##########
polaris-core/src/main/java/org/apache/polaris/core/persistence/cache/EntityWeigher.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.polaris.core.persistence.cache;
+
+import com.github.benmanes.caffeine.cache.Weigher;
+import org.checkerframework.checker.index.qual.NonNegative;
+
+/**
+ * A {@link Weigher} implementation that weighs {@link EntityCacheEntry} 
objects by the approximate
+ * size of the entity object.
+ */
+public class EntityWeigher implements Weigher<Long, EntityCacheEntry> {
+
+  /** The amount of weight that is expected to roughly equate to 1MB of memory 
usage */
+  public static final long WEIGHT_PER_MB = 1024 * 1024;
+
+  /* Represents the approximate size of an entity beyond the properties */
+  private static final int APPROXIMATE_ENTITY_OVERHEAD = 1000;
+
+  /** Singleton instance */
+  private static final EntityWeigher instance = new EntityWeigher();
+
+  private EntityWeigher() {}
+
+  /** Gets the singleton {@link EntityWeigher} */
+  public static EntityWeigher getInstance() {
+    return instance;
+  }
+
+  /**
+   * Computes the weight of a given entity
+   *
+   * @param key The entity's key; not used
+   * @param value The entity to be cached
+   * @return The weight of the entity
+   */
+  @Override
+  public @NonNegative int weigh(Long key, EntityCacheEntry value) {
+    return APPROXIMATE_ENTITY_OVERHEAD
+        + value.getEntity().getProperties().length()
+        + value.getEntity().getInternalProperties().length();

Review Comment:
   I'd good to see a clear linear relationship between weight and heap usage 
:+1: 
   
   Did you test include Grant Records inside cached entities? Do we expect 
those to be prevalent in the cache at all?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to