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