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 java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.TableName;
026import org.junit.Before;
027import org.junit.Test;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Based class for testing rpc timeout logic for {@link ConnectionImplementation}.
033 */
034public abstract class AbstractTestCIRpcTimeout extends AbstractTestCITimeout {
035
036  private static final Logger LOG = LoggerFactory.getLogger(AbstractTestCIRpcTimeout.class);
037
038  private TableName tableName;
039
040  @Before
041  public void setUp() throws IOException {
042    tableName = TableName.valueOf(name.getMethodName());
043    TableDescriptor htd =
044      TableDescriptorBuilder.newBuilder(tableName).setCoprocessor(SleepCoprocessor.class.getName())
045        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAM_NAM)).build();
046    TEST_UTIL.getAdmin().createTable(htd);
047  }
048
049  protected abstract void execute(Table table) throws IOException;
050
051  @Test
052  public void testRpcTimeout() throws IOException {
053    Configuration c = new Configuration(TEST_UTIL.getConfiguration());
054    try (Table table = TEST_UTIL.getConnection().getTableBuilder(tableName, null)
055      .setRpcTimeout(SleepCoprocessor.SLEEP_TIME / 2)
056      .setReadRpcTimeout(SleepCoprocessor.SLEEP_TIME / 2)
057      .setWriteRpcTimeout(SleepCoprocessor.SLEEP_TIME / 2)
058      .setOperationTimeout(SleepCoprocessor.SLEEP_TIME * 100).build()) {
059      execute(table);
060      fail("Get should not have succeeded");
061    } catch (RetriesExhaustedException e) {
062      LOG.info("We received an exception, as expected ", e);
063    }
064
065    // Again, with configuration based override
066    c.setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, SleepCoprocessor.SLEEP_TIME / 2);
067    c.setInt(HConstants.HBASE_RPC_READ_TIMEOUT_KEY, SleepCoprocessor.SLEEP_TIME / 2);
068    c.setInt(HConstants.HBASE_RPC_WRITE_TIMEOUT_KEY, SleepCoprocessor.SLEEP_TIME / 2);
069    c.setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, SleepCoprocessor.SLEEP_TIME * 100);
070    try (Connection conn = ConnectionFactory.createConnection(c)) {
071      try (Table table = conn.getTable(tableName)) {
072        execute(table);
073        fail("Get should not have succeeded");
074      } catch (RetriesExhaustedException e) {
075        LOG.info("We received an exception, as expected ", e);
076      }
077    }
078  }
079}