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  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
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 com.google.common.annotations.VisibleForTesting;
29  
30  public class MetricsUserAggregate {
31  
32    /** Provider for mapping principal names to Users */
33    private final UserProvider userProvider;
34  
35    private final MetricsUserAggregateSource source;
36  
37    public MetricsUserAggregate(Configuration conf) {
38      source = CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class)
39          .getUserAggregate();
40  
41      this.userProvider = UserProvider.instantiate(conf);
42    }
43  
44    /**
45     * Returns the active user to which authorization checks should be applied.
46     * If we are in the context of an RPC call, the remote user is used,
47     * otherwise the currently logged in user is used.
48     */
49    private String getActiveUser() {
50      User user = RpcServer.getRequestUser();
51      if (user == null) {
52        // for non-rpc handling, fallback to system user
53        try {
54          user = userProvider.getCurrent();
55        } catch (IOException ignore) {
56        }
57      }
58      return user != null ? user.getShortName() : null;
59    }
60  
61    @VisibleForTesting
62    MetricsUserAggregateSource getSource() {
63      return source;
64    }
65  
66    public void updatePut(long t) {
67      String user = getActiveUser();
68      if (user != null) {
69        source.getOrCreateMetricsUser(user).updatePut(t);
70      }
71    }
72  
73    public void updateDelete(long t) {
74      String user = getActiveUser();
75      if (user != null) {
76        source.getOrCreateMetricsUser(user).updateDelete(t);
77      }
78    }
79  
80    public void updateGet(long t) {
81      String user = getActiveUser();
82      if (user != null) {
83        source.getOrCreateMetricsUser(user).updateGet(t);
84      }
85    }
86  
87    public void updateIncrement(long t) {
88      String user = getActiveUser();
89      if (user != null) {
90        source.getOrCreateMetricsUser(user).updateIncrement(t);
91      }
92    }
93  
94    public void updateAppend(long t) {
95      String user = getActiveUser();
96      if (user != null) {
97        source.getOrCreateMetricsUser(user).updateAppend(t);
98      }
99    }
100 
101   public void updateReplay(long t) {
102     String user = getActiveUser();
103     if (user != null) {
104       source.getOrCreateMetricsUser(user).updateReplay(t);
105     }
106   }
107 
108   public void updateScanTime(long t) {
109     String user = getActiveUser();
110     if (user != null) {
111       source.getOrCreateMetricsUser(user).updateScanTime(t);
112     }
113   }
114 }