forked from box/opentsdb
/
RpcHandler.java
416 lines (378 loc) · 16.5 KB
/
RpcHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// This file is part of OpenTSDB.
// Copyright (C) 2010-2012 The OpenTSDB Authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version. This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details. You should have received a copy
// of the GNU Lesser General Public License along with this program. If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.tsd;
import java.util.Arrays;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicLong;
import com.google.common.base.Strings;
import com.google.common.net.HttpHeaders;
import com.stumbleupon.async.Deferred;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.timeout.IdleState;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelUpstreamHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.opentsdb.core.TSDB;
import net.opentsdb.stats.StatsCollector;
/**
* Stateless handler for all RPCs: telnet-style, built-in or plugin
* HTTP.
*/
final class RpcHandler extends IdleStateAwareChannelUpstreamHandler {
private static final Logger LOG = LoggerFactory.getLogger(RpcHandler.class);
private static final AtomicLong telnet_rpcs_received = new AtomicLong();
private static final AtomicLong http_rpcs_received = new AtomicLong();
private static final AtomicLong http_plugin_rpcs_received = new AtomicLong();
private static final AtomicLong exceptions_caught = new AtomicLong();
/** RPC executed when there's an unknown telnet-style command. */
private final TelnetRpc unknown_cmd = new Unknown();
/** List of domains to allow access to HTTP. By default this will be empty and
* all CORS headers will be ignored. */
private final HashSet<String> cors_domains;
/** List of headers allowed for access to HTTP. By default this will contain a
* set of known-to-work headers */
private final String cors_headers;
/** RPC plugins. Contains the handlers we dispatch requests to. */
private final RpcManager rpc_manager;
/** The TSDB to use. */
private final TSDB tsdb;
/**
* Constructor that loads the CORS domain list and prepares for
* handling requests. This constructor creates its own {@link RpcManager}.
* @param tsdb The TSDB to use.
* @param manager instance of a ready-to-use {@link RpcManager}.
* @throws IllegalArgumentException if there was an error with the CORS domain
* list
*/
public RpcHandler(final TSDB tsdb) {
this(tsdb, RpcManager.instance(tsdb));
}
/**
* Constructor that loads the CORS domain list and prepares for handling
* requests.
* @param tsdb The TSDB to use.
* @param manager instance of a ready-to-use {@link RpcManager}.
* @throws IllegalArgumentException if there was an error with the CORS domain
* list
*/
public RpcHandler(final TSDB tsdb, final RpcManager manager) {
this.tsdb = tsdb;
this.rpc_manager = manager;
final String cors = tsdb.getConfig().getString("tsd.http.request.cors_domains");
final String mode = tsdb.getConfig().getString("tsd.mode");
LOG.info("TSD is in " + mode + " mode");
if (cors == null || cors.isEmpty()) {
cors_domains = null;
LOG.info("CORS domain list was empty, CORS will not be enabled");
} else {
final String[] domains = cors.split(",");
cors_domains = new HashSet<String>(domains.length);
for (final String domain : domains) {
if (domain.equals("*") && domains.length > 1) {
throw new IllegalArgumentException(
"tsd.http.request.cors_domains must be a public resource (*) or "
+ "a list of specific domains, you cannot mix both.");
}
cors_domains.add(domain.trim().toUpperCase());
LOG.info("Loaded CORS domain (" + domain + ")");
}
}
cors_headers = tsdb.getConfig().getString("tsd.http.request.cors_headers")
.trim();
if ((cors_headers == null) || !cors_headers.matches("^([a-zA-Z0-9_.-]+,\\s*)*[a-zA-Z0-9_.-]+$")) {
throw new IllegalArgumentException(
"tsd.http.request.cors_headers must be a list of validly-formed "
+ "HTTP header names. No wildcards are allowed.");
} else {
LOG.info("Loaded CORS headers (" + cors_headers + ")");
}
}
@Override
public void messageReceived(final ChannelHandlerContext ctx,
final MessageEvent msgevent) {
try {
final Object message = msgevent.getMessage();
if (message instanceof String[]) {
handleTelnetRpc(msgevent.getChannel(), (String[]) message);
} else if (message instanceof HttpRequest) {
handleHttpQuery(tsdb, msgevent.getChannel(), (HttpRequest) message);
} else {
logError(msgevent.getChannel(), "Unexpected message type "
+ message.getClass() + ": " + message);
exceptions_caught.incrementAndGet();
}
} catch (Exception e) {
Object pretty_message = msgevent.getMessage();
if (pretty_message instanceof String[]) {
pretty_message = Arrays.toString((String[]) pretty_message);
}
logError(msgevent.getChannel(), "Unexpected exception caught"
+ " while serving " + pretty_message, e);
exceptions_caught.incrementAndGet();
}
}
/**
* Finds the right handler for a telnet-style RPC and executes it.
* @param chan The channel on which the RPC was received.
* @param command The split telnet-style command.
*/
private void handleTelnetRpc(final Channel chan, final String[] command) {
TelnetRpc rpc = rpc_manager.lookupTelnetRpc(command[0]);
if (rpc == null) {
rpc = unknown_cmd;
}
telnet_rpcs_received.incrementAndGet();
rpc.execute(tsdb, chan, command);
}
/**
* Using the request URI, creates a query instance capable of handling
* the given request.
* @param tsdb the TSDB instance we are running within
* @param request the incoming HTTP request
* @param chan the {@link Channel} the request came in on.
* @return a subclass of {@link AbstractHttpQuery}
* @throws BadRequestException if the request is invalid in a way that
* can be detected early, here.
*/
private AbstractHttpQuery createQueryInstance(final TSDB tsdb,
final HttpRequest request,
final Channel chan)
throws BadRequestException {
final String uri = request.getUri();
if (Strings.isNullOrEmpty(uri)) {
throw new BadRequestException("Request URI is empty");
} else if (uri.charAt(0) != '/') {
throw new BadRequestException("Request URI doesn't start with a slash");
} else if (rpc_manager.isHttpRpcPluginPath(uri)) {
http_plugin_rpcs_received.incrementAndGet();
return new HttpRpcPluginQuery(tsdb, request, chan);
} else {
http_rpcs_received.incrementAndGet();
HttpQuery builtinQuery = new HttpQuery(tsdb, request, chan);
return builtinQuery;
}
}
/**
* Helper method to apply CORS configuration to a request, either a built-in
* RPC or a user plugin.
* @return <code>true</code> if a status reply was sent (in the the case of
* certain HTTP methods); <code>false</code> otherwise.
*/
private boolean applyCorsConfig(final HttpRequest req, final AbstractHttpQuery query)
throws BadRequestException {
final String domain = req.headers().get("Origin");
// catch CORS requests and add the header or refuse them if the domain
// list has been configured
if (query.method() == HttpMethod.OPTIONS ||
(cors_domains != null && domain != null && !domain.isEmpty())) {
if (cors_domains == null || domain == null || domain.isEmpty()) {
throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED,
"Method not allowed", "The HTTP method [" +
query.method().getName() + "] is not permitted");
}
if (cors_domains.contains("*") ||
cors_domains.contains(domain.toUpperCase())) {
// when a domain has matched successfully, we need to add the header
query.response().headers().add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
domain);
query.response().headers().add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS,
"GET, POST, PUT, DELETE");
query.response().headers().add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,
cors_headers);
// if the method requested was for OPTIONS then we'll return an OK
// here and no further processing is needed.
if (query.method() == HttpMethod.OPTIONS) {
query.sendStatusOnly(HttpResponseStatus.OK);
return true;
}
} else {
// You'd think that they would want the server to return a 403 if
// the Origin wasn't in the CORS domain list, but they want a 200
// without the allow origin header. We'll return an error in the
// body though.
throw new BadRequestException(HttpResponseStatus.OK,
"CORS domain not allowed", "The domain [" + domain +
"] is not permitted access");
}
}
return false;
}
/**
* Finds the right handler for an HTTP query (either built-in or user plugin)
* and executes it. Also handles simple and pre-flight CORS requests if
* configured, rejecting requests that do not match a domain in the list.
* @param chan The channel on which the query was received.
* @param req The parsed HTTP request.
*/
private void handleHttpQuery(final TSDB tsdb, final Channel chan, final HttpRequest req) {
// quick bail if not GET/POST/OPTIONS/PUT/DELETE, no other methods are allowed anywhere
try {
RpcUtil.allowedMethods(req.getMethod(), HttpMethod.GET.getName(), HttpMethod.POST.getName(),
HttpMethod.OPTIONS.getName(), HttpMethod.PUT.getName(), HttpMethod.DELETE.getName());
} catch (BadRequestException bre) {
sendStatusAndClose(chan, HttpResponseStatus.METHOD_NOT_ALLOWED);
}
AbstractHttpQuery abstractQuery = null;
try {
abstractQuery = createQueryInstance(tsdb, req, chan);
if (!tsdb.getConfig().enable_chunked_requests() && req.isChunked()) {
logError(abstractQuery, "Received an unsupported chunked request: "
+ abstractQuery.request());
abstractQuery.badRequest(new BadRequestException("Chunked request not supported."));
return;
}
// NOTE: Some methods in HttpQuery have side-effects (getQueryBaseRoute and
// setSerializer for instance) so invocation order is important here.
final String route = abstractQuery.getQueryBaseRoute();
if (abstractQuery.getClass().isAssignableFrom(HttpRpcPluginQuery.class)) {
if (applyCorsConfig(req, abstractQuery)) {
return;
}
final HttpRpcPluginQuery pluginQuery = (HttpRpcPluginQuery) abstractQuery;
final HttpRpcPlugin rpc = rpc_manager.lookupHttpRpcPlugin(route);
if (rpc != null) {
rpc.execute(tsdb, pluginQuery);
} else {
pluginQuery.notFound();
}
} else if (abstractQuery.getClass().isAssignableFrom(HttpQuery.class)) {
final HttpQuery builtinQuery = (HttpQuery) abstractQuery;
builtinQuery.setSerializer();
if (applyCorsConfig(req, abstractQuery)) {
return;
}
final HttpRpc rpc = rpc_manager.lookupHttpRpc(route);
if (rpc != null) {
rpc.execute(tsdb, builtinQuery);
} else {
builtinQuery.notFound();
}
} else {
throw new IllegalStateException("Unknown instance of AbstractHttpQuery: "
+ abstractQuery.getClass().getName());
}
} catch (BadRequestException ex) {
if (abstractQuery == null) {
LOG.warn("{} Unable to create query for {}. Reason: {}", chan, req, ex);
sendStatusAndClose(chan, HttpResponseStatus.BAD_REQUEST);
} else {
abstractQuery.badRequest(ex);
}
} catch (Exception ex) {
exceptions_caught.incrementAndGet();
if (abstractQuery == null) {
LOG.warn("{} Unexpected error handling HTTP request {}. Reason: {} ", chan, req, ex);
sendStatusAndClose(chan, HttpResponseStatus.INTERNAL_SERVER_ERROR);
} else {
abstractQuery.internalError(ex);
}
}
}
/**
* Helper method for sending a status-only HTTP response. This is used in cases where
* {@link #createQueryInstance(TSDB, HttpRequest, Channel)} failed to determine a query
* and we still want to return an error status to the client.
*/
private void sendStatusAndClose(final Channel chan, final HttpResponseStatus status) {
if (chan.isConnected()) {
final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
final ChannelFuture future = chan.write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
}
/**
* Collects the stats and metrics tracked by this instance.
* @param collector The collector to use.
*/
public static void collectStats(final StatsCollector collector) {
collector.record("rpc.received", telnet_rpcs_received, "type=telnet");
collector.record("rpc.received", http_rpcs_received, "type=http");
collector.record("rpc.received", http_plugin_rpcs_received, "type=http_plugin");
collector.record("rpc.exceptions", exceptions_caught);
HttpQuery.collectStats(collector);
GraphHandler.collectStats(collector);
PutDataPointRpc.collectStats(collector);
QueryRpc.collectStats(collector);
}
/**
* Returns the directory path stored in the given system property.
* @param prop The name of the system property.
* @return The directory path.
* @throws IllegalStateException if the system property is not set
* or has an invalid value.
*/
static String getDirectoryFromSystemProp(final String prop) {
final String dir = System.getProperty(prop);
String err = null;
if (dir == null) {
err = "' is not set.";
} else if (dir.isEmpty()) {
err = "' is empty.";
} else if (dir.charAt(dir.length() - 1) != '/') { // Screw Windows.
err = "' is not terminated with `/'.";
}
if (err != null) {
throw new IllegalStateException("System property `" + prop + err);
}
return dir;
}
// ---------------------------- //
// Individual command handlers. //
// ---------------------------- //
/** For unknown commands. */
private static final class Unknown implements TelnetRpc {
public Deferred<Object> execute(final TSDB tsdb, final Channel chan,
final String[] cmd) {
logWarn(chan, "unknown command : " + Arrays.toString(cmd));
chan.write("unknown command: " + cmd[0] + ". Try `help'.\n");
return Deferred.fromResult(null);
}
}
@Override
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
if (e.getState() == IdleState.ALL_IDLE) {
final String channel_info = e.getChannel().toString();
LOG.debug("Closing idle socket: " + channel_info);
e.getChannel().close();
LOG.info("Closed idle socket: " + channel_info);
}
}
// ---------------- //
// Logging helpers. //
// ---------------- //
private static void logWarn(final AbstractHttpQuery query, final String msg) {
LOG.warn(query.channel().toString() + ' ' + msg);
}
private void logError(final AbstractHttpQuery query, final String msg) {
LOG.error(query.channel().toString() + ' ' + msg);
}
private static void logWarn(final Channel chan, final String msg) {
LOG.warn(chan.toString() + ' ' + msg);
}
private void logError(final Channel chan, final String msg) {
LOG.error(chan.toString() + ' ' + msg);
}
private void logError(final Channel chan, final String msg, final Exception e) {
LOG.error(chan.toString() + ' ' + msg, e);
}
}