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


##########
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:
   The constants in this expression do not "approximate" any actual sizes. I 
suggest: `ENTITY_SIZE_BOUND_HEURISTIC`, `CALIBRATED_BYTES_PER_CHAR`, plus add 
javadoc about how we arrived at these calibration parameters.



##########
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:
   TBH, this description is not helpful to users (like myself). The unit of 
measurement is not specified, the computation of the weight is hidden in code 
:shrug: What does "target" mean in this context?
   
   Proposal: `The maximum weight for the entity cache. This is a heuristic 
value expressed in bytes that 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);
+  }
+
+  /** Factory method to provide a typed Weigher */
+  public static Weigher<Long, ResolvedPolarisEntity> asWeigher() {
+    return (Weigher<Long, ResolvedPolarisEntity>) getInstance();

Review Comment:
   Why cast here? This class already has generics :thinking: 



-- 
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