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 java.util.Map;
021import org.apache.hadoop.hbase.executor.ExecutorService;
022import org.apache.hadoop.hbase.executor.ExecutorService.ExecutorStatus;
023import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSource;
024import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceImpl;
025import org.apache.hadoop.hbase.util.Pair;
026import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
027import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
028import org.apache.hadoop.util.StringUtils;
029import org.apache.yetus.audience.InterfaceAudience;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * The Class ExecutorStatusChore for collect Executor status info periodically and report to metrics
035 * system
036 */
037@InterfaceAudience.Private
038public class ExecutorStatusChore extends ScheduledChore {
039  private static final Logger LOG = LoggerFactory.getLogger(HealthCheckChore.class);
040  public static final String WAKE_FREQ = "hbase.executors.status.collect.period";
041  public static final int DEFAULT_WAKE_FREQ = 60000;
042  private ExecutorService service;
043  private DynamicMetricsRegistry metricsRegistry;
044
045  public ExecutorStatusChore(int sleepTime, Stoppable stopper, ExecutorService service,
046    MetricsRegionServerSource metrics) {
047    super("ExecutorStatusChore", stopper, sleepTime);
048    LOG.info("ExecutorStatusChore runs every {} ", StringUtils.formatTime(sleepTime));
049    this.service = service;
050    this.metricsRegistry = ((MetricsRegionServerSourceImpl) metrics).getMetricsRegistry();
051  }
052
053  @Override
054  protected void chore() {
055    try {
056      // thread pool monitor
057      Map<String, ExecutorStatus> statuses = service.getAllExecutorStatuses();
058      for (Map.Entry<String, ExecutorStatus> statusEntry : statuses.entrySet()) {
059        String name = statusEntry.getKey();
060        // Executor's name is generate by ExecutorType#getExecutorName
061        // include ExecutorType & Servername(split by '-'), here we only need the ExecutorType
062        String poolName = name.split("-")[0];
063        ExecutorStatus status = statusEntry.getValue();
064        MutableGaugeLong queued = metricsRegistry.getGauge(poolName + "_queued", 0L);
065        MutableGaugeLong running = metricsRegistry.getGauge(poolName + "_running", 0L);
066        int queueSize = status.getQueuedEvents().size();
067        int runningSize = status.getRunning().size();
068        if (queueSize > 0) {
069          LOG.warn("{}'s size info, queued: {}, running: {}", poolName, queueSize, runningSize);
070        }
071        queued.set(queueSize);
072        running.set(runningSize);
073      }
074    } catch (Throwable e) {
075      LOG.error(e.getMessage(), e);
076    }
077  }
078
079  public Pair<Long, Long> getExecutorStatus(String poolName) {
080    MutableGaugeLong running = metricsRegistry.getGauge(poolName + "_running", 0L);
081    MutableGaugeLong queued = metricsRegistry.getGauge(poolName + "_queued", 0L);
082    return new Pair<Long, Long>(running.value(), queued.value());
083  }
084}