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