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.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.RegionInfo;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.master.HMaster;
030import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil;
031import org.apache.hadoop.hbase.procedure2.Procedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041@Category({ MasterTests.class, LargeTests.class })
042public class TestSCP extends TestSCPBase {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestSCP.class);
046
047  private static final Logger LOG = LoggerFactory.getLogger(TestSCP.class);
048
049  @Test
050  public void testCrashTargetRs() throws Exception {
051    testRecoveryAndDoubleExecution(false, false);
052  }
053
054  @Test
055  public void testConcurrentSCPForSameServer() throws Exception {
056    final TableName tableName = TableName.valueOf("testConcurrentSCPForSameServer");
057    try (Table t = createTable(tableName)) {
058      // Load the table with a bit of data so some logs to split and some edits in each region.
059      this.util.loadTable(t, HBaseTestingUtility.COLUMNS[0]);
060      final int count = util.countRows(t);
061      assertTrue("expected some rows", count > 0);
062      // find the first server that match the request and executes the test
063      ServerName rsToKill = null;
064      for (RegionInfo hri : util.getAdmin().getRegions(tableName)) {
065        final ServerName serverName = AssignmentTestingUtil.getServerHoldingRegion(util, hri);
066        if (AssignmentTestingUtil.isServerHoldingMeta(util, serverName) == true) {
067          rsToKill = serverName;
068          break;
069        }
070      }
071      HMaster master = util.getHBaseCluster().getMaster();
072      final ProcedureExecutor<MasterProcedureEnv> pExecutor = master.getMasterProcedureExecutor();
073      ServerCrashProcedure procB =
074        new ServerCrashProcedure(pExecutor.getEnvironment(), rsToKill, false, false);
075      AssignmentTestingUtil.killRs(util, rsToKill);
076      long procId = getSCPProcId(pExecutor);
077      Procedure<?> procA = pExecutor.getProcedure(procId);
078      LOG.info("submit SCP procedureA");
079      util.waitFor(5000, () -> procA.hasLock());
080      LOG.info("procedureA acquired the lock");
081      assertEquals(Procedure.LockState.LOCK_EVENT_WAIT,
082        procB.acquireLock(pExecutor.getEnvironment()));
083      LOG.info("procedureB should not be able to get the lock");
084      util.waitFor(60000,
085        () -> procB.acquireLock(pExecutor.getEnvironment()) == Procedure.LockState.LOCK_ACQUIRED);
086      LOG.info("when procedure B get the lock, procedure A should be finished");
087      assertTrue(procA.isFinished());
088    }
089  }
090}