krummas commented on code in PR #4476:
URL: https://github.com/apache/cassandra/pull/4476#discussion_r2635757180
##########
src/java/org/apache/cassandra/db/compaction/CompactionStrategyManager.java:
##########
@@ -702,6 +772,89 @@ public boolean isLeveledCompaction()
}
}
+ /**
+ * Data class for accumulating UCS metrics computation state.
+ * Holds intermediate values during metric calculation across all
strategies and levels.
+ */
+ private static class CompactionStatsMetricsData
+ {
+ final double[] sum = new double[UnifiedCompactionStrategy.MAX_LEVELS];
+ final int[] count = new int[UnifiedCompactionStrategy.MAX_LEVELS];
+ final double[] max = new double[UnifiedCompactionStrategy.MAX_LEVELS];
+ int numberOfLevels = 0;
+
+ int levelIndex;
+ Level level;
+ SSTableReader sstable;
+ UnifiedCompactionStrategy strategy;
+ }
+
+ /**
+ * Generic helper to compute UCS metrics across all strategies and levels.
+ * Reduces code duplication for per-level metric calculations.
+ *
+ * @param accumulator processes each sstable and updates the metrics data
state
+ * @param finalizer computes the final result array from the accumulated
metrics data
+ * @return computed metric array, one value per level, or null if not
using UCS
+ */
+ private double[] computeUCSMetric(Consumer<CompactionStatsMetricsData>
accumulator, Function<CompactionStatsMetricsData, double[]> finalizer)
+ {
+ readLock.lock();
+ try
+ {
+ if (repaired.first() instanceof UnifiedCompactionStrategy)
+ {
+ CompactionStatsMetricsData data = new
CompactionStatsMetricsData();
+
+ for (AbstractCompactionStrategy strategy : getAllStrategies())
+ {
+ UnifiedCompactionStrategy ucsStrategy =
(UnifiedCompactionStrategy) strategy;
+ List<Level> levels = ucsStrategy.getLevelsSnapshot();
+
+ data.numberOfLevels = Math.max(data.numberOfLevels,
levels.size());
+ data.strategy = ucsStrategy;
+
+ for (int i = 0; i < levels.size(); i++)
+ {
+ data.levelIndex = i;
+ data.level = levels.get(i);
+ for (SSTableReader sstable :
levels.get(i).getSSTables())
+ {
+ data.sstable = sstable;
+ accumulator.accept(data);
+ }
+ }
+ }
+
+ return finalizer.apply(data);
+ }
+ return null;
+ }
+ finally {
+ readLock.unlock();
+ }
+ }
+
+ private static double[] averageFinalizer(CompactionStatsMetricsData data)
+ {
+ double[] res = new double[data.numberOfLevels];
+ for (int i = 0; i < data.numberOfLevels; i++)
+ res[i] = data.count[i] == 0 ? 0 : data.sum[i] /
data.numberOfLevels;
Review Comment:
this should divide by `data.count[i]`, not `numberOfLevels`
--
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]