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.io.encoding; 019 020import static org.junit.Assert.assertTrue; 021 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.Collections; 026import java.util.List; 027import java.util.Random; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.Cell; 030import org.apache.hadoop.hbase.CellUtil; 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.HTableDescriptor; 036import org.apache.hadoop.hbase.TableName; 037import org.apache.hadoop.hbase.client.Admin; 038import org.apache.hadoop.hbase.client.Connection; 039import org.apache.hadoop.hbase.client.ConnectionFactory; 040import org.apache.hadoop.hbase.client.Durability; 041import org.apache.hadoop.hbase.client.Get; 042import org.apache.hadoop.hbase.client.Put; 043import org.apache.hadoop.hbase.client.Result; 044import org.apache.hadoop.hbase.client.Table; 045import org.apache.hadoop.hbase.regionserver.HRegionServer; 046import org.apache.hadoop.hbase.testclassification.IOTests; 047import org.apache.hadoop.hbase.testclassification.LargeTests; 048import org.apache.hadoop.hbase.util.Bytes; 049import org.apache.hadoop.hbase.util.Threads; 050import org.junit.AfterClass; 051import org.junit.BeforeClass; 052import org.junit.ClassRule; 053import org.junit.Test; 054import org.junit.experimental.categories.Category; 055import org.slf4j.Logger; 056import org.slf4j.LoggerFactory; 057 058/** 059 * Tests changing data block encoding settings of a column family. 060 */ 061@Category({IOTests.class, LargeTests.class}) 062public class TestChangingEncoding { 063 064 @ClassRule 065 public static final HBaseClassTestRule CLASS_RULE = 066 HBaseClassTestRule.forClass(TestChangingEncoding.class); 067 068 private static final Logger LOG = LoggerFactory.getLogger(TestChangingEncoding.class); 069 static final String CF = "EncodingTestCF"; 070 static final byte[] CF_BYTES = Bytes.toBytes(CF); 071 072 private static final int NUM_ROWS_PER_BATCH = 100; 073 private static final int NUM_COLS_PER_ROW = 20; 074 075 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 076 private static final Configuration conf = TEST_UTIL.getConfiguration(); 077 078 private static final int TIMEOUT_MS = 600000; 079 080 private HColumnDescriptor hcd; 081 082 private TableName tableName; 083 private static final List<DataBlockEncoding> ENCODINGS_TO_ITERATE = 084 createEncodingsToIterate(); 085 086 private static final List<DataBlockEncoding> createEncodingsToIterate() { 087 List<DataBlockEncoding> encodings = new ArrayList<>(Arrays.asList(DataBlockEncoding.values())); 088 encodings.add(DataBlockEncoding.NONE); 089 return Collections.unmodifiableList(encodings); 090 } 091 092 /** A zero-based index of the current batch of test data being written */ 093 private int numBatchesWritten; 094 095 private void prepareTest(String testId) throws IOException { 096 tableName = TableName.valueOf("test_table_" + testId); 097 HTableDescriptor htd = new HTableDescriptor(tableName); 098 hcd = new HColumnDescriptor(CF); 099 htd.addFamily(hcd); 100 try (Admin admin = TEST_UTIL.getConnection().getAdmin()) { 101 admin.createTable(htd); 102 } 103 numBatchesWritten = 0; 104 } 105 106 @BeforeClass 107 public static void setUpBeforeClass() throws Exception { 108 // Use a small flush size to create more HFiles. 109 conf.setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, 1024 * 1024); 110 // ((Log4JLogger)RpcServerImplementation.LOG).getLogger().setLevel(Level.TRACE); 111 // ((Log4JLogger)RpcClient.LOG).getLogger().setLevel(Level.TRACE); 112 TEST_UTIL.startMiniCluster(); 113 } 114 115 @AfterClass 116 public static void tearDownAfterClass() throws Exception { 117 TEST_UTIL.shutdownMiniCluster(); 118 } 119 120 private static byte[] getRowKey(int batchId, int i) { 121 return Bytes.toBytes("batch" + batchId + "_row" + i); 122 } 123 124 private static byte[] getQualifier(int j) { 125 return Bytes.toBytes("col" + j); 126 } 127 128 private static byte[] getValue(int batchId, int i, int j) { 129 return Bytes.toBytes("value_for_" + Bytes.toString(getRowKey(batchId, i)) 130 + "_col" + j); 131 } 132 133 static void writeTestDataBatch(TableName tableName, 134 int batchId) throws Exception { 135 LOG.debug("Writing test data batch " + batchId); 136 List<Put> puts = new ArrayList<>(); 137 for (int i = 0; i < NUM_ROWS_PER_BATCH; ++i) { 138 Put put = new Put(getRowKey(batchId, i)); 139 for (int j = 0; j < NUM_COLS_PER_ROW; ++j) { 140 put.addColumn(CF_BYTES, getQualifier(j), getValue(batchId, i, j)); 141 } 142 put.setDurability(Durability.SKIP_WAL); 143 puts.add(put); 144 } 145 try (Connection conn = ConnectionFactory.createConnection(conf); 146 Table table = conn.getTable(tableName)) { 147 table.put(puts); 148 } 149 } 150 151 static void verifyTestDataBatch(TableName tableName, 152 int batchId) throws Exception { 153 LOG.debug("Verifying test data batch " + batchId); 154 Table table = TEST_UTIL.getConnection().getTable(tableName); 155 for (int i = 0; i < NUM_ROWS_PER_BATCH; ++i) { 156 Get get = new Get(getRowKey(batchId, i)); 157 Result result = table.get(get); 158 for (int j = 0; j < NUM_COLS_PER_ROW; ++j) { 159 Cell kv = result.getColumnLatestCell(CF_BYTES, getQualifier(j)); 160 if (kv == null) { 161 continue; 162 } 163 assertTrue(CellUtil.matchingValue(kv, getValue(batchId, i, j))); 164 } 165 } 166 table.close(); 167 } 168 169 private void writeSomeNewData() throws Exception { 170 writeTestDataBatch(tableName, numBatchesWritten); 171 ++numBatchesWritten; 172 } 173 174 private void verifyAllData() throws Exception { 175 for (int i = 0; i < numBatchesWritten; ++i) { 176 verifyTestDataBatch(tableName, i); 177 } 178 } 179 180 private void setEncodingConf(DataBlockEncoding encoding, 181 boolean onlineChange) throws Exception { 182 LOG.debug("Setting CF encoding to " + encoding + " (ordinal=" 183 + encoding.ordinal() + "), onlineChange=" + onlineChange); 184 hcd.setDataBlockEncoding(encoding); 185 try (Admin admin = TEST_UTIL.getConnection().getAdmin()) { 186 if (!onlineChange) { 187 admin.disableTable(tableName); 188 } 189 admin.modifyColumnFamily(tableName, hcd); 190 if (!onlineChange) { 191 admin.enableTable(tableName); 192 } 193 } 194 // This is a unit test, not integration test. So let's 195 // wait for regions out of transition. Otherwise, for online 196 // encoding change, verification phase may be flaky because 197 // regions could be still in transition. 198 TEST_UTIL.waitUntilNoRegionsInTransition(TIMEOUT_MS); 199 } 200 201 @Test 202 public void testChangingEncoding() throws Exception { 203 prepareTest("ChangingEncoding"); 204 for (boolean onlineChange : new boolean[]{false, true}) { 205 for (DataBlockEncoding encoding : ENCODINGS_TO_ITERATE) { 206 setEncodingConf(encoding, onlineChange); 207 writeSomeNewData(); 208 verifyAllData(); 209 } 210 } 211 } 212 213 @Test 214 public void testChangingEncodingWithCompaction() throws Exception { 215 prepareTest("ChangingEncodingWithCompaction"); 216 for (boolean onlineChange : new boolean[]{false, true}) { 217 for (DataBlockEncoding encoding : ENCODINGS_TO_ITERATE) { 218 setEncodingConf(encoding, onlineChange); 219 writeSomeNewData(); 220 verifyAllData(); 221 compactAndWait(); 222 verifyAllData(); 223 } 224 } 225 } 226 227 private void compactAndWait() throws IOException, InterruptedException { 228 LOG.debug("Compacting table " + tableName); 229 HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0); 230 Admin admin = TEST_UTIL.getAdmin(); 231 admin.majorCompact(tableName); 232 233 // Waiting for the compaction to start, at least .5s. 234 final long maxWaitime = System.currentTimeMillis() + 500; 235 boolean cont; 236 do { 237 cont = rs.compactSplitThread.getCompactionQueueSize() == 0; 238 Threads.sleep(1); 239 } while (cont && System.currentTimeMillis() < maxWaitime); 240 241 while (rs.compactSplitThread.getCompactionQueueSize() > 0) { 242 Threads.sleep(1); 243 } 244 LOG.debug("Compaction queue size reached 0, continuing"); 245 } 246 247 @Test 248 public void testCrazyRandomChanges() throws Exception { 249 prepareTest("RandomChanges"); 250 Random rand = new Random(2934298742974297L); 251 for (int i = 0; i < 10; ++i) { 252 int encodingOrdinal = rand.nextInt(DataBlockEncoding.values().length); 253 DataBlockEncoding encoding = DataBlockEncoding.values()[encodingOrdinal]; 254 setEncodingConf(encoding, rand.nextBoolean()); 255 writeSomeNewData(); 256 verifyAllData(); 257 } 258 } 259}