From 271761038ce1b77eea05b96f1226016b45f5f3d0 Mon Sep 17 00:00:00 2001 From: Jonathan Ellis Date: Fri, 25 May 2012 15:16:52 -0500 Subject: [PATCH] switch to Map to avoid re-hashing the list with every addition patch by jbellis; reviewed by thobbs for CASSANDRA-4287 --- CHANGES.txt | 2 ++ .../SizeTieredCompactionStrategy.java | 26 +++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 404a744aa8..b566c6f3b6 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -3,6 +3,8 @@ * ensure unique streaming session id's (CASSANDRA-4223) * kick off background compaction when min/max thresholds change (CASSANDRA-4279) + * improve ability of STCS.getBuckets to deal with 100s of 1000s of + sstables, such as when convertinb back from LCS (CASSANDRA-4287) 1.0.10 diff --git a/src/java/org/apache/cassandra/db/compaction/SizeTieredCompactionStrategy.java b/src/java/org/apache/cassandra/db/compaction/SizeTieredCompactionStrategy.java index 380982f417..747edcca59 100644 --- a/src/java/org/apache/cassandra/db/compaction/SizeTieredCompactionStrategy.java +++ b/src/java/org/apache/cassandra/db/compaction/SizeTieredCompactionStrategy.java @@ -122,7 +122,7 @@ public class SizeTieredCompactionStrategy extends AbstractCompactionStrategy } }); - Map, Long> buckets = new HashMap, Long>(); + Map> buckets = new HashMap>(); for (Pair pair: sortedFiles) { @@ -132,19 +132,19 @@ public class SizeTieredCompactionStrategy extends AbstractCompactionStrategy // look for a bucket containing similar-sized files: // group in the same bucket if it's w/in 50% of the average for this bucket, // or this file and the bucket are all considered "small" (less than `minSSTableSize`) - for (Entry, Long> entry : buckets.entrySet()) + for (Entry> entry : buckets.entrySet()) { - List bucket = entry.getKey(); - long averageSize = entry.getValue(); - if ((size > (averageSize / 2) && size < (3 * averageSize) / 2) - || (size < minSSTableSize && averageSize < minSSTableSize)) + List bucket = entry.getValue(); + long oldAverageSize = entry.getKey(); + if ((size > (oldAverageSize / 2) && size < (3 * oldAverageSize) / 2) + || (size < minSSTableSize && oldAverageSize < minSSTableSize)) { - // remove and re-add because adding changes the hash - buckets.remove(bucket); - long totalSize = bucket.size() * averageSize; - averageSize = (totalSize + size) / (bucket.size() + 1); + // remove and re-add under new new average size + buckets.remove(oldAverageSize); + long totalSize = bucket.size() * oldAverageSize; + long newAverageSize = (totalSize + size) / (bucket.size() + 1); bucket.add(pair.left); - buckets.put(bucket, averageSize); + buckets.put(newAverageSize, bucket); bFound = true; break; } @@ -154,11 +154,11 @@ public class SizeTieredCompactionStrategy extends AbstractCompactionStrategy { ArrayList bucket = new ArrayList(); bucket.add(pair.left); - buckets.put(bucket, size); + buckets.put(size, bucket); } } - return new ArrayList>(buckets.keySet()); + return new ArrayList>(buckets.values()); } private void updateEstimatedCompactionsByTasks(List tasks)