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