yifan-c commented on code in PR #249:
URL: https://github.com/apache/cassandra-sidecar/pull/249#discussion_r2292165145


##########
adapters/adapters-base/src/main/java/org/apache/cassandra/sidecar/adapters/base/CassandraMetricsOperations.java:
##########
@@ -250,4 +296,181 @@ private List<ClientConnectionEntry> 
statsToEntries(Stream<ConnectedClientStats>
                                          stat.authenticationMode,
                                          stat.authenticationMetadata);
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public CompactionStatsData compactionStats()
+    {
+        // Get compaction manager and storage service proxies
+        CompactionManagerJmxOperations compactionManager = 
jmxClient.proxy(CompactionManagerJmxOperations.class, 
COMPACTION_MANAGER_OBJ_NAME);
+        StorageJmxOperations storageService = 
jmxClient.proxy(StorageJmxOperations.class, STORAGE_SERVICE_OBJ_NAME);
+
+        // Get concurrent compactors from StorageService as per specification
+        long concurrentCompactors = storageService.getConcurrentCompactors();
+
+        // Get pending tasks grouped by keyspace and table
+        Map<String, Map<String, Integer>> pendingTasks = 
getPendingCompactionTasksByTable();
+        long totalPendingTasks = pendingTasks.values().stream()
+                                                   .mapToLong(tableMap -> 
tableMap.values().stream().mapToInt(Integer::intValue).sum())
+                                                   .sum();
+
+        // Get compaction metrics from JMX counters and meters
+        long completedCompactionsCount = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.TOTAL_COMPACTIONS_COMPLETED));
+        long dataCompactedBytes = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.BYTES_COMPACTED));
+        long abortedCompactionsCount = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.COMPACTIONS_ABORTED));
+        long reducedCompactionsCount = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.COMPACTIONS_REDUCED));
+        long sstablesDroppedFromCompactionCount = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.SSTABLES_DROPPED_FROM_COMPACTION));
+
+        // Get completed compactions rate with proper time conversions
+        CompletedCompactionsRateData completedCompactionsRate = 
getCompletedCompactionsRate();
+
+        // Get active compactions with all required fields
+        List<ActiveCompactionEntryData> activeCompactions = 
getActiveCompactions(compactionManager.getCompactions());
+        long activeCompactionsCount = activeCompactions.size();
+        
+        // Calculate remaining time in seconds based on throughput and 
remaining bytes
+        long activeCompactionsRemainingTime = 
calculateRemainingTimeSeconds(activeCompactions, storageService);
+
+        return CompactionStatsData.builder()
+            .concurrentCompactors(concurrentCompactors)
+            .pendingTasks(pendingTasks)
+            .totalPendingTasks(totalPendingTasks)
+            .completedCompactions(completedCompactionsCount)
+            .dataCompacted(dataCompactedBytes)
+            .abortedCompactions(abortedCompactionsCount)
+            .reducedCompactions(reducedCompactionsCount)
+            .sstablesDroppedFromCompaction(sstablesDroppedFromCompactionCount)
+            .completedCompactionsRate(completedCompactionsRate)
+            .activeCompactions(activeCompactions)
+            .activeCompactionsCount(activeCompactionsCount)
+            .activeCompactionsRemainingTime(activeCompactionsRemainingTime)
+            .build();
+    }
+
+    private Map<String, Map<String, Integer>> 
getPendingCompactionTasksByTable()
+    {
+        // Get pending tasks by table name from Gauge metric
+        Object value = 
getCompactionMetric(CompactionStatsMetrics.PENDING_TASKS_BY_TABLE_NAME);
+        return parsePendingTasksMap(value);
+    }
+
+    private Map<String, Map<String, Integer>> parsePendingTasksMap(Object 
value)
+    {
+        Map<?, ?> rawMap = safeCast(value, Map.class, "pending tasks");
+        Map<String, Map<String, Integer>> result = new HashMap<>();
+
+        for (Map.Entry<?, ?> entry : rawMap.entrySet())
+        {
+            String keyspace = safeCast(entry.getKey(), String.class, "keyspace 
name");
+            Map<?, ?> rawTableMap = safeCast(entry.getValue(), Map.class, 
"table data");
+            Map<String, Integer> tableMap = new HashMap<>();
+            
+            for (Map.Entry<?, ?> tableEntry : rawTableMap.entrySet())
+            {
+                String tableName = safeCast(tableEntry.getKey(), String.class, 
"table name");
+                Number taskCount = safeCast(tableEntry.getValue(), 
Number.class, "task count");
+                tableMap.put(tableName, taskCount.intValue());
+            }
+            
+            result.put(keyspace, tableMap);
+        }
+        
+        return result;
+    }
+
+    private Object getCompactionMetric(final CompactionStatsMetrics metric)
+    {
+        String metricObjectType = String.format(METRICS_OBJ_TYPE_COMPACTION, 
metric.metricName());
+        return queryMetric(metricObjectType, metric.type);
+    }
+
+    private CompletedCompactionsRateData getCompletedCompactionsRate()
+    {
+        // Get rates from meter metric for TotalCompactionsCompleted
+        String metricObjectType = String.format(METRICS_OBJ_TYPE_COMPACTION, 
"TotalCompactionsCompleted");
+        MeterMetricsJmxOperations metricsProxy = 
jmxClient.proxy(MeterMetricsJmxOperations.class, metricObjectType);
+
+        // Convert per-second rates to the specification:
+        // meanRate: compactions per hour
+        // fifteenMinuteRate: compactions per minute for last 15 minutes
+        double meanRateValuePerHour = metricsProxy.getMeanRate() * 3600; // 
Convert per second to per hour
+        double fifteenMinuteRateValuePerMinute = 
metricsProxy.getFifteenMinuteRate() * 60; // Convert per second to per minute
+
+        String meanRate = String.format("%.2f/hour", meanRateValuePerHour);
+        String fifteenMinuteRate = String.format("%.2f/minute", 
fifteenMinuteRateValuePerMinute);

Review Comment:
   Agree with Arjun. 
   In addition, the conversion to per hour or per minute should not be done 
here. In this layer, it should just return the value as-is. 
   If per hour and per minute is part of the API spec, could you do the 
conversion in the http response, `CompactionStatsResponse`? 



##########
server-common/src/main/java/org/apache/cassandra/sidecar/common/server/data/CompactionStatsData.java:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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.sidecar.common.server.data;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cassandra.sidecar.common.DataObjectBuilder;
+
+/**
+ * Data object representing compaction statistics
+ */
+public class CompactionStatsData
+{
+    private final long concurrentCompactors;
+    private final Map<String, Map<String, Integer>> pendingTasks;
+    private final long totalPendingTasks;
+    private final long completedCompactions;
+    private final long dataCompacted;
+    private final long abortedCompactions;
+    private final long reducedCompactions;
+    private final long sstablesDroppedFromCompaction;
+    private final CompletedCompactionsRateData completedCompactionsRate;
+    private final List<ActiveCompactionEntryData> activeCompactions;
+    private final long activeCompactionsCount;
+    private final long activeCompactionsRemainingTime;

Review Comment:
   nit: they can be just `public` and the getters can be removed. Not strong on 
this one. Feel free to ignore. 



-- 
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: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to