@Override
  public CompactionDetails getCompactionDetails(long totalCapacity, long usedCapacity, long segmentCapacity,
      long segmentHeaderSize, List<LogSegmentName> logSegmentsNotInJournal, BlobStoreStats blobStoreStats,
      String dataDir) throws StoreException {
    CompactionDetails details = null;
    logger.debug("UsedCapacity {} vs TotalCapacity {}", usedCapacity, totalCapacity);
    if (usedCapacity >= (storeConfig.storeMinUsedCapacityToTriggerCompactionInPercentage / 100.0) * totalCapacity) {
      if (logSegmentsNotInJournal != null) {
        Pair<Long, NavigableMap<LogSegmentName, Long>> validDataSizeByLogSegment =
            blobStoreStats.getValidDataSizeByLogSegment(
                new TimeRange(time.milliseconds() - messageRetentionTimeInMs - ERROR_MARGIN_MS, ERROR_MARGIN_MS));
        String sizeLog = "";
        if (validDataSizeByLogSegment != null) {
          sizeLog = blobStoreStats.dumpLogSegmentSize(validDataSizeByLogSegment.getSecond(), segmentCapacity, dataDir);
        }

        NavigableMap<LogSegmentName, Long> potentialLogSegmentValidSizeMap = validDataSizeByLogSegment.getSecond()
            .subMap(logSegmentsNotInJournal.get(0), true,
                logSegmentsNotInJournal.get(logSegmentsNotInJournal.size() - 1), true);

        CostBenefitInfo bestCandidateToCompact =
            getBestCandidateToCompact(potentialLogSegmentValidSizeMap, segmentCapacity, segmentHeaderSize,
                blobStoreStats.getMaxBlobSize(), blobStoreStats);
        if (bestCandidateToCompact != null) {
          details =
              new CompactionDetails(validDataSizeByLogSegment.getFirst(), bestCandidateToCompact.getSegmentsToCompact(),
                  bestCandidateToCompact);
          logger.info("[CAPACITY] Generating CompactionDetails {} using StatsBasedCompactionPolicy for {}. Stats {}",
              bestCandidateToCompact, dataDir, sizeLog);
        } else {
          logger.trace("No best candidate found");
        }
      }
    }
    return details;
  }
StatsBasedCompactionPolicy calculates the cost benefit ratio for every potential candidate to compact and finds the
best one to be compacted to be returned for {@link CompactionPolicy#getCompactionDetails(long, long, long, long, List, BlobStoreStats, String)}
ambry-store/src/main/java/com/github/ambry/store/StatsBasedCompactionPolicy.java