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.regionserver;
019
020import java.util.Collections;
021import java.util.Map;
022import java.util.concurrent.ConcurrentHashMap;
023import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
024import org.apache.hadoop.metrics2.MetricsCollector;
025import org.apache.hadoop.metrics2.MetricsRecordBuilder;
026import org.apache.hadoop.metrics2.lib.Interns;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031@InterfaceAudience.Private
032public class MetricsUserAggregateSourceImpl extends BaseSourceImpl
033  implements MetricsUserAggregateSource {
034
035  private static final Logger LOG = LoggerFactory.getLogger(MetricsUserAggregateSourceImpl.class);
036
037  private final ConcurrentHashMap<String, MetricsUserSource> userSources =
038    new ConcurrentHashMap<String, MetricsUserSource>();
039
040  public MetricsUserAggregateSourceImpl() {
041    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
042  }
043
044  public MetricsUserAggregateSourceImpl(String metricsName, String metricsDescription,
045    String metricsContext, String metricsJmxContext) {
046    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
047  }
048
049  @Override
050  public MetricsUserSource getOrCreateMetricsUser(String user) {
051    MetricsUserSource source = userSources.get(user);
052    if (source != null) {
053      return source;
054    }
055    source = new MetricsUserSourceImpl(user, this);
056    MetricsUserSource prev = userSources.putIfAbsent(user, source);
057
058    if (prev != null) {
059      return prev;
060    } else {
061      // register the new metrics now
062      register(source);
063    }
064    return source;
065  }
066
067  public void register(MetricsUserSource source) {
068    synchronized (this) {
069      source.register();
070    }
071  }
072
073  @Override
074  public void deregister(MetricsUserSource toRemove) {
075    try {
076      synchronized (this) {
077        MetricsUserSource source = userSources.remove(toRemove.getUser());
078        if (source != null) {
079          source.deregister();
080        }
081      }
082    } catch (Exception e) {
083      // Ignored. If this errors out it means that someone is double
084      // closing the user source and the user metrics is already nulled out.
085      LOG.info("Error trying to remove " + toRemove + " from " + getClass().getSimpleName(), e);
086    }
087  }
088
089  @Override
090  public Map<String, MetricsUserSource> getUserSources() {
091    return Collections.unmodifiableMap(userSources);
092  }
093
094  @Override
095  public void getMetrics(MetricsCollector collector, boolean all) {
096    MetricsRecordBuilder mrb = collector.addRecord(metricsName);
097
098    if (userSources != null) {
099      for (MetricsUserSource userMetricSource : userSources.values()) {
100        if (userMetricSource instanceof MetricsUserSourceImpl) {
101          ((MetricsUserSourceImpl) userMetricSource).snapshot(mrb, all);
102        }
103      }
104      mrb.addGauge(Interns.info(NUM_USERS, NUMBER_OF_USERS_DESC), userSources.size());
105      metricsRegistry.snapshot(mrb, all);
106    }
107  }
108}