/
Thrift.java
347 lines (292 loc) · 13.1 KB
/
Thrift.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.storm;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.storm.generated.Bolt;
import org.apache.storm.generated.ComponentCommon;
import org.apache.storm.generated.ComponentObject;
import org.apache.storm.generated.GlobalStreamId;
import org.apache.storm.generated.Grouping;
import org.apache.storm.generated.JavaObject;
import org.apache.storm.generated.JavaObjectArg;
import org.apache.storm.generated.NullStruct;
import org.apache.storm.generated.SpoutSpec;
import org.apache.storm.generated.StateSpoutSpec;
import org.apache.storm.generated.StormTopology;
import org.apache.storm.generated.StormTopology._Fields;
import org.apache.storm.generated.StreamInfo;
import org.apache.storm.shade.org.json.simple.JSONValue;
import org.apache.storm.task.IBolt;
import org.apache.storm.topology.BoltDeclarer;
import org.apache.storm.topology.IBasicBolt;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.IRichSpout;
import org.apache.storm.topology.SpoutDeclarer;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Thrift {
private static Logger LOG = LoggerFactory.getLogger(Thrift.class);
private static StormTopology._Fields[] STORM_TOPOLOGY_FIELDS = null;
private static StormTopology._Fields[] SPOUT_FIELDS =
{ StormTopology._Fields.SPOUTS, StormTopology._Fields.STATE_SPOUTS };
static {
Set<_Fields> keys = StormTopology.metaDataMap.keySet();
keys.toArray(STORM_TOPOLOGY_FIELDS = new StormTopology._Fields[keys.size()]);
}
public static StormTopology._Fields[] getTopologyFields() {
return STORM_TOPOLOGY_FIELDS;
}
public static StormTopology._Fields[] getSpoutFields() {
return SPOUT_FIELDS;
}
public static StreamInfo directOutputFields(List<String> fields) {
return new StreamInfo(fields, true);
}
public static StreamInfo outputFields(List<String> fields) {
return new StreamInfo(fields, false);
}
public static Grouping prepareShuffleGrouping() {
return Grouping.shuffle(new NullStruct());
}
public static Grouping prepareLocalOrShuffleGrouping() {
return Grouping.local_or_shuffle(new NullStruct());
}
public static Grouping prepareFieldsGrouping(List<String> fields) {
return Grouping.fields(fields);
}
public static Grouping prepareGlobalGrouping() {
return prepareFieldsGrouping(new ArrayList<String>());
}
public static Grouping prepareDirectGrouping() {
return Grouping.direct(new NullStruct());
}
public static Grouping prepareAllGrouping() {
return Grouping.all(new NullStruct());
}
public static Grouping prepareNoneGrouping() {
return Grouping.none(new NullStruct());
}
public static Grouping prepareCustomStreamGrouping(Object obj) {
return Grouping.custom_serialized(Utils.javaSerialize(obj));
}
public static Grouping prepareCustomJavaObjectGrouping(JavaObject obj) {
return Grouping.custom_object(obj);
}
public static Object instantiateJavaObject(JavaObject obj) {
List<JavaObjectArg> args = obj.get_args_list();
Class[] paraTypes = new Class[args.size()];
Object[] paraValues = new Object[args.size()];
for (int i = 0; i < args.size(); i++) {
JavaObjectArg arg = args.get(i);
paraValues[i] = arg.getFieldValue();
if (arg.getSetField().equals(JavaObjectArg._Fields.INT_ARG)) {
paraTypes[i] = Integer.class;
} else if (arg.getSetField().equals(JavaObjectArg._Fields.LONG_ARG)) {
paraTypes[i] = Long.class;
} else if (arg.getSetField().equals(JavaObjectArg._Fields.STRING_ARG)) {
paraTypes[i] = String.class;
} else if (arg.getSetField().equals(JavaObjectArg._Fields.BOOL_ARG)) {
paraTypes[i] = Boolean.class;
} else if (arg.getSetField().equals(JavaObjectArg._Fields.BINARY_ARG)) {
paraTypes[i] = ByteBuffer.class;
} else if (arg.getSetField().equals(JavaObjectArg._Fields.DOUBLE_ARG)) {
paraTypes[i] = Double.class;
} else {
paraTypes[i] = Object.class;
}
}
try {
Class clazz = Class.forName(obj.get_full_class_name());
Constructor cons = clazz.getConstructor(paraTypes);
return cons.newInstance(paraValues);
} catch (Exception e) {
LOG.error("java object instantiation failed", e);
}
return null;
}
public static Grouping._Fields groupingType(Grouping grouping) {
return grouping.getSetField();
}
public static List<String> fieldGrouping(Grouping grouping) {
if (!Grouping._Fields.FIELDS.equals(groupingType(grouping))) {
throw new IllegalArgumentException("Tried to get grouping fields from non fields grouping");
}
return grouping.get_fields();
}
public static boolean isGlobalGrouping(Grouping grouping) {
if (Grouping._Fields.FIELDS.equals(groupingType(grouping))) {
return fieldGrouping(grouping).isEmpty();
}
return false;
}
public static int getParallelismHint(ComponentCommon componentCommon) {
if (!componentCommon.is_set_parallelism_hint()) {
return 1;
} else {
return componentCommon.get_parallelism_hint();
}
}
public static ComponentObject serializeComponentObject(Object obj) {
return ComponentObject.serialized_java(Utils.javaSerialize(obj));
}
public static Object deserializeComponentObject(ComponentObject obj) {
if (obj.getSetField() != ComponentObject._Fields.SERIALIZED_JAVA) {
throw new RuntimeException("Cannot deserialize non-java-serialized object");
}
return Utils.javaDeserialize(obj.get_serialized_java(), Serializable.class);
}
public static ComponentCommon prepareComponentCommon(Map<GlobalStreamId, Grouping> inputs, Map<String,
StreamInfo> outputs, Integer parallelismHint) {
return prepareComponentCommon(inputs, outputs, parallelismHint, null);
}
public static ComponentCommon prepareComponentCommon(Map<GlobalStreamId, Grouping> inputs, Map<String, StreamInfo> outputs,
Integer parallelismHint, Map<String, Object> conf) {
Map<GlobalStreamId, Grouping> mappedInputs = new HashMap<>();
Map<String, StreamInfo> mappedOutputs = new HashMap<>();
if (inputs != null && !inputs.isEmpty()) {
mappedInputs.putAll(inputs);
}
if (outputs != null && !outputs.isEmpty()) {
mappedOutputs.putAll(outputs);
}
ComponentCommon component = new ComponentCommon(mappedInputs, mappedOutputs);
if (parallelismHint != null) {
component.set_parallelism_hint(parallelismHint);
}
if (conf != null) {
component.set_json_conf(JSONValue.toJSONString(conf));
}
return component;
}
public static SpoutSpec prepareSerializedSpoutDetails(IRichSpout spout, Map<String, StreamInfo> outputs) {
return new SpoutSpec(ComponentObject.serialized_java(Utils.javaSerialize(spout)),
prepareComponentCommon(new HashMap<>(), outputs, null, null));
}
public static Bolt prepareSerializedBoltDetails(Map<GlobalStreamId, Grouping> inputs, IBolt bolt, Map<String, StreamInfo> outputs,
Integer parallelismHint, Map<String, Object> conf) {
ComponentCommon common = prepareComponentCommon(inputs, outputs, parallelismHint, conf);
return new Bolt(ComponentObject.serialized_java(Utils.javaSerialize(bolt)), common);
}
public static BoltDetails prepareBoltDetails(Map<GlobalStreamId, Grouping> inputs, Object bolt) {
return prepareBoltDetails(inputs, bolt, null, null);
}
public static BoltDetails prepareBoltDetails(Map<GlobalStreamId, Grouping> inputs, Object bolt,
Integer parallelismHint) {
return prepareBoltDetails(inputs, bolt, parallelismHint, null);
}
public static BoltDetails prepareBoltDetails(Map<GlobalStreamId, Grouping> inputs, Object bolt,
Integer parallelismHint, Map<String, Object> conf) {
BoltDetails details = new BoltDetails(bolt, conf, parallelismHint, inputs);
return details;
}
public static SpoutDetails prepareSpoutDetails(IRichSpout spout) {
return prepareSpoutDetails(spout, null, null);
}
public static SpoutDetails prepareSpoutDetails(IRichSpout spout, Integer parallelismHint) {
return prepareSpoutDetails(spout, parallelismHint, null);
}
public static SpoutDetails prepareSpoutDetails(IRichSpout spout, Integer parallelismHint, Map<String, Object> conf) {
SpoutDetails details = new SpoutDetails(spout, parallelismHint, conf);
return details;
}
private static void addInputs(BoltDeclarer declarer, Map<GlobalStreamId, Grouping> inputs) {
for (Entry<GlobalStreamId, Grouping> entry : inputs.entrySet()) {
declarer.grouping(entry.getKey(), entry.getValue());
}
}
public static StormTopology buildTopology(HashMap<String, SpoutDetails> spoutMap,
HashMap<String, BoltDetails> boltMap, HashMap<String, StateSpoutSpec> stateMap) {
return buildTopology(spoutMap, boltMap);
}
public static StormTopology buildTopology(Map<String, SpoutDetails> spoutMap, Map<String, BoltDetails> boltMap) {
TopologyBuilder builder = new TopologyBuilder();
for (Entry<String, SpoutDetails> entry : spoutMap.entrySet()) {
String spoutId = entry.getKey();
SpoutDetails spec = entry.getValue();
SpoutDeclarer spoutDeclarer = builder.setSpout(spoutId, spec.getSpout(), spec.getParallelism());
spoutDeclarer.addConfigurations(spec.getConf());
}
for (Entry<String, BoltDetails> entry : boltMap.entrySet()) {
String spoutId = entry.getKey();
BoltDetails spec = entry.getValue();
BoltDeclarer boltDeclarer = null;
if (spec.bolt instanceof IRichBolt) {
boltDeclarer = builder.setBolt(spoutId, (IRichBolt) spec.getBolt(), spec.getParallelism());
} else {
boltDeclarer = builder.setBolt(spoutId, (IBasicBolt) spec.getBolt(), spec.getParallelism());
}
boltDeclarer.addConfigurations(spec.getConf());
addInputs(boltDeclarer, spec.getInputs());
}
return builder.createTopology();
}
public static class SpoutDetails {
private IRichSpout spout;
private Integer parallelism;
private Map<String, Object> conf;
public SpoutDetails(IRichSpout spout, Integer parallelism, Map<String, Object> conf) {
this.spout = spout;
this.parallelism = parallelism;
this.conf = conf;
}
public IRichSpout getSpout() {
return spout;
}
public Integer getParallelism() {
return parallelism;
}
public Map<String, Object> getConf() {
return conf;
}
}
public static class BoltDetails {
private Object bolt;
private Map<String, Object> conf;
private Integer parallelism;
private Map<GlobalStreamId, Grouping> inputs;
public BoltDetails(Object bolt, Map<String, Object> conf, Integer parallelism,
Map<GlobalStreamId, Grouping> inputs) {
this.bolt = bolt;
this.conf = conf;
this.parallelism = parallelism;
this.inputs = inputs;
}
public Object getBolt() {
return bolt;
}
public Map<String, Object> getConf() {
return conf;
}
public Map<GlobalStreamId, Grouping> getInputs() {
return inputs;
}
public Integer getParallelism() {
return parallelism;
}
}
}