From ea7228170ef79ee3cd695095c463ee24f84017d2 Mon Sep 17 00:00:00 2001 From: chenheng Date: Tue, 20 Oct 2015 11:54:00 +0800 Subject: [PATCH] HBASE-14643 Avoid Splits from once again opening a closed reader for fetching the first and last key --- .../hbase/regionserver/HRegionFileSystem.java | 27 +++++++++++----------- .../hadoop/hbase/regionserver/StoreFile.java | 15 ++++++++++++ .../hadoop/hbase/regionserver/TestStoreFile.java | 2 ++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java index b16738f..e3e28e1 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegionFileSystem.java @@ -30,6 +30,7 @@ import java.util.UUID; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; @@ -39,13 +40,6 @@ import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.permission.FsPermission; -import org.apache.hadoop.hbase.Cell; -import org.apache.hadoop.hbase.HColumnDescriptor; -import org.apache.hadoop.hbase.HConstants; -import org.apache.hadoop.hbase.HRegionInfo; -import org.apache.hadoop.hbase.HTableDescriptor; -import org.apache.hadoop.hbase.KeyValue; -import org.apache.hadoop.hbase.KeyValueUtil; import org.apache.hadoop.hbase.backup.HFileArchiver; import org.apache.hadoop.hbase.fs.HFileSystem; import org.apache.hadoop.hbase.io.Reference; @@ -53,6 +47,7 @@ import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.FSHDFSUtils; import org.apache.hadoop.hbase.util.FSUtils; import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil; +import org.apache.hadoop.hbase.CellComparator; /** * View to an on-disk Region. @@ -587,23 +582,29 @@ public class HRegionFileSystem { if (top) { //check if larger than last key. KeyValue splitKey = KeyValueUtil.createFirstOnRow(splitRow); - Cell lastKey = f.createReader().getLastKey(); + Cell lastKey = f.getLastKey(); // If lastKey is null means storefile is empty. if (lastKey == null) { - return null; + lastKey = f.createReader().getLastKey(); + if (lastKey == null) { + return null; + } } - if (f.getReader().getComparator().compare(splitKey, lastKey) > 0) { + if (CellComparator.COMPARATOR.compare(splitKey, lastKey) > 0) { return null; } } else { //check if smaller than first key KeyValue splitKey = KeyValueUtil.createLastOnRow(splitRow); - Cell firstKey = f.createReader().getFirstKey(); + Cell firstKey = f.getFirstKey(); // If firstKey is null means storefile is empty. if (firstKey == null) { - return null; + firstKey = f.createReader().getFirstKey(); + if (firstKey == null) { + return null; + } } - if (f.getReader().getComparator().compare(splitKey, firstKey) < 0) { + if (CellComparator.COMPARATOR.compare(splitKey, firstKey) < 0) { return null; } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFile.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFile.java index 11d71cf..a802c94 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFile.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFile.java @@ -129,6 +129,19 @@ public class StoreFile { // Set when we obtain a Reader. private long maxMemstoreTS = -1; + // firstKey and lastkey will be set when closeReader. + private Cell firstKey; + + private Cell lastKey; + + public Cell getFirstKey() { + return firstKey; + } + + public Cell getLastKey() { + return lastKey; + } + public long getMaxMemstoreTS() { return maxMemstoreTS; } @@ -517,6 +530,8 @@ public class StoreFile { public synchronized void closeReader(boolean evictOnClose) throws IOException { if (this.reader != null) { + firstKey = this.reader.getFirstKey(); + lastKey = this.reader.getLastKey(); this.reader.close(evictOnClose); this.reader = null; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestStoreFile.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestStoreFile.java index b763a22..89f6a43 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestStoreFile.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestStoreFile.java @@ -175,6 +175,8 @@ public class TestStoreFile extends HBaseTestCase { byte [] midRow = CellUtil.cloneRow(kv); kv = reader.getLastKey(); byte [] finalRow = CellUtil.cloneRow(kv); + hsf.closeReader(true); + // Make a reference HRegionInfo splitHri = new HRegionInfo(hri.getTable(), null, midRow); Path refPath = splitStoreFile(regionFs, splitHri, TEST_FAMILY, hsf, midRow, true); -- 1.9.3 (Apple Git-50)