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.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertNull;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.concurrent.CountDownLatch;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.PleaseHoldException;
032import org.apache.hadoop.hbase.ServerName;
033import org.apache.hadoop.hbase.StartTestingClusterOption;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.client.RegionInfo;
036import org.apache.hadoop.hbase.master.HMaster;
037import org.apache.hadoop.hbase.master.MasterServices;
038import org.apache.hadoop.hbase.master.RegionPlan;
039import org.apache.hadoop.hbase.master.ServerManager;
040import org.apache.hadoop.hbase.master.region.MasterRegion;
041import org.apache.hadoop.hbase.regionserver.HRegionServer;
042import org.apache.hadoop.hbase.testclassification.MasterTests;
043import org.apache.hadoop.hbase.testclassification.MediumTests;
044import org.apache.hadoop.hbase.util.Bytes;
045import org.junit.AfterClass;
046import org.junit.BeforeClass;
047import org.junit.ClassRule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050
051import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
052import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionRequest;
053import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionResponse;
054
055@Category({ MasterTests.class, MediumTests.class })
056public class TestRegionAssignedToMultipleRegionServers {
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060    HBaseClassTestRule.forClass(TestRegionAssignedToMultipleRegionServers.class);
061
062  private static final List<ServerName> EXCLUDE_SERVERS = new ArrayList<>();
063
064  private static boolean HALT = false;
065
066  private static boolean KILL = false;
067
068  private static CountDownLatch ARRIVE;
069
070  private static final class ServerManagerForTest extends ServerManager {
071
072    public ServerManagerForTest(MasterServices master) {
073      super(master);
074    }
075
076    @Override
077    public List<ServerName> createDestinationServersList() {
078      return super.createDestinationServersList(EXCLUDE_SERVERS);
079    }
080  }
081
082  private static final class AssignmentManagerForTest extends AssignmentManager {
083
084    public AssignmentManagerForTest(MasterServices master, MasterRegion masterRegion) {
085      super(master, masterRegion);
086    }
087
088    @Override
089    public ReportRegionStateTransitionResponse reportRegionStateTransition(
090        ReportRegionStateTransitionRequest req) throws PleaseHoldException {
091      if (req.getTransition(0).getTransitionCode() == TransitionCode.OPENED) {
092        if (ARRIVE != null) {
093          ARRIVE.countDown();
094          ARRIVE = null;
095        }
096        while (HALT) {
097          try {
098            Thread.sleep(100);
099          } catch (InterruptedException e) {
100            throw new RuntimeException(e);
101          }
102          if (KILL) {
103            throw new PleaseHoldException("Inject error!");
104          }
105        }
106      }
107      return super.reportRegionStateTransition(req);
108    }
109  }
110
111  public static final class HMasterForTest extends HMaster {
112
113    public HMasterForTest(Configuration conf) throws IOException {
114      super(conf);
115    }
116
117    @Override
118    protected AssignmentManager createAssignmentManager(MasterServices master,
119      MasterRegion masterRegion) {
120      return new AssignmentManagerForTest(master, masterRegion);
121    }
122
123    @Override
124    protected ServerManager createServerManager(MasterServices master) throws IOException {
125      setupClusterConnection();
126      return new ServerManagerForTest(master);
127    }
128  }
129
130  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
131
132  private static TableName NAME = TableName.valueOf("Assign");
133
134  private static byte[] CF = Bytes.toBytes("cf");
135
136  @BeforeClass
137  public static void setUp() throws Exception {
138    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
139    UTIL.startMiniCluster(
140      StartTestingClusterOption.builder().numMasters(2).numRegionServers(2).build());
141    UTIL.createTable(NAME, CF);
142    UTIL.waitTableAvailable(NAME);
143    UTIL.getAdmin().balancerSwitch(false, true);
144  }
145
146  @AfterClass
147  public static void tearDown() throws Exception {
148    UTIL.shutdownMiniCluster();
149  }
150
151  @Test
152  public void test() throws Exception {
153    RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
154    AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
155    RegionStateNode rsn = am.getRegionStates().getRegionStateNode(region);
156
157    ServerName sn = rsn.getRegionLocation();
158    ARRIVE = new CountDownLatch(1);
159    HALT = true;
160    am.moveAsync(new RegionPlan(region, sn, sn));
161    ARRIVE.await();
162
163    // let's restart the master
164    EXCLUDE_SERVERS.add(rsn.getRegionLocation());
165    KILL = true;
166    HMaster activeMaster = UTIL.getMiniHBaseCluster().getMaster();
167    activeMaster.abort("For testing");
168    activeMaster.join();
169    KILL = false;
170
171    // sleep a while to reproduce the problem, as after the fix in HBASE-21472 the execution logic
172    // is changed so the old code to reproduce the problem can not compile...
173    Thread.sleep(10000);
174    HALT = false;
175    Thread.sleep(5000);
176
177    HRegionServer rs = UTIL.getMiniHBaseCluster().getRegionServer(sn);
178    assertNotNull(rs.getRegion(region.getEncodedName()));
179    assertNull(UTIL.getOtherRegionServer(rs).getRegion(region.getEncodedName()));
180  }
181}