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;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertFalse;
024import static org.junit.jupiter.api.Assertions.assertNotNull;
025import static org.junit.jupiter.api.Assertions.assertTrue;
026
027import java.util.List;
028import java.util.Optional;
029import org.apache.hadoop.fs.FileStatus;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.master.HMaster;
035import org.apache.hadoop.hbase.master.SplitWALManager;
036import org.apache.hadoop.hbase.procedure2.Procedure;
037import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
038import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
039import org.apache.hadoop.hbase.regionserver.HRegionServer;
040import org.apache.hadoop.hbase.testclassification.MasterTests;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.junit.jupiter.api.AfterEach;
044import org.junit.jupiter.api.BeforeEach;
045import org.junit.jupiter.api.Tag;
046import org.junit.jupiter.api.Test;
047
048@Tag(MasterTests.TAG)
049@Tag(MediumTests.TAG)
050public class TestSplitWALProcedure {
051
052  private static HBaseTestingUtil TEST_UTIL;
053  private HMaster master;
054  private TableName TABLE_NAME;
055  private SplitWALManager splitWALManager;
056  private byte[] FAMILY;
057
058  @BeforeEach
059  public void setup() throws Exception {
060    TEST_UTIL = new HBaseTestingUtil();
061    TEST_UTIL.getConfiguration().setBoolean(HBASE_SPLIT_WAL_COORDINATED_BY_ZK, false);
062    TEST_UTIL.getConfiguration().setInt(HBASE_SPLIT_WAL_MAX_SPLITTER, 1);
063    TEST_UTIL.startMiniCluster(3);
064    master = TEST_UTIL.getHBaseCluster().getMaster();
065    splitWALManager = master.getSplitWALManager();
066    TABLE_NAME = TableName.valueOf(Bytes.toBytes("TestSplitWALProcedure"));
067    FAMILY = Bytes.toBytes("test");
068  }
069
070  @AfterEach
071  public void teardown() throws Exception {
072    if (this.master != null) {
073      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(master.getMasterProcedureExecutor(),
074        false);
075    }
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    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    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    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    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    assertTrue(procedure.isPresent());
129    assertTrue(procedure.get().isSuccess());
130    assertFalse(TEST_UTIL.getTestFileSystem().exists(wals.get(0).getPath()));
131  }
132
133}