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(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); 119 Mockito.when(rs.getRegionServerAccounting()).thenReturn(rsAccounting); 120 Mockito.when(rs.isAborted()).thenReturn(false); 121 Mockito.when(rs.getNonceManager()).thenReturn(null); 122 Mockito.when(rs.getServerName()).thenReturn(ServerName 123 .valueOf("test", 0, 111)); 124 Mockito.when(rs.getConfiguration()).thenReturn(CONF); 125 //create a region 126 TableName testTable = TableName.valueOf("testRecoveredEidtsReplayAndAbort"); 127 TableDescriptor htd = TableDescriptorBuilder.newBuilder(testTable) 128 .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(fam1).build()) 129 .build(); 130 HRegionInfo info = new HRegionInfo(htd.getTableName(), 131 HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, false); 132 Path logDir = TEST_UTIL 133 .getDataTestDirOnTestFS("TestRecoveredEidtsReplayAndAbort.log"); 134 final WAL wal = HBaseTestingUtility.createWal(CONF, logDir, info); 135 Path rootDir = TEST_UTIL.getDataTestDir(); 136 Path tableDir = CommonFSUtils.getTableDir(rootDir, info.getTable()); 137 HRegionFileSystem 138 .createRegionOnFileSystem(CONF, TEST_UTIL.getTestFileSystem(), tableDir, info); 139 region = HRegion.newHRegion(tableDir, wal, TEST_UTIL.getTestFileSystem(), CONF, info, 140 htd, rs); 141 //create some recovered.edits 142 final WALFactory wals = new WALFactory(CONF, method); 143 try { 144 Path regiondir = region.getRegionFileSystem().getRegionDir(); 145 FileSystem fs = region.getRegionFileSystem().getFileSystem(); 146 byte[] regionName = region.getRegionInfo().getEncodedNameAsBytes(); 147 148 Path recoveredEditsDir = WALSplitUtil 149 .getRegionDirRecoveredEditsDir(regiondir); 150 long maxSeqId = 1200; 151 long minSeqId = 1000; 152 long totalEdits = maxSeqId - minSeqId; 153 for (long i = minSeqId; i <= maxSeqId; i += 100) { 154 Path recoveredEdits = new Path(recoveredEditsDir, 155 String.format("%019d", i)); 156 LOG.info("Begin to write recovered.edits : " + recoveredEdits); 157 fs.create(recoveredEdits); 158 WALProvider.Writer writer = wals 159 .createRecoveredEditsWriter(fs, recoveredEdits); 160 for (long j = i; j < i + 100; j++) { 161 long time = System.nanoTime(); 162 WALEdit edit = new WALEdit(); 163 // 200KB kv 164 byte[] value = new byte[200 * 1024]; 165 random.nextBytes(value); 166 edit.add( 167 new KeyValue(row, fam1, Bytes.toBytes(j), time, KeyValue.Type.Put, 168 value)); 169 writer.append(new WAL.Entry( 170 new WALKeyImpl(regionName, tableName, j, time, 171 HConstants.DEFAULT_CLUSTER_ID), edit)); 172 } 173 writer.close(); 174 } 175 MonitoredTask status = TaskMonitor.get().createStatus(method); 176 //try to replay the edits 177 try { 178 region.initialize(new CancelableProgressable() { 179 private long replayedEdits = 0; 180 181 @Override 182 public boolean progress() { 183 replayedEdits++; 184 //during replay, rsAccounting should align with global memstore, because 185 //there is only one memstore here 186 Assert.assertEquals(rsAccounting.getGlobalMemStoreDataSize(), 187 region.getMemStoreDataSize()); 188 Assert.assertEquals(rsAccounting.getGlobalMemStoreHeapSize(), 189 region.getMemStoreHeapSize()); 190 Assert.assertEquals(rsAccounting.getGlobalMemStoreOffHeapSize(), 191 region.getMemStoreOffHeapSize()); 192 // abort the replay before finishing, leaving some edits in the memory 193 return replayedEdits < totalEdits - 10; 194 } 195 }); 196 Assert.fail("Should not reach here"); 197 } catch (IOException t) { 198 LOG.info("Current memstore: " + region.getMemStoreDataSize() + ", " + region 199 .getMemStoreHeapSize() + ", " + region 200 .getMemStoreOffHeapSize()); 201 } 202 //After aborting replay, there should be no data in the memory 203 Assert.assertEquals(0, rsAccounting.getGlobalMemStoreDataSize()); 204 Assert.assertEquals(0, region.getMemStoreDataSize()); 205 //All the chunk in the MSLAB should be recycled, otherwise, there might be 206 //a memory leak. 207 Assert.assertEquals(0, ChunkCreator.getInstance().numberOfMappedChunks()); 208 } finally { 209 HBaseTestingUtility.closeRegionAndWAL(this.region); 210 this.region = null; 211 wals.close(); 212 } 213 } 214}