diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/HConstants.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/HConstants.java index 60601b7..40497a6 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/HConstants.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/HConstants.java @@ -281,12 +281,19 @@ public final class HConstants { public static final String HREGION_MEMSTORE_FLUSH_SIZE = "hbase.hregion.memstore.flush.size"; + /** Conf key for the periodic flush interval */ + public static final String MEMSTORE_PERIODIC_FLUSH_INTERVAL = + "hbase.regionserver.optionalcacheflushinterval"; + public static final String HREGION_EDITS_REPLAY_SKIP_ERRORS = "hbase.hregion.edits.replay.skip.errors"; public static final boolean DEFAULT_HREGION_EDITS_REPLAY_SKIP_ERRORS = false; + /** Default interval for the memstore flush */ + public static final int DEFAULT_CACHE_FLUSH_INTERVAL = 600000; + /** Default size of a reservation block */ public static final int DEFAULT_SIZE_RESERVATION_BLOCK = 1024 * 1024 * 5; diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java index e49ef99..a693e88 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java @@ -362,6 +362,7 @@ public class HRegion implements HeapSize { // , Writable{ final RegionServerServices rsServices; private RegionServerAccounting rsAccounting; private List> recentFlushes = new ArrayList>(); + private long flushCheckInterval; private long blockingMemStoreSize; final long threadWakeFrequency; // Used to guard closes @@ -442,6 +443,9 @@ public class HRegion implements HeapSize { // , Writable{ .add(confParam) .addStringMap(htd.getConfiguration()) .addWritableMap(htd.getValues()); + this.flushCheckInterval = conf.getInt( + HConstants.MEMSTORE_PERIODIC_FLUSH_INTERVAL, + HConstants.DEFAULT_CACHE_FLUSH_INTERVAL); this.rowLockWaitDuration = conf.getInt("hbase.rowlock.wait.duration", DEFAULT_ROWLOCK_WAIT_DURATION); @@ -1469,6 +1473,25 @@ public class HRegion implements HeapSize { // , Writable{ } /** + * Should the memstore be flushed now + */ + boolean shouldFlush() { + long now = EnvironmentEdgeManager.currentTimeMillis(); + //if we flushed in the recent past, we don't need to do again now + if (!(now - getLastFlushTime() > HConstants.DEFAULT_CACHE_FLUSH_INTERVAL)) { + return false; + } + //since we didn't flush in the recent past, flush now if there are edits and + //no flush happened *after* the edits. Return true on first such memstore hit. + for (Store s : this.getStores().values()) { + if (s.timeOfLastEdit() > getLastFlushTime()) { + return true; + } + } + return false; + } + + /** * Flush the memstore. * * Flushing the memstore is a little tricky. We have a lot of updates in the diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java index 4a6b331..eda3d13 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java @@ -226,7 +226,7 @@ public class HRegionServer implements ClientProtocol, public static final Log LOG = LogFactory.getLog(HRegionServer.class); - private final Random rand = new Random(); + private Random rand = new Random(); /* * Strings to be used in forming the exception message for @@ -356,6 +356,11 @@ public class HRegionServer implements ClientProtocol, */ Chore compactionChecker; + /* + * Check for flushes + */ + Chore periodicFlusher; + // HLog and HLog roller. log is protected rather than private to avoid // eclipse warning when accessed by inner classes protected volatile HLog hlog; @@ -499,6 +504,7 @@ public class HRegionServer implements ClientProtocol, throw new IllegalArgumentException("Failed resolve of " + initialIsa); } + this.rand = new Random(initialIsa.hashCode()); this.rpcServer = HBaseServerRPC.getServer(AdminProtocol.class, this, new Class[]{ClientProtocol.class, AdminProtocol.class, HBaseRPCErrorHandler.class, @@ -821,6 +827,9 @@ public class HRegionServer implements ClientProtocol, ".multiplier", 1000); this.compactionChecker = new CompactionChecker(this, this.threadWakeFrequency * multiplier, this); + this.periodicFlusher = new PeriodicMemstoreFlusher( + this.conf.getInt(HConstants.MEMSTORE_PERIODIC_FLUSH_INTERVAL, + HConstants.DEFAULT_CACHE_FLUSH_INTERVAL), this); // Health checker thread. int sleepTime = this.conf.getInt(HConstants.HEALTH_CHORE_WAKE_FREQ, HConstants.DEFAULT_THREAD_WAKE_FREQUENCY); @@ -1389,6 +1398,38 @@ public class HRegionServer implements ClientProtocol, } } + class PeriodicMemstoreFlusher extends Chore { + HRegionServer server; + int RANGE_OF_DELAY = 120000; + public PeriodicMemstoreFlusher(int cacheFlushInterval, final HRegionServer server) { + super(server.getServerName() + "-MemstoreFlusherChore", cacheFlushInterval, server); + this.server = server; + + } + + @Override + protected void chore() { + for (HRegion r : this.server.onlineRegions.values()) { + if (r == null) + continue; + if (r.shouldFlush()) { + //before we flush, we sleep for a random period of time upto two minutes + long randomSleepInterval = rand.nextInt(RANGE_OF_DELAY + 1); + try { + Thread.sleep(randomSleepInterval); + } catch (InterruptedException ie){ + //ignore + } + FlushRequester requester = server.getFlushRequester(); + if (requester != null) { + LOG.info(getName() + " requesting flush for region " + r.getRegionNameAsString()); + requester.requestFlush(r); + } + } + } + } + } + /** * Report the status of the server. A server is online once all the startup is * completed (setting up filesystem, starting service threads, etc.). This @@ -1534,6 +1575,8 @@ public class HRegionServer implements ClientProtocol, this.cacheFlusher.start(uncaughtExceptionHandler); Threads.setDaemonThreadRunning(this.compactionChecker.getThread(), n + ".compactionChecker", uncaughtExceptionHandler); + Threads.setDaemonThreadRunning(this.periodicFlusher.getThread(), n + + ".periodicFlusher", uncaughtExceptionHandler); if (this.healthCheckChore != null) { Threads .setDaemonThreadRunning(this.healthCheckChore.getThread(), n + ".healthChecker", @@ -1614,7 +1657,8 @@ public class HRegionServer implements ClientProtocol, // Verify that all threads are alive if (!(leases.isAlive() && cacheFlusher.isAlive() && hlogRoller.isAlive() - && this.compactionChecker.isAlive())) { + && this.compactionChecker.isAlive()) + && this.periodicFlusher.isAlive()) { stop("One or more threads are no longer alive -- stop"); return false; } @@ -1789,6 +1833,7 @@ public class HRegionServer implements ClientProtocol, */ protected void join() { Threads.shutdown(this.compactionChecker.getThread()); + Threads.shutdown(this.periodicFlusher.getThread()); this.cacheFlusher.join(); if (this.healthCheckChore != null) { Threads.shutdown(this.healthCheckChore.getThread()); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java index 808dfaa..2334cc0 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java @@ -496,6 +496,11 @@ public class HStore implements Store, StoreConfiguration { } } + @Override + public long timeOfLastEdit() { + return memstore.timeOfLastEdit(); + } + /** * Adds a value to the memstore * diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MemStore.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MemStore.java index 64eb206..5d06294 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MemStore.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MemStore.java @@ -43,6 +43,7 @@ import org.apache.hadoop.hbase.io.HeapSize; import org.apache.hadoop.hbase.regionserver.MemStoreLAB.Allocation; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.ClassSize; +import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; /** * The MemStore holds in-memory modifications to the Store. Modifications @@ -88,6 +89,9 @@ public class MemStore implements HeapSize { // Used to track own heapSize final AtomicLong size; + // Used to track when to flush + volatile long timeLastAddCalled; + TimeRangeTracker timeRangeTracker; TimeRangeTracker snapshotTimeRangeTracker; @@ -218,6 +222,26 @@ public class MemStore implements HeapSize { } } + long timeOfLastEdit() { + return timeLastAddCalled; + } + + private boolean addToKVSet(KeyValue e) { + boolean b = this.kvset.add(e); + setLastEditTimeToNow(); + return b; + } + + private boolean removeFromKVSet(KeyValue e) { + boolean b = this.kvset.remove(e); + setLastEditTimeToNow(); + return b; + } + + void setLastEditTimeToNow() { + timeLastAddCalled = EnvironmentEdgeManager.currentTimeMillis(); + } + /** * Internal version of add() that doesn't clone KVs with the * allocator, and doesn't take the lock. @@ -225,7 +249,7 @@ public class MemStore implements HeapSize { * Callers should ensure they already have the read lock taken */ private long internalAdd(final KeyValue toAdd) { - long s = heapSizeChange(toAdd, this.kvset.add(toAdd)); + long s = heapSizeChange(toAdd, addToKVSet(toAdd)); timeRangeTracker.includeTimestamp(toAdd); this.size.addAndGet(s); return s; @@ -273,7 +297,7 @@ public class MemStore implements HeapSize { // If the key is in the memstore, delete it. Update this.size. found = this.kvset.get(kv); if (found != null && found.getMemstoreTS() == kv.getMemstoreTS()) { - this.kvset.remove(kv); + removeFromKVSet(kv); long s = heapSizeChange(kv, true); this.size.addAndGet(-s); } @@ -292,7 +316,7 @@ public class MemStore implements HeapSize { this.lock.readLock().lock(); try { KeyValue toAdd = maybeCloneWithAllocator(delete); - s += heapSizeChange(toAdd, this.kvset.add(toAdd)); + s += heapSizeChange(toAdd, addToKVSet(toAdd)); timeRangeTracker.includeTimestamp(toAdd); } finally { this.lock.readLock().unlock(); @@ -588,6 +612,7 @@ public class MemStore implements HeapSize { // false means there was a change, so give us the size. addedSize -= heapSizeChange(cur, true); it.remove(); + setLastEditTimeToNow(); } else { versionsVisible++; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/Store.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/Store.java index 32d581f..2228b8e 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/Store.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/Store.java @@ -96,6 +96,11 @@ public interface Store extends HeapSize { public long add(KeyValue kv); /** + * When was the last edit done in the memstore + */ + long timeOfLastEdit(); + + /** * Removes a kv from the memstore. The KeyValue is removed only if its key & memstoreTS match the * key & memstoreTS value of the kv parameter. * @param kv diff --git a/hbase-server/src/main/resources/hbase-default.xml b/hbase-server/src/main/resources/hbase-default.xml index 7e8dfaa..48007a5 100644 --- a/hbase-server/src/main/resources/hbase-default.xml +++ b/hbase-server/src/main/resources/hbase-default.xml @@ -349,6 +349,14 @@ + hbase.regionserver.optionalcacheflushinterval + 600000 + + Amount of time to wait since the last time a region was flushed before + invoking an optional cache flush. Default 1 hour. + + + hbase.hregion.memstore.flush.size 134217728