View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import org.apache.hadoop.hbase.io.TimeRange;
23  import org.apache.hadoop.hbase.testclassification.SmallTests;
24  import org.junit.Test;
25  import org.junit.experimental.categories.Category;
26  
27  @Category({SmallTests.class})
28  public class TestTimeRangeTracker {
29    @Test
30    public void testAlwaysDecrementingSetsMaximum() {
31      TimeRangeTracker trr = new TimeRangeTracker();
32      trr.includeTimestamp(3);
33      trr.includeTimestamp(2);
34      trr.includeTimestamp(1);
35      assertTrue(trr.getMinimumTimestamp() != TimeRangeTracker.INITIAL_MINIMUM_TIMESTAMP);
36      assertTrue(trr.getMaximumTimestamp() != -1 /*The initial max value*/);
37    }
38  
39    @Test
40    public void testSimpleInRange() {
41      TimeRangeTracker trr = new TimeRangeTracker();
42      trr.includeTimestamp(0);
43      trr.includeTimestamp(2);
44      assertTrue(trr.includesTimeRange(new TimeRange(1)));
45    }
46  
47    /**
48     * Run a bunch of threads against a single TimeRangeTracker and ensure we arrive
49     * at right range.  Here we do ten threads each incrementing over 100k at an offset
50     * of the thread index; max is 10 * 10k and min is 0.
51     * @throws InterruptedException
52     */
53    @Test
54    public void testArriveAtRightAnswer() throws InterruptedException {
55      final TimeRangeTracker trr = new TimeRangeTracker();
56      final int threadCount = 10;
57      final int calls = 1000 * 1000;
58      Thread [] threads = new Thread[threadCount];
59      for (int i = 0; i < threads.length; i++) {
60        Thread t = new Thread("" + i) {
61          @Override
62          public void run() {
63            int offset = Integer.parseInt(getName());
64            boolean even = offset % 2 == 0;
65            if (even) {
66              for (int i = (offset * calls); i < calls; i++) trr.includeTimestamp(i);
67            } else {
68              int base = offset * calls;
69              for (int i = base + calls; i >= base; i--) trr.includeTimestamp(i);
70            }
71          }
72        };
73        t.start();
74        threads[i] = t;
75      }
76      for (int i = 0; i < threads.length; i++) {
77        threads[i].join();
78      }
79      assertTrue(trr.getMaximumTimestamp() == calls * threadCount);
80      assertTrue(trr.getMinimumTimestamp() == 0);
81    }
82  
83    /**
84     * Bit of code to test concurrent access on this class.
85     * @param args
86     * @throws InterruptedException
87     */
88    public static void main(String[] args) throws InterruptedException {
89      long start = System.currentTimeMillis();
90      final TimeRangeTracker trr = new TimeRangeTracker();
91      final int threadCount = 5;
92      final int calls = 1024 * 1024 * 128;
93      Thread [] threads = new Thread[threadCount];
94      for (int i = 0; i < threads.length; i++) {
95        Thread t = new Thread("" + i) {
96          @Override
97          public void run() {
98            for (int i = 0; i < calls; i++) trr.includeTimestamp(i);
99          }
100       };
101       t.start();
102       threads[i] = t;
103     }
104     for (int i = 0; i < threads.length; i++) {
105       threads[i].join();
106     }
107     System.out.println(trr.getMinimumTimestamp() + " " + trr.getMaximumTimestamp() + " " +
108       (System.currentTimeMillis() - start));
109   }
110 }