001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.metrics.impl; 020 021import java.util.Collection; 022import java.util.Collections; 023import java.util.Optional; 024import java.util.Set; 025 026import org.apache.hadoop.hbase.metrics.MetricRegistries; 027import org.apache.hadoop.hbase.metrics.MetricRegistry; 028import org.apache.hadoop.hbase.metrics.MetricRegistryFactory; 029import org.apache.hadoop.hbase.metrics.MetricRegistryInfo; 030import org.apache.yetus.audience.InterfaceAudience; 031 032/** 033 * Implementation of MetricRegistries that does ref-counting. 034 */ 035@InterfaceAudience.Private 036public class MetricRegistriesImpl extends MetricRegistries { 037 private final MetricRegistryFactory factory; 038 private final RefCountingMap<MetricRegistryInfo, MetricRegistry> registries; 039 040 public MetricRegistriesImpl() { 041 this(new MetricRegistryFactoryImpl()); 042 } 043 044 public MetricRegistriesImpl(MetricRegistryFactory factory) { 045 this.factory = factory; 046 this.registries = new RefCountingMap<>(); 047 } 048 049 @Override 050 public MetricRegistry create(MetricRegistryInfo info) { 051 return registries.put(info, () -> factory.create(info)); 052 } 053 054 @Override 055 public boolean remove(MetricRegistryInfo key) { 056 return registries.remove(key) == null; 057 } 058 059 @Override 060 public Optional<MetricRegistry> get(MetricRegistryInfo info) { 061 return Optional.ofNullable(registries.get(info)); 062 } 063 064 @Override 065 public Collection<MetricRegistry> getMetricRegistries() { 066 return Collections.unmodifiableCollection(registries.values()); 067 } 068 069 @Override 070 public void clear() { 071 registries.clear(); 072 } 073 074 @Override 075 public Set<MetricRegistryInfo> getMetricRegistryInfos() { 076 return Collections.unmodifiableSet(registries.keySet()); 077 } 078}