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.io.IOException;
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.ipc.RpcServer;
26  import org.apache.hadoop.hbase.security.User;
27  import org.apache.hadoop.hbase.security.UserProvider;
28  import org.apache.hadoop.hbase.util.LossyCounting;
29  
30  @InterfaceAudience.Private
31  public class MetricsUserAggregateImpl implements MetricsUserAggregate{
32  
33    /** Provider for mapping principal names to Users */
34    private final UserProvider userProvider;
35  
36    private final MetricsUserAggregateSource source;
37    private final LossyCounting<MetricsUserSource> userMetricLossyCounting;
38  
39    public MetricsUserAggregateImpl(Configuration conf) {
40      source = CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class)
41          .getUserAggregate();
42      userMetricLossyCounting = new LossyCounting<>("userMetrics", conf,
43        new LossyCounting.LossyCountingListener<MetricsUserSource>() {
44          @Override
45          public void sweep(MetricsUserSource key) {
46            source.deregister(key);
47          }
48        });
49      this.userProvider = UserProvider.instantiate(conf);
50    }
51  
52    /**
53     * Returns the active user to which authorization checks should be applied.
54     * If we are in the context of an RPC call, the remote user is used,
55     * otherwise the currently logged in user is used.
56     */
57    private String getActiveUser() {
58      User user = RpcServer.getRequestUser();
59      if (user == null) {
60        try {
61          user = userProvider.getCurrent();
62        } catch (IOException e) { }
63      }
64      return user != null ? user.getShortName() : null;
65    }
66  
67    MetricsUserAggregateSource getSource() {
68      return source;
69    }
70  
71    @Override
72    public void updatePut(long t) {
73      String user = getActiveUser();
74      if (user != null) {
75        getOrCreateMetricsUser(user).updatePut(t);
76      }
77    }
78  
79    @Override
80    public void updateDelete(long t) {
81      String user = getActiveUser();
82      if (user != null) {
83        getOrCreateMetricsUser(user).updateDelete(t);
84      }
85    }
86  
87    @Override
88    public void updateGet(long t) {
89      String user = getActiveUser();
90      if (user != null) {
91        getOrCreateMetricsUser(user).updateGet(t);
92      }
93    }
94  
95    @Override
96    public void updateIncrement(long t) {
97      String user = getActiveUser();
98      if (user != null) {
99        getOrCreateMetricsUser(user).updateIncrement(t);
100     }
101   }
102 
103   @Override
104   public void updateAppend(long t) {
105     String user = getActiveUser();
106     if (user != null) {
107       getOrCreateMetricsUser(user).updateAppend(t);
108     }
109   }
110 
111   @Override
112   public void updateReplay(long t) {
113     String user = getActiveUser();
114     if (user != null) {
115       getOrCreateMetricsUser(user).updateReplay(t);
116     }
117   }
118 
119   @Override
120   public void updateScanTime(long t) {
121     String user = getActiveUser();
122     if (user != null) {
123       getOrCreateMetricsUser(user).updateScanTime(t);
124     }
125   }
126 
127   private MetricsUserSource getOrCreateMetricsUser(String user) {
128     MetricsUserSource userSource = source.getOrCreateMetricsUser(user);
129     userMetricLossyCounting.add(userSource);
130     return userSource;
131   }
132 }