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.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Arrays;
025import java.util.List;
026import java.util.stream.Collectors;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.master.HMaster;
032import org.apache.hadoop.hbase.master.RegionState;
033import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
034import org.apache.hadoop.hbase.master.procedure.TableProcedureInterface;
035import org.apache.hadoop.hbase.procedure2.Procedure;
036import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
037import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
038import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
039import org.apache.hadoop.hbase.regionserver.HRegionServer;
040import org.apache.hadoop.hbase.testclassification.ClientTests;
041import org.apache.hadoop.hbase.testclassification.LargeTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.junit.AfterClass;
044import org.junit.Before;
045import org.junit.BeforeClass;
046import org.junit.ClassRule;
047import org.junit.Rule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050import org.junit.rules.TestName;
051import org.junit.runner.RunWith;
052import org.junit.runners.Parameterized;
053import org.junit.runners.Parameterized.Parameter;
054import org.junit.runners.Parameterized.Parameters;
055import org.slf4j.Logger;
056import org.slf4j.LoggerFactory;
057
058import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
059
060import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
061
062/**
063 * Class to test HBaseHbck. Spins up the minicluster once at test start and then takes it down
064 * afterward. Add any testing of HBaseHbck functionality here.
065 */
066@RunWith(Parameterized.class)
067@Category({ LargeTests.class, ClientTests.class })
068public class TestHbck {
069  @ClassRule
070  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestHbck.class);
071
072  private static final Logger LOG = LoggerFactory.getLogger(TestHbck.class);
073  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
074
075  @Rule
076  public TestName name = new TestName();
077
078  @Parameter
079  public boolean async;
080
081  private static final TableName TABLE_NAME = TableName.valueOf(TestHbck.class.getSimpleName());
082
083  private static ProcedureExecutor<MasterProcedureEnv> procExec;
084
085  private static AsyncConnection ASYNC_CONN;
086
087  @Parameters(name = "{index}: async={0}")
088  public static List<Object[]> params() {
089    return Arrays.asList(new Object[] { false }, new Object[] { true });
090  }
091
092  private Hbck getHbck() throws Exception {
093    if (async) {
094      return ASYNC_CONN.getHbck().get();
095    } else {
096      return TEST_UTIL.getHbck();
097    }
098  }
099
100  @BeforeClass
101  public static void setUpBeforeClass() throws Exception {
102    TEST_UTIL.startMiniCluster(3);
103    TEST_UTIL.createMultiRegionTable(TABLE_NAME, Bytes.toBytes("family1"), 5);
104    procExec = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
105    ASYNC_CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
106  }
107
108  @AfterClass
109  public static void tearDownAfterClass() throws Exception {
110    Closeables.close(ASYNC_CONN, true);
111    TEST_UTIL.shutdownMiniCluster();
112  }
113
114  @Before
115  public void setUp() throws IOException {
116    TEST_UTIL.ensureSomeRegionServersAvailable(3);
117  }
118
119  public static class SuspendProcedure extends
120      ProcedureTestingUtility.NoopProcedure<MasterProcedureEnv> implements TableProcedureInterface {
121    public SuspendProcedure() {
122      super();
123    }
124
125    @SuppressWarnings({ "rawtypes", "unchecked" })
126    @Override
127    protected Procedure[] execute(final MasterProcedureEnv env) throws ProcedureSuspendedException {
128      // Always suspend the procedure
129      throw new ProcedureSuspendedException();
130    }
131
132    @Override
133    public TableName getTableName() {
134      return TABLE_NAME;
135    }
136
137    @Override
138    public TableOperationType getTableOperationType() {
139      return TableOperationType.READ;
140    }
141  }
142
143  @Test
144  public void testBypassProcedure() throws Exception {
145    // SuspendProcedure
146    final SuspendProcedure proc = new SuspendProcedure();
147    long procId = procExec.submitProcedure(proc);
148    Thread.sleep(500);
149
150    // bypass the procedure
151    List<Long> pids = Arrays.<Long> asList(procId);
152    List<Boolean> results = getHbck().bypassProcedure(pids, 30000, false, false);
153    assertTrue("Failed to by pass procedure!", results.get(0));
154    TEST_UTIL.waitFor(5000, () -> proc.isSuccess() && proc.isBypass());
155    LOG.info("{} finished", proc);
156  }
157
158  @Test
159  public void testSetTableStateInMeta() throws Exception {
160    Hbck hbck = getHbck();
161    // set table state to DISABLED
162    hbck.setTableStateInMeta(new TableState(TABLE_NAME, TableState.State.DISABLED));
163    // Method {@link Hbck#setTableStateInMeta()} returns previous state, which in this case
164    // will be DISABLED
165    TableState prevState =
166      hbck.setTableStateInMeta(new TableState(TABLE_NAME, TableState.State.ENABLED));
167    assertTrue("Incorrect previous state! expeced=DISABLED, found=" + prevState.getState(),
168      prevState.isDisabled());
169  }
170
171  @Test
172  public void testAssigns() throws Exception {
173    Hbck hbck = getHbck();
174    try (Admin admin = TEST_UTIL.getConnection().getAdmin()) {
175      List<RegionInfo> regions = admin.getRegions(TABLE_NAME);
176      for (RegionInfo ri : regions) {
177        RegionState rs = TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager()
178          .getRegionStates().getRegionState(ri.getEncodedName());
179        LOG.info("RS: {}", rs.toString());
180      }
181      List<Long> pids =
182        hbck.unassigns(regions.stream().map(r -> r.getEncodedName()).collect(Collectors.toList()));
183      waitOnPids(pids);
184      for (RegionInfo ri : regions) {
185        RegionState rs = TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager()
186          .getRegionStates().getRegionState(ri.getEncodedName());
187        LOG.info("RS: {}", rs.toString());
188        assertTrue(rs.toString(), rs.isClosed());
189      }
190      pids =
191        hbck.assigns(regions.stream().map(r -> r.getEncodedName()).collect(Collectors.toList()));
192      waitOnPids(pids);
193      for (RegionInfo ri : regions) {
194        RegionState rs = TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager()
195          .getRegionStates().getRegionState(ri.getEncodedName());
196        LOG.info("RS: {}", rs.toString());
197        assertTrue(rs.toString(), rs.isOpened());
198      }
199      // What happens if crappy region list passed?
200      pids = hbck.assigns(
201        Arrays.stream(new String[] { "a", "some rubbish name" }).collect(Collectors.toList()));
202      for (long pid : pids) {
203        assertEquals(org.apache.hadoop.hbase.procedure2.Procedure.NO_PROC_ID, pid);
204      }
205    }
206  }
207
208  @Test
209  public void testScheduleSCP() throws Exception {
210    HRegionServer testRs = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
211    TEST_UTIL.loadTable(TEST_UTIL.getConnection().getTable(TABLE_NAME), Bytes.toBytes("family1"),
212      true);
213    ServerName serverName = testRs.getServerName();
214    Hbck hbck = getHbck();
215    List<Long> pids =
216      hbck.scheduleServerCrashProcedure(Arrays.asList(ProtobufUtil.toServerName(serverName)));
217    assertTrue(pids.get(0) > 0);
218    LOG.info("pid is {}", pids.get(0));
219
220    List<Long> newPids =
221      hbck.scheduleServerCrashProcedure(Arrays.asList(ProtobufUtil.toServerName(serverName)));
222    assertTrue(newPids.get(0) < 0);
223    LOG.info("pid is {}", newPids.get(0));
224    waitOnPids(pids);
225  }
226
227  @Test
228  public void testRunHbckChore() throws Exception {
229    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
230    long endTimestamp = master.getHbckChore().getCheckingEndTimestamp();
231    Hbck hbck = getHbck();
232    boolean ran = false;
233    while (!ran) {
234      ran = hbck.runHbckChore();
235      if (ran) {
236        assertTrue(master.getHbckChore().getCheckingEndTimestamp() > endTimestamp);
237      }
238    }
239  }
240
241  private void waitOnPids(List<Long> pids) {
242    TEST_UTIL.waitFor(60000, () -> pids.stream().allMatch(procExec::isFinished));
243  }
244}