maedhroz commented on code in PR #2534:
URL: https://github.com/apache/cassandra/pull/2534#discussion_r1317629724


##########
src/java/org/apache/cassandra/metrics/AccordStateCacheMetrics.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.cassandra.metrics;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import com.codahale.metrics.Histogram;
+import org.apache.cassandra.cache.CacheSize;
+
+import static org.apache.cassandra.metrics.CassandraMetricsRegistry.Metrics;
+
+public class AccordStateCacheMetrics extends CacheMetrics
+{
+    public final Histogram objectSize;
+
+    /**
+     * Create metrics for given cache.
+     *
+     * @param type  Type of Cache to identify metrics.
+     * @param cache Cache to measure metrics
+     */
+    public AccordStateCacheMetrics(String type, CacheSize cache)
+    {
+        super(type, cache);
+
+        objectSize = Metrics.histogram(factory.createMetricName("ObjectSize"), 
false);
+    }
+
+    public static class Provider
+    {
+        public static final String ACCORD_STATE_CACHE = "accord-state-cache";
+
+        private final Set<CacheSize> globalCacheSizes;
+        private final AccordStateCacheMetrics globalMetrics;
+
+        private final Map<Class<?>, AccordStateCacheMetrics> instanceMetrics;
+        private final Map<Class<?>, Set<CacheSize>> instanceCacheSizes;
+
+        private final String type;
+
+        public Provider()
+        {
+            this(ACCORD_STATE_CACHE);
+        }
+
+        @VisibleForTesting
+        public Provider(String type)
+        {
+            this.type = type;
+            globalCacheSizes = new CopyOnWriteArraySet<>();
+            globalMetrics = new AccordStateCacheMetrics(type, new 
CacheSize.Summing(globalCacheSizes));
+            instanceMetrics = new HashMap<>(2);
+            instanceCacheSizes = new HashMap<>(2);
+        }
+
+        public synchronized AccordStateCacheMetrics forInstance(Class<?> 
klass, CacheSize commandStoreInstanceCacheSize)
+        {
+            Set<CacheSize> cacheSizes = 
instanceCacheSizes.computeIfAbsent(klass, k -> new CopyOnWriteArraySet<>());
+            cacheSizes.add(commandStoreInstanceCacheSize);
+            return instanceMetrics.computeIfAbsent(klass, k -> new 
AccordStateCacheMetrics(String.format("%s-%s", type, k.getSimpleName()), new 
CacheSize.Summing(cacheSizes)));

Review Comment:
   Looking at how the caches work at the instance level, there may be a problem 
w/ the summing approach here.
   
   ```
   @Override
   public long capacity()
   {
       return AccordStateCache.this.capacity();
   }
   
   @Override
   public void setCapacity(long capacity)
   {
       AccordStateCache.this.setCapacity(capacity);
   }
   
   @Override
   public int size()
   {
       return AccordStateCache.this.size();
   }
   
   @Override
   public long weightedSize()
   {
       return bytesCached;
   }
   ```
   If we take the sum of all the `Instance` `size()` methods, won't we be 
taking the same shared size from `cache` in the top level `AccordStateCache`? 
(i.e. We're still actually looking at a global size?)
   
   At this point, I'm not sure we should have `Instance` implement `CacheSize` 
at all, and I'm not sure having instance-level metrics is worth the complexity. 
The `Instance` caches share the same physical cache, and are less caches than 
they are cache accessors...



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to