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.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import org.apache.hadoop.hbase.io.TimeRange;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.util.Writables;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category({SmallTests.class})
33  public class TestTimeRangeTracker {
34    @Test
35    public void testExtreme() {
36      TimeRange tr = new TimeRange();
37      assertTrue(tr.includesTimeRange(new TimeRange()));
38      TimeRangeTracker trt = new TimeRangeTracker();
39      assertFalse(trt.includesTimeRange(new TimeRange()));
40      trt.includeTimestamp(1);
41      trt.includeTimestamp(10);
42      assertTrue(trt.includesTimeRange(new TimeRange()));
43    }
44  
45    @Test
46    public void testTimeRangeInitialized() {
47      TimeRangeTracker src = new TimeRangeTracker();
48      TimeRange tr = new TimeRange(System.currentTimeMillis());
49      assertFalse(src.includesTimeRange(tr));
50    }
51  
52    @Test
53    public void testTimeRangeTrackerNullIsSameAsTimeRangeNull() throws IOException {
54      TimeRangeTracker src = new TimeRangeTracker(1, 2);
55      byte [] bytes = Writables.getBytes(src);
56      TimeRange tgt = TimeRangeTracker.getTimeRange(bytes);
57      assertEquals(src.getMin(), tgt.getMin());
58      assertEquals(src.getMax(), tgt.getMax());
59    }
60  
61    @Test
62    public void testSerialization() throws IOException {
63      TimeRangeTracker src = new TimeRangeTracker(1, 2);
64      TimeRangeTracker tgt = new TimeRangeTracker();
65      Writables.copyWritable(src, tgt);
66      assertEquals(src.getMin(), tgt.getMin());
67      assertEquals(src.getMax(), tgt.getMax());
68    }
69  
70    @Test
71    public void testAlwaysDecrementingSetsMaximum() {
72      TimeRangeTracker trr = new TimeRangeTracker();
73      trr.includeTimestamp(3);
74      trr.includeTimestamp(2);
75      trr.includeTimestamp(1);
76      assertTrue(trr.getMin() != TimeRangeTracker.INITIAL_MIN_TIMESTAMP);
77      assertTrue(trr.getMax() != -1 /*The initial max value*/);
78    }
79  
80    @Test
81    public void testSimpleInRange() {
82      TimeRangeTracker trr = new TimeRangeTracker();
83      trr.includeTimestamp(0);
84      trr.includeTimestamp(2);
85      assertTrue(trr.includesTimeRange(new TimeRange(1)));
86    }
87  
88    /**
89     * Run a bunch of threads against a single TimeRangeTracker and ensure we arrive
90     * at right range.  Here we do ten threads each incrementing over 100k at an offset
91     * of the thread index; max is 10 * 10k and min is 0.
92     * @throws InterruptedException
93     */
94    @Test
95    public void testArriveAtRightAnswer() throws InterruptedException {
96      final TimeRangeTracker trr = new TimeRangeTracker();
97      final int threadCount = 10;
98      final int calls = 1000 * 1000;
99      Thread [] threads = new Thread[threadCount];
100     for (int i = 0; i < threads.length; i++) {
101       Thread t = new Thread("" + i) {
102         @Override
103         public void run() {
104           int offset = Integer.parseInt(getName());
105           boolean even = offset % 2 == 0;
106           if (even) {
107             for (int i = (offset * calls); i < calls; i++) trr.includeTimestamp(i);
108           } else {
109             int base = offset * calls;
110             for (int i = base + calls; i >= base; i--) trr.includeTimestamp(i);
111           }
112         }
113       };
114       t.start();
115       threads[i] = t;
116     }
117     for (int i = 0; i < threads.length; i++) {
118       threads[i].join();
119     }
120     assertTrue(trr.getMax() == calls * threadCount);
121     assertTrue(trr.getMin() == 0);
122   }
123 
124   /**
125    * Bit of code to test concurrent access on this class.
126    * @param args
127    * @throws InterruptedException
128    */
129   public static void main(String[] args) throws InterruptedException {
130     long start = System.currentTimeMillis();
131     final TimeRangeTracker trr = new TimeRangeTracker();
132     final int threadCount = 5;
133     final int calls = 1024 * 1024 * 128;
134     Thread [] threads = new Thread[threadCount];
135     for (int i = 0; i < threads.length; i++) {
136       Thread t = new Thread("" + i) {
137         @Override
138         public void run() {
139           for (int i = 0; i < calls; i++) trr.includeTimestamp(i);
140         }
141       };
142       t.start();
143       threads[i] = t;
144     }
145     for (int i = 0; i < threads.length; i++) {
146       threads[i].join();
147     }
148     System.out.println(trr.getMin() + " " + trr.getMax() + " " +
149       (System.currentTimeMillis() - start));
150   }
151 }