Source:
<code>public int compactPartitions() { if (partitions.isEmpty()) { logger.info("Skipping compaction as no partitions are assigned."); return 0; } List<PartitionId> partitionSnapshot = new ArrayList<>(partitions); long compactionStartTime = System.currentTimeMillis(); long timeToQuit = System.currentTimeMillis() + compactionTimeLimitMs; int compactionInProgress = 0; doneLatch.set(new CountDownLatch(1)); int totalBlobsPurged = 0; int compactedPartitionCount = 0; try { while (true) { while (compactionInProgress < numThreads) { if (partitionSnapshot.isEmpty()) { break; } PartitionId partitionId = partitionSnapshot.remove(0); executorCompletionService.submit(() -> compactPartition(partitionId)); compactionInProgress++; } totalBlobsPurged += executorCompletionService.take().get(); compactionInProgress--; compactedPartitionCount++; if (System.currentTimeMillis() >= timeToQuit) { logger.info("Compaction terminated due to time limit exceeded."); break; } if (isShuttingDown()) { logger.info("Compaction terminated due to shut down."); break; } if (partitionSnapshot.isEmpty()) { break; } } while (compactionInProgress > 0) { <START> totalBlobsPurged += executorCompletionService.take().get(); <END> compactionInProgress--; compactedPartitionCount++; } doneLatch.get().countDown(); } catch (Throwable th) { logger.error("Hit exception running compaction task", th); } finally { long compactionTime = (System.currentTimeMillis() - compactionStartTime) / 1000; logger.info("Purged blobs in partitions taking seconds", totalBlobsPurged, compactedPartitionCount, compactionTime); } return totalBlobsPurged; }</code><technical_language>Is idea wait threads done assigning new work? optimal a long running partition other threads idle for a while</technical_language>

Target:
public int compactPartitions() { if (partitions.isEmpty()) { logger.info("Skipping compaction as no partitions are assigned."); return 0; } List<PartitionId> partitionSnapshot = new ArrayList<>(partitions); long compactionStartTime = System.currentTimeMillis(); long timeToQuit = System.currentTimeMillis() + compactionTimeLimitMs; int compactionInProgress = 0; doneLatch.set(new CountDownLatch(1)); int totalBlobsPurged = 0; int compactedPartitionCount = 0; try { while (!partitionSnapshot.isEmpty()) { while (compactionInProgress < numThreads) { if (partitionSnapshot.isEmpty()) { break; } PartitionId partitionId = partitionSnapshot.remove(0); executorCompletionService.submit(() -> compactPartition(partitionId)); compactionInProgress++; } try { totalBlobsPurged += executorCompletionService.take().get(); compactedPartitionCount++; } catch (ExecutionException ex) { vcrMetrics.compactionFailureCount.inc(); } compactionInProgress--; if (System.currentTimeMillis() >= timeToQuit) { logger.info("Compaction terminated due to time limit exceeded."); break; } if (isShutDown()) { logger.info("Compaction terminated due to shut down."); break; } } while (compactionInProgress > 0) { try { totalBlobsPurged += executorCompletionService.take().get(); compactedPartitionCount++; } catch (ExecutionException ex) { vcrMetrics.compactionFailureCount.inc(); } compactionInProgress--; } doneLatch.get().countDown(); } catch (Throwable th) { logger.error("Hit exception running compaction task", th); } finally { long compactionTime = (System.currentTimeMillis() - compactionStartTime) / 1000; logger.info("Purged blobs in partitions taking seconds", totalBlobsPurged, compactedPartitionCount, compactionTime); } return totalBlobsPurged; }