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    if (this.master != null) {
075      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(
076        master.getMasterProcedureExecutor(), false);
077    }
078    TEST_UTIL.shutdownMiniCluster();
079  }
080
081  @Test
082  public void testHandleDeadWorker() throws Exception {
083    Table table = TEST_UTIL.createTable(TABLE_NAME, FAMILY, TEST_UTIL.KEYS_FOR_HBA_CREATE_TABLE);
084    for (int i = 0; i < 10; i++) {
085      TEST_UTIL.loadTable(table, FAMILY);
086    }
087    HRegionServer testServer = TEST_UTIL.getHBaseCluster().getRegionServer(0);
088    ProcedureExecutor<MasterProcedureEnv> masterPE = master.getMasterProcedureExecutor();
089    List<FileStatus> wals = splitWALManager.getWALsToSplit(testServer.getServerName(), false);
090    Assert.assertEquals(1, wals.size());
091    TEST_UTIL.getHBaseCluster().killRegionServer(testServer.getServerName());
092    TEST_UTIL.waitFor(30000, () -> master.getProcedures().stream()
093        .anyMatch(procedure -> procedure instanceof SplitWALProcedure));
094    Procedure splitWALProcedure = master.getProcedures().stream()
095        .filter(procedure -> procedure instanceof SplitWALProcedure).findAny().get();
096    Assert.assertNotNull(splitWALProcedure);
097    TEST_UTIL.waitFor(5000, () -> ((SplitWALProcedure) splitWALProcedure).getWorker() != null);
098    TEST_UTIL.getHBaseCluster()
099        .killRegionServer(((SplitWALProcedure) splitWALProcedure).getWorker());
100    ProcedureTestingUtility.waitProcedure(masterPE, splitWALProcedure.getProcId());
101    Assert.assertTrue(splitWALProcedure.isSuccess());
102    ProcedureTestingUtility.waitAllProcedures(masterPE);
103  }
104
105  @Test
106  public void testMasterRestart() throws Exception {
107    Table table = TEST_UTIL.createTable(TABLE_NAME, FAMILY, TEST_UTIL.KEYS_FOR_HBA_CREATE_TABLE);
108    for (int i = 0; i < 10; i++) {
109      TEST_UTIL.loadTable(table, FAMILY);
110    }
111    HRegionServer testServer = TEST_UTIL.getHBaseCluster().getRegionServer(0);
112    List<FileStatus> wals = splitWALManager.getWALsToSplit(testServer.getServerName(), false);
113    Assert.assertEquals(1, wals.size());
114    SplitWALProcedure splitWALProcedure =
115        new SplitWALProcedure(wals.get(0).getPath().toString(), testServer.getServerName());
116    long pid = ProcedureTestingUtility.submitProcedure(master.getMasterProcedureExecutor(),
117        splitWALProcedure, HConstants.NO_NONCE, HConstants.NO_NONCE);
118    TEST_UTIL.waitFor(5000, () -> splitWALProcedure.getWorker() != null);
119    // Kill master
120    TEST_UTIL.getHBaseCluster().killMaster(master.getServerName());
121    TEST_UTIL.getHBaseCluster().waitForMasterToStop(master.getServerName(), 20000);
122    // restart master
123    TEST_UTIL.getHBaseCluster().startMaster();
124    TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster();
125    this.master = TEST_UTIL.getHBaseCluster().getMaster();
126    ProcedureTestingUtility.waitProcedure(master.getMasterProcedureExecutor(), pid);
127    Optional<Procedure<?>> procedure =
128        master.getProcedures().stream().filter(p -> p.getProcId() == pid).findAny();
129    // make sure procedure is successful and wal is deleted
130    Assert.assertTrue(procedure.isPresent());
131    Assert.assertTrue(procedure.get().isSuccess());
132    Assert.assertFalse(TEST_UTIL.getTestFileSystem().exists(wals.get(0).getPath()));
133  }
134
135}