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