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.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.PleaseHoldException;
032import org.apache.hadoop.hbase.ServerName;
033import org.apache.hadoop.hbase.StartMiniClusterOption;
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.regionserver.HRegionServer;
041import org.apache.hadoop.hbase.testclassification.MasterTests;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.zookeeper.KeeperException;
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) {
085      super(master);
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, KeeperException {
114      super(conf);
115    }
116
117    @Override
118    protected AssignmentManager createAssignmentManager(MasterServices master) {
119      return new AssignmentManagerForTest(master);
120    }
121
122    @Override
123    protected ServerManager createServerManager(MasterServices master) throws IOException {
124      setupClusterConnection();
125      return new ServerManagerForTest(master);
126    }
127  }
128
129  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
130
131  private static TableName NAME = TableName.valueOf("Assign");
132
133  private static byte[] CF = Bytes.toBytes("cf");
134
135  @BeforeClass
136  public static void setUp() throws Exception {
137    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
138    UTIL
139      .startMiniCluster(StartMiniClusterOption.builder().numMasters(2).numRegionServers(2).build());
140    UTIL.createTable(NAME, CF);
141    UTIL.waitTableAvailable(NAME);
142    UTIL.getAdmin().balancerSwitch(false, true);
143  }
144
145  @AfterClass
146  public static void tearDown() throws Exception {
147    UTIL.shutdownMiniCluster();
148  }
149
150  @Test
151  public void test() throws Exception {
152    RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
153    AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
154    RegionStateNode rsn = am.getRegionStates().getRegionStateNode(region);
155
156    ServerName sn = rsn.getRegionLocation();
157    ARRIVE = new CountDownLatch(1);
158    HALT = true;
159    am.moveAsync(new RegionPlan(region, sn, sn));
160    ARRIVE.await();
161
162    // let's restart the master
163    EXCLUDE_SERVERS.add(rsn.getRegionLocation());
164    KILL = true;
165    HMaster activeMaster = UTIL.getMiniHBaseCluster().getMaster();
166    activeMaster.abort("For testing");
167    activeMaster.getThread().join();
168    KILL = false;
169
170    // sleep a while to reproduce the problem, as after the fix in HBASE-21472 the execution logic
171    // is changed so the old code to reproduce the problem can not compile...
172    Thread.sleep(10000);
173    HALT = false;
174    Thread.sleep(5000);
175
176    HRegionServer rs = UTIL.getMiniHBaseCluster().getRegionServer(sn);
177    assertNotNull(rs.getRegion(region.getEncodedName()));
178    assertNull(UTIL.getOtherRegionServer(rs).getRegion(region.getEncodedName()));
179  }
180}