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.procedure;
019
020import static org.apache.hadoop.hbase.HConstants.HBASE_SPLIT_WAL_COORDINATED_BY_ZK;
021import static org.apache.hadoop.hbase.HConstants.HBASE_SPLIT_WAL_MAX_SPLITTER;
022
023import java.util.List;
024import java.util.Optional;
025
026import org.apache.hadoop.fs.FileStatus;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.master.HMaster;
033import org.apache.hadoop.hbase.master.SplitWALManager;
034import org.apache.hadoop.hbase.procedure2.Procedure;
035import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
036import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
037import org.apache.hadoop.hbase.regionserver.HRegionServer;
038import org.apache.hadoop.hbase.testclassification.MasterTests;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.junit.After;
042import org.junit.Assert;
043import org.junit.Before;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047
048@Category({ MasterTests.class, MediumTests.class })
049public class TestSplitWALProcedure {
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestSplitWALProcedure.class);
053
054  private static HBaseTestingUtility TEST_UTIL;
055  private HMaster master;
056  private TableName TABLE_NAME;
057  private SplitWALManager splitWALManager;
058  private byte[] FAMILY;
059
060  @Before
061  public void setup() throws Exception {
062    TEST_UTIL = new HBaseTestingUtility();
063    TEST_UTIL.getConfiguration().setBoolean(HBASE_SPLIT_WAL_COORDINATED_BY_ZK, false);
064    TEST_UTIL.getConfiguration().setInt(HBASE_SPLIT_WAL_MAX_SPLITTER, 1);
065    TEST_UTIL.startMiniCluster(3);
066    master = TEST_UTIL.getHBaseCluster().getMaster();
067    splitWALManager = master.getSplitWALManager();
068    TABLE_NAME = TableName.valueOf(Bytes.toBytes("TestSplitWALProcedure"));
069    FAMILY = Bytes.toBytes("test");
070  }
071
072  @After
073  public void teardown() throws Exception {
074    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(
075            master.getMasterProcedureExecutor(), false);
076    TEST_UTIL.shutdownMiniCluster();
077  }
078
079  @Test
080  public void testHandleDeadWorker() throws Exception {
081    Table table = TEST_UTIL.createTable(TABLE_NAME, FAMILY, TEST_UTIL.KEYS_FOR_HBA_CREATE_TABLE);
082    for (int i = 0; i < 10; i++) {
083      TEST_UTIL.loadTable(table, FAMILY);
084    }
085    HRegionServer testServer = TEST_UTIL.getHBaseCluster().getRegionServer(0);
086    ProcedureExecutor<MasterProcedureEnv> masterPE = master.getMasterProcedureExecutor();
087    List<FileStatus> wals = splitWALManager.getWALsToSplit(testServer.getServerName(), false);
088    Assert.assertEquals(1, wals.size());
089    TEST_UTIL.getHBaseCluster().killRegionServer(testServer.getServerName());
090    TEST_UTIL.waitFor(30000, () -> master.getProcedures().stream()
091        .anyMatch(procedure -> procedure instanceof SplitWALProcedure));
092    Procedure splitWALProcedure = master.getProcedures().stream()
093        .filter(procedure -> procedure instanceof SplitWALProcedure).findAny().get();
094    Assert.assertNotNull(splitWALProcedure);
095    TEST_UTIL.waitFor(5000, () -> ((SplitWALProcedure) splitWALProcedure).getWorker() != null);
096    TEST_UTIL.getHBaseCluster()
097        .killRegionServer(((SplitWALProcedure) splitWALProcedure).getWorker());
098    ProcedureTestingUtility.waitProcedure(masterPE, splitWALProcedure.getProcId());
099    Assert.assertTrue(splitWALProcedure.isSuccess());
100    ProcedureTestingUtility.waitAllProcedures(masterPE);
101  }
102
103  @Test
104  public void testMasterRestart() throws Exception {
105    Table table = TEST_UTIL.createTable(TABLE_NAME, FAMILY, TEST_UTIL.KEYS_FOR_HBA_CREATE_TABLE);
106    for (int i = 0; i < 10; i++) {
107      TEST_UTIL.loadTable(table, FAMILY);
108    }
109    HRegionServer testServer = TEST_UTIL.getHBaseCluster().getRegionServer(0);
110    List<FileStatus> wals = splitWALManager.getWALsToSplit(testServer.getServerName(), false);
111    Assert.assertEquals(1, wals.size());
112    SplitWALProcedure splitWALProcedure =
113        new SplitWALProcedure(wals.get(0).getPath().toString(), testServer.getServerName());
114    long pid = ProcedureTestingUtility.submitProcedure(master.getMasterProcedureExecutor(),
115        splitWALProcedure, HConstants.NO_NONCE, HConstants.NO_NONCE);
116    TEST_UTIL.waitFor(5000, () -> splitWALProcedure.getWorker() != null);
117    // Kill master
118    TEST_UTIL.getHBaseCluster().killMaster(master.getServerName());
119    TEST_UTIL.getHBaseCluster().waitForMasterToStop(master.getServerName(), 20000);
120    // restart master
121    TEST_UTIL.getHBaseCluster().startMaster();
122    TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster();
123    this.master = TEST_UTIL.getHBaseCluster().getMaster();
124    ProcedureTestingUtility.waitProcedure(master.getMasterProcedureExecutor(), pid);
125    Optional<Procedure<?>> procedure =
126        master.getProcedures().stream().filter(p -> p.getProcId() == pid).findAny();
127    // make sure procedure is successful and wal is deleted
128    Assert.assertTrue(procedure.isPresent());
129    Assert.assertTrue(procedure.get().isSuccess());
130    Assert.assertFalse(TEST_UTIL.getTestFileSystem().exists(wals.get(0).getPath()));
131  }
132
133}