View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.metrics;
20  
21  
22  
23  import com.google.common.cache.CacheBuilder;
24  import com.google.common.cache.CacheLoader;
25  import com.google.common.cache.LoadingCache;
26  
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  import java.util.concurrent.TimeUnit;
30  
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.hbase.classification.InterfaceStability;
33  import org.apache.hadoop.metrics2.MetricsInfo;
34  import org.apache.hadoop.metrics2.MetricsTag;
35  
36  
37  /**
38   * Helpers to create interned metrics info
39   */
40  @InterfaceAudience.Private
41  @InterfaceStability.Evolving
42  public final class Interns {
43  
44    private static LoadingCache<String, ConcurrentHashMap<String, MetricsInfo>> infoCache =
45        CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.DAYS)
46            .build(new CacheLoader<String, ConcurrentHashMap<String, MetricsInfo>>() {
47              public ConcurrentHashMap<String, MetricsInfo> load(String key) {
48                return new ConcurrentHashMap<String, MetricsInfo>();
49              }
50            });
51    private static LoadingCache<MetricsInfo, ConcurrentHashMap<String, MetricsTag>> tagCache =
52        CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.DAYS)
53            .build(new CacheLoader<MetricsInfo, ConcurrentHashMap<String, MetricsTag>>() {
54              public ConcurrentHashMap<String, MetricsTag> load(MetricsInfo key) {
55                return new ConcurrentHashMap<String, MetricsTag>();
56              }
57            });
58  
59    private Interns(){}
60  
61    /**
62     * Get a metric info object
63     *
64     * @return an interned metric info object
65     */
66    public static MetricsInfo info(String name, String description) {
67      Map<String, MetricsInfo> map = infoCache.getUnchecked(name);
68      MetricsInfo info = map.get(description);
69      if (info == null) {
70        info = new MetricsInfoImpl(name, description);
71        map.put(description, info);
72      }
73      return info;
74    }
75  
76    /**
77     * Get a metrics tag
78     *
79     * @param info  of the tag
80     * @param value of the tag
81     * @return an interned metrics tag
82     */
83    public static MetricsTag tag(MetricsInfo info, String value) {
84      Map<String, MetricsTag> map = tagCache.getUnchecked(info);
85      MetricsTag tag = map.get(value);
86      if (tag == null) {
87        tag = new MetricsTag(info, value);
88        map.put(value, tag);
89      }
90      return tag;
91    }
92  
93    /**
94     * Get a metrics tag
95     *
96     * @param name        of the tag
97     * @param description of the tag
98     * @param value       of the tag
99     * @return an interned metrics tag
100    */
101   public static MetricsTag tag(String name, String description, String value) {
102     return tag(info(name, description), value);
103   }
104 }