2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.client
;
20 import java
.io
.IOException
;
21 import java
.util
.List
;
22 import java
.util
.Optional
;
23 import java
.util
.concurrent
.atomic
.AtomicLong
;
24 import org
.apache
.hadoop
.hbase
.Cell
;
25 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
26 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
27 import org
.apache
.hadoop
.hbase
.HConstants
;
28 import org
.apache
.hadoop
.hbase
.TableName
;
29 import org
.apache
.hadoop
.hbase
.coprocessor
.ObserverContext
;
30 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionCoprocessor
;
31 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionCoprocessorEnvironment
;
32 import org
.apache
.hadoop
.hbase
.coprocessor
.RegionObserver
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
34 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
35 import org
.apache
.hadoop
.hbase
.util
.Threads
;
36 import org
.junit
.AfterClass
;
37 import org
.junit
.BeforeClass
;
38 import org
.junit
.ClassRule
;
39 import org
.junit
.Rule
;
40 import org
.junit
.Test
;
41 import org
.junit
.experimental
.categories
.Category
;
42 import org
.junit
.rules
.TestName
;
43 import org
.slf4j
.Logger
;
44 import org
.slf4j
.LoggerFactory
;
47 * Test a drop timeout request.
48 * This test used to be in TestHCM but it has particulare requirements -- i.e. one handler only --
49 * so run it apart from the rest of TestHCM.
51 @Category({MediumTests
.class})
52 public class TestDropTimeoutRequest
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestDropTimeoutRequest
.class);
59 public TestName name
= new TestName();
61 private static final Logger LOG
= LoggerFactory
.getLogger(TestDropTimeoutRequest
.class);
62 private final static HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
63 private static final byte[] FAM_NAM
= Bytes
.toBytes("f");
64 private static final int RPC_RETRY
= 5;
67 * Coprocessor that sleeps a while the first time you do a Get
69 public static class SleepLongerAtFirstCoprocessor
implements RegionCoprocessor
, RegionObserver
{
70 public static final int SLEEP_TIME
= 2000;
71 static final AtomicLong ct
= new AtomicLong(0);
74 public Optional
<RegionObserver
> getRegionObserver() {
75 return Optional
.of(this);
79 public void preGetOp(final ObserverContext
<RegionCoprocessorEnvironment
> e
,
80 final Get get
, final List
<Cell
> results
) throws IOException
{
81 // After first sleep, all requests are timeout except the last retry. If we handle
82 // all the following requests, finally the last request is also timeout. If we drop all
83 // timeout requests, we can handle the last request immediately and it will not timeout.
84 if (ct
.incrementAndGet() <= 1) {
85 Threads
.sleep(SLEEP_TIME
* RPC_RETRY
* 2);
87 Threads
.sleep(SLEEP_TIME
);
93 public static void setUpBeforeClass() throws Exception
{
94 TEST_UTIL
.getConfiguration().setBoolean(HConstants
.STATUS_PUBLISHED
, true);
95 // Up the handlers; this test needs more than usual.
96 TEST_UTIL
.getConfiguration().setInt(HConstants
.REGION_SERVER_HIGH_PRIORITY_HANDLER_COUNT
, 10);
97 TEST_UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_RETRIES_NUMBER
, RPC_RETRY
);
98 // Simulate queue blocking in testDropTimeoutRequest
99 TEST_UTIL
.getConfiguration().setInt(HConstants
.REGION_SERVER_HANDLER_COUNT
, 1);
100 TEST_UTIL
.startMiniCluster(2);
105 public static void tearDownAfterClass() throws Exception
{
106 TEST_UTIL
.shutdownMiniCluster();
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
);
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
));