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 public static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
50 public 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 public ConnectionCache getConnectionCache() {
69 return connectionCache;
70 }
71
72
73
74
75
76
77
78 public synchronized static RESTServlet getInstance(Configuration conf,
79 UserProvider userProvider) throws IOException {
80 if (INSTANCE == null) {
81 INSTANCE = new RESTServlet(conf, userProvider);
82 }
83 return INSTANCE;
84 }
85
86 public synchronized static void stop() {
87 if (INSTANCE != null) {
88 INSTANCE.shutdown();
89 INSTANCE = null;
90 }
91 }
92
93
94
95
96
97
98
99 RESTServlet(final Configuration conf,
100 final UserProvider userProvider) throws IOException {
101 this.realUser = userProvider.getCurrent().getUGI();
102 this.conf = conf;
103 registerCustomFilter(conf);
104
105 int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
106 int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
107 connectionCache = new ConnectionCache(
108 conf, userProvider, cleanInterval, maxIdleTime);
109 if (supportsProxyuser()) {
110 ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
111 }
112
113 metrics = new MetricsREST();
114
115 pauseMonitor = new JvmPauseMonitor(conf, metrics.getSource());
116 pauseMonitor.start();
117 }
118
119 Admin getAdmin() throws IOException {
120 return connectionCache.getAdmin();
121 }
122
123
124
125
126 Table getTable(String tableName) throws IOException {
127 return connectionCache.getTable(tableName);
128 }
129
130 Configuration getConfiguration() {
131 return conf;
132 }
133
134 MetricsREST getMetrics() {
135 return metrics;
136 }
137
138
139
140
141
142
143 boolean isReadOnly() {
144 return getConfiguration().getBoolean("hbase.rest.readonly", false);
145 }
146
147 void setEffectiveUser(String effectiveUser) {
148 connectionCache.setEffectiveUser(effectiveUser);
149 }
150
151
152
153
154 void shutdown() {
155 if (pauseMonitor != null) pauseMonitor.stop();
156 if (connectionCache != null) connectionCache.shutdown();
157 }
158
159 boolean supportsProxyuser() {
160 return conf.getBoolean(HBASE_REST_SUPPORT_PROXYUSER, false);
161 }
162
163 private void registerCustomFilter(Configuration conf) {
164 String[] filterList = conf.getStrings(Constants.CUSTOM_FILTERS);
165 if (filterList != null) {
166 for (String filterClass : filterList) {
167 String[] filterPart = filterClass.split(":");
168 if (filterPart.length != 2) {
169 LOG.warn(
170 "Invalid filter specification " + filterClass + " - skipping");
171 } else {
172 ParseFilter.registerFilter(filterPart[0], filterPart[1]);
173 }
174 }
175 }
176 }
177 }