1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.rest;
20
21 import java.io.IOException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.client.Admin;
28 import org.apache.hadoop.hbase.client.Table;
29 import org.apache.hadoop.hbase.filter.ParseFilter;
30 import org.apache.hadoop.hbase.security.UserProvider;
31 import org.apache.hadoop.hbase.util.ConnectionCache;
32 import org.apache.hadoop.hbase.util.JvmPauseMonitor;
33 import org.apache.hadoop.security.UserGroupInformation;
34 import org.apache.hadoop.security.authorize.ProxyUsers;
35
36
37
38
39 @InterfaceAudience.Private
40 public class RESTServlet implements Constants {
41 private static final Log LOG = LogFactory.getLog(RESTServlet.class);
42 private static RESTServlet INSTANCE;
43 private final Configuration conf;
44 private final MetricsREST metrics;
45 private final ConnectionCache connectionCache;
46 private final UserGroupInformation realUser;
47 private final JvmPauseMonitor pauseMonitor;
48
49 static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
50 static final String MAX_IDLETIME = "hbase.rest.connection.max-idletime";
51 static final String HBASE_REST_SUPPORT_PROXYUSER = "hbase.rest.support.proxyuser";
52
53 UserGroupInformation getRealUser() {
54 return realUser;
55 }
56
57
58
59
60 public synchronized static RESTServlet getInstance() {
61 assert(INSTANCE != null);
62 return INSTANCE;
63 }
64
65
66
67
68
69
70
71 public synchronized static RESTServlet getInstance(Configuration conf,
72 UserProvider userProvider) throws IOException {
73 if (INSTANCE == null) {
74 INSTANCE = new RESTServlet(conf, userProvider);
75 }
76 return INSTANCE;
77 }
78
79 public synchronized static void stop() {
80 if (INSTANCE != null) {
81 INSTANCE.shutdown();
82 INSTANCE = null;
83 }
84 }
85
86
87
88
89
90
91
92 RESTServlet(final Configuration conf,
93 final UserProvider userProvider) throws IOException {
94 this.realUser = userProvider.getCurrent().getUGI();
95 this.conf = conf;
96 registerCustomFilter(conf);
97
98 int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
99 int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
100 connectionCache = new ConnectionCache(
101 conf, userProvider, cleanInterval, maxIdleTime);
102 if (supportsProxyuser()) {
103 ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
104 }
105
106 metrics = new MetricsREST();
107
108 pauseMonitor = new JvmPauseMonitor(conf, metrics.getSource());
109 pauseMonitor.start();
110 }
111
112 Admin getAdmin() throws IOException {
113 return connectionCache.getAdmin();
114 }
115
116
117
118
119 Table getTable(String tableName) throws IOException {
120 return connectionCache.getTable(tableName);
121 }
122
123 Configuration getConfiguration() {
124 return conf;
125 }
126
127 MetricsREST getMetrics() {
128 return metrics;
129 }
130
131
132
133
134
135
136 boolean isReadOnly() {
137 return getConfiguration().getBoolean("hbase.rest.readonly", false);
138 }
139
140 void setEffectiveUser(String effectiveUser) {
141 connectionCache.setEffectiveUser(effectiveUser);
142 }
143
144
145
146
147 void shutdown() {
148 if (pauseMonitor != null) pauseMonitor.stop();
149 if (connectionCache != null) connectionCache.shutdown();
150 }
151
152 boolean supportsProxyuser() {
153 return conf.getBoolean(HBASE_REST_SUPPORT_PROXYUSER, false);
154 }
155
156 private void registerCustomFilter(Configuration conf) {
157 String[] filterList = conf.getStrings(Constants.CUSTOM_FILTERS);
158 if (filterList != null) {
159 for (String filterClass : filterList) {
160 String[] filterPart = filterClass.split(":");
161 if (filterPart.length != 2) {
162 LOG.warn(
163 "Invalid filter specification " + filterClass + " - skipping");
164 } else {
165 ParseFilter.registerFilter(filterPart[0], filterPart[1]);
166 }
167 }
168 }
169 }
170 }