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