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.assertTrue;
022
023import java.io.IOException;
024import java.util.Arrays;
025import java.util.List;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.Cell;
029import org.apache.hadoop.hbase.CellComparator;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtility;
032import org.apache.hadoop.hbase.HColumnDescriptor;
033import org.apache.hadoop.hbase.HConstants;
034import org.apache.hadoop.hbase.HRegionInfo;
035import org.apache.hadoop.hbase.HTableDescriptor;
036import org.apache.hadoop.hbase.KeyValue;
037import org.apache.hadoop.hbase.TableName;
038import org.apache.hadoop.hbase.testclassification.RegionServerTests;
039import org.apache.hadoop.hbase.testclassification.SmallTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
042import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
043import org.junit.After;
044import org.junit.Before;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048
049/**
050 * Test the {@link MemStoreCompactorSegmentsIterator} and {@link MemStoreMergerSegmentsIterator}
051 * class, Test for bug : HBASE-22324
052 */
053@Category({ RegionServerTests.class, SmallTests.class })
054public class TestMemStoreSegmentsIterator {
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058    HBaseClassTestRule.forClass(TestMemStoreSegmentsIterator.class);
059
060  private static String TABLE = "test_mscsi";
061  private static String FAMILY = "f";
062  private static String COLUMN = "c";
063  private static String ROOT_SUB_PATH = "testMemStoreSegmentsIterator";
064  private static long LESS_THAN_INTEGER_MAX_VALUE_SEQ_ID = Long.valueOf(Integer.MAX_VALUE) - 1;
065  private static long GREATER_THAN_INTEGER_MAX_VALUE_SEQ_ID = Long.valueOf(Integer.MAX_VALUE) + 1;
066
067  private CellComparator comparator;
068  private int compactionKVMax;
069  private HRegion region;
070  private HStore store;
071
072  @Before
073  public void setup() throws IOException {
074    Configuration conf = new Configuration();
075    HBaseTestingUtility hbaseUtility = HBaseTestingUtility.createLocalHTU(conf);
076    HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
077    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLE));
078    htd.addFamily(hcd);
079    HRegionInfo info = new HRegionInfo(TableName.valueOf(TABLE), null, null, false);
080    Path rootPath = hbaseUtility.getDataTestDir(ROOT_SUB_PATH);
081    this.region = HBaseTestingUtility.createRegionAndWAL(info, rootPath, conf, htd);
082    this.store = region.getStore(hcd.getName());
083    this.comparator = CellComparator.getInstance();
084    this.compactionKVMax = HConstants.COMPACTION_KV_MAX_DEFAULT;
085  }
086
087  @Test
088  public void testMemStoreCompactorSegmentsIteratorNext() throws IOException {
089    List<ImmutableSegment> segments = Arrays.asList(createTestImmutableSegment());
090    MemStoreCompactorSegmentsIterator iterator = new MemStoreCompactorSegmentsIterator(segments,
091      this.comparator, this.compactionKVMax, this.store);
092    verifyNext(iterator);
093    closeTestSegments(segments);
094  }
095
096  @Test
097  public void testMemStoreMergerSegmentsIteratorNext() throws IOException {
098    List<ImmutableSegment> segments = Arrays.asList(createTestImmutableSegment());
099    MemStoreMergerSegmentsIterator iterator =
100      new MemStoreMergerSegmentsIterator(segments, this.comparator, this.compactionKVMax);
101    verifyNext(iterator);
102    closeTestSegments(segments);
103  }
104
105  protected ImmutableSegment createTestImmutableSegment() {
106    ImmutableSegment segment1 = SegmentFactory.instance().createImmutableSegment(this.comparator);
107    final byte[] one = Bytes.toBytes(1);
108    final byte[] two = Bytes.toBytes(2);
109    final byte[] f = Bytes.toBytes(FAMILY);
110    final byte[] q = Bytes.toBytes(COLUMN);
111    final byte[] v = Bytes.toBytes(3);
112    final KeyValue kv1 = new KeyValue(one, f, q, EnvironmentEdgeManager.currentTime(), v);
113    final KeyValue kv2 = new KeyValue(two, f, q, EnvironmentEdgeManager.currentTime(), v);
114    // the seqId of first cell less than Integer.MAX_VALUE,
115    // the seqId of second cell greater than integer.MAX_VALUE
116    kv1.setSequenceId(LESS_THAN_INTEGER_MAX_VALUE_SEQ_ID);
117    kv2.setSequenceId(GREATER_THAN_INTEGER_MAX_VALUE_SEQ_ID);
118    segment1.internalAdd(kv1, false, null, true);
119    segment1.internalAdd(kv2, false, null, true);
120    return segment1;
121  }
122
123  protected void closeTestSegments(List<ImmutableSegment> segments) {
124    for (Segment segment : segments) {
125      segment.close();
126    }
127  }
128
129  protected void verifyNext(MemStoreSegmentsIterator iterator) {
130    // check first cell
131    assertTrue(iterator.hasNext());
132    Cell firstCell = iterator.next();
133    assertEquals(LESS_THAN_INTEGER_MAX_VALUE_SEQ_ID, firstCell.getSequenceId());
134
135    // check second cell
136    assertTrue(iterator.hasNext());
137    Cell secondCell = iterator.next();
138    assertEquals(GREATER_THAN_INTEGER_MAX_VALUE_SEQ_ID, secondCell.getSequenceId());
139  }
140
141  @After
142  public void tearDown() throws Exception {
143    EnvironmentEdgeManagerTestHelper.reset();
144    if (region != null) {
145      HBaseTestingUtility.closeRegionAndWAL(region);
146    }
147  }
148}