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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
029import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
030import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
031import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility.NoopProcedure;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
035import org.junit.jupiter.api.AfterAll;
036import org.junit.jupiter.api.BeforeAll;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040/**
041 * The master must initialize when its procedure store holds more than one unfinished
042 * ServerCrashProcedure for the same server.
043 */
044@Tag(MasterTests.TAG)
045@Tag(MediumTests.TAG)
046public class TestMasterStartupWithDuplicateSCPs {
047
048  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
049
050  @BeforeAll
051  public static void setUp() throws Exception {
052    // the restarted master gets a new port, so the default rpc connection registry can not work
053    UTIL.getConfiguration().set(HConstants.CLIENT_CONNECTION_REGISTRY_IMPL_CONF_KEY,
054      HConstants.ZK_CONNECTION_REGISTRY_CLASS);
055    UTIL.startMiniCluster(1);
056  }
057
058  @AfterAll
059  public static void tearDown() throws IOException {
060    UTIL.shutdownMiniCluster();
061  }
062
063  @Test
064  public void testRestartWithDuplicateSCPs() throws Exception {
065    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
066    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
067    MasterProcedureEnv env = procExec.getEnvironment();
068    ServerName deadServer =
069      ServerName.valueOf("dead.example.org", 16020, EnvironmentEdgeManager.currentTime());
070
071    // Hold the server lock so both SCPs stay queued, and therefore unfinished, in the store.
072    NoopProcedure<MasterProcedureEnv> lockHolder = new NoopProcedure<>();
073    assertFalse(env.getProcedureScheduler().waitServerExclusiveLock(lockHolder, deadServer));
074    long pid1 = procExec.submitProcedure(new ServerCrashProcedure(env, deadServer, false, false));
075    long pid2 = procExec.submitProcedure(new ServerCrashProcedure(env, deadServer, false, false));
076    assertEquals(2,
077      procExec
078        .getActiveProceduresNoCopy().stream().filter(p -> p instanceof ServerCrashProcedure
079          && !p.isFinished() && ((ServerCrashProcedure) p).getServerName().equals(deadServer))
080        .count());
081
082    UTIL.getMiniHBaseCluster().stopMaster(0).join();
083    UTIL.getMiniHBaseCluster().startMaster();
084    UTIL.waitFor(60000, () -> {
085      HMaster m = UTIL.getMiniHBaseCluster().getMaster();
086      return m != null && m.isInitialized();
087    });
088
089    HMaster newMaster = UTIL.getMiniHBaseCluster().getMaster();
090    assertTrue(newMaster.getServerManager().getDeadServers().isDeadServer(deadServer));
091    ProcedureExecutor<MasterProcedureEnv> newProcExec = newMaster.getMasterProcedureExecutor();
092    UTIL.waitFor(60000, () -> newProcExec.isFinished(pid1) && newProcExec.isFinished(pid2));
093  }
094}