sklaha commented on code in PR #249:
URL: https://github.com/apache/cassandra-sidecar/pull/249#discussion_r2286692115


##########
adapters/adapters-base/src/main/java/org/apache/cassandra/sidecar/adapters/base/CassandraMetricsOperations.java:
##########
@@ -250,4 +278,187 @@ private List<ClientConnectionEntry> 
statsToEntries(Stream<ConnectedClientStats>
                                          stat.authenticationMode,
                                          stat.authenticationMetadata);
     }
+
+    /**
+     * Represents the metrics related to compaction stats that are supported 
by the Sidecar
+     */
+    public enum CompactionStatsMetrics
+    {
+        TOTAL_COMPACTIONS_COMPLETED("TotalCompactionsCompleted", 
MetricType.COUNTER),
+        BYTES_COMPACTED("BytesCompacted", MetricType.COUNTER),
+        COMPACTIONS_ABORTED("CompactionsAborted", MetricType.COUNTER),
+        COMPACTIONS_REDUCED("CompactionsReduced", MetricType.COUNTER),
+        SSTABLES_DROPPED_FROM_COMPACTION("SSTablesDroppedFromCompaction", 
MetricType.COUNTER),
+        PENDING_TASKS_BY_TABLE_NAME("PendingTasksByTableName", 
MetricType.GAUGE);
+
+        private final String metricName;
+        private final MetricType type;
+
+        CompactionStatsMetrics(String metricName, MetricType type)
+        {
+            this.metricName = metricName;
+            this.type = type;
+        }
+
+        String metricName()
+        {
+            return metricName;
+        }
+    }
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public CompactionStatsResponse 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 completedCompactions = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.TOTAL_COMPACTIONS_COMPLETED));
+        long dataCompacted = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.BYTES_COMPACTED));
+        long abortedCompactions = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.COMPACTIONS_ABORTED));
+        long reducedCompactions = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.COMPACTIONS_REDUCED));
+        long sstablesDroppedFromCompaction = 
getValueAsLong(getCompactionMetric(CompactionStatsMetrics.SSTABLES_DROPPED_FROM_COMPACTION));
+
+        // Get completed compactions rate with proper time conversions
+        CompactionStatsResponse.CompletedCompactionsRate 
completedCompactionsRate = getCompletedCompactionsRate();
+
+        // Get active compactions with all required fields
+        List<ActiveCompactionEntry> activeCompactions = 
getActiveCompactions(compactionManager.getCompactions());
+        long activeCompactionsCount = activeCompactions.size();
+        
+        // Calculate remaining time in seconds based on throughput and 
remaining bytes
+        String activeCompactionsRemainingTime = 
calculateRemainingTimeSeconds(activeCompactions, storageService);
+
+        return new CompactionStatsResponse(concurrentCompactors, pendingTasks, 
totalPendingTasks,
+                                           completedCompactions, 
dataCompacted, abortedCompactions,
+                                           reducedCompactions, 
sstablesDroppedFromCompaction,
+                                           completedCompactionsRate,
+                                           activeCompactions, 
activeCompactionsCount, activeCompactionsRemainingTime);

Review Comment:
   I agree that we could utilize data access objects in the JMX layer. However, 
I opted to return the response object for the following reasons:
   
   - Other APIs are doing the same.
   - Incorporating data access objects would necessitate the creation of object 
mappers.
   
   We now have two options to consider:
   
   1. Maintain the current practice of returning the response object from JMX. 
When we require reusing the object for some other internal purpose, we can then 
create the data access objects and mappers.
   2. Create the data access objects and mappers now.
   
   Please share your thoughts on this matter.



##########
adapters/adapters-base/src/main/java/org/apache/cassandra/sidecar/adapters/base/CassandraMetricsOperations.java:
##########
@@ -61,6 +67,27 @@ public class CassandraMetricsOperations implements 
MetricsOperations
     private final ConnectedClientStatsDatabaseAccessor dbAccessor;
     protected final JmxClient jmxClient;
 
+    private static final String METRICS_OBJ_TYPE_KEYSPACE_TABLE_FORMAT = 
"org.apache.cassandra.metrics:type=Table,keyspace=%s,scope=%s,name=%s";
+    private static final String METRICS_OBJ_TYPE_COMPACTION = 
"org.apache.cassandra.metrics:type=Compaction,name=%s";
+
+    // Constants for compaction info map keys
+    public static final String ID = "id";
+    public static final String KEYSPACE = "keyspace";
+    public static final String COLUMNFAMILY = "columnfamily";
+    public static final String COMPLETED = "completed";
+    public static final String TOTAL = "total";
+    public static final String TASK_TYPE = "taskType";
+    public static final String COMPACTION_ID = "compactionId";
+    public static final String SSTABLES = "sstables";
+    public static final String TARGET_DIRECTORY = "targetDirectory";
+
+    public static final String TIME_FORMAT = "%dh%02dm%02ds";
+
+    // Default values
+    public static final String DEFAULTVAL_STRING = "unknown";
+    public static final String DEFAULTVAL_NUMBER = "-1";
+    public static final String DEFAULTVAL_N_A = "n/a";

Review Comment:
   Done



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