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 // create some recovered.edits 124 final WALFactory wals = new WALFactory(CONF, method); 125 try { 126 Path regiondir = region.getRegionFileSystem().getRegionDir(); 127 FileSystem fs = region.getRegionFileSystem().getFileSystem(); 128 byte[] regionName = region.getRegionInfo().getEncodedNameAsBytes(); 129 130 Path recoveredEditsDir = WALSplitUtil.getRegionDirRecoveredEditsDir(regiondir); 131 long maxSeqId = 1200; 132 long minSeqId = 1000; 133 long totalEdits = maxSeqId - minSeqId; 134 for (long i = minSeqId; i <= maxSeqId; i += 100) { 135 Path recoveredEdits = new Path(recoveredEditsDir, String.format("%019d", i)); 136 LOG.info("Begin to write recovered.edits : " + recoveredEdits); 137 fs.create(recoveredEdits); 138 WALProvider.Writer writer = wals.createRecoveredEditsWriter(fs, recoveredEdits); 139 for (long j = i; j < i + 100; j++) { 140 long time = System.nanoTime(); 141 WALEdit edit = new WALEdit(); 142 // 200KB kv 143 byte[] value = new byte[200 * 1024]; 144 Bytes.random(value); 145 WALEditInternalHelper.addExtendedCell(edit, 146 new KeyValue(row, fam1, Bytes.toBytes(j), time, KeyValue.Type.Put, value)); 147 writer.append(new WAL.Entry( 148 new WALKeyImpl(regionName, tableName, j, time, HConstants.DEFAULT_CLUSTER_ID), edit)); 149 } 150 writer.close(); 151 } 152 TaskMonitor.get().createStatus(method); 153 // try to replay the edits 154 try { 155 region.initialize(new CancelableProgressable() { 156 private long replayedEdits = 0; 157 158 @Override 159 public boolean progress() { 160 replayedEdits++; 161 // during replay, rsAccounting should align with global memstore, because 162 // there is only one memstore here 163 assertEquals(rsAccounting.getGlobalMemStoreDataSize(), region.getMemStoreDataSize()); 164 assertEquals(rsAccounting.getGlobalMemStoreHeapSize(), region.getMemStoreHeapSize()); 165 assertEquals(rsAccounting.getGlobalMemStoreOffHeapSize(), 166 region.getMemStoreOffHeapSize()); 167 // abort the replay before finishing, leaving some edits in the memory 168 return replayedEdits < totalEdits - 10; 169 } 170 }); 171 fail("Should not reach here"); 172 } catch (IOException t) { 173 LOG.info("Current memstore: " + region.getMemStoreDataSize() + ", " 174 + region.getMemStoreHeapSize() + ", " + region.getMemStoreOffHeapSize()); 175 } 176 // After aborting replay, there should be no data in the memory 177 assertEquals(0, rsAccounting.getGlobalMemStoreDataSize()); 178 assertEquals(0, region.getMemStoreDataSize()); 179 // All the chunk in the MSLAB should be recycled, otherwise, there might be 180 // a memory leak. 181 assertEquals(0, ChunkCreator.getInstance().numberOfMappedChunks()); 182 } finally { 183 HBaseTestingUtil.closeRegionAndWAL(this.region); 184 this.region = null; 185 wals.close(); 186 } 187 } 188}