001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.DataOutputStream;
025import java.io.IOException;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.io.ByteArrayOutputStream;
028import org.apache.hadoop.hbase.io.TimeRange;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({ RegionServerTests.class, SmallTests.class })
037public class TestSimpleTimeRangeTracker {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestSimpleTimeRangeTracker.class);
042
043  protected TimeRangeTracker getTimeRangeTracker() {
044    return TimeRangeTracker.create(TimeRangeTracker.Type.NON_SYNC);
045  }
046
047  protected TimeRangeTracker getTimeRangeTracker(long min, long max) {
048    return TimeRangeTracker.create(TimeRangeTracker.Type.NON_SYNC, min, max);
049  }
050
051  @Test
052  public void testExtreme() {
053    TimeRange tr = TimeRange.allTime();
054    assertTrue(tr.includesTimeRange(TimeRange.allTime()));
055    TimeRangeTracker trt = getTimeRangeTracker();
056    assertFalse(trt.includesTimeRange(TimeRange.allTime()));
057    trt.includeTimestamp(1);
058    trt.includeTimestamp(10);
059    assertTrue(trt.includesTimeRange(TimeRange.allTime()));
060  }
061
062  @Test
063  public void testTimeRangeInitialized() {
064    TimeRangeTracker src = getTimeRangeTracker();
065    TimeRange tr = TimeRange.from(EnvironmentEdgeManager.currentTime());
066    assertFalse(src.includesTimeRange(tr));
067  }
068
069  @Test
070  public void testTimeRangeTrackerNullIsSameAsTimeRangeNull() throws IOException {
071    TimeRangeTracker src = getTimeRangeTracker(1, 2);
072    byte[] bytes = TimeRangeTracker.toByteArray(src);
073    TimeRange tgt = TimeRangeTracker.parseFrom(bytes).toTimeRange();
074    assertEquals(src.getMin(), tgt.getMin());
075    assertEquals(src.getMax(), tgt.getMax());
076  }
077
078  @Test
079  public void testSerialization() throws IOException {
080    TimeRangeTracker src = getTimeRangeTracker(1, 2);
081    TimeRangeTracker tgt = TimeRangeTracker.parseFrom(TimeRangeTracker.toByteArray(src));
082    assertEquals(src.getMin(), tgt.getMin());
083    assertEquals(src.getMax(), tgt.getMax());
084  }
085
086  @Test
087  public void testLegacySerialization() throws IOException {
088    ByteArrayOutputStream data = new ByteArrayOutputStream();
089    DataOutputStream output = new DataOutputStream(data);
090    output.writeLong(100);
091    output.writeLong(200);
092    TimeRangeTracker tgt = TimeRangeTracker.parseFrom(data.toByteArray());
093    assertEquals(100, tgt.getMin());
094    assertEquals(200, tgt.getMax());
095  }
096
097  @Test
098  public void testAlwaysDecrementingSetsMaximum() {
099    TimeRangeTracker trr = getTimeRangeTracker();
100    trr.includeTimestamp(3);
101    trr.includeTimestamp(2);
102    trr.includeTimestamp(1);
103    assertTrue(trr.getMin() != TimeRangeTracker.INITIAL_MIN_TIMESTAMP);
104    assertTrue(trr.getMax() != -1 /* The initial max value */);
105  }
106
107  @Test
108  public void testSimpleInRange() {
109    TimeRangeTracker trr = getTimeRangeTracker();
110    trr.includeTimestamp(0);
111    trr.includeTimestamp(2);
112    assertTrue(trr.includesTimeRange(TimeRange.from(1)));
113  }
114
115  @Test
116  public void testRangeConstruction() throws IOException {
117    TimeRange defaultRange = TimeRange.allTime();
118    assertEquals(0L, defaultRange.getMin());
119    assertEquals(Long.MAX_VALUE, defaultRange.getMax());
120    assertTrue(defaultRange.isAllTime());
121
122    TimeRange oneArgRange = TimeRange.from(0L);
123    assertEquals(0L, oneArgRange.getMin());
124    assertEquals(Long.MAX_VALUE, oneArgRange.getMax());
125    assertTrue(oneArgRange.isAllTime());
126
127    TimeRange oneArgRange2 = TimeRange.from(1);
128    assertEquals(1, oneArgRange2.getMin());
129    assertEquals(Long.MAX_VALUE, oneArgRange2.getMax());
130    assertFalse(oneArgRange2.isAllTime());
131
132    TimeRange twoArgRange = TimeRange.between(0L, Long.MAX_VALUE);
133    assertEquals(0L, twoArgRange.getMin());
134    assertEquals(Long.MAX_VALUE, twoArgRange.getMax());
135    assertTrue(twoArgRange.isAllTime());
136
137    TimeRange twoArgRange2 = TimeRange.between(0L, Long.MAX_VALUE - 1);
138    assertEquals(0L, twoArgRange2.getMin());
139    assertEquals(Long.MAX_VALUE - 1, twoArgRange2.getMax());
140    assertFalse(twoArgRange2.isAllTime());
141
142    TimeRange twoArgRange3 = TimeRange.between(1, Long.MAX_VALUE);
143    assertEquals(1, twoArgRange3.getMin());
144    assertEquals(Long.MAX_VALUE, twoArgRange3.getMax());
145    assertFalse(twoArgRange3.isAllTime());
146  }
147
148}