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 java.io.IOException;
021import java.util.List;
022import java.util.Optional;
023import java.util.concurrent.atomic.AtomicLong;
024import org.apache.hadoop.hbase.Cell;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.coprocessor.ObserverContext;
030import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
031import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
032import org.apache.hadoop.hbase.coprocessor.RegionObserver;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.Threads;
036import org.junit.AfterClass;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.Rule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.rules.TestName;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046/**
047 * Test a drop timeout request.
048 * This test used to be in TestHCM but it has particulare requirements -- i.e. one handler only --
049 * so run it apart from the rest of TestHCM.
050 */
051@Category({MediumTests.class})
052public class TestDropTimeoutRequest {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestDropTimeoutRequest.class);
057
058  @Rule
059  public TestName name = new TestName();
060
061  private static final Logger LOG = LoggerFactory.getLogger(TestDropTimeoutRequest.class);
062  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
063  private static final byte[] FAM_NAM = Bytes.toBytes("f");
064  private static final int RPC_RETRY = 5;
065
066  /**
067   * Coprocessor that sleeps a while the first time you do a Get
068   */
069  public static class SleepLongerAtFirstCoprocessor implements RegionCoprocessor, RegionObserver {
070    public static final int SLEEP_TIME = 2000;
071    static final AtomicLong ct = new AtomicLong(0);
072
073    @Override
074    public Optional<RegionObserver> getRegionObserver() {
075      return Optional.of(this);
076    }
077
078    @Override
079    public void preGetOp(final ObserverContext<RegionCoprocessorEnvironment> e,
080        final Get get, final List<Cell> results) throws IOException {
081      // After first sleep, all requests are timeout except the last retry. If we handle
082      // all the following requests, finally the last request is also timeout. If we drop all
083      // timeout requests, we can handle the last request immediately and it will not timeout.
084      if (ct.incrementAndGet() <= 1) {
085        Threads.sleep(SLEEP_TIME * RPC_RETRY * 2);
086      } else {
087        Threads.sleep(SLEEP_TIME);
088      }
089    }
090  }
091
092  @BeforeClass
093  public static void setUpBeforeClass() throws Exception {
094    TEST_UTIL.getConfiguration().setBoolean(HConstants.STATUS_PUBLISHED, true);
095    // Up the handlers; this test needs more than usual.
096    TEST_UTIL.getConfiguration().setInt(HConstants.REGION_SERVER_HIGH_PRIORITY_HANDLER_COUNT, 10);
097    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, RPC_RETRY);
098    // Simulate queue blocking in testDropTimeoutRequest
099    TEST_UTIL.getConfiguration().setInt(HConstants.REGION_SERVER_HANDLER_COUNT, 1);
100    TEST_UTIL.startMiniCluster(2);
101
102  }
103
104  @AfterClass
105  public static void tearDownAfterClass() throws Exception {
106    TEST_UTIL.shutdownMiniCluster();
107  }
108
109  @Test
110  public void testDropTimeoutRequest() throws Exception {
111    // Simulate the situation that the server is slow and client retries for several times because
112    // of timeout. When a request can be handled after waiting in the queue, we will drop it if
113    // it has been considered as timeout at client. If we don't drop it, the server will waste time
114    // on handling timeout requests and finally all requests timeout and client throws exception.
115    TableDescriptorBuilder builder =
116        TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()));
117    builder.setCoprocessor(SleepLongerAtFirstCoprocessor.class.getName());
118    ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(FAM_NAM).build();
119    builder.setColumnFamily(cfd);
120    TableDescriptor td = builder.build();
121    try (Admin admin = TEST_UTIL.getConnection().getAdmin()) {
122      admin.createTable(td);
123    }
124    TableBuilder tb = TEST_UTIL.getConnection().getTableBuilder(td.getTableName(), null);
125    tb.setReadRpcTimeout(SleepLongerAtFirstCoprocessor.SLEEP_TIME * 2);
126    tb.setWriteRpcTimeout(SleepLongerAtFirstCoprocessor.SLEEP_TIME * 2);
127    try (Table table = tb.build()) {
128      table.get(new Get(FAM_NAM));
129    }
130  }
131}