1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24 import java.util.concurrent.atomic.AtomicBoolean;
25 import java.util.concurrent.atomic.AtomicInteger;
26 import org.apache.hadoop.hbase.executor.EventType;
27 import org.apache.hadoop.hbase.executor.ExecutorService;
28 import org.apache.hadoop.hbase.executor.ExecutorType;
29 import org.apache.hadoop.hbase.executor.TestExecutorService.TestEventHandler;
30 import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSource;
31 import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceFactory;
32 import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceImpl;
33 import org.apache.hadoop.hbase.testclassification.MiscTests;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.apache.hadoop.hbase.util.Pair;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @Category({MiscTests.class, SmallTests.class})
42 public class TestExecutorStatusChore {
43 private static final Logger LOG = LoggerFactory.getLogger(TestExecutorStatusChore.class);
44
45 @Test
46 public void testMetricsCollect() throws Exception {
47 int maxThreads = 5;
48 int maxTries = 10;
49 int sleepInterval = 10;
50
51 Server mockedServer = mock(Server.class);
52 when(mockedServer.getConfiguration()).thenReturn(HBaseConfiguration.create());
53
54
55 ExecutorService executorService = new ExecutorService("unit_test");
56 executorService.startExecutorService(
57 ExecutorType.RS_PARALLEL_SEEK, maxThreads);
58
59 MetricsRegionServerSource serverSource = CompatibilitySingletonFactory
60 .getInstance(MetricsRegionServerSourceFactory.class).createServer(null);
61 assertTrue(serverSource instanceof MetricsRegionServerSourceImpl);
62
63 ExecutorStatusChore statusChore = new ExecutorStatusChore(60000,
64 mockedServer, executorService, serverSource);
65
66 AtomicBoolean lock = new AtomicBoolean(true);
67 AtomicInteger counter = new AtomicInteger(0);
68
69 for (int i = 0; i < maxThreads + 1; i++) {
70 executorService.submit(new TestEventHandler(mockedServer,
71 EventType.RS_PARALLEL_SEEK, lock, counter));
72 }
73
74
75 int tries = 0;
76 while (counter.get() < maxThreads && tries < maxTries) {
77 LOG.info("Waiting for all event handlers to start...");
78 Thread.sleep(sleepInterval);
79 tries++;
80 }
81
82
83 assertEquals(maxThreads, counter.get());
84
85 statusChore.chore();
86 Pair<Long, Long> executorStatus = statusChore.getExecutorStatus("RS_PARALLEL_SEEK");
87 assertEquals(maxThreads, executorStatus.getFirst().intValue());
88 assertEquals(1, executorStatus.getSecond().intValue());
89
90
91 synchronized (lock) {
92 lock.set(false);
93 lock.notifyAll();
94 }
95 executorService.shutdown();
96 }
97 }