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        public ConcurrentHashMap<String, MetricsInfo> load(String key) {
043          return new ConcurrentHashMap<>();
044        }
045      });
046  private static LoadingCache<MetricsInfo, ConcurrentHashMap<String, MetricsTag>> tagCache =
047    CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.DAYS)
048      .build(new CacheLoader<MetricsInfo, ConcurrentHashMap<String, MetricsTag>>() {
049        public ConcurrentHashMap<String, MetricsTag> load(MetricsInfo key) {
050          return new ConcurrentHashMap<>();
051        }
052      });
053
054  private Interns() {
055  }
056
057  /**
058   * Get a metric info object
059   * @return an interned metric info object
060   */
061  public static MetricsInfo info(String name, String description) {
062    Map<String, MetricsInfo> map = infoCache.getUnchecked(name);
063    MetricsInfo info = map.get(description);
064    if (info == null) {
065      info = new MetricsInfoImpl(name, description);
066      map.put(description, info);
067    }
068    return info;
069  }
070
071  /**
072   * Get a metrics tag
073   * @param info  of the tag
074   * @param value of the tag
075   * @return an interned metrics tag
076   */
077  public static MetricsTag tag(MetricsInfo info, String value) {
078    Map<String, MetricsTag> map = tagCache.getUnchecked(info);
079    MetricsTag tag = map.get(value);
080    if (tag == null) {
081      tag = new MetricsTag(info, value);
082      map.put(value, tag);
083    }
084    return tag;
085  }
086
087  /**
088   * Get a metrics tag
089   * @param name        of the tag
090   * @param description of the tag
091   * @param value       of the tag
092   * @return an interned metrics tag
093   */
094  public static MetricsTag tag(String name, String description, String value) {
095    return tag(info(name, description), value);
096  }
097}