1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.net.SocketTimeoutException;
26 import java.util.Random;
27 import java.util.concurrent.atomic.AtomicInteger;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.MasterNotRunningException;
36 import org.apache.hadoop.hbase.MediumTests;
37 import org.apache.hadoop.hbase.ServerName;
38 import org.apache.hadoop.hbase.ipc.RpcClient;
39 import org.apache.hadoop.hbase.security.User;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44
45 import com.google.protobuf.BlockingRpcChannel;
46 import com.google.protobuf.Descriptors.MethodDescriptor;
47 import com.google.protobuf.Message;
48 import com.google.protobuf.RpcController;
49 import com.google.protobuf.ServiceException;
50
51 @Category(MediumTests.class)
52 public class TestClientTimeouts {
53 final Log LOG = LogFactory.getLog(getClass());
54 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55 protected static int SLAVES = 1;
56
57
58
59
60 @BeforeClass
61 public static void setUpBeforeClass() throws Exception {
62 TEST_UTIL.startMiniCluster(SLAVES);
63 }
64
65
66
67
68 @AfterClass
69 public static void tearDownAfterClass() throws Exception {
70 TEST_UTIL.shutdownMiniCluster();
71 }
72
73
74
75
76
77
78 @Test
79 public void testAdminTimeout() throws Exception {
80 long lastLimit = HConstants.DEFAULT_HBASE_CLIENT_PREFETCH_LIMIT;
81 HConnection lastConnection = null;
82 boolean lastFailed = false;
83 int initialInvocations = RandomTimeoutBlockingRpcChannel.invokations.get();
84 RpcClient rpcClient = new RpcClient(TEST_UTIL.getConfiguration(), TEST_UTIL.getClusterKey()) {
85
86 @Override
87 public BlockingRpcChannel createBlockingRpcChannel(ServerName sn,
88 User ticket, int rpcTimeout) {
89 return new RandomTimeoutBlockingRpcChannel(this, sn, ticket, rpcTimeout);
90 }
91 };
92 try {
93 for (int i = 0; i < 5 || (lastFailed && i < 100); ++i) {
94 lastFailed = false;
95
96 Configuration conf = HBaseConfiguration.create(TEST_UTIL.getConfiguration());
97 conf.setLong(HConstants.HBASE_CLIENT_PREFETCH_LIMIT, ++lastLimit);
98 HBaseAdmin admin = null;
99 try {
100 admin = new HBaseAdmin(conf);
101 HConnection connection = admin.getConnection();
102 assertFalse(connection == lastConnection);
103 lastConnection = connection;
104
105 ((HConnectionManager.HConnectionImplementation)connection).setRpcClient(rpcClient);
106
107 HBaseAdmin.checkHBaseAvailable(conf);
108 admin.setBalancerRunning(false, false);
109 } catch (MasterNotRunningException ex) {
110
111
112 lastFailed = true;
113 } finally {
114 admin.close();
115 }
116 }
117
118 assertFalse(lastFailed);
119 assertTrue(RandomTimeoutBlockingRpcChannel.invokations.get() > initialInvocations);
120 } finally {
121 rpcClient.stop();
122 }
123 }
124
125
126
127
128 static class RandomTimeoutBlockingRpcChannel extends RpcClient.BlockingRpcChannelImplementation {
129 private static final Random RANDOM = new Random(System.currentTimeMillis());
130 public static final double CHANCE_OF_TIMEOUT = 0.3;
131 private static AtomicInteger invokations = new AtomicInteger();
132
133 RandomTimeoutBlockingRpcChannel(final RpcClient rpcClient, final ServerName sn,
134 final User ticket, final int rpcTimeout) {
135 super(rpcClient, sn, ticket, rpcTimeout);
136 }
137
138 @Override
139 public Message callBlockingMethod(MethodDescriptor md,
140 RpcController controller, Message param, Message returnType)
141 throws ServiceException {
142 invokations.getAndIncrement();
143 if (RANDOM.nextFloat() < CHANCE_OF_TIMEOUT) {
144
145
146
147 throw new ServiceException(new SocketTimeoutException("fake timeout"));
148 }
149 return super.callBlockingMethod(md, controller, param, returnType);
150 }
151 }
152 }