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