forked from OpenTSDB/opentsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PutDataPointRpc.java
126 lines (117 loc) · 5.16 KB
/
PutDataPointRpc.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
// This file is part of OpenTSDB.
// Copyright (C) 2010 StumbleUpon, Inc.
//
// 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 3 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.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import com.stumbleupon.async.Callback;
import com.stumbleupon.async.Deferred;
import org.jboss.netty.channel.Channel;
import net.opentsdb.core.TSDB;
import net.opentsdb.core.Tags;
import net.opentsdb.core.WritableDataPoints;
import net.opentsdb.stats.StatsCollector;
import net.opentsdb.uid.NoSuchUniqueName;
/** Implements the "put" telnet-style command. */
final class PutDataPointRpc implements TelnetRpc {
private static final AtomicLong requests = new AtomicLong();
private static final AtomicLong hbase_errors = new AtomicLong();
private static final AtomicLong invalid_values = new AtomicLong();
private static final AtomicLong illegal_arguments = new AtomicLong();
private static final AtomicLong unknown_metrics = new AtomicLong();
public Deferred<Object> execute(final TSDB tsdb, final Channel chan,
final String[] cmd) {
requests.incrementAndGet();
String errmsg = null;
try {
final class PutErrback implements Callback<Exception, Exception> {
public Exception call(final Exception arg) {
if (chan.isConnected()) {
chan.write("put: HBase error: " + arg.getMessage() + '\n');
}
hbase_errors.incrementAndGet();
return arg;
}
public String toString() {
return "report error to channel";
}
}
return importDataPoint(tsdb, cmd).addErrback(new PutErrback());
} catch (NumberFormatException x) {
errmsg = "put: invalid value: " + x.getMessage() + '\n';
invalid_values.incrementAndGet();
} catch (IllegalArgumentException x) {
errmsg = "put: illegal argument: " + x.getMessage() + '\n';
illegal_arguments.incrementAndGet();
} catch (NoSuchUniqueName x) {
errmsg = "put: unknown metric: " + x.getMessage() + '\n';
unknown_metrics.incrementAndGet();
}
if (errmsg != null && chan.isConnected()) {
chan.write(errmsg);
}
return Deferred.fromResult(null);
}
/**
* 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", requests, "type=put");
collector.record("rpc.errors", hbase_errors, "type=hbase_errors");
collector.record("rpc.errors", invalid_values, "type=invalid_values");
collector.record("rpc.errors", illegal_arguments, "type=illegal_arguments");
collector.record("rpc.errors", unknown_metrics, "type=unknown_metrics");
}
/**
* Imports a single data point.
* @param tsdb The TSDB to import the data point into.
* @param words The words describing the data point to import, in
* the following format: {@code [metric, timestamp, value, ..tags..]}
* @return A deferred object that indicates the completion of the request.
* @throws NumberFormatException if the timestamp or value is invalid.
* @throws IllegalArgumentException if any other argument is invalid.
* @throws NoSuchUniqueName if the metric isn't registered.
*/
private Deferred<Object> importDataPoint(final TSDB tsdb, final String[] words) {
words[0] = null; // Ditch the "put".
if (words.length < 5) { // Need at least: metric timestamp value tag
// ^ 5 and not 4 because words[0] is "put".
throw new IllegalArgumentException("not enough arguments"
+ " (need least 4, got " + (words.length - 1) + ')');
}
final String metric = words[1];
if (metric.length() <= 0) {
throw new IllegalArgumentException("empty metric name");
}
final long timestamp = Tags.parseLong(words[2]);
if (timestamp <= 0) {
throw new IllegalArgumentException("invalid timestamp: " + timestamp);
}
final String value = words[3];
if (value.length() <= 0) {
throw new IllegalArgumentException("empty value");
}
final HashMap<String, String> tags = new HashMap<String, String>();
for (int i = 4; i < words.length; i++) {
if (!words[i].isEmpty()) {
Tags.parse(tags, words[i]);
}
}
if (value.indexOf('.') < 0) { // integer value
return tsdb.addPoint(metric, timestamp, Tags.parseLong(value), tags);
} else { // floating point value
return tsdb.addPoint(metric, timestamp, Float.parseFloat(value), tags);
}
}
}