-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopologyBuilder.java
223 lines (185 loc) · 8.77 KB
/
TopologyBuilder.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
package org.middleware.project;
import java.io.*;
import java.util.*;
public class TopologyBuilder {
private static boolean isLocal = false;
public Properties loadEnvProperties(String fileName) {
/**
* utility function:
* loads properties from ./resources folder
* @param : relative path to .properties
*/
Properties prop = new Properties();
try {
InputStream inputStream = new FileInputStream("resources/" + fileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
}
} catch (IOException ex) {
ex.printStackTrace();
}
return prop;
}
private static final void err() {
/**
* utiility function:
* exits
*/
System.out.println("exiting");
System.exit(1);
}
public ArrayList<Properties> buildStage(int pos) {
int outTopic_pos = pos + 1;
Properties global_prop = this.loadEnvProperties("config.properties");
String stage_processors_str = global_prop.getProperty("processors.at." + (pos));
String stage_function = global_prop.getProperty("function.at." + pos);
assert stage_function != null;
assert stage_processors_str != null;
int stage_processors = Integer.parseInt(stage_processors_str);
Random r = new Random();
int low = 4;
ArrayList<Properties> ls_properties = new ArrayList<>();
System.out.println("searching a match for:" + stage_function);
switch (stage_function) {
case "flatmap":
for (int i = 0; i < stage_processors; i++) {
Properties props = new Properties();
if (i % 2 == 0) { // set false not to simulate a crash
props.put("simulateCrash", Integer.toString(r.nextInt(10 - low) + low)); // change here to simulate crash
} else props.put("simulateCrash", Integer.toString(0));
props.put("id", Integer.toString(i));
props.put("type", "stateless");
props.put("group.id", "group_" + pos);
props.put("inTopic", "topic_" + pos);
props.put("outTopic", "topic_" + outTopic_pos);
props.put("function", "flatmap");
if (isLocal) props.put("bootstrap.servers", "localhost:9092");
else props.put("bootstrap.servers", global_prop.getProperty("bootstrap.servers"));
ls_properties.add(props);
}
break;
case "map":
for (int i = 0; i < stage_processors; i++) {
Properties props = new Properties();
if (i % 2 == 0) { // set false not to simulate a crash
props.put("simulateCrash", Integer.toString(r.nextInt(10 - low) + low)); // change here to simulate crash
} else props.put("simulateCrash", Integer.toString(0));
props.put("id", Integer.toString(i));
props.put("type", "stateless");
props.put("group.id", "group_" + pos);
props.put("inTopic", "topic_" + pos);
props.put("outTopic", "topic_" + outTopic_pos);
props.put("function", "map");
if (isLocal) props.put("bootstrap.servers", "localhost:9092");
else props.put("bootstrap.servers", global_prop.getProperty("bootstrap.servers"));
ls_properties.add(props);
}
break;
case "filter":
for (int i = 0; i < stage_processors; i++) {
Properties props = new Properties();
if (i % 2 == 0) { // set false not to simulate a crash
props.put("simulateCrash", Integer.toString(r.nextInt(10 - low) + low)); // change here to simulate crash
} else props.put("simulateCrash", Integer.toString(0));
props.put("id", Integer.toString(i));
props.put("type", "stateless");
props.put("group.id", "group_" + pos);
props.put("inTopic", "topic_" + pos);
props.put("outTopic", "topic_" + outTopic_pos);
props.put("function", "filter");
if (isLocal) props.put("bootstrap.servers", "localhost:9092");
else props.put("bootstrap.servers", global_prop.getProperty("bootstrap.servers"));
ls_properties.add(props);
}
break;
case "windowaggregate":
for (int i = 0; i < stage_processors; i++) {
Properties props = new Properties();
if (i % 2 == 0) { // set false not to simulate a crash
props.put("simulateCrash", Integer.toString(r.nextInt(10 - low) + low)); // change here to simulate crash
} else props.put("simulateCrash", Integer.toString(0));
props.put("id", Integer.toString(i));
props.put("crash", "between"); // possible values: before | after | between
props.put("type", "stateful");
props.put("group.id", "group_" + pos);
props.put("inTopic", "topic_" + pos);
props.put("outTopic", "topic_" + outTopic_pos);
props.put("function", "windowaggregate");
if (isLocal) props.put("bootstrap.servers", "localhost:9092");
else props.put("bootstrap.servers", global_prop.getProperty("bootstrap.servers"));
ls_properties.add(props);
}
break;
default:
System.out.println("the function you provided at stage " + pos + " doesn't match any function implemented");
System.out.println("retry");
err();
}
System.out.println("Stage [" + pos + "] created with " + stage_processors + " processors");
return ls_properties;
}
public Properties build_source() {
Properties props = new Properties();
Properties global_prop = this.loadEnvProperties("config.properties");
props.put("outTopic", "topic_" + 1);
props.put("type", "source");
if (isLocal) props.put("bootstrap.servers", "localhost:9092");
else props.put("bootstrap.servers", global_prop.getProperty("bootstrap.servers"));
props.put("transactionId", "source_transactional_id");
System.out.println("Source configured");
return props;
}
static void appendUsingPrintWriter(String filePath, String text) {
/**
* utility function:
* appends to file
* @param: path to file to write into
* @param: text to append
* @return: void
*/
File file = new File(filePath);
FileWriter fr = null;
BufferedWriter br = null;
PrintWriter pr = null;
try {
// to append to file, you need to initialize FileWriter using below constructor
fr = new FileWriter(file, true);
br = new BufferedWriter(fr);
pr = new PrintWriter(br);
pr.println(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
pr.close();
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public ArrayList<Properties> build_stages() {
Properties global_prop = this.loadEnvProperties("config.properties");
int pipeline_length = Integer.parseInt(global_prop.getProperty("pipeline.length"));
//build stages properties
ArrayList<Properties> lst_stage_props = new ArrayList<>();
for (int i = 0; i < pipeline_length; i++) {
lst_stage_props.addAll(this.buildStage(i + 1));
}
return lst_stage_props;
}
public Properties build_sink() {
Properties props = new Properties();
Properties global_prop = this.loadEnvProperties("config.properties");
int pipelineLength = Integer.parseInt(global_prop.getProperty("pipeline.length"));
props.put("type", "sink");
props.put("inTopic", "topic_" + (pipelineLength + 1));
if (isLocal) props.put("bootstrap.servers", "localhost:9092");
else props.put("bootstrap.servers", global_prop.getProperty("bootstrap.servers"));
System.out.println("Sink configured");
return props;
}
}