-
Notifications
You must be signed in to change notification settings - Fork 52
/
Bireme.java
351 lines (297 loc) · 9.83 KB
/
Bireme.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
/**
* Copyright HashData. All Rights Reserved.
*/
package cn.hashdata.bireme;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.commons.daemon.DaemonInitException;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.PartitionInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import cn.hashdata.bireme.pipeline.DebeziumPipeLine;
import cn.hashdata.bireme.pipeline.KafkaPipeLine;
import cn.hashdata.bireme.pipeline.MaxwellPipeLine;
import cn.hashdata.bireme.pipeline.PipeLine;
import cn.hashdata.bireme.pipeline.PipeLine.PipeLineState;
import cn.hashdata.bireme.pipeline.SourceConfig;
/**
* {@code Bireme} is an incremental synchronization tool. It could sync update in MySQL to GreenPlum
* / Hashdata database.
*
* @author yuze
*
*/
public class Bireme implements Daemon {
static final protected Long TIMEOUT_MS = 1000L;
private static final String DEFAULT_CONFIG_FILE = "etc/config.properties";
private DaemonContext context;
private Context cxt;
private Logger logger = LogManager.getLogger("Bireme");
private ConsoleReporter consoleReporter;
private JmxReporter jmxReporter;
protected void parseCommandLine(String[] args)
throws DaemonInitException, ConfigurationException, BiremeException {
Option help = new Option("help", "print this message");
Option configFile =
Option.builder("config_file").hasArg().argName("file").desc("config file location").build();
Options opts = new Options();
opts.addOption(help);
opts.addOption(configFile);
CommandLine cmd = null;
CommandLineParser parser = new DefaultParser();
try {
cmd = parser.parse(opts, args);
if (cmd.hasOption("help")) {
throw new ParseException("print help message");
}
} catch (ParseException e) {
HelpFormatter formatter = new HelpFormatter();
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
formatter.printHelp(writer, formatter.getWidth(), "Bireme", null, opts,
formatter.getLeftPadding(), formatter.getDescPadding(), null, true);
writer.flush();
String result = out.toString();
throw new DaemonInitException(result);
}
String config = cmd.getOptionValue("config_file", DEFAULT_CONFIG_FILE);
cxt = new Context(new Config(config));
}
/**
* Get metadata about the table from target database.
*
* @throws BiremeException when fail to connect or get the metadata
*/
protected void getTableInfo() throws BiremeException {
logger.info("Start getting metadata of target tables from target database.");
String[] strArray;
Connection conn = BiremeUtility.jdbcConn(cxt.conf.targetDatabase);
for (String fullname : cxt.tableMap.values()) {
if (cxt.tablesInfo.containsKey(fullname)) {
continue;
}
strArray = fullname.split("\\.");
cxt.tablesInfo.put(fullname, new Table(strArray[0], strArray[1], conn));
}
try {
conn.close();
} catch (SQLException ignore) {
}
logger.info("Finish getting metadata of target tables from target database.");
}
/**
* Establish connections to the target database.
*
* @throws BiremeException fail to connect
*/
protected void initLoaderConnections() throws BiremeException {
logger.info("Start establishing connections for loaders.");
LinkedBlockingQueue<Connection> conns = cxt.loaderConnections;
HashMap<Connection, HashSet<String>> temporatyTables = cxt.temporaryTables;
Connection conn = null;
try {
for (int i = 0, number = cxt.conf.loader_conn_size; i < number; i++) {
conn = BiremeUtility.jdbcConn(cxt.conf.targetDatabase);
conn.setAutoCommit(true);
Statement stmt = conn.createStatement();
stmt.execute("set enable_nestloop = on;");
stmt.execute("set enable_seqscan = off;");
stmt.execute("set enable_hashjoin = off;");
try {
stmt.execute("set gp_autostats_mode = none;");
} catch (SQLException ignore) {
}
conn.setAutoCommit(false);
conns.add(conn);
temporatyTables.put(conn, new HashSet<String>());
}
} catch (SQLException e) {
for (Connection closeConn : temporatyTables.keySet()) {
try {
closeConn.close();
} catch (SQLException ignore) {
}
}
throw new BiremeException("Could not establish connection to target database.\n", e);
}
logger.info("Finishing establishing {} connections for loaders.", cxt.conf.loader_conn_size);
}
protected void closeConnections() throws SQLException {
for (Connection conn : cxt.loaderConnections) {
conn.close();
}
}
protected void createPipeLine() {
for (SourceConfig conf : cxt.conf.sourceConfig.values()) {
switch (conf.type) {
case MAXWELL:
KafkaConsumer<String, String> consumer =
KafkaPipeLine.createConsumer(conf.server, conf.groupID);
Iterator<PartitionInfo> iter = consumer.partitionsFor(conf.topic).iterator();
while (iter.hasNext()) {
PipeLine pipeLine = new MaxwellPipeLine(cxt, conf, iter.next().partition());
cxt.pipeLines.add(pipeLine);
conf.pipeLines.add(pipeLine);
}
break;
case DEBEZIUM:
for (String sourceTable : conf.tableMap.keySet()) {
PipeLine pipeLine = new DebeziumPipeLine(cxt, conf, sourceTable);
cxt.pipeLines.add(pipeLine);
conf.pipeLines.add(pipeLine);
}
break;
default:
break;
}
}
// pipeline state statistics
Gauge<Integer> allCount = new Gauge<Integer>() {
@Override
public Integer getValue() {
return cxt.pipeLines.size();
}
};
Gauge<Integer> liveCount = new Gauge<Integer>() {
@Override
public Integer getValue() {
int live = 0;
for (PipeLine pipeline : cxt.pipeLines) {
if (pipeline.state == PipeLineState.NORMAL) {
live++;
}
}
return live;
}
};
cxt.register.register(MetricRegistry.name("All Pipeline Number"), allCount);
cxt.register.register(MetricRegistry.name("All Live Pipeline Number"), liveCount);
}
/**
* Start metrics reporter.
*
*/
protected void startReporter() {
switch (cxt.conf.reporter) {
case "console":
consoleReporter = ConsoleReporter.forRegistry(cxt.register)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
consoleReporter.start(cxt.conf.report_interval, TimeUnit.SECONDS);
break;
case "jmx":
jmxReporter = JmxReporter.forRegistry(cxt.register).build();
jmxReporter.start();
break;
default:
break;
}
}
@Override
public void init(DaemonContext context) throws Exception {
logger.info("initialize Bireme daemon");
this.context = context;
try {
parseCommandLine(context.getArguments());
} catch (Exception e) {
logger.fatal("start failed. Message: {}.", e.getMessage());
logger.fatal("Stack Trace: ", e);
}
}
@Override
public void start() throws BiremeException {
logger.info("start Bireme daemon.");
try {
getTableInfo();
initLoaderConnections();
} catch (BiremeException e) {
logger.fatal("start failed. Message: {}.", e.getMessage());
logger.fatal("Stack Trace: ", e);
throw e;
}
createPipeLine();
cxt.startScheduler();
startReporter();
if (context != null) {
cxt.startWatchDog(context.getController());
}
}
@Override
public void stop() {
logger.info("stop Bireme daemon");
if (cxt == null) {
return;
}
cxt.stop = true;
logger.info("set stop flag to true");
cxt.waitForExit();
try {
closeConnections();
} catch (SQLException e) {
logger.warn(e.getMessage());
}
logger.info("Bireme exit");
}
@Override
public void destroy() {
logger.info("destroy Bireme daemon");
}
/**
* An entry to start {@code Bireme}.
*
* @param args arguments from command line.
*/
public void entry(String[] args) {
try {
parseCommandLine(args);
} catch (Exception e) {
logger.fatal("Init failed: {}.", e.getMessage());
logger.fatal("Stack Trace: ", e);
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(1);
}
try {
start();
cxt.waitForStop();
} catch (Exception e) {
logger.fatal("Bireme stop abnormally since: {}", e.getMessage());
logger.fatal("Stack Trace: ", e);
}
cxt.waitForExit();
try {
closeConnections();
} catch (SQLException e) {
logger.warn(e.getMessage());
}
logger.info("Bireme exit");
}
public static void main(String[] args) {
Bireme service = new Bireme();
service.entry(args);
}
}