View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  package org.apache.hadoop.hbase.metrics.impl;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Set;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.metrics.MetricRegistries;
29  import org.apache.hadoop.hbase.metrics.MetricRegistry;
30  import org.apache.hadoop.hbase.metrics.MetricRegistryFactory;
31  import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
32  
33  import com.google.common.base.Optional;
34  import com.google.common.base.Supplier;
35  
36  /**
37   * Implementation of MetricRegistries that does ref-counting.
38   */
39  @InterfaceAudience.Private
40  public class MetricRegistriesImpl extends MetricRegistries {
41    private final MetricRegistryFactory factory;
42    private final RefCountingMap<MetricRegistryInfo, MetricRegistry> registries;
43  
44    public MetricRegistriesImpl() {
45      this(new MetricRegistryFactoryImpl());
46    }
47  
48    public MetricRegistriesImpl(MetricRegistryFactory factory) {
49      this.factory = factory;
50      this.registries = new RefCountingMap<>();
51    }
52  
53    @Override
54    public MetricRegistry create(final MetricRegistryInfo info) {
55      return registries.put(info, new Supplier<MetricRegistry>() {
56        @Override
57        public MetricRegistry get() {
58          return factory.create(info);
59        }
60      });
61    }
62  
63    @Override
64    public boolean remove(MetricRegistryInfo key) {
65      return registries.remove(key) == null;
66    }
67  
68    @Override
69    public Optional<MetricRegistry> get(MetricRegistryInfo info) {
70      return Optional.fromNullable(registries.get(info));
71    }
72  
73    @Override
74    public Collection<MetricRegistry> getMetricRegistries() {
75      return Collections.unmodifiableCollection(registries.values());
76    }
77  
78    @Override
79    public void clear() {
80      registries.clear();
81    }
82  
83    @Override
84    public Set<MetricRegistryInfo> getMetricRegistryInfos() {
85      return Collections.unmodifiableSet(registries.keySet());
86    }
87  }