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