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 private void register(MetricsTableSource source) { 047 source.registerMetrics(); 048 } 049 050 @Override 051 public void deleteTableSource(String table) { 052 try { 053 MetricsTableSource source = tableSources.remove(table); 054 if (source != null) { 055 source.close(); 056 } 057 } catch (Exception e) { 058 // Ignored. If this errors out it means that someone is double 059 // closing the user source and the user metrics is already nulled out. 060 LOG.info("Error trying to remove " + table + " from " + getClass().getSimpleName(), e); 061 } 062 } 063 064 @Override 065 public MetricsTableSource getOrCreateTableSource(String table, 066 MetricsTableWrapperAggregate wrapper) { 067 MetricsTableSource source = tableSources.get(table); 068 if (source != null) { 069 return source; 070 } 071 MetricsTableSource newSource = CompatibilitySingletonFactory 072 .getInstance(MetricsRegionServerSourceFactory.class).createTable(table, wrapper); 073 return tableSources.computeIfAbsent(table, k -> { 074 // register the new metrics now 075 newSource.registerMetrics(); 076 return newSource; 077 }); 078 } 079 080 /** 081 * Yes this is a get function that doesn't return anything. Thanks Hadoop for breaking all 082 * expectations of java programmers. Instead of returning anything Hadoop metrics expects 083 * getMetrics to push the metrics into the collector. 084 * @param collector the collector 085 * @param all get all the metrics regardless of when they last changed. 086 */ 087 @Override 088 public void getMetrics(MetricsCollector collector, boolean all) { 089 MetricsRecordBuilder mrb = collector.addRecord(metricsName); 090 if (tableSources != null) { 091 for (MetricsTableSource tableMetricSource : tableSources.values()) { 092 if (tableMetricSource instanceof MetricsTableSourceImpl) { 093 ((MetricsTableSourceImpl) tableMetricSource).snapshot(mrb, all); 094 } 095 } 096 mrb.addGauge(Interns.info(NUM_TABLES, NUMBER_OF_TABLES_DESC), tableSources.size()); 097 metricsRegistry.snapshot(mrb, all); 098 } 099 } 100}