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