/
AcknowledgeService.java
314 lines (291 loc) · 17.1 KB
/
AcknowledgeService.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
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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.jumpmind.symmetric.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.db.sql.mapper.NumberMapper;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ErrorConstants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.io.stage.IStagedResource;
import org.jumpmind.symmetric.model.AbstractBatch.Status;
import org.jumpmind.symmetric.model.BatchAck;
import org.jumpmind.symmetric.model.BatchAckResult;
import org.jumpmind.symmetric.model.Channel;
import org.jumpmind.symmetric.model.OutgoingBatch;
import org.jumpmind.symmetric.model.OutgoingBatches;
import org.jumpmind.symmetric.service.IAcknowledgeService;
import org.jumpmind.symmetric.service.IOutgoingBatchService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.jumpmind.symmetric.statistic.RouterStats;
import org.jumpmind.symmetric.transport.IAcknowledgeEventListener;
/**
* @see IAcknowledgeService
*/
public class AcknowledgeService extends AbstractService implements IAcknowledgeService {
private ISymmetricEngine engine;
public AcknowledgeService(ISymmetricEngine engine) {
super(engine.getParameterService(), engine.getSymmetricDialect());
this.engine = engine;
setSqlMap(new AcknowledgeServiceSqlMap(symmetricDialect.getPlatform(), createSqlReplacementTokens()));
}
public BatchAckResult ack(final BatchAck batch) {
IRegistrationService registrationService = engine.getRegistrationService();
IOutgoingBatchService outgoingBatchService = engine.getOutgoingBatchService();
BatchAckResult result = new BatchAckResult(batch);
for (IAcknowledgeEventListener listener : engine.getExtensionService().getExtensionPointList(IAcknowledgeEventListener.class)) {
listener.onAcknowledgeEvent(batch);
}
if (batch.getBatchId() == Constants.VIRTUAL_BATCH_FOR_REGISTRATION) {
if (batch.isOk()) {
registrationService.markNodeAsRegistered(batch.getNodeId());
}
} else if (batch.getBatchId() != Constants.BATCH_ID_MISSING) {
OutgoingBatch outgoingBatch = outgoingBatchService.findOutgoingBatch(batch.getBatchId(), batch.getNodeId());
Status status = batch.isResend() ? Status.RS : batch.isOk() ? Status.OK : Status.ER;
if (outgoingBatch != null && outgoingBatch.getStatus() != Status.RQ) {
// Allow an outside system/user to indicate that a batch is OK
if (outgoingBatch.getStatus() == Status.IG && status == Status.OK) {
log.info("Ignoring batch {}", outgoingBatch.getNodeBatchId());
} else if (outgoingBatch.getStatus() == Status.OK && status != Status.OK) {
log.info("Setting status to ignore for batch {} because status was set to OK by user", outgoingBatch.getNodeBatchId());
status = Status.IG;
}
boolean isFirstTimeAsOkStatus = outgoingBatch.getStatus() != Status.OK && status == Status.OK;
outgoingBatch.setStatus(status);
outgoingBatch.setErrorFlag(status == Status.ER);
outgoingBatch.setNetworkMillis(batch.getNetworkMillis());
outgoingBatch.setFilterMillis(batch.getFilterMillis());
outgoingBatch.setLoadMillis(batch.getLoadMillis());
outgoingBatch.setLoadStartTime(new Date(batch.getStartTime()));
outgoingBatch.setSqlCode(batch.getSqlCode());
outgoingBatch.setSqlState(batch.getSqlState());
outgoingBatch.setSqlMessage(batch.getSqlMessage());
outgoingBatch.setLoadRowCount(batch.getLoadRowCount());
outgoingBatch.setLoadInsertRowCount(batch.getLoadInsertRowCount());
outgoingBatch.setLoadUpdateRowCount(batch.getLoadUpdateRowCount());
outgoingBatch.setTransformLoadMillis(batch.getTransformLoadMillis());
outgoingBatch.setLoadDeleteRowCount(batch.getLoadDeleteRowCount());
outgoingBatch.setFallbackInsertCount(batch.getFallbackInsertCount());
outgoingBatch.setFallbackUpdateCount(batch.getFallbackUpdateCount());
outgoingBatch.setIgnoreRowCount(batch.getIgnoreRowCount());
outgoingBatch.setMissingDeleteCount(batch.getMissingDeleteCount());
outgoingBatch.setSkipCount(batch.getSkipCount());
if (batch.isIgnored()) {
outgoingBatch.incrementIgnoreCount();
}
if (status == Status.OK) {
outgoingBatch.setFailedDataId(0);
outgoingBatch.setFailedLineNumber(0);
}
boolean isNewError = false;
if (status == Status.ER && batch.getErrorLine() != 0) {
if (outgoingBatch.isLoadFlag()) {
isNewError = outgoingBatch.getSentCount() == 1;
} else if (batch.getErrorLine() != outgoingBatch.getFailedLineNumber()){
String sql = getSql("selectDataIdSql");
if (parameterService.is(ParameterConstants.DBDIALECT_ORACLE_SEQUENCE_NOORDER, false)) {
sql = getSql("selectDataIdByCreateTimeSql");
} else if (parameterService.is(ParameterConstants.ROUTING_DATA_READER_ORDER_BY_DATA_ID_ENABLED, true)) {
sql += getSql("orderByDataId");
}
List<Number> ids = sqlTemplateDirty.query(sql, new NumberMapper(), outgoingBatch.getBatchId());
if (ids.size() >= batch.getErrorLine()) {
long failedDataId = ids.get((int) batch.getErrorLine() - 1).longValue();
isNewError = outgoingBatch.getFailedDataId() == 0 || outgoingBatch.getFailedDataId() != failedDataId;
outgoingBatch.setFailedDataId(failedDataId);
}
outgoingBatch.setFailedLineNumber(batch.getErrorLine());
}
}
if (status == Status.ER) {
boolean suppressError = false;
if (isNewError) {
engine.getStatisticManager().incrementDataLoadedOutgoingErrors(outgoingBatch.getChannelId(), 1);
}
if (isNewError && outgoingBatch.getSqlCode() == ErrorConstants.FK_VIOLATION_CODE) {
if (!outgoingBatch.isLoadFlag() && outgoingBatch.getReloadRowCount() == 0 &&
parameterService.is(ParameterConstants.AUTO_RESOLVE_FOREIGN_KEY_VIOLATION)) {
engine.getDataService().reloadMissingForeignKeyRows(outgoingBatch.getBatchId(), outgoingBatch.getNodeId(),
outgoingBatch.getFailedDataId(), outgoingBatch.getFailedLineNumber());
suppressError = true;
}
if (outgoingBatch.isLoadFlag() && parameterService.is(ParameterConstants.AUTO_RESOLVE_FOREIGN_KEY_VIOLATION_REVERSE_RELOAD)) {
suppressError = true;
}
}
if (outgoingBatch.getSqlCode() == ErrorConstants.PROTOCOL_VIOLATION_CODE
&& ErrorConstants.PROTOCOL_VIOLATION_STATE.equals(outgoingBatch.getSqlState())) {
if (outgoingBatch.isLoadFlag()) {
log.info("The batch {} may be corrupt in staging. Not removing the batch because it was a load batch, but you may need to clear the batch from staging manually.",
outgoingBatch.getNodeBatchId());
} else {
IStagedResource resource = engine.getStagingManager().find(Constants.STAGING_CATEGORY_OUTGOING,
outgoingBatch.getStagedLocation(), outgoingBatch.getBatchId());
if (resource != null) {
log.info("The batch {} may be corrupt in staging, so removing it.", outgoingBatch.getNodeBatchId());
resource.delete();
suppressError = isNewError;
}
}
}
if (isNewError && (outgoingBatch.getSqlCode() == ErrorConstants.DEADLOCK_CODE ||
outgoingBatch.getSqlCode() == ErrorConstants.CONFLICT_CODE)) {
suppressError = true;
}
if (suppressError) {
outgoingBatch.setErrorFlag(false);
} else {
log.error("The outgoing batch {} failed: {}{}", outgoingBatch.getNodeBatchId(),
(batch.getSqlCode() != 0 ? "[" + batch.getSqlState() + "," + batch.getSqlCode() + "] " : ""), batch.getSqlMessage());
RouterStats routerStats = engine.getStatisticManager().getRouterStatsByBatch(batch.getBatchId());
if (routerStats != null) {
log.info("Router stats for batch " + outgoingBatch.getBatchId() + ": " + routerStats.toString());
}
}
} else if (status == Status.RS) {
log.info("The outgoing batch {} received resend request", outgoingBatch.getNodeBatchId());
}
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
outgoingBatchService.updateOutgoingBatch(transaction, outgoingBatch);
if (status == Status.OK && isFirstTimeAsOkStatus && outgoingBatch.getLoadId() > 0) {
engine.getDataExtractorService().updateExtractRequestLoadTime(transaction, new Date(), outgoingBatch);
}
transaction.commit();
if (status == Status.OK) {
if (isFirstTimeAsOkStatus) {
engine.getStatisticManager().incrementDataLoadedOutgoing(outgoingBatch.getChannelId(), outgoingBatch.getLoadRowCount());
engine.getStatisticManager().incrementDataBytesLoadedOutgoing(outgoingBatch.getChannelId(), outgoingBatch.getByteCount());
}
if (parameterService.is(ParameterConstants.STREAM_TO_FILE_ENABLED)) {
purgeBatchesFromStaging(outgoingBatch);
}
Channel channel = engine.getConfigurationService().getChannel(outgoingBatch.getChannelId());
if (channel != null && channel.isFileSyncFlag()){
/* Acknowledge the file_sync in case the file needs deleted. */
engine.getFileSyncService().acknowledgeFiles(outgoingBatch);
}
engine.getStatisticManager().removeRouterStatsByBatch(batch.getBatchId());
}
} catch (Error ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} catch (RuntimeException ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
close(transaction);
}
} else if (outgoingBatch == null) {
log.error("Could not find batch {}-{} to acknowledge as {}", new Object[] {batch.getNodeId(), batch.getBatchId(),
status.name()});
result.setOk(false);
}
}
return result;
}
protected void purgeBatchesFromStaging(OutgoingBatch outgoingBatch) {
long threshold = parameterService.getLong(ParameterConstants.INITIAL_LOAD_PURGE_STAGE_IMMEDIATE_THRESHOLD_ROWS);
if (threshold >= 0 && outgoingBatch.isLoadFlag() && !outgoingBatch.isCommonFlag()) {
if (outgoingBatch.getDataRowCount() > threshold) {
IStagedResource resource = engine.getStagingManager().find(Constants.STAGING_CATEGORY_OUTGOING,
outgoingBatch.getStagedLocation(), outgoingBatch.getBatchId());
if (resource != null) {
resource.delete();
}
}
}
long streamToFileThreshold = parameterService.getLong(ParameterConstants.STREAM_TO_FILE_THRESHOLD);
if (streamToFileThreshold > 0 && !outgoingBatch.isCommonFlag() && outgoingBatch.getByteCount() <= streamToFileThreshold) {
IStagedResource resource = engine.getStagingManager().find(Constants.STAGING_CATEGORY_OUTGOING,
outgoingBatch.getStagedLocation(), outgoingBatch.getBatchId());
if (resource != null && resource.isMemoryResource() && !resource.isInUse()) {
resource.delete();
}
}
}
public List<BatchAckResult> ack(List<BatchAck> batches) {
List<BatchAckResult> results = new ArrayList<BatchAckResult>();
for (BatchAck batch:batches) {
results.add(ack(batch));
}
return results;
}
public void checkMissingAck(List<BatchAck> acks, String queue) {
boolean hasCorruptBatch = false;
String nodeId = null;
for (BatchAck batch : acks) {
if (batch.getBatchId() == Constants.BATCH_ID_MISSING) {
hasCorruptBatch = true;
nodeId = batch.getNodeId();
}
}
if (hasCorruptBatch && parameterService.is(ParameterConstants.STREAM_TO_FILE_ENABLED)) {
OutgoingBatches batches = engine.getOutgoingBatchService().getOutgoingBatches(nodeId, queue, false);
if (batches.containsBatches()) {
Map<Long, OutgoingBatch> batchMap = new HashMap<Long, OutgoingBatch>();
for (OutgoingBatch batch : batches.getBatches()) {
if (batch.getStatus() == Status.LD) {
batchMap.put(batch.getBatchId(), batch);
}
}
for (BatchAck ack : acks) {
batchMap.remove(ack.getBatchId());
}
if (batchMap.size() > 0) {
for (OutgoingBatch batch : batchMap.values()) {
StringBuilder message = new StringBuilder(128);
message.append("Expected but did not receive an ack for batch ");
message.append(batch.getNodeBatchId()).append(". ");
if (!batch.isLoadFlag()) {
message.append("This could be because the batch is corrupt - removing the batch from staging.");
log.warn(message.toString());
IStagedResource resource = engine.getStagingManager().find(Constants.STAGING_CATEGORY_OUTGOING,
batch.getStagedLocation(), batch.getBatchId());
if (resource != null) {
resource.delete();
} else {
log.warn("Unable to find outgoing staging file for node {} that has corrupt batch data", nodeId);
}
} else {
message.append(
"This could be because the batch is corrupt. Not removing the batch because it was a load batch, but you may need to clear the batch from staging manually.");
log.warn(message.toString());
}
}
}
} else {
log.warn("Unable to find outgoing batch for node {} that has corrupt batch data", nodeId);
}
}
}
}