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