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.procedure2.RemoteProcedureDispatcher.DISPATCH_DELAY_CONF_KEY;
021import static org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.DISPATCH_MAX_QUEUE_SIZE_CONF_KEY;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
029import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
030import org.apache.hadoop.hbase.regionserver.HRegionServer;
031import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.junit.jupiter.api.AfterEach;
034import org.junit.jupiter.api.BeforeEach;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.Test;
037import org.junit.jupiter.api.TestInfo;
038
039@Tag(MediumTests.TAG)
040public class TestLogRollProcedure {
041  private String testMethodName;
042
043  @BeforeEach
044  public void setTestMethod(TestInfo testInfo) {
045    testMethodName = testInfo.getTestMethod().get().getName();
046  }
047
048  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
049
050  private Configuration conf;
051
052  @BeforeEach
053  public void setUp() throws Exception {
054    conf = TEST_UTIL.getConfiguration();
055    conf.set(DISPATCH_DELAY_CONF_KEY, "2000");
056    conf.set(DISPATCH_MAX_QUEUE_SIZE_CONF_KEY, "128");
057    TEST_UTIL.startMiniCluster(2);
058  }
059
060  @AfterEach
061  public void tearDown() throws Exception {
062    TEST_UTIL.shutdownMiniCluster();
063  }
064
065  @Test
066  public void testSimpleLogRoll() throws IOException {
067    HRegionServer rs = TEST_UTIL.getHBaseCluster().getRegionServer(0);
068    long fileNumBefore = ((AbstractFSWAL<?>) rs.getWAL(null)).getFilenum();
069
070    TEST_UTIL.getAdmin().rollAllWALWriters();
071
072    long fileNumAfter = ((AbstractFSWAL<?>) rs.getWAL(null)).getFilenum();
073    assertTrue(fileNumAfter > fileNumBefore);
074  }
075
076  @Test
077  public void testMasterRestarts() throws IOException {
078    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
079    HRegionServer rs = cluster.getRegionServer(0);
080    long fileNumBefore = ((AbstractFSWAL<?>) rs.getWAL(null)).getFilenum();
081
082    LogRollProcedure procedure = new LogRollProcedure();
083    long procId = cluster.getMaster().getMasterProcedureExecutor().submitProcedure(procedure);
084
085    TEST_UTIL.waitFor(60000, () -> cluster.getMaster().getMasterProcedureExecutor().getProcedures()
086      .stream().anyMatch(p -> p instanceof LogRollRemoteProcedure));
087    ServerName serverName = cluster.getMaster().getServerName();
088    cluster.killMaster(serverName);
089    cluster.waitForMasterToStop(serverName, 30000);
090    cluster.startMaster();
091    cluster.waitForActiveAndReadyMaster();
092
093    ProcedureExecutor<MasterProcedureEnv> exec = cluster.getMaster().getMasterProcedureExecutor();
094    TEST_UTIL.waitFor(30000, () -> exec.isRunning() && exec.isFinished(procId));
095
096    long fileNumAfter = ((AbstractFSWAL<?>) rs.getWAL(null)).getFilenum();
097
098    assertTrue(fileNumAfter > fileNumBefore);
099  }
100}