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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.fail; 022 023import java.io.IOException; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.HConstants; 029import org.apache.hadoop.hbase.KeyValue; 030import org.apache.hadoop.hbase.ServerName; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 033import org.apache.hadoop.hbase.client.RegionInfo; 034import org.apache.hadoop.hbase.client.RegionInfoBuilder; 035import org.apache.hadoop.hbase.client.TableDescriptor; 036import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 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.WALEditInternalHelper; 046import org.apache.hadoop.hbase.wal.WALFactory; 047import org.apache.hadoop.hbase.wal.WALKeyImpl; 048import org.apache.hadoop.hbase.wal.WALProvider; 049import org.apache.hadoop.hbase.wal.WALSplitUtil; 050import org.junit.jupiter.api.AfterEach; 051import org.junit.jupiter.api.BeforeEach; 052import org.junit.jupiter.api.Tag; 053import org.junit.jupiter.api.Test; 054import org.junit.jupiter.api.TestInfo; 055import org.mockito.Mockito; 056import org.slf4j.Logger; 057import org.slf4j.LoggerFactory; 058 059/** 060 * HBASE-21031 If replay edits fails, we need to make sure memstore is rollbacked And if MSLAB is 061 * used, all chunk is released too. 062 */ 063@Tag(RegionServerTests.TAG) 064@Tag(SmallTests.TAG) 065public class TestRecoveredEditsReplayAndAbort { 066 067 private static final Logger LOG = LoggerFactory.getLogger(TestRecoveredEditsReplayAndAbort.class); 068 069 protected final byte[] row = Bytes.toBytes("rowA"); 070 071 protected final static byte[] fam1 = Bytes.toBytes("colfamily11"); 072 073 // Test names 074 protected TableName tableName; 075 protected String method; 076 077 protected static HBaseTestingUtil TEST_UTIL; 078 public static Configuration CONF; 079 private HRegion region = null; 080 081 @BeforeEach 082 public void setup(TestInfo testInfo) throws IOException { 083 TEST_UTIL = new HBaseTestingUtil(); 084 CONF = TEST_UTIL.getConfiguration(); 085 method = testInfo.getTestMethod().get().getName(); 086 tableName = TableName.valueOf(method); 087 } 088 089 @AfterEach 090 public void tearDown() throws Exception { 091 LOG.info("Cleaning test directory: " + TEST_UTIL.getDataTestDir()); 092 TEST_UTIL.cleanupTestDir(); 093 } 094 095 @Test 096 public void test() throws Exception { 097 // set flush size to 10MB 098 CONF.setInt("hbase.hregion.memstore.flush.size", 1024 * 1024 * 10); 099 // set the report interval to a very small value 100 CONF.setInt("hbase.hstore.report.interval.edits", 1); 101 CONF.setInt("hbase.hstore.report.period", 0); 102 // mock a RegionServerServices 103 final RegionServerAccounting rsAccounting = new RegionServerAccounting(CONF); 104 RegionServerServices rs = Mockito.mock(RegionServerServices.class); 105 ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null, 106 MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT); 107 Mockito.when(rs.getRegionServerAccounting()).thenReturn(rsAccounting); 108 Mockito.when(rs.isAborted()).thenReturn(false); 109 Mockito.when(rs.getNonceManager()).thenReturn(null); 110 Mockito.when(rs.getServerName()).thenReturn(ServerName.valueOf("test", 0, 111)); 111 Mockito.when(rs.getConfiguration()).thenReturn(CONF); 112 // create a region 113 TableName testTable = TableName.valueOf("testRecoveredEidtsReplayAndAbort"); 114 TableDescriptor htd = TableDescriptorBuilder.newBuilder(testTable) 115 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(fam1).build()).build(); 116 RegionInfo info = RegionInfoBuilder.newBuilder(htd.getTableName()).build(); 117 Path logDir = TEST_UTIL.getDataTestDirOnTestFS("TestRecoveredEidtsReplayAndAbort.log"); 118 final WAL wal = HBaseTestingUtil.createWal(CONF, logDir, info); 119 Path rootDir = TEST_UTIL.getDataTestDir(); 120 Path tableDir = CommonFSUtils.getTableDir(rootDir, info.getTable()); 121 HRegionFileSystem.createRegionOnFileSystem(CONF, TEST_UTIL.getTestFileSystem(), tableDir, info); 122 region = HRegion.newHRegion(tableDir, wal, TEST_UTIL.getTestFileSystem(), CONF, info, htd, rs, 123 rs.getKeyManagementService()); 124 // create some recovered.edits 125 final WALFactory wals = new WALFactory(CONF, method); 126 try { 127 Path regiondir = region.getRegionFileSystem().getRegionDir(); 128 FileSystem fs = region.getRegionFileSystem().getFileSystem(); 129 byte[] regionName = region.getRegionInfo().getEncodedNameAsBytes(); 130 131 Path recoveredEditsDir = WALSplitUtil.getRegionDirRecoveredEditsDir(regiondir); 132 long maxSeqId = 1200; 133 long minSeqId = 1000; 134 long totalEdits = maxSeqId - minSeqId; 135 for (long i = minSeqId; i <= maxSeqId; i += 100) { 136 Path recoveredEdits = new Path(recoveredEditsDir, String.format("%019d", i)); 137 LOG.info("Begin to write recovered.edits : " + recoveredEdits); 138 fs.create(recoveredEdits); 139 WALProvider.Writer writer = wals.createRecoveredEditsWriter(fs, recoveredEdits); 140 for (long j = i; j < i + 100; j++) { 141 long time = System.nanoTime(); 142 WALEdit edit = new WALEdit(); 143 // 200KB kv 144 byte[] value = new byte[200 * 1024]; 145 Bytes.random(value); 146 WALEditInternalHelper.addExtendedCell(edit, 147 new KeyValue(row, fam1, Bytes.toBytes(j), time, KeyValue.Type.Put, value)); 148 writer.append(new WAL.Entry( 149 new WALKeyImpl(regionName, tableName, j, time, HConstants.DEFAULT_CLUSTER_ID), edit)); 150 } 151 writer.close(); 152 } 153 TaskMonitor.get().createStatus(method); 154 // try to replay the edits 155 try { 156 region.initialize(new CancelableProgressable() { 157 private long replayedEdits = 0; 158 159 @Override 160 public boolean progress() { 161 replayedEdits++; 162 // during replay, rsAccounting should align with global memstore, because 163 // there is only one memstore here 164 assertEquals(rsAccounting.getGlobalMemStoreDataSize(), region.getMemStoreDataSize()); 165 assertEquals(rsAccounting.getGlobalMemStoreHeapSize(), region.getMemStoreHeapSize()); 166 assertEquals(rsAccounting.getGlobalMemStoreOffHeapSize(), 167 region.getMemStoreOffHeapSize()); 168 // abort the replay before finishing, leaving some edits in the memory 169 return replayedEdits < totalEdits - 10; 170 } 171 }); 172 fail("Should not reach here"); 173 } catch (IOException t) { 174 LOG.info("Current memstore: " + region.getMemStoreDataSize() + ", " 175 + region.getMemStoreHeapSize() + ", " + region.getMemStoreOffHeapSize()); 176 } 177 // After aborting replay, there should be no data in the memory 178 assertEquals(0, rsAccounting.getGlobalMemStoreDataSize()); 179 assertEquals(0, region.getMemStoreDataSize()); 180 // All the chunk in the MSLAB should be recycled, otherwise, there might be 181 // a memory leak. 182 assertEquals(0, ChunkCreator.getInstance().numberOfMappedChunks()); 183 } finally { 184 HBaseTestingUtil.closeRegionAndWAL(this.region); 185 this.region = null; 186 wals.close(); 187 } 188 } 189}