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.assignment;
019
020import static org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RegionStateTransitionState.REGION_STATE_TRANSITION_CONFIRM_OPENED_VALUE;
021
022import java.io.IOException;
023import java.util.concurrent.CountDownLatch;
024import java.util.concurrent.Future;
025import java.util.concurrent.atomic.AtomicReference;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.PleaseHoldException;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.RegionInfo;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.master.HMaster;
036import org.apache.hadoop.hbase.master.MasterServices;
037import org.apache.hadoop.hbase.master.RegionPlan;
038import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
039import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
040import org.apache.hadoop.hbase.testclassification.MasterTests;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.junit.AfterClass;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048
049import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionRequest;
050import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionResponse;
051
052@Category({ MasterTests.class, MediumTests.class })
053public class TestReportRegionStateTransitionRetry {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestReportRegionStateTransitionRetry.class);
058
059  private static final AtomicReference<CountDownLatch> RESUME_AND_FAIL = new AtomicReference<>();
060
061  private static final class AssignmentManagerForTest extends AssignmentManager {
062
063    public AssignmentManagerForTest(MasterServices master) {
064      super(master);
065    }
066
067    @Override
068    public ReportRegionStateTransitionResponse reportRegionStateTransition(
069        ReportRegionStateTransitionRequest req) throws PleaseHoldException {
070      ReportRegionStateTransitionResponse resp = super.reportRegionStateTransition(req);
071      CountDownLatch latch = RESUME_AND_FAIL.getAndSet(null);
072      if (latch != null) {
073        try {
074          latch.await();
075        } catch (InterruptedException e) {
076          throw new RuntimeException(e);
077        }
078        throw new PleaseHoldException("Inject error");
079      }
080      return resp;
081    }
082  }
083
084  public static final class HMasterForTest extends HMaster {
085
086    public HMasterForTest(Configuration conf) throws IOException {
087      super(conf);
088    }
089
090    @Override
091    protected AssignmentManager createAssignmentManager(MasterServices master) {
092      return new AssignmentManagerForTest(master);
093    }
094  }
095
096  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
097
098  private static TableName NAME = TableName.valueOf("Retry");
099
100  private static byte[] CF = Bytes.toBytes("cf");
101
102  @BeforeClass
103  public static void setUp() throws Exception {
104    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
105    UTIL.startMiniCluster(1);
106    UTIL.createTable(NAME, CF);
107    UTIL.waitTableAvailable(NAME);
108  }
109
110  @AfterClass
111  public static void tearDown() throws Exception {
112    UTIL.shutdownMiniCluster();
113  }
114
115  @Test
116  public void testRetryOnClose() throws Exception {
117    RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
118    ProcedureExecutor<MasterProcedureEnv> procExec =
119      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
120    AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
121    RegionStateNode rsn = am.getRegionStates().getRegionStateNode(region);
122
123    CountDownLatch latch = new CountDownLatch(1);
124    RESUME_AND_FAIL.set(latch);
125    Future<byte[]> future =
126      am.moveAsync(new RegionPlan(region, rsn.getRegionLocation(), rsn.getRegionLocation()));
127    TransitRegionStateProcedure proc =
128      procExec.getProcedures().stream().filter(p -> p instanceof TransitRegionStateProcedure)
129        .filter(p -> !p.isFinished()).map(p -> (TransitRegionStateProcedure) p).findAny().get();
130
131    // wait until we schedule the OpenRegionProcedure
132    UTIL.waitFor(10000,
133      () -> proc.getCurrentStateId() == REGION_STATE_TRANSITION_CONFIRM_OPENED_VALUE);
134    // Fail the reportRegionStateTransition for closing
135    latch.countDown();
136    future.get();
137
138    // confirm that the region can still be write
139    try (Table table = UTIL.getConnection().getTableBuilder(NAME, null).setWriteRpcTimeout(1000)
140      .setOperationTimeout(2000).build()) {
141      table.put(
142        new Put(Bytes.toBytes("key")).addColumn(CF, Bytes.toBytes("cq"), Bytes.toBytes("val")));
143    }
144  }
145}