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.rest;
20  
21  import java.io.IOException;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.client.HBaseAdmin;
26  import org.apache.hadoop.hbase.client.HTableInterface;
27  import org.apache.hadoop.hbase.security.UserProvider;
28  import org.apache.hadoop.hbase.util.ConnectionCache;
29  import org.apache.hadoop.hbase.util.JvmPauseMonitor;
30  import org.apache.hadoop.security.UserGroupInformation;
31  import org.apache.hadoop.security.authorize.ProxyUsers;
32  
33  /**
34   * Singleton class encapsulating global REST servlet state and functions.
35   */
36  @InterfaceAudience.Private
37  public class RESTServlet implements Constants {
38    private static RESTServlet INSTANCE;
39    private final Configuration conf;
40    private final MetricsREST metrics;
41    private final ConnectionCache connectionCache;
42    private final UserGroupInformation realUser;
43    private final JvmPauseMonitor pauseMonitor;
44  
45    static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
46    static final String MAX_IDLETIME = "hbase.rest.connection.max-idletime";
47    static final String HBASE_REST_SUPPORT_PROXYUSER = "hbase.rest.support.proxyuser";
48  
49    UserGroupInformation getRealUser() {
50      return realUser;
51    }
52  
53    /**
54     * @return the RESTServlet singleton instance
55     */
56    public synchronized static RESTServlet getInstance() {
57      assert(INSTANCE != null);
58      return INSTANCE;
59    }
60  
61    /**
62     * @param conf Existing configuration to use in rest servlet
63     * @param userProvider the login user provider
64     * @return the RESTServlet singleton instance
65     * @throws IOException
66     */
67    public synchronized static RESTServlet getInstance(Configuration conf,
68        UserProvider userProvider) throws IOException {
69      if (INSTANCE == null) {
70        INSTANCE = new RESTServlet(conf, userProvider);
71      }
72      return INSTANCE;
73    }
74  
75    public synchronized static void stop() {
76      if (INSTANCE != null)  INSTANCE = null;
77    }
78  
79    /**
80     * Constructor with existing configuration
81     * @param conf existing configuration
82     * @param userProvider the login user provider
83     * @throws IOException
84     */
85    RESTServlet(final Configuration conf,
86        final UserProvider userProvider) throws IOException {
87      this.realUser = userProvider.getCurrent().getUGI();
88      this.conf = conf;
89  
90      int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
91      int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
92      connectionCache = new ConnectionCache(
93        conf, userProvider, cleanInterval, maxIdleTime);
94      if (supportsProxyuser()) {
95        ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
96      }
97  
98      metrics = new MetricsREST();
99  
100     pauseMonitor = new JvmPauseMonitor(conf, metrics.getSource());
101     pauseMonitor.start();
102   }
103 
104   HBaseAdmin getAdmin() throws IOException {
105     return connectionCache.getAdmin();
106   }
107 
108   /**
109    * Caller closes the table afterwards.
110    */
111   HTableInterface getTable(String tableName) throws IOException {
112     return connectionCache.getTable(tableName);
113   }
114 
115   Configuration getConfiguration() {
116     return conf;
117   }
118 
119   MetricsREST getMetrics() {
120     return metrics;
121   }
122 
123   /**
124    * Helper method to determine if server should
125    * only respond to GET HTTP method requests.
126    * @return boolean for server read-only state
127    */
128   boolean isReadOnly() {
129     return getConfiguration().getBoolean("hbase.rest.readonly", false);
130   }
131 
132   void setEffectiveUser(String effectiveUser) {
133     connectionCache.setEffectiveUser(effectiveUser);
134   }
135 
136   boolean supportsProxyuser() {
137     return conf.getBoolean(HBASE_REST_SUPPORT_PROXYUSER, false);
138   }
139 }