View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
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      // Start an executor service pool with max 5 threads
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      // The TestEventHandler will increment counter when it starts.
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      // Assert that pool is at max threads.
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()); // running
88      assertEquals(1, executorStatus.getSecond().intValue()); // pending
89  
90      // Now interrupt the running Executor
91      synchronized (lock) {
92        lock.set(false);
93        lock.notifyAll();
94      }
95      executorService.shutdown();
96    }
97  }