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