jacek-lewandowski commented on code in PR #2534:
URL: https://github.com/apache/cassandra/pull/2534#discussion_r1318239122


##########
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:
   It actually works in a way that for the `AccordCommandStore` global metrics, 
the provider always returns the same metrics object; a similar approach was 
taken for the instance cache - for the same instance type, the same metrics are 
returned. We do not have to worry about summing up cache access metrics (hits, 
misses, requests) across different command stores because they all are reported 
to the same metrics instances.
   
   For `CacheSize`, I believe we need to do summing on demand because the cache 
size can be individually set on each `AccordCommandStore`. Also, `size` and 
`weightedSize` are computed internally and separately for each command store 
and instance. So, whenever the operator requests the current number of items or 
the current number of bytes caches, for, say, instance X or global, we need to 
go through all the command stores (implementing `CacheSize`) and sum up the 
outputs.



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