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 java.util.Map;
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.executor.ExecutorService;
23  import org.apache.hadoop.hbase.executor.ExecutorService.ExecutorStatus;
24  import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSource;
25  import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceImpl;
26  import org.apache.hadoop.hbase.util.Pair;
27  import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
28  import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
29  import org.apache.hadoop.util.StringUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * The Class ExecutorStatusChore for collect Executor status info periodically
35   * and report to metrics system
36   */
37  @InterfaceAudience.Private
38  public class ExecutorStatusChore extends ScheduledChore {
39    private static final Logger LOG = LoggerFactory.getLogger(HealthCheckChore.class);
40    public static final String WAKE_FREQ = "hbase.executors.status.collect.period";
41    public static final int DEFAULT_WAKE_FREQ = 60000;
42    private ExecutorService service;
43    private DynamicMetricsRegistry metricsRegistry;
44  
45    public ExecutorStatusChore(int sleepTime, Stoppable stopper, ExecutorService service,
46        MetricsRegionServerSource metrics) {
47      super("ExecutorStatusChore", stopper, sleepTime);
48      LOG.info("ExecutorStatusChore runs every {} ", StringUtils.formatTime(sleepTime));
49      this.service = service;
50      this.metricsRegistry = ((MetricsRegionServerSourceImpl) metrics).getMetricsRegistry();
51    }
52  
53    @Override
54    protected void chore() {
55      try{
56        // thread pool monitor
57        Map<String, ExecutorStatus> statuses = service.getAllExecutorStatuses();
58        for (Map.Entry<String, ExecutorStatus> statusEntry : statuses.entrySet()) {
59          String name = statusEntry.getKey();
60          // Executor's name is generate by ExecutorType#getExecutorName
61          // include ExecutorType & Servername(split by '-'), here we only need the ExecutorType
62          String poolName = name.split("-")[0];
63          ExecutorStatus status = statusEntry.getValue();
64          MutableGaugeLong queued = metricsRegistry.getGauge(poolName + "_queued", 0L);
65          MutableGaugeLong running = metricsRegistry.getGauge(poolName + "_running", 0L);
66          int queueSize = status.getQueuedEvents().size();
67          int runningSize = status.getRunning().size();
68          if (queueSize > 0) {
69            LOG.warn("{}'s size info, queued: {}, running: {}", poolName, queueSize, runningSize);
70          }
71          queued.set(queueSize);
72          running.set(runningSize);
73        }
74      } catch(Throwable e) {
75        LOG.error(e.getMessage(), e);
76      }
77    }
78  
79    public Pair<Long, Long> getExecutorStatus(String poolName) {
80      MutableGaugeLong running = metricsRegistry.getGauge(poolName + "_running", 0L);
81      MutableGaugeLong queued = metricsRegistry.getGauge(poolName + "_queued", 0L);
82      return new Pair<Long, Long>(running.value(), queued.value());
83    }
84  }