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