diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactSelection.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactSelection.java index 66d8a0f..e363d82 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactSelection.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/compactions/CompactSelection.java @@ -1,5 +1,4 @@ -/** - * +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -20,6 +19,7 @@ package org.apache.hadoop.hbase.regionserver.compactions; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicLong; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -29,37 +29,30 @@ import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; @InterfaceAudience.Private public class CompactSelection { - private static final long serialVersionUID = 1L; - static final Log LOG = LogFactory.getLog(CompactSelection.class); - // the actual list - this is needed to handle methods like "sublist" - // correctly - List filesToCompact = new ArrayList(); + private static final Log LOG = LogFactory.getLog(CompactSelection.class); + + /** the actual list - this is needed to handle methods like "sublist" correctly */ + private List filesToCompact; /** * Number of off peak compactions either in the compaction queue or - * happening now. Please lock compactionCountLock before modifying. + * happening now. */ - static long numOutstandingOffPeakCompactions = 0; + private static AtomicLong numOutstandingOffPeakCompactions = new AtomicLong(); - /** - * Lock object for numOutstandingOffPeakCompactions - */ - private final static Object compactionCountLock = new Object(); + /** was this compaction promoted to an off-peak */ + private boolean isOffPeakCompaction = false; - // was this compaction promoted to an off-peak - boolean isOffPeakCompaction = false; - // CompactSelection object creation time. - private final long selectionTime; + /** CompactSelection object creation time. */ + private final long selectionTime = EnvironmentEdgeManager.currentTimeMillis(); public CompactSelection(List filesToCompact) { - this.selectionTime = EnvironmentEdgeManager.currentTimeMillis(); this.filesToCompact = filesToCompact; - this.isOffPeakCompaction = false; } /** * Select the expired store files to compact - * + * * @param maxExpiredTimeStamp * The store file will be marked as expired if its max time stamp is * less than this maxExpiredTimeStamp. @@ -70,9 +63,7 @@ public class CompactSelection { long maxExpiredTimeStamp) { if (filesToCompact == null || filesToCompact.size() == 0) return null; - ArrayList expiredStoreFiles = null; - boolean hasExpiredStoreFiles = false; - CompactSelection expiredSFSelection = null; + List expiredStoreFiles = null; for (StoreFile storeFile : this.filesToCompact) { if (storeFile.getReader().getMaxTimestamp() < maxExpiredTimeStamp) { @@ -80,18 +71,17 @@ public class CompactSelection { + storeFile.getPath() + " whose maxTimeStamp is " + storeFile.getReader().getMaxTimestamp() + " while the max expired timestamp is " + maxExpiredTimeStamp); - if (!hasExpiredStoreFiles) { + if (expiredStoreFiles == null) { expiredStoreFiles = new ArrayList(); - hasExpiredStoreFiles = true; } expiredStoreFiles.add(storeFile); } } - if (hasExpiredStoreFiles) { - expiredSFSelection = new CompactSelection(expiredStoreFiles); + if (expiredStoreFiles != null) { + return new CompactSelection(expiredStoreFiles); } - return expiredSFSelection; + return null; } /** @@ -100,12 +90,8 @@ public class CompactSelection { */ public void finishRequest() { if (isOffPeakCompaction) { - long newValueToLog = -1; - synchronized(compactionCountLock) { - assert !isOffPeakCompaction : "Double-counting off-peak count for compaction"; - newValueToLog = --numOutstandingOffPeakCompactions; - isOffPeakCompaction = false; - } + long newValueToLog = numOutstandingOffPeakCompactions.decrementAndGet(); + isOffPeakCompaction = false; LOG.info("Compaction done, numOutstandingOffPeakCompactions is now " + newValueToLog); } @@ -122,12 +108,9 @@ public class CompactSelection { public void emptyFileList() { filesToCompact.clear(); if (isOffPeakCompaction) { - long newValueToLog = -1; - synchronized(compactionCountLock) { - // reset the off peak count - newValueToLog = --numOutstandingOffPeakCompactions; - isOffPeakCompaction = false; - } + // reset the off peak count + long newValueToLog = numOutstandingOffPeakCompactions.decrementAndGet(); + isOffPeakCompaction = false; LOG.info("Nothing to compact, numOutstandingOffPeakCompactions is now " + newValueToLog); } @@ -138,9 +121,7 @@ public class CompactSelection { } public static long getNumOutStandingOffPeakCompactions() { - synchronized(compactionCountLock) { - return numOutstandingOffPeakCompactions; - } + return numOutstandingOffPeakCompactions.get(); } /** @@ -150,11 +131,10 @@ public class CompactSelection { */ public boolean trySetOffpeak() { assert !isOffPeakCompaction : "Double-setting off-peak for compaction " + this; - synchronized(compactionCountLock) { - if (numOutstandingOffPeakCompactions == 0) { - numOutstandingOffPeakCompactions++; - isOffPeakCompaction = true; - } + + // Currently only one off peak compaction is present in the compaction queue. + if(numOutstandingOffPeakCompactions.compareAndSet(0, 1)) { + isOffPeakCompaction = true; } return isOffPeakCompaction; } @@ -163,10 +143,6 @@ public class CompactSelection { return selectionTime; } - public CompactSelection subList(int start, int end) { - throw new UnsupportedOperationException(); - } - public CompactSelection getSubList(int start, int end) { filesToCompact = filesToCompact.subList(start, end); return this; @@ -175,8 +151,4 @@ public class CompactSelection { public void clearSubList(int start, int end) { filesToCompact.subList(start, end).clear(); } - - private boolean isValidHour(int hour) { - return (hour >= 0 && hour <= 23); - } }