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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.fail;
022
023import org.apache.hadoop.hbase.CallQueueTooBigException;
024import org.apache.hadoop.hbase.DoNotRetryIOException;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.RetriesExhaustedException;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035@Tag(MasterTests.TAG)
036@Tag(MediumTests.TAG)
037public class TestAMServerFailedOpen extends TestAssignmentManagerBase {
038
039  private static final Logger LOG = LoggerFactory.getLogger(TestAMServerFailedOpen.class);
040
041  @Override
042  protected int getAssignMaxAttempts() {
043    // do not need to retry so many times as we will finally fail...
044    return 10;
045  }
046
047  @Test
048  public void testServerNotYetRunning() throws Exception {
049    testRetriesExhaustedFailure(TableName.valueOf(testMethodName),
050      new ServerNotYetRunningRsExecutor());
051  }
052
053  private void testRetriesExhaustedFailure(final TableName tableName, final MockRSExecutor executor)
054    throws Exception {
055    RegionInfo hri = createRegionInfo(tableName, 1);
056
057    // collect AM metrics before test
058    collectAssignmentManagerMetrics();
059
060    // Test Assign operation failure
061    rsDispatcher.setMockRsExecutor(executor);
062    try {
063      waitOnFuture(submitProcedure(createAssignProcedure(hri)));
064      fail("unexpected assign completion");
065    } catch (RetriesExhaustedException e) {
066      // expected exception
067      LOG.info("expected exception from assign operation: " + e.getMessage(), e);
068    }
069
070    // Assign the region (without problems)
071    rsDispatcher.setMockRsExecutor(new GoodRsExecutor());
072    waitOnFuture(submitProcedure(createAssignProcedure(hri)));
073  }
074
075  @Test
076  public void testDoNotRetryExceptionOnAssignment() throws Exception {
077    // collect AM metrics before test
078    collectAssignmentManagerMetrics();
079
080    testFailedOpen(TableName.valueOf("testDoNotRetryExceptionOnAssignment"),
081      new FaultyRsExecutor(new DoNotRetryIOException("test do not retry fault")));
082
083    assertEquals(assignSubmittedCount + 1, assignProcMetrics.getSubmittedCounter().getCount());
084    assertEquals(assignFailedCount + 1, assignProcMetrics.getFailedCounter().getCount());
085  }
086
087  private void testFailedOpen(final TableName tableName, final MockRSExecutor executor)
088    throws Exception {
089    final RegionInfo hri = createRegionInfo(tableName, 1);
090
091    // Test Assign operation failure
092    rsDispatcher.setMockRsExecutor(executor);
093    try {
094      waitOnFuture(submitProcedure(createAssignProcedure(hri)));
095      fail("unexpected assign completion");
096    } catch (RetriesExhaustedException e) {
097      // expected exception
098      LOG.info("REGION STATE " + am.getRegionStates().getRegionStateNode(hri));
099      LOG.info("expected exception from assign operation: " + e.getMessage(), e);
100      assertEquals(true, am.getRegionStates().getRegionState(hri).isFailedOpen());
101    }
102  }
103
104  @Test
105  public void testCallQueueTooBigExceptionOnAssignment() throws Exception {
106    // collect AM metrics before test
107    collectAssignmentManagerMetrics();
108
109    testFailedOpen(TableName.valueOf("testCallQueueTooBigExceptionOnAssignment"),
110      new FaultyRsExecutor(new CallQueueTooBigException("test do not retry fault")));
111
112    assertEquals(assignSubmittedCount + 1, assignProcMetrics.getSubmittedCounter().getCount());
113    assertEquals(assignFailedCount + 1, assignProcMetrics.getFailedCounter().getCount());
114  }
115}