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