|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.regionserver.storefiletracker; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.List; |
| 26 | +import org.apache.hadoop.conf.Configuration; |
| 27 | +import org.apache.hadoop.fs.Path; |
| 28 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 29 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 30 | +import org.apache.hadoop.hbase.TableName; |
| 31 | +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; |
| 32 | +import org.apache.hadoop.hbase.client.Get; |
| 33 | +import org.apache.hadoop.hbase.client.Put; |
| 34 | +import org.apache.hadoop.hbase.client.RegionInfo; |
| 35 | +import org.apache.hadoop.hbase.client.RegionInfoBuilder; |
| 36 | +import org.apache.hadoop.hbase.client.Result; |
| 37 | +import org.apache.hadoop.hbase.client.TableDescriptor; |
| 38 | +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; |
| 39 | +import org.apache.hadoop.hbase.regionserver.ChunkCreator; |
| 40 | +import org.apache.hadoop.hbase.regionserver.HRegion; |
| 41 | +import org.apache.hadoop.hbase.regionserver.MemStoreLAB; |
| 42 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 43 | +import org.apache.hadoop.hbase.testclassification.RegionServerTests; |
| 44 | +import org.apache.hadoop.hbase.util.Bytes; |
| 45 | +import org.apache.hadoop.hbase.wal.WAL; |
| 46 | +import org.junit.After; |
| 47 | +import org.junit.Before; |
| 48 | +import org.junit.BeforeClass; |
| 49 | +import org.junit.ClassRule; |
| 50 | +import org.junit.Rule; |
| 51 | +import org.junit.Test; |
| 52 | +import org.junit.experimental.categories.Category; |
| 53 | +import org.junit.rules.TestName; |
| 54 | +import org.junit.runner.RunWith; |
| 55 | +import org.junit.runners.Parameterized; |
| 56 | +import org.junit.runners.Parameterized.Parameter; |
| 57 | +import org.junit.runners.Parameterized.Parameters; |
| 58 | + |
| 59 | +import org.apache.hbase.thirdparty.com.google.common.io.Closeables; |
| 60 | + |
| 61 | +@RunWith(Parameterized.class) |
| 62 | +@Category({ RegionServerTests.class, MediumTests.class }) |
| 63 | +public class TestMigrationStoreFileTracker { |
| 64 | + |
| 65 | + @ClassRule |
| 66 | + public static final HBaseClassTestRule CLASS_RULE = |
| 67 | + HBaseClassTestRule.forClass(TestMigrationStoreFileTracker.class); |
| 68 | + |
| 69 | + private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); |
| 70 | + |
| 71 | + private static final byte[] CF = Bytes.toBytes("cf"); |
| 72 | + |
| 73 | + private static final byte[] CQ = Bytes.toBytes("cq"); |
| 74 | + |
| 75 | + private static final TableDescriptor TD = |
| 76 | + TableDescriptorBuilder.newBuilder(TableName.valueOf("file_based_tracker")) |
| 77 | + .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).build(); |
| 78 | + |
| 79 | + private static final RegionInfo RI = RegionInfoBuilder.newBuilder(TD.getTableName()).build(); |
| 80 | + |
| 81 | + @Rule |
| 82 | + public TestName name = new TestName(); |
| 83 | + |
| 84 | + @Parameter(0) |
| 85 | + public Class<? extends StoreFileTrackerBase> srcImplClass; |
| 86 | + |
| 87 | + @Parameter(1) |
| 88 | + public Class<? extends StoreFileTrackerBase> dstImplClass; |
| 89 | + |
| 90 | + private HRegion region; |
| 91 | + |
| 92 | + private Path rootDir; |
| 93 | + |
| 94 | + private WAL wal; |
| 95 | + |
| 96 | + @Parameters(name = "{index}: src={0}, dst={1}") |
| 97 | + public static List<Object[]> params() { |
| 98 | + List<Class<? extends StoreFileTrackerBase>> impls = |
| 99 | + Arrays.asList(DefaultStoreFileTracker.class, FileBasedStoreFileTracker.class); |
| 100 | + List<Object[]> params = new ArrayList<>(); |
| 101 | + for (Class<? extends StoreFileTrackerBase> src : impls) { |
| 102 | + for (Class<? extends StoreFileTrackerBase> dst : impls) { |
| 103 | + if (src.equals(dst)) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + params.add(new Object[] { src, dst }); |
| 107 | + } |
| 108 | + } |
| 109 | + return params; |
| 110 | + } |
| 111 | + |
| 112 | + @BeforeClass |
| 113 | + public static void setUpBeforeClass() { |
| 114 | + ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null, |
| 115 | + MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT); |
| 116 | + } |
| 117 | + |
| 118 | + @Before |
| 119 | + public void setUp() throws IOException { |
| 120 | + Configuration conf = UTIL.getConfiguration(); |
| 121 | + conf.setClass(MigrationStoreFileTracker.SRC_IMPL, srcImplClass, StoreFileTrackerBase.class); |
| 122 | + conf.setClass(MigrationStoreFileTracker.DST_IMPL, dstImplClass, StoreFileTrackerBase.class); |
| 123 | + rootDir = UTIL.getDataTestDir(name.getMethodName().replaceAll("[=:\\[ ]", "_")); |
| 124 | + wal = HBaseTestingUtil.createWal(conf, rootDir, RI); |
| 125 | + } |
| 126 | + |
| 127 | + @After |
| 128 | + public void tearDown() throws IOException { |
| 129 | + if (region != null) { |
| 130 | + region.close(); |
| 131 | + } |
| 132 | + Closeables.close(wal, true); |
| 133 | + UTIL.cleanupTestDir(); |
| 134 | + } |
| 135 | + |
| 136 | + private HRegion createRegion(Class<? extends StoreFileTrackerBase> trackerImplClass) |
| 137 | + throws IOException { |
| 138 | + Configuration conf = new Configuration(UTIL.getConfiguration()); |
| 139 | + conf.setClass(StoreFileTrackerFactory.TRACK_IMPL, trackerImplClass, StoreFileTracker.class); |
| 140 | + return HRegion.createHRegion(RI, rootDir, conf, TD, wal, true); |
| 141 | + } |
| 142 | + |
| 143 | + private HRegion reopenRegion(Class<? extends StoreFileTrackerBase> trackerImplClass) |
| 144 | + throws IOException { |
| 145 | + region.close(); |
| 146 | + Configuration conf = new Configuration(UTIL.getConfiguration()); |
| 147 | + conf.setClass(StoreFileTrackerFactory.TRACK_IMPL, trackerImplClass, StoreFileTracker.class); |
| 148 | + return HRegion.openHRegion(rootDir, RI, TD, wal, conf); |
| 149 | + } |
| 150 | + |
| 151 | + private void putData(int start, int end) throws IOException { |
| 152 | + for (int i = start; i < end; i++) { |
| 153 | + region.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i))); |
| 154 | + if (i % 30 == 0) { |
| 155 | + region.flush(true); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private void verifyData(int start, int end) throws IOException { |
| 161 | + for (int i = start; i < end; i++) { |
| 162 | + Result result = region.get(new Get(Bytes.toBytes(i))); |
| 163 | + assertEquals(i, Bytes.toInt(result.getValue(CF, CQ))); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testMigration() throws IOException { |
| 169 | + region = createRegion(srcImplClass); |
| 170 | + putData(0, 100); |
| 171 | + verifyData(0, 100); |
| 172 | + region = reopenRegion(MigrationStoreFileTracker.class); |
| 173 | + verifyData(0, 100); |
| 174 | + region.compact(true); |
| 175 | + putData(100, 200); |
| 176 | + region = reopenRegion(dstImplClass); |
| 177 | + verifyData(0, 200); |
| 178 | + } |
| 179 | +} |
0 commit comments