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.fail;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.junit.Before;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035@Category({ ClientTests.class, MediumTests.class })
036public class TestCISleep extends AbstractTestCITimeout {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestCISleep.class);
041
042  private static Logger LOG = LoggerFactory.getLogger(TestCISleep.class);
043
044  private TableName tableName;
045
046  @Before
047  public void setUp() {
048    tableName = TableName.valueOf(name.getMethodName());
049  }
050
051  /**
052   * Test starting from 0 index when RpcRetryingCaller calculate the backoff time.
053   */
054  @Test
055  public void testRpcRetryingCallerSleep() throws Exception {
056    TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName)
057      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAM_NAM))
058      .setCoprocessor(CoprocessorDescriptorBuilder.newBuilder(SleepAndFailFirstTime.class.getName())
059        .setProperty(SleepAndFailFirstTime.SLEEP_TIME_CONF_KEY, String.valueOf(2000)).build())
060      .build();
061    TEST_UTIL.getAdmin().createTable(htd);
062
063    Configuration c = new Configuration(TEST_UTIL.getConfiguration());
064    c.setInt(HConstants.HBASE_CLIENT_PAUSE, 3000);
065    c.setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 4000);
066
067    try (Connection conn = ConnectionFactory.createConnection(c)) {
068      SleepAndFailFirstTime.ct.set(0);
069      try (Table table = conn.getTableBuilder(tableName, null).setOperationTimeout(8000).build()) {
070        // Check that it works. Because 2s + 3s * RETRY_BACKOFF[0] + 2s < 8s
071        table.get(new Get(FAM_NAM));
072      }
073      SleepAndFailFirstTime.ct.set(0);
074      try (Table table = conn.getTableBuilder(tableName, null).setOperationTimeout(6000).build()) {
075        // Will fail this time. After sleep, there are not enough time for second retry
076        // Beacuse 2s + 3s + 2s > 6s
077        table.get(new Get(FAM_NAM));
078        fail("We expect an exception here");
079      } catch (RetriesExhaustedException e) {
080        LOG.info("We received an exception, as expected ", e);
081      }
082    }
083  }
084}