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.regionserver;
20  
21  import java.util.concurrent.ConcurrentHashMap;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
24  import org.apache.hadoop.metrics2.MetricsCollector;
25  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
26  import org.apache.hadoop.metrics2.lib.Interns;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  @InterfaceAudience.Private
31  public class MetricsUserAggregateSourceImpl extends BaseSourceImpl
32    implements MetricsUserAggregateSource {
33  
34    private static final Logger LOG = LoggerFactory.getLogger(MetricsUserAggregateSourceImpl.class);
35  
36    private final ConcurrentHashMap<String, MetricsUserSource> userSources =
37        new ConcurrentHashMap<String, MetricsUserSource>();
38  
39    public MetricsUserAggregateSourceImpl() {
40      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
41    }
42  
43    public MetricsUserAggregateSourceImpl(String metricsName,
44        String metricsDescription,
45        String metricsContext,
46        String metricsJmxContext) {
47      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
48    }
49  
50    @Override
51    public MetricsUserSource getOrCreateMetricsUser(String user) {
52      MetricsUserSource source = userSources.get(user);
53      if (source != null) {
54        return source;
55      }
56      source = new MetricsUserSourceImpl(user, this);
57      MetricsUserSource prev = userSources.putIfAbsent(user, source);
58  
59      if (prev != null) {
60        return prev;
61      } else {
62        // register the new metrics now
63        register(source);
64      }
65      return source;
66    }
67  
68    public void register(MetricsUserSource source) {
69      synchronized (this) {
70        source.register();
71      }
72    }
73  
74    @Override
75    public void deregister(MetricsUserSource toRemove) {
76      try {
77        synchronized (this) {
78          MetricsUserSource source = userSources.remove(toRemove.getUser());
79          if (source != null) {
80            source.deregister();
81          }
82        }
83      } catch (Exception e) {
84        // Ignored. If this errors out it means that someone is double
85        // closing the user source and the user metrics is already nulled out.
86        LOG.info("Error trying to remove " + toRemove + " from " + getClass().getSimpleName(), e);
87      }
88    }
89  
90    public ConcurrentHashMap<String, MetricsUserSource> getUserSources() {
91      return userSources;
92    }
93  
94    @Override
95    public void getMetrics(MetricsCollector collector, boolean all) {
96      MetricsRecordBuilder mrb = collector.addRecord(metricsName);
97  
98      if (userSources != null) {
99        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 }