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


##########
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:
   > Also, drops in the charts mean GC reclaimed memory, so having these drops 
invalidates the data 
   
   That's why the charts are zoomed in on the period of time before a GC -- 
refer to the spreadsheet to see that much more data was typically collected.
   
   > How about we try to correlate total size of properties (per entity) times 
the number of entities to heap usage?
   
   That is what the weigher is doing. When determining the weight of an entry, 
it looks at the size of the properties. My analysis above relates the heap 
memory to the number of entities times the length of their properties. The 
relationship is found to be a factor between 2 - 6.
   
   We don't have to find the perfect scalar multiplier here, and the analysis 
above seems to corroborate my suggestion above that there may be no perfect 
value. We just need to stop having an unbounded cache. 
   
   What we actually care about is that the cache's size (in bytes) is bounded 
and that we can increase this bound semi-linearly with a config. The tests 
above seem to suggest this PR does both. 



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