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