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