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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024
025import java.util.concurrent.atomic.AtomicBoolean;
026import java.util.concurrent.atomic.AtomicInteger;
027import org.apache.hadoop.hbase.executor.EventType;
028import org.apache.hadoop.hbase.executor.ExecutorService;
029import org.apache.hadoop.hbase.executor.ExecutorType;
030import org.apache.hadoop.hbase.executor.TestExecutorService.TestEventHandler;
031import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSource;
032import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceFactory;
033import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceImpl;
034import org.apache.hadoop.hbase.testclassification.MiscTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.Pair;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042@Tag(MiscTests.TAG)
043@Tag(SmallTests.TAG)
044public class TestExecutorStatusChore {
045
046  private static final Logger LOG = LoggerFactory.getLogger(TestExecutorStatusChore.class);
047
048  @Test
049  public void testMetricsCollect() throws Exception {
050    int maxThreads = 5;
051    int maxTries = 10;
052    int sleepInterval = 1000;
053
054    Server mockedServer = mock(Server.class);
055    when(mockedServer.getConfiguration()).thenReturn(HBaseConfiguration.create());
056
057    // Start an executor service pool with max 5 threads
058    ExecutorService executorService = new ExecutorService("unit_test");
059    executorService.startExecutorService(executorService.new ExecutorConfig()
060      .setExecutorType(ExecutorType.RS_PARALLEL_SEEK).setCorePoolSize(maxThreads));
061
062    MetricsRegionServerSource serverSource = CompatibilitySingletonFactory
063      .getInstance(MetricsRegionServerSourceFactory.class).createServer(null);
064    assertTrue(serverSource instanceof MetricsRegionServerSourceImpl);
065
066    ExecutorStatusChore statusChore =
067      new ExecutorStatusChore(60000, mockedServer, executorService, serverSource);
068
069    AtomicBoolean lock = new AtomicBoolean(true);
070    AtomicInteger counter = new AtomicInteger(0);
071    for (int i = 0; i < maxThreads + 1; i++) {
072      executorService
073        .submit(new TestEventHandler(mockedServer, EventType.RS_PARALLEL_SEEK, lock, counter));
074    }
075
076    // The TestEventHandler will increment counter when it starts.
077    int tries = 0;
078    while (counter.get() < maxThreads && tries < maxTries) {
079      LOG.info("Waiting for all event handlers to start...");
080      Thread.sleep(sleepInterval);
081      tries++;
082    }
083
084    // Assert that pool is at max threads.
085    assertEquals(maxThreads, counter.get());
086
087    statusChore.chore();
088    Pair<Long, Long> executorStatus = statusChore.getExecutorStatus("RS_PARALLEL_SEEK");
089    assertEquals(maxThreads, executorStatus.getFirst().intValue()); // running
090    assertEquals(1, executorStatus.getSecond().intValue()); // pending
091
092    // Now interrupt the running Executor
093    synchronized (lock) {
094      lock.set(false);
095      lock.notifyAll();
096    }
097    executorService.shutdown();
098  }
099}