/
DataLoaderService.java
453 lines (406 loc) · 19.4 KB
/
DataLoaderService.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Eric Long <erilong@users.sourceforge.net>,
* Chris Henson <chenson42@users.sourceforge.net>
*
* This library 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 library 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 library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.service.impl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ErrorConstants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.io.ThresholdFileWriter;
import org.jumpmind.symmetric.load.IBatchListener;
import org.jumpmind.symmetric.load.IColumnFilter;
import org.jumpmind.symmetric.load.IDataLoader;
import org.jumpmind.symmetric.load.IDataLoaderFilter;
import org.jumpmind.symmetric.load.IDataLoaderStatistics;
import org.jumpmind.symmetric.model.IncomingBatch;
import org.jumpmind.symmetric.model.IncomingBatchHistory;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.IncomingBatchHistory.Status;
import org.jumpmind.symmetric.service.IDataLoaderService;
import org.jumpmind.symmetric.service.IIncomingBatchService;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.RegistrationNotOpenException;
import org.jumpmind.symmetric.service.RegistrationRequiredException;
import org.jumpmind.symmetric.statistic.IStatisticManager;
import org.jumpmind.symmetric.statistic.StatisticNameConstants;
import org.jumpmind.symmetric.transport.AuthenticationException;
import org.jumpmind.symmetric.transport.ConnectionRejectedException;
import org.jumpmind.symmetric.transport.IIncomingTransport;
import org.jumpmind.symmetric.transport.ITransportManager;
import org.jumpmind.symmetric.transport.TransportException;
import org.jumpmind.symmetric.transport.file.FileIncomingTransport;
import org.jumpmind.symmetric.transport.internal.InternalIncomingTransport;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class DataLoaderService extends AbstractService implements IDataLoaderService, BeanFactoryAware {
protected static final Log logger = LogFactory.getLog(DataLoaderService.class);
protected IDbDialect dbDialect;
protected IIncomingBatchService incomingBatchService;
protected ITransportManager transportManager;
protected TransactionTemplate transactionTemplate;
protected BeanFactory beanFactory;
protected List<IDataLoaderFilter> filters;
protected IStatisticManager statisticManager;
protected INodeService nodeService;
protected Map<String, IColumnFilter> columnFilters = new HashMap<String, IColumnFilter>();
protected List<IBatchListener> batchListeners;
/**
* Connect to the remote node and pull data. The acknowledgment of
* commit/error status is sent separately after the data is processed.
*/
public boolean loadData(Node remote, Node local) throws IOException {
boolean wasWorkDone = false;
try {
List<IncomingBatchHistory> list = loadDataAndReturnBatches(transportManager.getPullTransport(remote, local));
if (list.size() > 0) {
sendAck(remote, local, list);
wasWorkDone = true;
}
} catch (RegistrationRequiredException e) {
logger.warn("Registration was lost, attempting to re-register");
loadData(transportManager.getRegisterTransport(local));
nodeService.findIdentity(false);
wasWorkDone = true;
} catch (MalformedURLException e) {
logger.error("Could not connect to the " + remote + " node's transport because of a bad URL: "
+ e.getMessage());
}
return wasWorkDone;
}
/**
* Try a configured number of times to get the ACK through.
*/
private void sendAck(Node remote, Node local, List<IncomingBatchHistory> list) throws IOException {
Exception error = null;
boolean sendAck = false;
int numberOfStatusSendRetries = parameterService.getInt(ParameterConstants.DATA_LOADER_NUM_OF_ACK_RETRIES);
for (int i = 0; i < numberOfStatusSendRetries && !sendAck; i++) {
try {
sendAck = transportManager.sendAcknowledgement(remote, list, local);
} catch (IOException ex) {
logger.warn("Ack was not sent successfully on try number " + i + 1 + ". " + ex.getMessage());
error = ex;
} catch (RuntimeException ex) {
logger.warn("Ack was not sent successfully on try number " + i + 1 + ". " + ex.getMessage());
error = ex;
}
if (!sendAck) {
if (i < numberOfStatusSendRetries - 1) {
sleepBetweenFailedAcks();
} else if (error instanceof RuntimeException) {
throw (RuntimeException) error;
} else if (error instanceof IOException) {
throw (IOException) error;
}
}
}
}
private final void sleepBetweenFailedAcks() {
try {
Thread.sleep(parameterService.getLong(ParameterConstants.DATA_LOADER_TIME_BETWEEN_ACK_RETRIES));
} catch (InterruptedException e) {
}
}
public IDataLoader openDataLoader(BufferedReader reader) throws IOException {
IDataLoader dataLoader = (IDataLoader) beanFactory.getBean(Constants.DATALOADER);
dataLoader.open(reader, filters, columnFilters);
return dataLoader;
}
public IDataLoaderStatistics loadDataBatch(String batchData) throws IOException {
BufferedReader reader = new BufferedReader(new StringReader(batchData));
IDataLoader dataLoader = openDataLoader(reader);
IDataLoaderStatistics stats = null;
try {
while (dataLoader.hasNext()) {
dataLoader.load();
IncomingBatchHistory history = new IncomingBatchHistory(new IncomingBatch(dataLoader.getContext()), dataLoader.getContext());
history.setValues(dataLoader.getStatistics(), true);
fireBatchComplete(dataLoader, history);
}
} finally {
stats = dataLoader.getStatistics();
dataLoader.close();
}
return stats;
}
/**
* Load database from input stream and return a list of batch statuses. This
* is used for a pull request that responds with data, and the
* acknowledgment is sent later.
*
* @param in
*/
protected List<IncomingBatchHistory> loadDataAndReturnBatches(IIncomingTransport transport) throws IOException {
List<IncomingBatchHistory> list = new ArrayList<IncomingBatchHistory>();
IncomingBatch status = null;
IncomingBatchHistory history = null;
IDataLoader dataLoader = null;
try {
long totalNetworkMillis = System.currentTimeMillis();
if (parameterService.is(ParameterConstants.STREAM_TO_FILE_ENABLED)) {
transport = writeToFile(transport);
totalNetworkMillis = System.currentTimeMillis() - totalNetworkMillis;
}
dataLoader = openDataLoader(transport.open());
while (dataLoader.hasNext()) {
status = new IncomingBatch(dataLoader.getContext());
history = new IncomingBatchHistory(status, dataLoader.getContext());
list.add(history);
loadBatch(dataLoader, status, history);
status = null;
}
if (parameterService.is(ParameterConstants.STREAM_TO_FILE_ENABLED)) {
estimateNetworkMillis(list, totalNetworkMillis);
}
for (IncomingBatchHistory incomingBatchHistory : list) {
if (incomingBatchHistory.getBatch().isPersistable()) {
incomingBatchService.insertIncomingBatchHistory(incomingBatchHistory);
}
}
} catch (RegistrationRequiredException ex) {
throw ex;
} catch (ConnectException ex) {
statisticManager.getStatistic(StatisticNameConstants.INCOMING_TRANSPORT_CONNECT_ERROR_COUNT).increment();
throw ex;
} catch (UnknownHostException ex) {
logger.warn(ErrorConstants.COULD_NOT_CONNECT_TO_TRANSPORT + " Unknown host name of " + ex.getMessage());
statisticManager.getStatistic(StatisticNameConstants.INCOMING_TRANSPORT_CONNECT_ERROR_COUNT).increment();
throw ex;
} catch (RegistrationNotOpenException ex) {
logger.warn(ErrorConstants.REGISTRATION_NOT_OPEN);
} catch (ConnectionRejectedException ex) {
logger.warn(ErrorConstants.TRANSPORT_REJECTED_CONNECTION);
statisticManager.getStatistic(StatisticNameConstants.INCOMING_TRANSPORT_REJECTED_COUNT).increment();
throw ex;
} catch (AuthenticationException ex) {
logger.warn(ErrorConstants.NOT_AUTHENTICATED);
} catch (Throwable e) {
if (dataLoader != null && status != null) {
if (e instanceof IOException || e instanceof TransportException) {
logger.warn("Failed to load batch " + status.getNodeBatchId() + " because: " + e.getMessage());
history.setSqlMessage(e.getMessage());
statisticManager.getStatistic(StatisticNameConstants.INCOMING_TRANSPORT_ERROR_COUNT).increment();
} else {
logger.error("Failed to load batch " + status.getNodeBatchId(), e);
SQLException se = unwrapSqlException(e);
if (se != null) {
statisticManager.getStatistic(StatisticNameConstants.INCOMING_DATABASE_ERROR_COUNT).increment();
history.setSqlState(se.getSQLState());
history.setSqlCode(se.getErrorCode());
history.setSqlMessage(se.getMessage());
} else {
history.setSqlMessage(e.getMessage());
statisticManager.getStatistic(StatisticNameConstants.INCOMING_OTHER_ERROR_COUNT).increment();
}
}
history.setValues(dataLoader.getStatistics(), false);
handleBatchError(status, history);
} else {
if (e instanceof IOException) {
logger.error("Failed while reading batch because: " + e.getMessage());
} else {
logger.error("Failed while parsing batch.", e);
}
}
} finally {
if (dataLoader != null) {
dataLoader.close();
}
transport.close();
recordStatistics(list);
}
return list;
}
protected void estimateNetworkMillis(List<IncomingBatchHistory> list, long totalNetworkMillis) {
long totalNumberOfBytes = 0;
for (IncomingBatchHistory incomingBatchHistory : list) {
totalNumberOfBytes += incomingBatchHistory.getByteCount();
}
for (IncomingBatchHistory incomingBatchHistory : list) {
if (totalNumberOfBytes > 0) {
double ratio = (double)incomingBatchHistory.getByteCount()/(double)totalNumberOfBytes;
incomingBatchHistory.setNetworkMillis((long)(totalNetworkMillis*ratio));
}
}
}
protected IIncomingTransport writeToFile(IIncomingTransport transport) throws IOException {
ThresholdFileWriter writer = null;
try {
writer = new ThresholdFileWriter(parameterService.getLong(ParameterConstants.STREAM_TO_FILE_THRESHOLD), "load");
IOUtils.copy(transport.open(), writer);
} finally {
IOUtils.closeQuietly(writer);
transport.close();
}
return new FileIncomingTransport(writer);
}
private void recordStatistics(List<IncomingBatchHistory> list) {
if (list != null) {
statisticManager.getStatistic(StatisticNameConstants.INCOMING_BATCH_COUNT).add(list.size());
for (IncomingBatchHistory incomingBatchHistory : list) {
statisticManager.getStatistic(StatisticNameConstants.INCOMING_MS_PER_ROW).add(
incomingBatchHistory.getDatabaseMillis(), incomingBatchHistory.getStatementCount());
statisticManager.getStatistic(StatisticNameConstants.INCOMING_BATCH_COUNT).increment();
if (IncomingBatchHistory.Status.SK.equals(incomingBatchHistory.getStatus())) {
statisticManager.getStatistic(StatisticNameConstants.INCOMING_SKIP_BATCH_COUNT).increment();
}
}
}
}
public boolean loadData(IIncomingTransport transport) throws IOException {
boolean inError = false;
List<IncomingBatchHistory> list = loadDataAndReturnBatches(transport);
if (list != null && list.size() > 0) {
for (IncomingBatchHistory incomingBatchHistory : list) {
inError |= incomingBatchHistory.getStatus() != Status.OK;
}
} else {
inError = true;
}
return !inError;
}
protected void loadBatch(final IDataLoader dataLoader, final IncomingBatch status,
final IncomingBatchHistory history) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus transactionstatus) {
try {
dbDialect.disableSyncTriggers(dataLoader.getContext().getNodeId());
if (incomingBatchService.acquireIncomingBatch(status)) {
dataLoader.load();
} else {
history.setStatus(IncomingBatchHistory.Status.SK);
dataLoader.skip();
}
history.setValues(dataLoader.getStatistics(), true);
fireBatchComplete(dataLoader, history);
} catch (IOException e) {
throw new TransportException(e);
} finally {
dbDialect.enableSyncTriggers();
}
}
});
}
private void fireBatchComplete(IDataLoader loader, IncomingBatchHistory history) {
if (batchListeners != null) {
long ts = System.currentTimeMillis();
for (IBatchListener listener : batchListeners) {
listener.batchComplete(loader, history);
}
// update the filter milliseconds so batch listeners are also
// included
history.setFilterMillis(history.getFilterMillis() + (System.currentTimeMillis() - ts));
}
}
protected void handleBatchError(final IncomingBatch status, final IncomingBatchHistory history) {
try {
if (!status.isRetry()) {
status.setStatus(IncomingBatch.Status.ER);
incomingBatchService.insertIncomingBatch(status);
}
} catch (Exception e) {
logger.error("Failed to record status of batch " + status.getNodeBatchId());
}
try {
history.setStatus(IncomingBatchHistory.Status.ER);
incomingBatchService.insertIncomingBatchHistory(history);
} catch (Exception e) {
logger.error("Failed to record history of batch " + status.getNodeBatchId());
}
}
/**
* Load database from input stream and write acknowledgment to output
* stream. This is used for a "push" request with a response of an
* acknowledgment.
*
* @param in
* @param out
* @throws IOException
*/
public void loadData(InputStream in, OutputStream out) throws IOException {
List<IncomingBatchHistory> list = loadDataAndReturnBatches(new InternalIncomingTransport(in));
transportManager.writeAcknowledgement(out, list);
}
public void setDataLoaderFilters(List<IDataLoaderFilter> filters) {
this.filters = filters;
}
public void addDataLoaderFilter(IDataLoaderFilter filter) {
if (filters == null) {
filters = new ArrayList<IDataLoaderFilter>();
}
filters.add(filter);
}
public void removeDataLoaderFilter(IDataLoaderFilter filter) {
filters.remove(filter);
}
public void setTransportManager(ITransportManager remoteService) {
this.transportManager = remoteService;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void setIncomingBatchService(IIncomingBatchService incomingBatchService) {
this.incomingBatchService = incomingBatchService;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void setDbDialect(IDbDialect dbDialect) {
this.dbDialect = dbDialect;
}
public void addColumnFilter(String tableName, IColumnFilter filter) {
this.columnFilters.put(tableName, filter);
}
public void setStatisticManager(IStatisticManager statisticManager) {
this.statisticManager = statisticManager;
}
public void addBatchListener(IBatchListener batchListener) {
if (this.batchListeners == null) {
this.batchListeners = new ArrayList<IBatchListener>();
}
this.batchListeners.add(batchListener);
}
public void setBatchListeners(List<IBatchListener> batchListeners) {
this.batchListeners = batchListeners;
}
public void setNodeService(INodeService nodeService) {
this.nodeService = nodeService;
}
}