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 */ 018 019package org.apache.hadoop.hbase.metrics; 020 021import java.util.Map; 022import java.util.concurrent.ConcurrentHashMap; 023import java.util.concurrent.TimeUnit; 024 025import org.apache.hadoop.metrics2.MetricsInfo; 026import org.apache.hadoop.metrics2.MetricsTag; 027import org.apache.yetus.audience.InterfaceAudience; 028import org.apache.yetus.audience.InterfaceStability; 029 030import org.apache.hbase.thirdparty.com.google.common.cache.CacheBuilder; 031import org.apache.hbase.thirdparty.com.google.common.cache.CacheLoader; 032import org.apache.hbase.thirdparty.com.google.common.cache.LoadingCache; 033 034/** 035 * Helpers to create interned metrics info 036 */ 037@InterfaceAudience.Private 038@InterfaceStability.Evolving 039public final class Interns { 040 041 private static LoadingCache<String, ConcurrentHashMap<String, MetricsInfo>> infoCache = 042 CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.DAYS) 043 .build(new CacheLoader<String, ConcurrentHashMap<String, MetricsInfo>>() { 044 public ConcurrentHashMap<String, MetricsInfo> load(String key) { 045 return new ConcurrentHashMap<>(); 046 } 047 }); 048 private static LoadingCache<MetricsInfo, ConcurrentHashMap<String, MetricsTag>> tagCache = 049 CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.DAYS) 050 .build(new CacheLoader<MetricsInfo, ConcurrentHashMap<String, MetricsTag>>() { 051 public ConcurrentHashMap<String, MetricsTag> load(MetricsInfo key) { 052 return new ConcurrentHashMap<>(); 053 } 054 }); 055 056 private Interns(){} 057 058 /** 059 * Get a metric info object 060 * 061 * @return an interned metric info object 062 */ 063 public static MetricsInfo info(String name, String description) { 064 Map<String, MetricsInfo> map = infoCache.getUnchecked(name); 065 MetricsInfo info = map.get(description); 066 if (info == null) { 067 info = new MetricsInfoImpl(name, description); 068 map.put(description, info); 069 } 070 return info; 071 } 072 073 /** 074 * Get a metrics tag 075 * 076 * @param info of the tag 077 * @param value of the tag 078 * @return an interned metrics tag 079 */ 080 public static MetricsTag tag(MetricsInfo info, String value) { 081 Map<String, MetricsTag> map = tagCache.getUnchecked(info); 082 MetricsTag tag = map.get(value); 083 if (tag == null) { 084 tag = new MetricsTag(info, value); 085 map.put(value, tag); 086 } 087 return tag; 088 } 089 090 /** 091 * Get a metrics tag 092 * 093 * @param name of the tag 094 * @param description of the tag 095 * @param value of the tag 096 * @return an interned metrics tag 097 */ 098 public static MetricsTag tag(String name, String description, String value) { 099 return tag(info(name, description), value); 100 } 101}