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