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 */ 018 019package org.apache.hadoop.hbase.regionserver; 020 021import java.util.concurrent.ConcurrentHashMap; 022 023import org.apache.hadoop.hbase.metrics.BaseSourceImpl; 024import org.apache.hadoop.hbase.metrics.Interns; 025import org.apache.hadoop.metrics2.MetricsCollector; 026import org.apache.hadoop.metrics2.MetricsRecordBuilder; 027import org.apache.yetus.audience.InterfaceAudience; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031@InterfaceAudience.Private 032public class MetricsTableAggregateSourceImpl extends BaseSourceImpl 033 implements MetricsTableAggregateSource { 034 035 private static final Logger LOG = LoggerFactory.getLogger(MetricsTableAggregateSourceImpl.class); 036 private ConcurrentHashMap<String, MetricsTableSource> tableSources = new ConcurrentHashMap<>(); 037 038 public MetricsTableAggregateSourceImpl() { 039 this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT); 040 } 041 042 public MetricsTableAggregateSourceImpl(String metricsName, 043 String metricsDescription, 044 String metricsContext, 045 String metricsJmxContext) { 046 super(metricsName, metricsDescription, metricsContext, metricsJmxContext); 047 } 048 049 @Override 050 public void register(String table, MetricsTableSource source) { 051 tableSources.put(table, source); 052 } 053 054 @Override 055 public void deregister(String table) { 056 try { 057 tableSources.remove(table); 058 } catch (Exception e) { 059 // Ignored. If this errors out it means that someone is double 060 // closing the region source and the region is already nulled out. 061 LOG.info( 062 "Error trying to remove " + table + " from " + this.getClass().getSimpleName(), 063 e); 064 } 065 } 066 067 /** 068 * Yes this is a get function that doesn't return anything. Thanks Hadoop for breaking all 069 * expectations of java programmers. Instead of returning anything Hadoop metrics expects 070 * getMetrics to push the metrics into the collector. 071 * 072 * @param collector the collector 073 * @param all get all the metrics regardless of when they last changed. 074 */ 075 @Override 076 public void getMetrics(MetricsCollector collector, boolean all) { 077 MetricsRecordBuilder mrb = collector.addRecord(metricsName); 078 079 if (tableSources != null) { 080 for (MetricsTableSource tableMetricSource : tableSources.values()) { 081 if (tableMetricSource instanceof MetricsTableSourceImpl) { 082 ((MetricsTableSourceImpl) tableMetricSource).snapshot(mrb, all); 083 } 084 } 085 mrb.addGauge(Interns.info(NUM_TABLES, NUMBER_OF_TABLES_DESC), tableSources.size()); 086 metricsRegistry.snapshot(mrb, all); 087 } 088 } 089}