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.assertEquals;
021import static org.junit.Assert.assertNotEquals;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.Get;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.master.procedure.MasterProcedureConstants;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.testclassification.MasterTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.After;
040import org.junit.Before;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047@Category({MasterTests.class, LargeTests.class})
048public class TestAssignmentOnRSCrash {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestAssignmentOnRSCrash.class);
053
054  private static final Logger LOG = LoggerFactory.getLogger(TestAssignmentOnRSCrash.class);
055
056  private static final TableName TEST_TABLE = TableName.valueOf("testb");
057  private static final String FAMILY_STR = "f";
058  private static final byte[] FAMILY = Bytes.toBytes(FAMILY_STR);
059  private static final int NUM_RS = 3;
060
061  private HBaseTestingUtility UTIL;
062
063  private static void setupConf(Configuration conf) {
064    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
065    conf.setInt(MasterProcedureConstants.MASTER_URGENT_PROCEDURE_THREADS, 0);
066    conf.set("hbase.balancer.tablesOnMaster", "none");
067  }
068
069  @Before
070  public void setup() throws Exception {
071    UTIL = new HBaseTestingUtility();
072
073    setupConf(UTIL.getConfiguration());
074    UTIL.startMiniCluster(NUM_RS);
075
076    UTIL.createTable(TEST_TABLE, new byte[][] { FAMILY }, new byte[][] {
077      Bytes.toBytes("B"), Bytes.toBytes("D"), Bytes.toBytes("F"), Bytes.toBytes("L")
078    });
079  }
080
081  @After
082  public void tearDown() throws Exception {
083    UTIL.shutdownMiniCluster();
084  }
085
086  @Test
087  public void testKillRsWithUserRegionWithData() throws Exception {
088    testCrashRsWithUserRegion(true, true);
089  }
090
091  @Test
092  public void testKillRsWithUserRegionWithoutData() throws Exception {
093    testCrashRsWithUserRegion(true, false);
094  }
095
096  @Test
097  public void testStopRsWithUserRegionWithData() throws Exception {
098    testCrashRsWithUserRegion(false, true);
099  }
100
101  @Test
102  public void testStopRsWithUserRegionWithoutData() throws Exception {
103    testCrashRsWithUserRegion(false, false);
104  }
105
106  private void testCrashRsWithUserRegion(final boolean kill, final boolean withData)
107      throws Exception {
108    final int NROWS = 100;
109    int nkilled = 0;
110    for (RegionInfo hri: UTIL.getHBaseAdmin().getTableRegions(TEST_TABLE)) {
111      ServerName serverName = AssignmentTestingUtil.getServerHoldingRegion(UTIL, hri);
112      if (AssignmentTestingUtil.isServerHoldingMeta(UTIL, serverName)) continue;
113
114      if (withData) {
115        testInsert(hri, NROWS);
116      }
117
118      // wait for regions to enter in transition and then to get out of transition
119      AssignmentTestingUtil.crashRs(UTIL, serverName, kill);
120      AssignmentTestingUtil.waitForRegionToBeInTransition(UTIL, hri);
121      UTIL.waitUntilNoRegionsInTransition();
122
123      if (withData) {
124        assertEquals(NROWS, testGet(hri, NROWS));
125      }
126
127      // region should be moved to another RS
128      assertNotEquals(serverName, AssignmentTestingUtil.getServerHoldingRegion(UTIL, hri));
129
130      if (++nkilled == (NUM_RS - 1)) {
131        break;
132      }
133    }
134    assertTrue("expected RSs to be killed", nkilled > 0);
135  }
136
137  @Test
138  public void testKillRsWithMetaRegion() throws Exception {
139    testCrashRsWithMetaRegion(true);
140  }
141
142  @Test
143  public void testStopRsWithMetaRegion() throws Exception {
144    testCrashRsWithMetaRegion(false);
145  }
146
147  private void testCrashRsWithMetaRegion(final boolean kill) throws Exception {
148    int nkilled = 0;
149    for (RegionInfo hri: AssignmentTestingUtil.getMetaRegions(UTIL)) {
150      ServerName serverName = AssignmentTestingUtil.crashRsWithRegion(UTIL, hri, kill);
151
152      // wait for region to enter in transition and then to get out of transition
153      AssignmentTestingUtil.waitForRegionToBeInTransition(UTIL, hri);
154      UTIL.waitUntilNoRegionsInTransition();
155      testGet(hri, 10);
156
157      // region should be moved to another RS
158      assertNotEquals(serverName, AssignmentTestingUtil.getServerHoldingRegion(UTIL, hri));
159
160      if (++nkilled == (NUM_RS - 1)) {
161        break;
162      }
163    }
164    assertTrue("expected RSs to be killed", nkilled > 0);
165  }
166
167  private void testInsert(final RegionInfo hri, final int nrows) throws IOException {
168    final Table table = UTIL.getConnection().getTable(hri.getTable());
169    for (int i = 0; i < nrows; ++i) {
170      final byte[] row = Bytes.add(hri.getStartKey(), Bytes.toBytes(i));
171      final Put put = new Put(row);
172      put.addColumn(FAMILY, null, row);
173      table.put(put);
174    }
175  }
176
177  public int testGet(final RegionInfo hri, final int nrows) throws IOException {
178    int nresults = 0;
179    final Table table = UTIL.getConnection().getTable(hri.getTable());
180    for (int i = 0; i < nrows; ++i) {
181      final byte[] row = Bytes.add(hri.getStartKey(), Bytes.toBytes(i));
182      final Result result = table.get(new Get(row));
183      if (result != null && !result.isEmpty() &&
184          Bytes.equals(row, result.getValue(FAMILY, null))) {
185        nresults++;
186      }
187    }
188    return nresults;
189  }
190}