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.master.region;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.util.Arrays;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileStatus;
028import org.apache.hadoop.fs.FileSystem;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.Cell;
031import org.apache.hadoop.hbase.CellUtil;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.HConstants;
034import org.apache.hadoop.hbase.client.Get;
035import org.apache.hadoop.hbase.client.Put;
036import org.apache.hadoop.hbase.client.Result;
037import org.apache.hadoop.hbase.testclassification.MasterTests;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046@Category({ MasterTests.class, MediumTests.class })
047public class TestMasterRegionWALRecovery extends MasterRegionTestBase {
048  private static final Logger LOG = LoggerFactory.getLogger(TestMasterRegionWALRecovery.class);
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestMasterRegionWALRecovery.class);
053
054  private Path masterRegionDir;
055
056  @Override
057  protected void postSetUp() throws IOException {
058    Configuration conf = htu.getConfiguration();
059    Path testDir = htu.getDataTestDir();
060    FileSystem fs = testDir.getFileSystem(conf);
061    masterRegionDir = new Path(testDir, REGION_DIR_NAME);
062  }
063
064  @Test
065  public void test() throws IOException, InterruptedException {
066    region
067      .update(r -> r.put(new Put(Bytes.toBytes(1)).addColumn(CF1, QUALIFIER, Bytes.toBytes(1))));
068    region.flush(true);
069
070    Path testDir = htu.getDataTestDir();
071    FileSystem fs = testDir.getFileSystem(htu.getConfiguration());
072    region.close(false);
073
074    Path masterRegionWalDir = new Path(masterRegionDir, HConstants.HREGION_LOGDIR_NAME);
075    LOG.info("WAL dir: {}", masterRegionWalDir);
076    assertTrue(fs.exists(masterRegionWalDir));
077    // Make sure we have the WAL for the localhost "server"
078    FileStatus[] files = fs.listStatus(masterRegionWalDir);
079    LOG.info("WAL files: {}", Arrays.toString(files));
080    assertEquals(1, files.length);
081    LOG.info("Deleting {}", masterRegionWalDir);
082    // Delete the WAL directory
083    fs.delete(masterRegionWalDir, true);
084
085    // Re-create the MasterRegion and hit the MasterRegion#open() code-path
086    // (rather than bootstrap())
087    createMasterRegion();
088
089    // Make sure we can read the same data we wrote (we flushed before nuking the WALs,
090    // so data should be durable)
091    Result r = region.get(new Get(Bytes.toBytes(1)));
092    Cell c = r.getColumnLatestCell(CF1, QUALIFIER);
093    assertArrayEquals(Bytes.toBytes(1), CellUtil.cloneValue(c));
094  }
095}