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


##########
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:
   Or perhaps here's another approach...
   
   One of our problems here is that the instances caches are just views on an 
actual physical cache. `CacheMetrics` is really only appropriate for the global 
case. We could split out the bits of `CacheMetrics` (hits, misses, etc.) that 
are totally unambiguous for the instance "caches" and avoiding having to worry 
about any summing logic around those. (Would probably have to be a super-class 
of `CacheMetrics` w/o size information, like `CacheAccessMetrics`. Could have 
the same type name...)



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