eric-maynard commented on code in PR #490:
URL: https://github.com/apache/polaris/pull/490#discussion_r2006808489


##########
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:
   > Would you be able to plot the total weight on the X axis and heap size 
(after GC) on the Y axis? Ideally, dots should cluster around a line Y = A * X 
+ B.
   
   The total weight in all of these tests is the default, `100 * 
WEIGHT_PER_MB`. What is being adjusted (and debated, I think?) is the 
multiplier between bytes of the entity and weight units. So that is what is 
plotted on the X axis.
   
   If you'd like me to instead pin the multiplier and adjust the weight limit, 
what would you like the multiplier to be pinned to? I can run that test and 
render a chart like the one above pretty quickly using the linked branch.
   
   Or instead are you talking about not the weight when the cache stabilizes, 
but as entries are first added to it? If so, these are the charts I shared 
earlier like in [this 
comment](https://github.com/apache/polaris/pull/490#discussion_r1996336633). 
You can see how much heap size increases as more bytes are written.
   
   > I personally do not see any value, from the user's perspective, in 
exposing a weight limit if the user is not able to reasonably well correlate it 
with actual heap usage.
   
   I think it's hard to argue that there's no benefit to the user in making the 
cache bounded instead of unbounded. I am happy to remove the configurable 
weight limit; earlier versions of this PR did not have one. Ultimately my 
position is not that these are the perfect values but that they are 
incrementally better than the current setup.
   



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