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