001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.regionserver; 020 021import java.io.IOException; 022import java.util.Random; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.HRegionInfo; 030import org.apache.hadoop.hbase.KeyValue; 031import org.apache.hadoop.hbase.ServerName; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 034import org.apache.hadoop.hbase.client.TableDescriptor; 035import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 036import org.apache.hadoop.hbase.monitoring.MonitoredTask; 037import org.apache.hadoop.hbase.monitoring.TaskMonitor; 038import org.apache.hadoop.hbase.testclassification.RegionServerTests; 039import org.apache.hadoop.hbase.testclassification.SmallTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.apache.hadoop.hbase.util.CancelableProgressable; 042import org.apache.hadoop.hbase.util.CommonFSUtils; 043import org.apache.hadoop.hbase.wal.WAL; 044import org.apache.hadoop.hbase.wal.WALEdit; 045import org.apache.hadoop.hbase.wal.WALFactory; 046import org.apache.hadoop.hbase.wal.WALKeyImpl; 047import org.apache.hadoop.hbase.wal.WALProvider; 048import org.apache.hadoop.hbase.wal.WALSplitUtil; 049import org.junit.After; 050import org.junit.Assert; 051import org.junit.Before; 052import org.junit.ClassRule; 053import org.junit.Rule; 054import org.junit.Test; 055import org.junit.experimental.categories.Category; 056import org.junit.rules.TestName; 057import org.mockito.Mockito; 058import org.slf4j.Logger; 059import org.slf4j.LoggerFactory; 060 061/** 062 * HBASE-21031 063 * If replay edits fails, we need to make sure memstore is rollbacked 064 * And if MSLAB is used, all chunk is released too. 065 */ 066@Category({RegionServerTests.class, SmallTests.class }) 067public class TestRecoveredEditsReplayAndAbort { 068 @ClassRule 069 public static final HBaseClassTestRule CLASS_RULE = 070 HBaseClassTestRule.forClass(TestRecoveredEditsReplayAndAbort.class); 071 072 private static final Logger LOG = LoggerFactory 073 .getLogger(TestRecoveredEditsReplayAndAbort.class); 074 075 protected final byte[] row = Bytes.toBytes("rowA"); 076 077 protected final static byte [] fam1 = Bytes.toBytes("colfamily11"); 078 079 @Rule 080 public TestName name = new TestName(); 081 082 // Test names 083 protected TableName tableName; 084 protected String method; 085 086 protected static HBaseTestingUtility TEST_UTIL; 087 public static Configuration CONF ; 088 private static FileSystem FILESYSTEM; 089 private HRegion region = null; 090 091 private final Random random = new Random(); 092 093 @Before 094 public void setup() throws IOException { 095 TEST_UTIL = new HBaseTestingUtility(); 096 FILESYSTEM = TEST_UTIL.getTestFileSystem(); 097 CONF = TEST_UTIL.getConfiguration(); 098 method = name.getMethodName(); 099 tableName = TableName.valueOf(method); 100 } 101 102 @After 103 public void tearDown() throws Exception { 104 LOG.info("Cleaning test directory: " + TEST_UTIL.getDataTestDir()); 105 TEST_UTIL.cleanupTestDir(); 106 } 107 108 @Test 109 public void test() throws Exception { 110 //set flush size to 10MB 111 CONF.setInt("hbase.hregion.memstore.flush.size", 1024 * 1024 * 10); 112 //set the report interval to a very small value 113 CONF.setInt("hbase.hstore.report.interval.edits", 1); 114 CONF.setInt("hbase.hstore.report.period", 0); 115 //mock a RegionServerServices 116 final RegionServerAccounting rsAccounting = new RegionServerAccounting(CONF); 117 RegionServerServices rs = Mockito.mock(RegionServerServices.class); 118 ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 119 0, null, MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT); 120 Mockito.when(rs.getRegionServerAccounting()).thenReturn(rsAccounting); 121 Mockito.when(rs.isAborted()).thenReturn(false); 122 Mockito.when(rs.getNonceManager()).thenReturn(null); 123 Mockito.when(rs.getServerName()).thenReturn(ServerName 124 .valueOf("test", 0, 111)); 125 Mockito.when(rs.getConfiguration()).thenReturn(CONF); 126 //create a region 127 TableName testTable = TableName.valueOf("testRecoveredEidtsReplayAndAbort"); 128 TableDescriptor htd = TableDescriptorBuilder.newBuilder(testTable) 129 .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(fam1).build()) 130 .build(); 131 HRegionInfo info = new HRegionInfo(htd.getTableName(), 132 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, false); 133 Path logDir = TEST_UTIL 134 .getDataTestDirOnTestFS("TestRecoveredEidtsReplayAndAbort.log"); 135 final WAL wal = HBaseTestingUtility.createWal(CONF, logDir, info); 136 Path rootDir = TEST_UTIL.getDataTestDir(); 137 Path tableDir = CommonFSUtils.getTableDir(rootDir, info.getTable()); 138 HRegionFileSystem 139 .createRegionOnFileSystem(CONF, TEST_UTIL.getTestFileSystem(), tableDir, info); 140 region = HRegion.newHRegion(tableDir, wal, TEST_UTIL.getTestFileSystem(), CONF, info, 141 htd, rs); 142 //create some recovered.edits 143 final WALFactory wals = new WALFactory(CONF, method); 144 try { 145 Path regiondir = region.getRegionFileSystem().getRegionDir(); 146 FileSystem fs = region.getRegionFileSystem().getFileSystem(); 147 byte[] regionName = region.getRegionInfo().getEncodedNameAsBytes(); 148 149 Path recoveredEditsDir = WALSplitUtil 150 .getRegionDirRecoveredEditsDir(regiondir); 151 long maxSeqId = 1200; 152 long minSeqId = 1000; 153 long totalEdits = maxSeqId - minSeqId; 154 for (long i = minSeqId; i <= maxSeqId; i += 100) { 155 Path recoveredEdits = new Path(recoveredEditsDir, 156 String.format("%019d", i)); 157 LOG.info("Begin to write recovered.edits : " + recoveredEdits); 158 fs.create(recoveredEdits); 159 WALProvider.Writer writer = wals 160 .createRecoveredEditsWriter(fs, recoveredEdits); 161 for (long j = i; j < i + 100; j++) { 162 long time = System.nanoTime(); 163 WALEdit edit = new WALEdit(); 164 // 200KB kv 165 byte[] value = new byte[200 * 1024]; 166 random.nextBytes(value); 167 edit.add( 168 new KeyValue(row, fam1, Bytes.toBytes(j), time, KeyValue.Type.Put, 169 value)); 170 writer.append(new WAL.Entry( 171 new WALKeyImpl(regionName, tableName, j, time, 172 HConstants.DEFAULT_CLUSTER_ID), edit)); 173 } 174 writer.close(); 175 } 176 MonitoredTask status = TaskMonitor.get().createStatus(method); 177 //try to replay the edits 178 try { 179 region.initialize(new CancelableProgressable() { 180 private long replayedEdits = 0; 181 182 @Override 183 public boolean progress() { 184 replayedEdits++; 185 //during replay, rsAccounting should align with global memstore, because 186 //there is only one memstore here 187 Assert.assertEquals(rsAccounting.getGlobalMemStoreDataSize(), 188 region.getMemStoreDataSize()); 189 Assert.assertEquals(rsAccounting.getGlobalMemStoreHeapSize(), 190 region.getMemStoreHeapSize()); 191 Assert.assertEquals(rsAccounting.getGlobalMemStoreOffHeapSize(), 192 region.getMemStoreOffHeapSize()); 193 // abort the replay before finishing, leaving some edits in the memory 194 return replayedEdits < totalEdits - 10; 195 } 196 }); 197 Assert.fail("Should not reach here"); 198 } catch (IOException t) { 199 LOG.info("Current memstore: " + region.getMemStoreDataSize() + ", " + region 200 .getMemStoreHeapSize() + ", " + region 201 .getMemStoreOffHeapSize()); 202 } 203 //After aborting replay, there should be no data in the memory 204 Assert.assertEquals(0, rsAccounting.getGlobalMemStoreDataSize()); 205 Assert.assertEquals(0, region.getMemStoreDataSize()); 206 //All the chunk in the MSLAB should be recycled, otherwise, there might be 207 //a memory leak. 208 Assert.assertEquals(0, ChunkCreator.getInstance().numberOfMappedChunks()); 209 } finally { 210 HBaseTestingUtility.closeRegionAndWAL(this.region); 211 this.region = null; 212 wals.close(); 213 } 214 } 215}