1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Random;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.Future;
32 import java.util.concurrent.atomic.AtomicInteger;
33 import java.util.concurrent.atomic.AtomicLong;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.hadoop.conf.Configuration;
38 import org.apache.hadoop.hbase.ClusterStatus;
39 import org.apache.hadoop.hbase.HBaseTestingUtility;
40 import org.apache.hadoop.hbase.HColumnDescriptor;
41 import org.apache.hadoop.hbase.HConstants;
42 import org.apache.hadoop.hbase.HTableDescriptor;
43 import org.apache.hadoop.hbase.TableName;
44 import org.apache.hadoop.hbase.exceptions.PreemptiveFastFailException;
45 import org.apache.hadoop.hbase.regionserver.HRegion;
46 import org.apache.hadoop.hbase.regionserver.HRegionServer;
47 import org.apache.hadoop.hbase.testclassification.MediumTests;
48 import org.apache.hadoop.hbase.util.Bytes;
49 import org.apache.hadoop.hbase.util.test.LoadTestKVGenerator;
50 import org.junit.After;
51 import org.junit.AfterClass;
52 import org.junit.Before;
53 import org.junit.BeforeClass;
54 import org.junit.Test;
55 import org.junit.experimental.categories.Category;
56
57 @Category({MediumTests.class})
58 public class TestFastFail {
59 final Log LOG = LogFactory.getLog(getClass());
60 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
61 private static byte[] FAMILY = Bytes.toBytes("testFamily");
62 private static final Random random = new Random();
63 private static int SLAVES = 3;
64 private static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
65 private static final int SLEEPTIME = 1000;
66
67
68
69
70 @BeforeClass
71 public static void setUpBeforeClass() throws Exception {
72 TEST_UTIL.startMiniCluster(SLAVES);
73 }
74
75
76
77
78 @AfterClass
79 public static void tearDownAfterClass() throws Exception {
80 TEST_UTIL.shutdownMiniCluster();
81 }
82
83
84
85
86 @Before
87 public void setUp() throws Exception {
88 MyPreemptiveFastFailInterceptor.numBraveSouls.set(0);
89 }
90
91
92
93
94 @After
95 public void tearDown() throws Exception {
96
97 }
98
99 @Test
100 public void testFastFail() throws IOException, InterruptedException {
101 Admin admin = TEST_UTIL.getHBaseAdmin();
102
103 final String tableName = "testClientRelearningExperiment";
104 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(Bytes
105 .toBytes(tableName)));
106 desc.addFamily(new HColumnDescriptor(FAMILY));
107 admin.createTable(desc, Bytes.toBytes("aaaa"), Bytes.toBytes("zzzz"), 32);
108 final long numRows = 1000;
109
110 Configuration conf = TEST_UTIL.getConfiguration();
111 conf.setLong(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, SLEEPTIME * 100);
112 conf.setInt(HConstants.HBASE_CLIENT_PAUSE, SLEEPTIME / 10);
113 conf.setBoolean(HConstants.HBASE_CLIENT_FAST_FAIL_MODE_ENABLED, true);
114 conf.setLong(HConstants.HBASE_CLIENT_FAST_FAIL_THREASHOLD_MS, 0);
115 conf.setClass(HConstants.HBASE_CLIENT_FAST_FAIL_INTERCEPTOR_IMPL,
116 MyPreemptiveFastFailInterceptor.class,
117 PreemptiveFastFailInterceptor.class);
118
119 final Connection connection = ConnectionFactory.createConnection(conf);
120
121
122
123
124 List<Put> puts = new ArrayList<>();
125 for (long i = 0; i < numRows; i++) {
126 byte[] rowKey = longToByteArrayKey(i);
127 Put put = new Put(rowKey);
128 byte[] value = rowKey;
129 put.add(FAMILY, QUALIFIER, value);
130 puts.add(put);
131 }
132 try (Table table = connection.getTable(TableName.valueOf(tableName))) {
133 table.put(puts);
134 LOG.info("Written all puts.");
135 }
136
137
138
139
140
141 int nThreads = 200;
142 ExecutorService service = Executors.newFixedThreadPool(nThreads);
143 final CountDownLatch continueOtherHalf = new CountDownLatch(1);
144 final CountDownLatch doneHalfway = new CountDownLatch(nThreads);
145
146 final AtomicInteger numSuccessfullThreads = new AtomicInteger(0);
147 final AtomicInteger numFailedThreads = new AtomicInteger(0);
148
149
150 final AtomicLong totalTimeTaken = new AtomicLong(0);
151 final AtomicInteger numBlockedWorkers = new AtomicInteger(0);
152 final AtomicInteger numPreemptiveFastFailExceptions = new AtomicInteger(0);
153
154 List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
155 for (int i = 0; i < nThreads; i++) {
156 futures.add(service.submit(new Callable<Boolean>() {
157
158
159
160
161
162 public Boolean call() throws Exception {
163 try (Table table = connection.getTable(TableName.valueOf(tableName))) {
164 Thread.sleep(Math.abs(random.nextInt()) % 100);
165
166 byte[] row = longToByteArrayKey(Math.abs(random.nextLong())
167 % numRows);
168 Get g = new Get(row);
169 g.addColumn(FAMILY, QUALIFIER);
170 try {
171 table.get(g);
172 } catch (Exception e) {
173 LOG.debug("Get failed : ", e);
174 doneHalfway.countDown();
175 return false;
176 }
177
178
179 doneHalfway.countDown();
180 continueOtherHalf.await();
181
182 long startTime = System.currentTimeMillis();
183 g = new Get(row);
184 g.addColumn(FAMILY, QUALIFIER);
185 try {
186 table.get(g);
187
188 numSuccessfullThreads.addAndGet(1);
189 } catch (Exception e) {
190 if (e instanceof PreemptiveFastFailException) {
191
192 numPreemptiveFastFailExceptions.addAndGet(1);
193 }
194
195 numFailedThreads.addAndGet(1);
196 return false;
197 } finally {
198 long enTime = System.currentTimeMillis();
199 totalTimeTaken.addAndGet(enTime - startTime);
200 if ((enTime - startTime) >= SLEEPTIME) {
201
202
203
204
205
206 numBlockedWorkers.addAndGet(1);
207 }
208 }
209 return true;
210 } catch (Exception e) {
211 LOG.error("Caught unknown exception", e);
212 doneHalfway.countDown();
213 return false;
214 }
215 }
216 }));
217 }
218
219 doneHalfway.await();
220
221 ClusterStatus status = TEST_UTIL.getHBaseCluster().getClusterStatus();
222
223
224 for (int i = 0; i < SLAVES; i++) {
225 HRegionServer server = TEST_UTIL.getHBaseCluster().getRegionServer(i);
226 List<HRegion> regions = server.getOnlineRegions(TableName.META_TABLE_NAME);
227 if (regions.size() > 0) continue;
228
229 server.getRpcServer().stop();
230 server.stop("Testing");
231 }
232
233
234 continueOtherHalf.countDown();
235
236 Thread.sleep(2 * SLEEPTIME);
237
238 TEST_UTIL.getHBaseCluster().restoreClusterStatus(status);
239
240 int numThreadsReturnedFalse = 0;
241 int numThreadsReturnedTrue = 0;
242 int numThreadsThrewExceptions = 0;
243 for (Future<Boolean> f : futures) {
244 try {
245 numThreadsReturnedTrue += f.get() ? 1 : 0;
246 numThreadsReturnedFalse += f.get() ? 0 : 1;
247 } catch (Exception e) {
248 numThreadsThrewExceptions++;
249 }
250 }
251 LOG.debug("numThreadsReturnedFalse:"
252 + numThreadsReturnedFalse
253 + " numThreadsReturnedTrue:"
254 + numThreadsReturnedTrue
255 + " numThreadsThrewExceptions:"
256 + numThreadsThrewExceptions
257 + " numFailedThreads:"
258 + numFailedThreads.get()
259 + " numSuccessfullThreads:"
260 + numSuccessfullThreads.get()
261 + " numBlockedWorkers:"
262 + numBlockedWorkers.get()
263 + " totalTimeWaited: "
264 + totalTimeTaken.get()
265 / (numBlockedWorkers.get() == 0 ? Long.MAX_VALUE : numBlockedWorkers
266 .get()) + " numPFFEs: " + numPreemptiveFastFailExceptions.get());
267
268 assertEquals("The expected number of all the successfull and the failed "
269 + "threads should equal the total number of threads that we spawned",
270 nThreads, numFailedThreads.get() + numSuccessfullThreads.get());
271 assertEquals(
272 "All the failures should be coming from the secondput failure",
273 numFailedThreads.get(), numThreadsReturnedFalse);
274 assertEquals("Number of threads that threw execution exceptions "
275 + "otherwise should be 0", numThreadsThrewExceptions, 0);
276 assertEquals("The regionservers that returned true should equal to the"
277 + " number of successful threads", numThreadsReturnedTrue,
278 numSuccessfullThreads.get());
279 assertTrue(
280 "There should be atleast one thread that retried instead of failing",
281 MyPreemptiveFastFailInterceptor.numBraveSouls.get() > 0);
282 assertTrue(
283 "There should be atleast one PreemptiveFastFail exception,"
284 + " otherwise, the test makes little sense."
285 + "numPreemptiveFastFailExceptions: "
286 + numPreemptiveFastFailExceptions.get(),
287 numPreemptiveFastFailExceptions.get() > 0);
288 assertTrue(
289 "Only few thread should ideally be waiting for the dead "
290 + "regionserver to be coming back. numBlockedWorkers:"
291 + numBlockedWorkers.get() + " threads that retried : "
292 + MyPreemptiveFastFailInterceptor.numBraveSouls.get(),
293 numBlockedWorkers.get() <= MyPreemptiveFastFailInterceptor.numBraveSouls
294 .get());
295 }
296
297 public static class MyPreemptiveFastFailInterceptor extends
298 PreemptiveFastFailInterceptor {
299 public static AtomicInteger numBraveSouls = new AtomicInteger();
300
301 @Override
302 protected boolean shouldRetryInspiteOfFastFail(FailureInfo fInfo) {
303 boolean ret = super.shouldRetryInspiteOfFastFail(fInfo);
304 if (ret)
305 numBraveSouls.addAndGet(1);
306 return ret;
307 }
308
309 public MyPreemptiveFastFailInterceptor(Configuration conf) {
310 super(conf);
311 }
312 }
313
314 private byte[] longToByteArrayKey(long rowKey) {
315 return LoadTestKVGenerator.md5PrefixedKey(rowKey).getBytes();
316 }
317 }