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.List; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.Cell; 029import org.apache.hadoop.hbase.CellComparatorImpl; 030import org.apache.hadoop.hbase.CellScanner; 031import org.apache.hadoop.hbase.CellUtil; 032import org.apache.hadoop.hbase.HBaseClassTestRule; 033import org.apache.hadoop.hbase.HBaseTestingUtility; 034import org.apache.hadoop.hbase.HColumnDescriptor; 035import org.apache.hadoop.hbase.HConstants; 036import org.apache.hadoop.hbase.HRegionInfo; 037import org.apache.hadoop.hbase.HTableDescriptor; 038import org.apache.hadoop.hbase.MemoryCompactionPolicy; 039import org.apache.hadoop.hbase.PrivateCellUtil; 040import org.apache.hadoop.hbase.TableName; 041import org.apache.hadoop.hbase.client.Get; 042import org.apache.hadoop.hbase.client.Result; 043import org.apache.hadoop.hbase.testclassification.MediumTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.apache.hadoop.hbase.util.FSUtils; 046import org.apache.hadoop.hbase.wal.WAL; 047import org.apache.hadoop.hbase.wal.WALEdit; 048import org.apache.hadoop.hbase.wal.WALFactory; 049import org.apache.hadoop.hbase.wal.WALKey; 050import org.apache.hadoop.hbase.wal.WALSplitter; 051import org.junit.ClassRule; 052import org.junit.Rule; 053import org.junit.Test; 054import org.junit.experimental.categories.Category; 055import org.junit.rules.TestName; 056import org.slf4j.Logger; 057import org.slf4j.LoggerFactory; 058 059/** 060 * Tests around replay of recovered.edits content. 061 */ 062@Category({MediumTests.class}) 063public class TestRecoveredEdits { 064 065 @ClassRule 066 public static final HBaseClassTestRule CLASS_RULE = 067 HBaseClassTestRule.forClass(TestRecoveredEdits.class); 068 069 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 070 private static final Logger LOG = LoggerFactory.getLogger(TestRecoveredEdits.class); 071 @Rule public TestName testName = new TestName(); 072 073 /** 074 * HBASE-12782 ITBLL fails for me if generator does anything but 5M per maptask. 075 * Create a region. Close it. Then copy into place a file to replay, one that is bigger than 076 * configured flush size so we bring on lots of flushes. Then reopen and confirm all edits 077 * made it in. 078 * @throws IOException 079 */ 080 @Test 081 public void testReplayWorksThoughLotsOfFlushing() throws 082 IOException { 083 for(MemoryCompactionPolicy policy : MemoryCompactionPolicy.values()) { 084 testReplayWorksWithMemoryCompactionPolicy(policy); 085 } 086 } 087 088 private void testReplayWorksWithMemoryCompactionPolicy(MemoryCompactionPolicy policy) throws 089 IOException { 090 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 091 // Set it so we flush every 1M or so. Thats a lot. 092 conf.setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, 1024*1024); 093 conf.set(CompactingMemStore.COMPACTING_MEMSTORE_TYPE_KEY, String.valueOf(policy)); 094 // The file of recovered edits has a column family of 'meta'. Also has an encoded regionname 095 // of 4823016d8fca70b25503ee07f4c6d79f which needs to match on replay. 096 final String encodedRegionName = "4823016d8fca70b25503ee07f4c6d79f"; 097 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(testName.getMethodName())); 098 final String columnFamily = "meta"; 099 byte [][] columnFamilyAsByteArray = new byte [][] {Bytes.toBytes(columnFamily)}; 100 htd.addFamily(new HColumnDescriptor(columnFamily)); 101 HRegionInfo hri = new HRegionInfo(htd.getTableName()) { 102 @Override 103 public synchronized String getEncodedName() { 104 return encodedRegionName; 105 } 106 107 // Cache the name because lots of lookups. 108 private byte [] encodedRegionNameAsBytes = null; 109 @Override 110 public synchronized byte[] getEncodedNameAsBytes() { 111 if (encodedRegionNameAsBytes == null) { 112 this.encodedRegionNameAsBytes = Bytes.toBytes(getEncodedName()); 113 } 114 return this.encodedRegionNameAsBytes; 115 } 116 }; 117 Path hbaseRootDir = TEST_UTIL.getDataTestDir(); 118 ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); 119 FileSystem fs = FileSystem.get(TEST_UTIL.getConfiguration()); 120 Path tableDir = FSUtils.getTableDir(hbaseRootDir, htd.getTableName()); 121 HRegionFileSystem hrfs = 122 new HRegionFileSystem(TEST_UTIL.getConfiguration(), fs, tableDir, hri); 123 if (fs.exists(hrfs.getRegionDir())) { 124 LOG.info("Region directory already exists. Deleting."); 125 fs.delete(hrfs.getRegionDir(), true); 126 } 127 HRegion region = HRegion.createHRegion(hri, hbaseRootDir, conf, htd, null); 128 assertEquals(encodedRegionName, region.getRegionInfo().getEncodedName()); 129 List<String> storeFiles = region.getStoreFileList(columnFamilyAsByteArray); 130 // There should be no store files. 131 assertTrue(storeFiles.isEmpty()); 132 region.close(); 133 Path regionDir = region.getRegionDir(hbaseRootDir, hri); 134 Path recoveredEditsDir = WALSplitter.getRegionDirRecoveredEditsDir(regionDir); 135 // This is a little fragile getting this path to a file of 10M of edits. 136 Path recoveredEditsFile = new Path( 137 System.getProperty("test.build.classes", "target/test-classes"), 138 "0000000000000016310"); 139 // Copy this file under the region's recovered.edits dir so it is replayed on reopen. 140 Path destination = new Path(recoveredEditsDir, recoveredEditsFile.getName()); 141 fs.copyToLocalFile(recoveredEditsFile, destination); 142 assertTrue(fs.exists(destination)); 143 // Now the file 0000000000000016310 is under recovered.edits, reopen the region to replay. 144 region = HRegion.openHRegion(region, null); 145 assertEquals(encodedRegionName, region.getRegionInfo().getEncodedName()); 146 storeFiles = region.getStoreFileList(columnFamilyAsByteArray); 147 // Our 0000000000000016310 is 10MB. Most of the edits are for one region. Lets assume that if 148 // we flush at 1MB, that there are at least 3 flushed files that are there because of the 149 // replay of edits. 150 if(policy == MemoryCompactionPolicy.EAGER || policy == MemoryCompactionPolicy.ADAPTIVE) { 151 assertTrue("Files count=" + storeFiles.size(), storeFiles.size() >= 1); 152 } else { 153 assertTrue("Files count=" + storeFiles.size(), storeFiles.size() > 10); 154 } 155 // Now verify all edits made it into the region. 156 int count = verifyAllEditsMadeItIn(fs, conf, recoveredEditsFile, region); 157 LOG.info("Checked " + count + " edits made it in"); 158 } 159 160 /** 161 * @param fs 162 * @param conf 163 * @param edits 164 * @param region 165 * @return Return how many edits seen. 166 * @throws IOException 167 */ 168 private int verifyAllEditsMadeItIn(final FileSystem fs, final Configuration conf, 169 final Path edits, final HRegion region) 170 throws IOException { 171 int count = 0; 172 // Based on HRegion#replayRecoveredEdits 173 WAL.Reader reader = null; 174 try { 175 reader = WALFactory.createReader(fs, edits, conf); 176 WAL.Entry entry; 177 while ((entry = reader.next()) != null) { 178 WALKey key = entry.getKey(); 179 WALEdit val = entry.getEdit(); 180 count++; 181 // Check this edit is for this region. 182 if (!Bytes.equals(key.getEncodedRegionName(), 183 region.getRegionInfo().getEncodedNameAsBytes())) { 184 continue; 185 } 186 Cell previous = null; 187 for (Cell cell: val.getCells()) { 188 if (CellUtil.matchingFamily(cell, WALEdit.METAFAMILY)) continue; 189 if (previous != null && CellComparatorImpl.COMPARATOR.compareRows(previous, cell) == 0) 190 continue; 191 previous = cell; 192 Get g = new Get(CellUtil.cloneRow(cell)); 193 Result r = region.get(g); 194 boolean found = false; 195 for (CellScanner scanner = r.cellScanner(); scanner.advance();) { 196 Cell current = scanner.current(); 197 if (PrivateCellUtil.compareKeyIgnoresMvcc(CellComparatorImpl.COMPARATOR, cell, 198 current) == 0) { 199 found = true; 200 break; 201 } 202 } 203 assertTrue("Failed to find " + cell, found); 204 } 205 } 206 } finally { 207 if (reader != null) reader.close(); 208 } 209 return count; 210 } 211}