HBASE-12148 Remove TimeRangeTracker as point of contention when many threads writing a Store
Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/319a9d3f Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/319a9d3f Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/319a9d3f Branch: refs/heads/branch-1 Commit: 319a9d3ffb2f958dc8e7af757416a08ca9428d79 Parents: ccd1605 Author: stack <st...@apache.org> Authored: Thu Oct 2 12:41:07 2014 -0700 Committer: stack <st...@apache.org> Committed: Thu Oct 2 12:41:07 2014 -0700 ---------------------------------------------------------------------- .../regionserver/TestTimeRangeTracker.java | 110 +++++++++++++++++++ 1 file changed, 110 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/319a9d3f/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestTimeRangeTracker.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..fff597e --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestTimeRangeTracker.java @@ -0,0 +1,110 @@ +/** + * 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 + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.regionserver; + +import static org.junit.Assert.assertTrue; + +import org.apache.hadoop.hbase.io.TimeRange; +import org.apache.hadoop.hbase.SmallTests; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({SmallTests.class}) +public class TestTimeRangeTracker { + @Test + public void testAlwaysDecrementingSetsMaximum() { + TimeRangeTracker trr = new TimeRangeTracker(); + trr.includeTimestamp(3); + trr.includeTimestamp(2); + trr.includeTimestamp(1); + assertTrue(trr.getMinimumTimestamp() != TimeRangeTracker.INITIAL_MINIMUM_TIMESTAMP); + assertTrue(trr.getMaximumTimestamp() != -1 /*The initial max value*/); + } + + @Test + public void testSimpleInRange() { + TimeRangeTracker trr = new TimeRangeTracker(); + trr.includeTimestamp(0); + trr.includeTimestamp(2); + assertTrue(trr.includesTimeRange(new TimeRange(1))); + } + + /** + * Run a bunch of threads against a single TimeRangeTracker and ensure we arrive + * at right range. Here we do ten threads each incrementing over 100k at an offset + * of the thread index; max is 10 * 10k and min is 0. + * @throws InterruptedException + */ + @Test + public void testArriveAtRightAnswer() throws InterruptedException { + final TimeRangeTracker trr = new TimeRangeTracker(); + final int threadCount = 10; + final int calls = 1000 * 1000; + Thread [] threads = new Thread[threadCount]; + for (int i = 0; i < threads.length; i++) { + Thread t = new Thread("" + i) { + @Override + public void run() { + int offset = Integer.parseInt(getName()); + boolean even = offset % 2 == 0; + if (even) { + for (int i = (offset * calls); i < calls; i++) trr.includeTimestamp(i); + } else { + int base = offset * calls; + for (int i = base + calls; i >= base; i--) trr.includeTimestamp(i); + } + } + }; + t.start(); + threads[i] = t; + } + for (int i = 0; i < threads.length; i++) { + threads[i].join(); + } + assertTrue(trr.getMaximumTimestamp() == calls * threadCount); + assertTrue(trr.getMinimumTimestamp() == 0); + } + + /** + * Bit of code to test concurrent access on this class. + * @param args + * @throws InterruptedException + */ + public static void main(String[] args) throws InterruptedException { + long start = System.currentTimeMillis(); + final TimeRangeTracker trr = new TimeRangeTracker(); + final int threadCount = 5; + final int calls = 1024 * 1024 * 128; + Thread [] threads = new Thread[threadCount]; + for (int i = 0; i < threads.length; i++) { + Thread t = new Thread("" + i) { + @Override + public void run() { + for (int i = 0; i < calls; i++) trr.includeTimestamp(i); + } + }; + t.start(); + threads[i] = t; + } + for (int i = 0; i < threads.length; i++) { + threads[i].join(); + } + System.out.println(trr.getMinimumTimestamp() + " " + trr.getMaximumTimestamp() + " " + + (System.currentTimeMillis() - start)); + } +}