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
020
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtility;
023import org.apache.hadoop.hbase.master.MasterServices;
024import org.apache.hadoop.hbase.master.assignment.MockMasterServices;
025import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
026import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
027import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
028import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos;
029import org.apache.hadoop.hbase.testclassification.MasterTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.mockito.Mockito;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038import java.io.IOException;
039
040import static org.junit.Assert.assertEquals;
041
042@Category({MasterTests.class, SmallTests.class})
043public class TestRecoverMetaProcedure {
044  private static final Logger LOG = LoggerFactory.getLogger(TestRecoverMetaProcedure.class);
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestRecoverMetaProcedure.class);
048  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
049
050  /**
051   * Test the new prepare step.
052   * Here we test that our Mock is faking out the precedure well-enough for it to progress past the
053   * first prepare stage.
054   */
055  @Test
056  public void testPrepare() throws ProcedureSuspendedException, ProcedureYieldException,
057      InterruptedException, IOException {
058    RecoverMetaProcedure rmp = new RecoverMetaProcedure();
059    MasterProcedureEnv env = Mockito.mock(MasterProcedureEnv.class);
060    MasterServices masterServices =
061        new MockMasterServices(UTIL.getConfiguration(), null);
062    Mockito.when(env.getMasterServices()).thenReturn(masterServices);
063    assertEquals(StateMachineProcedure.Flow.HAS_MORE_STATE,
064        rmp.executeFromState(env, rmp.getInitialState()));
065    int stateId = rmp.getCurrentStateId();
066    assertEquals(MasterProcedureProtos.RecoverMetaState.RECOVER_META_SPLIT_LOGS_VALUE,
067        rmp.getCurrentStateId());
068  }
069
070  /**
071   * Test the new prepare step.
072   * If Master is stopping, procedure should skip the assign by returning NO_MORE_STATE
073   */
074  @Test
075  public void testPrepareWithMasterStopping() throws ProcedureSuspendedException,
076      ProcedureYieldException, InterruptedException, IOException {
077    RecoverMetaProcedure rmp = new RecoverMetaProcedure();
078    MasterProcedureEnv env = Mockito.mock(MasterProcedureEnv.class);
079    MasterServices masterServices = new MockMasterServices(UTIL.getConfiguration(), null) {
080      @Override
081      public boolean isStopping() {
082        return true;
083      }
084    };
085    Mockito.when(env.getMasterServices()).thenReturn(masterServices);
086    assertEquals(StateMachineProcedure.Flow.NO_MORE_STATE,
087        rmp.executeFromState(env, rmp.getInitialState()));
088  }
089
090  /**
091   * Test the new prepare step.
092   * If cluster is down, procedure should skip the assign by returning NO_MORE_STATE
093   */
094  @Test
095  public void testPrepareWithNoCluster() throws ProcedureSuspendedException,
096      ProcedureYieldException, InterruptedException, IOException {
097    RecoverMetaProcedure rmp = new RecoverMetaProcedure();
098    MasterProcedureEnv env = Mockito.mock(MasterProcedureEnv.class);
099    MasterServices masterServices = new MockMasterServices(UTIL.getConfiguration(), null) {
100      @Override
101      public boolean isClusterUp() {
102        return false;
103      }
104    };
105    Mockito.when(env.getMasterServices()).thenReturn(masterServices);
106    assertEquals(StateMachineProcedure.Flow.NO_MORE_STATE,
107        rmp.executeFromState(env, rmp.getInitialState()));
108  }
109}