From 10ecdb6e9b752cbb2cfa67ea6d7fdc7d9ea33683 Mon Sep 17 00:00:00 2001 From: stack Date: Tue, 5 Jul 2016 11:48:08 -0700 Subject: [PATCH] HBASE-16074 ITBLL fails, reports lost big or tiny families Change HFile Writer constructor so we pass in the TimeRangeTracker, if one, on construction rather than set later (the flag and reference were not volatile so could have made for issues in concurrent case) 2. Make sure the construction of a TimeRange from a TimeRangeTracer on open of an HFile Reader never makes a bad minimum value, one that would preclude us reading any values from a file (add a log and set min to 0) M hbase-common/src/main/java/org/apache/hadoop/hbase/io/TimeRange.java Call through to next constructor (if minStamp was 0, we'd skip setting allTime=true) M hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HStore.java Add constructor override that takes a TimeRangeTracker (set when flushing but not when compacting) M hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/Store.java Add override creating an HFile in tmp that takes a TimeRangeTracker M hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFile.java Add override for HFile Writer that takes a TimeRangeTracker Take it on construction instead of having it passed by a setter later (flags and reference set by the setter were not volatile... could have been prob in concurrent case) M hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/TimeRangeTracker.java Log WARN if bad initial TimeRange value (and then 'fix' it) M hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestTimeRangeTracker.java A few tests to prove serialization works as expected and that we'll get a bad min if not constructed properly. --- .../java/org/apache/hadoop/hbase/client/Query.java | 13 ++--- .../java/org/apache/hadoop/hbase/io/TimeRange.java | 48 ++++++++++------- .../hbase/regionserver/DefaultStoreFlusher.java | 4 +- .../apache/hadoop/hbase/regionserver/HStore.java | 27 ++++++++-- .../apache/hadoop/hbase/regionserver/Store.java | 20 ++++++- .../hadoop/hbase/regionserver/StoreFile.java | 63 ++++++++++++++++------ .../hbase/regionserver/StripeStoreFlusher.java | 4 +- .../hbase/regionserver/TimeRangeTracker.java | 23 ++++---- .../hbase/regionserver/TestTimeRangeTracker.java | 30 +++++++++++ 9 files changed, 163 insertions(+), 69 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Query.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Query.java index 53e680d..99d5a6a 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Query.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Query.java @@ -17,7 +17,6 @@ */ package org.apache.hadoop.hbase.client; -import java.io.IOException; import java.util.Map; import com.google.common.collect.Maps; @@ -193,12 +192,8 @@ public abstract class Query extends OperationWithAttributes { */ public Query setColumnFamilyTimeRange(byte[] cf, long minStamp, long maxStamp) { - try { - colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp)); - return this; - } catch (IOException ioe) { - throw new IllegalArgumentException(ioe); - } + colFamTimeRangeMap.put(cf, new TimeRange(minStamp, maxStamp)); + return this; } /** @@ -207,6 +202,4 @@ public abstract class Query extends OperationWithAttributes { public Map getColumnFamilyTimeRange() { return this.colFamTimeRangeMap; } - - -} +} \ No newline at end of file diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/TimeRange.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/TimeRange.java index 2b70644..81146ed 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/TimeRange.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/TimeRange.java @@ -36,12 +36,10 @@ import org.apache.hadoop.hbase.util.Bytes; @InterfaceAudience.Public @InterfaceStability.Stable public class TimeRange { - static final long INITIAL_MIN_TIMESTAMP = 0L; - private static final long MIN_TIME = INITIAL_MIN_TIMESTAMP; - static final long INITIAL_MAX_TIMESTAMP = Long.MAX_VALUE; - static final long MAX_TIME = INITIAL_MAX_TIMESTAMP; - private long minStamp = MIN_TIME; - private long maxStamp = MAX_TIME; + public static final long INITIAL_MIN_TIMESTAMP = 0L; + public static final long INITIAL_MAX_TIMESTAMP = Long.MAX_VALUE; + private long minStamp; + private long maxStamp; private final boolean allTime; /** @@ -51,7 +49,7 @@ public class TimeRange { */ @Deprecated public TimeRange() { - allTime = true; + this(INITIAL_MIN_TIMESTAMP, INITIAL_MAX_TIMESTAMP); } /** @@ -61,8 +59,7 @@ public class TimeRange { */ @Deprecated public TimeRange(long minStamp) { - this.minStamp = minStamp; - this.allTime = this.minStamp == MIN_TIME; + this(minStamp, INITIAL_MAX_TIMESTAMP); } /** @@ -72,8 +69,7 @@ public class TimeRange { */ @Deprecated public TimeRange(byte [] minStamp) { - this.minStamp = Bytes.toLong(minStamp); - this.allTime = false; + this(Bytes.toLong(minStamp), INITIAL_MAX_TIMESTAMP); } /** @@ -85,17 +81,25 @@ public class TimeRange { * @deprecated This is made @InterfaceAudience.Private in the 2.0 line and above */ @Deprecated - public TimeRange(long minStamp, long maxStamp) throws IOException { + public TimeRange(long minStamp, long maxStamp) { + check(minStamp, maxStamp); + this.minStamp = minStamp; + this.maxStamp = maxStamp; + this.allTime = isAllTime(minStamp, maxStamp); + } + + private static boolean isAllTime(long minStamp, long maxStamp) { + return minStamp == INITIAL_MIN_TIMESTAMP && maxStamp == INITIAL_MAX_TIMESTAMP; + } + + private static void check(long minStamp, long maxStamp) { if (minStamp < 0 || maxStamp < 0) { throw new IllegalArgumentException("Timestamp cannot be negative. minStamp:" + minStamp + ", maxStamp:" + maxStamp); } if (maxStamp < minStamp) { - throw new IOException("maxStamp is smaller than minStamp"); + throw new IllegalArgumentException("maxStamp is smaller than minStamp"); } - this.minStamp = minStamp; - this.maxStamp = maxStamp; - this.allTime = this.minStamp == MIN_TIME && this.maxStamp == MAX_TIME; } /** @@ -143,7 +147,9 @@ public class TimeRange { * @return true if within TimeRange, false if not */ public boolean withinTimeRange(byte [] bytes, int offset) { - if(allTime) return true; + if (allTime) { + return true; + } return withinTimeRange(Bytes.toLong(bytes, offset)); } @@ -186,7 +192,9 @@ public class TimeRange { * @return true if within TimeRange, false if not */ public boolean withinOrAfterTimeRange(long timestamp) { - if(allTime) return true; + if (allTime) { + return true; + } // check if >= minStamp return (timestamp >= minStamp); } @@ -199,7 +207,9 @@ public class TimeRange { * 1 if timestamp is greater than timerange */ public int compare(long timestamp) { - if (allTime) return 0; + if (allTime) { + return 0; + } if (timestamp < minStamp) { return -1; } else if (timestamp >= maxStamp) { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/DefaultStoreFlusher.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/DefaultStoreFlusher.java index 935813c..90c16f9 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/DefaultStoreFlusher.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/DefaultStoreFlusher.java @@ -68,8 +68,8 @@ public class DefaultStoreFlusher extends StoreFlusher { /* isCompaction = */ false, /* includeMVCCReadpoint = */ true, /* includesTags = */ snapshot.isTagsPresent(), - /* shouldDropBehind = */ false); - writer.setTimeRangeTracker(snapshot.getTimeRangeTracker()); + /* shouldDropBehind = */ false, + snapshot.getTimeRangeTracker()); IOException e = null; try { performFlush(scanner, writer, smallestReadPoint, throughputController); 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 db66641..66eaebc 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 @@ -999,6 +999,23 @@ public class HStore implements Store { boolean isCompaction, boolean includeMVCCReadpoint, boolean includesTag, boolean shouldDropBehind) throws IOException { + return createWriterInTmp(maxKeyCount, compression, isCompaction, includeMVCCReadpoint, + includesTag, shouldDropBehind, null); + } + + /* + * @param maxKeyCount + * @param compression Compression algorithm to use + * @param isCompaction whether we are creating a new file in a compaction + * @param includesMVCCReadPoint - whether to include MVCC or not + * @param includesTag - includesTag or not + * @return Writer for a new StoreFile in the tmp dir. + */ + @Override + public StoreFile.Writer createWriterInTmp(long maxKeyCount, Compression.Algorithm compression, + boolean isCompaction, boolean includeMVCCReadpoint, boolean includesTag, + boolean shouldDropBehind, final TimeRangeTracker trt) + throws IOException { final CacheConfig writerCacheConf; if (isCompaction) { // Don't cache data on write on compactions. @@ -1014,7 +1031,7 @@ public class HStore implements Store { } HFileContext hFileContext = createFileContext(compression, includeMVCCReadpoint, includesTag, cryptoContext); - StoreFile.Writer w = new StoreFile.WriterBuilder(conf, writerCacheConf, + StoreFile.WriterBuilder builder = new StoreFile.WriterBuilder(conf, writerCacheConf, this.getFileSystem()) .withFilePath(fs.createTempName()) .withComparator(comparator) @@ -1022,9 +1039,11 @@ public class HStore implements Store { .withMaxKeyCount(maxKeyCount) .withFavoredNodes(favoredNodes) .withFileContext(hFileContext) - .withShouldDropCacheBehind(shouldDropBehind) - .build(); - return w; + .withShouldDropCacheBehind(shouldDropBehind); + if (trt != null) { + builder.withTimeRangeTracker(trt); + } + return builder.build(); } private HFileContext createFileContext(Compression.Algorithm compression, 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 dec27ad..e7a4de5 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 @@ -211,8 +211,24 @@ public interface Store extends HeapSize, StoreConfigInformation, PropagatingConf boolean shouldDropBehind ) throws IOException; - - + /** + * @param maxKeyCount + * @param compression Compression algorithm to use + * @param isCompaction whether we are creating a new file in a compaction + * @param includeMVCCReadpoint whether we should out the MVCC readpoint + * @param shouldDropBehind should the writer drop caches behind writes + * @param trt Ready-made timetracker to use. + * @return Writer for a new StoreFile in the tmp dir. + */ + StoreFile.Writer createWriterInTmp( + long maxKeyCount, + Compression.Algorithm compression, + boolean isCompaction, + boolean includeMVCCReadpoint, + boolean includesTags, + boolean shouldDropBehind, + final TimeRangeTracker trt + ) throws IOException; // Compaction oriented methods 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 845a8d2..4abc153 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 @@ -617,6 +617,7 @@ public class StoreFile { private Path filePath; private InetSocketAddress[] favoredNodes; private HFileContext fileContext; + private TimeRangeTracker trt; public WriterBuilder(Configuration conf, CacheConfig cacheConf, FileSystem fs) { @@ -626,6 +627,17 @@ public class StoreFile { } /** + * @param trt A premade TimeRangeTracker to use rather than build one per append (building one + * of these is expensive so good to pass one in if you have one). + * @return this (for chained invocation) + */ + public WriterBuilder withTimeRangeTracker(final TimeRangeTracker trt) { + Preconditions.checkNotNull(trt); + this.trt = trt; + return this; + } + + /** * Use either this method or {@link #withFilePath}, but not both. * @param dir Path to column family directory. The directory is created if * does not exist. The file is given a unique name within this @@ -718,7 +730,7 @@ public class StoreFile { comparator = KeyValue.COMPARATOR; } return new Writer(fs, filePath, - conf, cacheConf, comparator, bloomType, maxKeyCount, favoredNodes, fileContext); + conf, cacheConf, comparator, bloomType, maxKeyCount, favoredNodes, fileContext, trt); } } @@ -794,7 +806,6 @@ public class StoreFile { private Cell lastDeleteFamilyCell = null; private long deleteFamilyCnt = 0; - TimeRangeTracker timeRangeTracker = new TimeRangeTracker(); /** * timeRangeTrackerSet is used to figure if we were passed a filled-out TimeRangeTracker or not. * When flushing a memstore, we set the TimeRangeTracker that it accumulated during updates to @@ -802,7 +813,8 @@ public class StoreFile { * recalculate the timeRangeTracker bounds; it was done already as part of add-to-memstore. * A completed TimeRangeTracker is not set in cases of compactions when it is recalculated. */ - boolean timeRangeTrackerSet = false; + private final boolean timeRangeTrackerSet; + final TimeRangeTracker timeRangeTracker; protected HFile.Writer writer; @@ -825,6 +837,36 @@ public class StoreFile { final KVComparator comparator, BloomType bloomType, long maxKeys, InetSocketAddress[] favoredNodes, HFileContext fileContext) throws IOException { + this(fs, path, conf, cacheConf, comparator, bloomType, maxKeys, favoredNodes, fileContext, + null); + } + + /** + * Creates an HFile.Writer that also write helpful meta data. + * @param fs file system to write to + * @param path file name to create + * @param conf user configuration + * @param comparator key comparator + * @param bloomType bloom filter setting + * @param maxKeys the expected maximum number of keys to be added. Was used + * for Bloom filter size in {@link HFile} format version 1. + * @param favoredNodes + * @param fileContext - The HFile context + * @param trt Ready-made timetracker to use. + * @throws IOException problem writing to FS + */ + private Writer(FileSystem fs, Path path, + final Configuration conf, + CacheConfig cacheConf, + final KVComparator comparator, BloomType bloomType, long maxKeys, + InetSocketAddress[] favoredNodes, HFileContext fileContext, + final TimeRangeTracker trt) + throws IOException { + // If passed a TimeRangeTracker, use it. Set timeRangeTrackerSet so we don't destroy it. + // TODO: put the state of the TRT on the TRT; i.e. make a read-only version (TimeRange) when + // it no longer writable. + this.timeRangeTrackerSet = trt != null; + this.timeRangeTracker = trt != null? trt: new TimeRangeTracker(); writer = HFile.getWriterFactory(conf, cacheConf) .withPath(fs, path) .withComparator(comparator) @@ -886,19 +928,6 @@ public class StoreFile { } /** - * Set TimeRangeTracker. - * Called when flushing to pass us a pre-calculated TimeRangeTracker, one made during updates - * to memstore so we don't have to make one ourselves as Cells get appended. Call before first - * append. If this method is not called, we will calculate our own range of the Cells that - * comprise this StoreFile (and write them on the end as metadata). It is good to have this stuff - * passed because it is expensive to make. - */ - public void setTimeRangeTracker(final TimeRangeTracker trt) { - this.timeRangeTracker = trt; - timeRangeTrackerSet = true; - } - - /** * Record the earlest Put timestamp. * * If the timeRangeTracker is not set, @@ -1657,7 +1686,7 @@ public class StoreFile { } public long getMaxTimestamp() { - return timeRange == null ? Long.MAX_VALUE : timeRange.getMax(); + return timeRange == null ? TimeRange.INITIAL_MAX_TIMESTAMP: timeRange.getMax(); } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StripeStoreFlusher.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StripeStoreFlusher.java index 0c3432c..c367b52 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StripeStoreFlusher.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StripeStoreFlusher.java @@ -115,8 +115,8 @@ public class StripeStoreFlusher extends StoreFlusher { /* isCompaction = */ false, /* includeMVCCReadpoint = */ true, /* includesTags = */ true, - /* shouldDropBehind = */ false); - writer.setTimeRangeTracker(tracker); + /* shouldDropBehind = */ false, + tracker); return writer; } }; diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/TimeRangeTracker.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/TimeRangeTracker.java index f4175cc..78950f8 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/TimeRangeTracker.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/TimeRangeTracker.java @@ -30,7 +30,7 @@ import org.apache.hadoop.hbase.util.Writables; import org.apache.hadoop.io.Writable; /** - * Stores minimum and maximum timestamp values. Both timestamps are inclusive. + * Stores minimum and maximum timestamp values. * Use this class at write-time ONLY. Too much synchronization to use at read time * (TODO: there are two scenarios writing, once when lots of concurrency as part of memstore * updates but then later we can make one as part of a compaction when there is only one thread @@ -45,8 +45,8 @@ import org.apache.hadoop.io.Writable; @InterfaceAudience.Private public class TimeRangeTracker implements Writable { static final long INITIAL_MIN_TIMESTAMP = Long.MAX_VALUE; + static final long INITIAL_MAX_TIMESTAMP = -1L; long minimumTimestamp = INITIAL_MIN_TIMESTAMP; - static final long INITIAL_MAX_TIMESTAMP = -1; long maximumTimestamp = INITIAL_MAX_TIMESTAMP; /** @@ -125,7 +125,7 @@ public class TimeRangeTracker implements Writable { } /** - * Check if the range has any overlap with TimeRange + * Check if the range has ANY overlap with TimeRange * @param tr TimeRange * @return True if there is overlap, false otherwise */ @@ -185,22 +185,19 @@ public class TimeRangeTracker implements Writable { return trt == null? null: trt.toTimeRange(); } - private boolean isFreshInstance() { - return getMin() == INITIAL_MIN_TIMESTAMP && getMax() == INITIAL_MAX_TIMESTAMP; - } - /** * @return Make a TimeRange from current state of this. */ TimeRange toTimeRange() throws IOException { long min = getMin(); long max = getMax(); - // Check for the case where the TimeRangeTracker is fresh. In that case it has - // initial values that are antithetical to a TimeRange... Return an uninitialized TimeRange - // if passed an uninitialized TimeRangeTracker. - if (isFreshInstance()) { - return new TimeRange(); + // Initial TimeRangeTracker timestamps are the opposite of what you want for a TimeRange. Fix! + if (min == INITIAL_MIN_TIMESTAMP) { + min = TimeRange.INITIAL_MIN_TIMESTAMP; + } + if (max == INITIAL_MAX_TIMESTAMP) { + max = TimeRange.INITIAL_MAX_TIMESTAMP; } return new TimeRange(min, max); } -} +} \ No newline at end of file diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestTimeRangeTracker.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestTimeRangeTracker.java index 46fe611..cfd80e2 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestTimeRangeTracker.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestTimeRangeTracker.java @@ -17,16 +17,46 @@ */ package org.apache.hadoop.hbase.regionserver; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.IOException; + import org.apache.hadoop.hbase.io.TimeRange; import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.hbase.util.Writables; import org.junit.Test; import org.junit.experimental.categories.Category; @Category({SmallTests.class}) public class TestTimeRangeTracker { @Test + public void testTimeRangeInitialized() { + TimeRangeTracker src = new TimeRangeTracker(); + TimeRange tr = new TimeRange(System.currentTimeMillis()); + assertFalse(src.includesTimeRange(tr)); + } + + @Test + public void testTimeRangeTrackerNullIsSameAsTimeRangeNull() throws IOException { + TimeRangeTracker src = new TimeRangeTracker(1, 2); + byte [] bytes = Writables.getBytes(src); + TimeRange tgt = TimeRangeTracker.getTimeRange(bytes); + assertEquals(src.getMin(), tgt.getMin()); + assertEquals(src.getMax(), tgt.getMax()); + } + + @Test + public void testSerialization() throws IOException { + TimeRangeTracker src = new TimeRangeTracker(1, 2); + TimeRangeTracker tgt = new TimeRangeTracker(); + Writables.copyWritable(src, tgt); + assertEquals(src.getMin(), tgt.getMin()); + assertEquals(src.getMax(), tgt.getMax()); + } + + @Test public void testAlwaysDecrementingSetsMaximum() { TimeRangeTracker trr = new TimeRangeTracker(); trr.includeTimestamp(3); -- 2.6.1