/
PurgeService.java
437 lines (387 loc) · 18.7 KB
/
PurgeService.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
/**
* 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.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.time.DateUtils;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.Row;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.model.DataGap;
import org.jumpmind.symmetric.model.ExtractRequest;
import org.jumpmind.symmetric.model.IncomingBatch;
import org.jumpmind.symmetric.model.OutgoingBatch;
import org.jumpmind.symmetric.model.RegistrationRequest;
import org.jumpmind.symmetric.service.ClusterConstants;
import org.jumpmind.symmetric.service.IClusterService;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.service.IPurgeService;
import org.jumpmind.symmetric.statistic.IStatisticManager;
/**
* @see IPurgeService
*/
public class PurgeService extends AbstractService implements IPurgeService {
enum MinMaxDeleteSql {
DATA, DATA_EVENT, OUTGOING_BATCH, STRANDED_DATA
};
private IClusterService clusterService;
private IStatisticManager statisticManager;
public PurgeService(IParameterService parameterService, ISymmetricDialect symmetricDialect,
IClusterService clusterService, IStatisticManager statisticManager) {
super(parameterService, symmetricDialect);
this.clusterService = clusterService;
this.statisticManager = statisticManager;
setSqlMap(new PurgeServiceSqlMap(symmetricDialect.getPlatform(),
createSqlReplacementTokens()));
}
public long purgeOutgoing(boolean force) {
long rowsPurged = 0;
Calendar retentionCutoff = Calendar.getInstance();
retentionCutoff.add(Calendar.MINUTE,
-parameterService.getInt(ParameterConstants.PURGE_RETENTION_MINUTES));
rowsPurged += purgeOutgoing(retentionCutoff, force);
return rowsPurged;
}
public long purgeIncoming(boolean force) {
long rowsPurged = 0;
Calendar retentionCutoff = Calendar.getInstance();
retentionCutoff.add(Calendar.MINUTE,
-parameterService.getInt(ParameterConstants.PURGE_RETENTION_MINUTES));
rowsPurged += purgeIncoming(retentionCutoff, force);
return rowsPurged;
}
public long purgeDataGaps(boolean force) {
long rowsPurged = 0;
Calendar retentionCutoff = Calendar.getInstance();
retentionCutoff.add(Calendar.MINUTE, -parameterService
.getInt(ParameterConstants.ROUTING_DATA_READER_TYPE_GAP_RETENTION_MINUTES));
rowsPurged += purgeDataGaps(retentionCutoff, force);
return rowsPurged;
}
public long purgeDataGaps(Calendar retentionCutoff, boolean force) {
long rowsPurged = -1l;
try {
if (force || clusterService.lock(ClusterConstants.PURGE_DATA_GAPS)) {
try {
log.info("The data gap purge process is about to run");
rowsPurged = sqlTemplate.update(getSql("deleteFromDataGapsSql"), new Object[] {
retentionCutoff.getTime(), DataGap.Status.GP.name() });
log.info("Purged {} data gap rows", rowsPurged);
} finally {
if (!force) {
clusterService.unlock(ClusterConstants.PURGE_DATA_GAPS);
}
log.info("The data gap purge process has completed");
}
} else {
log.warn("Did not run the data gap purge process because the cluster service has it locked");
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
return rowsPurged;
}
public long purgeOutgoing(Calendar retentionCutoff, boolean force) {
long rowsPurged = 0;
try {
if (force || clusterService.lock(ClusterConstants.PURGE_OUTGOING)) {
try {
log.info("The outgoing purge process is about to run for data older than {}",
SimpleDateFormat.getDateTimeInstance()
.format(retentionCutoff.getTime()));
rowsPurged += purgeStrandedBatches();
rowsPurged += purgeDataRows(retentionCutoff);
rowsPurged += purgeOutgoingBatch(retentionCutoff);
rowsPurged += purgeExtractRequests();
} finally {
if (!force) {
clusterService.unlock(ClusterConstants.PURGE_OUTGOING);
}
log.info("The outgoing purge process has completed");
}
} else {
log.debug("Could not get a lock to run an outgoing purge");
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
return rowsPurged;
}
private long purgeOutgoingBatch(final Calendar time) {
log.info("Getting range for outgoing batch");
long[] minMax = queryForMinMax(getSql("selectOutgoingBatchRangeSql"),
new Object[] { time.getTime(), OutgoingBatch.Status.OK.name() });
int maxNumOfBatchIdsToPurgeInTx = parameterService
.getInt(ParameterConstants.PURGE_MAX_NUMBER_OF_BATCH_IDS);
int maxNumOfDataEventsToPurgeInTx = parameterService
.getInt(ParameterConstants.PURGE_MAX_NUMBER_OF_EVENT_BATCH_IDS);
int dataEventsPurgedCount = purgeByMinMax(minMax, MinMaxDeleteSql.DATA_EVENT,
time.getTime(), maxNumOfDataEventsToPurgeInTx);
statisticManager.incrementPurgedDataEventRows(dataEventsPurgedCount);
int outgoingbatchPurgedCount = purgeByMinMax(minMax, MinMaxDeleteSql.OUTGOING_BATCH,
time.getTime(), maxNumOfBatchIdsToPurgeInTx);
statisticManager.incrementPurgedBatchOutgoingRows(outgoingbatchPurgedCount);
return dataEventsPurgedCount + outgoingbatchPurgedCount;
}
private long purgeStrandedBatches() {
int updateStrandedBatchesCount = sqlTemplate.update(getSql("updateStrandedBatches"),
OutgoingBatch.Status.OK.name(), 1, OutgoingBatch.Status.OK.name());
if (updateStrandedBatchesCount > 0) {
log.info(
"Set the status to {} for {} batches that no longer are associated with valid nodes",
OutgoingBatch.Status.OK.name(), updateStrandedBatchesCount);
statisticManager.incrementPurgedBatchOutgoingRows(updateStrandedBatchesCount);
}
return updateStrandedBatchesCount;
}
private long purgeDataRows(final Calendar time) {
log.info("Getting range for data");
long[] minMax = queryForMinMax(getSql("selectDataRangeSql"), new Object[0]);
int maxNumOfDataIdsToPurgeInTx = parameterService
.getInt(ParameterConstants.PURGE_MAX_NUMBER_OF_DATA_IDS);
int dataDeletedCount = purgeByMinMax(minMax, MinMaxDeleteSql.DATA, time.getTime(),
maxNumOfDataIdsToPurgeInTx);
statisticManager.incrementPurgedDataRows(dataDeletedCount);
int strandedDeletedCount = purgeByMinMax(minMax, MinMaxDeleteSql.STRANDED_DATA,
time.getTime(), maxNumOfDataIdsToPurgeInTx);
statisticManager.incrementPurgedDataRows(strandedDeletedCount);
return dataDeletedCount + strandedDeletedCount;
}
private long[] queryForMinMax(String sql, Object... params) {
long[] minMax = sqlTemplate.queryForObject(sql, new ISqlRowMapper<long[]>() {
public long[] mapRow(Row rs) {
return new long[] { rs.getLong("min_id"), rs.getLong("max_id")-1 };
}
}, params);
return minMax;
}
private long purgeExtractRequests() {
Calendar retentionCutoff = Calendar.getInstance();
retentionCutoff.add(Calendar.MINUTE, -parameterService
.getInt(ParameterConstants.PURGE_EXTRACT_REQUESTS_RETENTION_MINUTES));
log.info("Purging extract requests that are older than {}", retentionCutoff.getTime());
long count = sqlTemplate.update(getSql("deleteExtractRequestSql"),
ExtractRequest.ExtractStatus.OK.name(), retentionCutoff.getTime());
if (count > 0) {
log.info("Purged {} extract requests", count);
}
return count;
}
private long purgeRegistrationRequests() {
Calendar retentionCutoff = Calendar.getInstance();
retentionCutoff.add(Calendar.MINUTE, -parameterService
.getInt(ParameterConstants.PURGE_REGISTRATION_REQUEST_RETENTION_MINUTES));
log.info("Purging registration requests that are older than {}", retentionCutoff.getTime());
long count = sqlTemplate.update(getSql("deleteRegistrationRequestSql"),
RegistrationRequest.RegistrationStatus.OK.name(),
RegistrationRequest.RegistrationStatus.IG.name(),
RegistrationRequest.RegistrationStatus.RR.name(), retentionCutoff.getTime());
if (count > 0) {
log.info("Purged {} registration requests", count);
}
return count;
}
private int purgeByMinMax(long[] minMax, MinMaxDeleteSql identifier, Date retentionTime,
int maxNumtoPurgeinTx) {
long minId = minMax[0];
long purgeUpToId = minMax[1];
long ts = System.currentTimeMillis();
int totalCount = 0;
int totalDeleteStmts = 0;
int idSqlType = symmetricDialect.getSqlTypeForIds();
Timestamp cutoffTime = new Timestamp(retentionTime.getTime());
log.info("About to purge {}", identifier.toString().toLowerCase());
while (minId <= purgeUpToId) {
totalDeleteStmts++;
long maxId = minId + maxNumtoPurgeinTx;
if (maxId > purgeUpToId) {
maxId = purgeUpToId;
}
String deleteSql = null;
Object[] args = null;
int[] argTypes = null;
switch (identifier) {
case DATA:
deleteSql = getSql("deleteDataSql");
args = new Object[] { minId, maxId, cutoffTime, minId, maxId, minId, maxId,
OutgoingBatch.Status.OK.name() };
argTypes = new int[] { idSqlType, idSqlType, Types.TIMESTAMP,
idSqlType, idSqlType, idSqlType, idSqlType, Types.VARCHAR};
break;
case DATA_EVENT:
deleteSql = getSql("deleteDataEventSql");
args = new Object[] { minId, maxId, OutgoingBatch.Status.OK.name(), minId,
maxId };
argTypes = new int[] { idSqlType, idSqlType, Types.VARCHAR, idSqlType, idSqlType};
break;
case OUTGOING_BATCH:
deleteSql = getSql("deleteOutgoingBatchSql");
args = new Object[] { OutgoingBatch.Status.OK.name(), minId, maxId, minId,
maxId };
argTypes = new int[] {Types.VARCHAR, idSqlType, idSqlType, idSqlType, idSqlType};
break;
case STRANDED_DATA:
deleteSql = getSql("deleteStrandedData");
args = new Object[] { minId, maxId, cutoffTime, minId, maxId };
argTypes = new int[] { idSqlType, idSqlType, Types.TIMESTAMP, idSqlType, idSqlType};
break;
}
log.debug("Running the following statement: {} with the following arguments: {}", deleteSql, Arrays.toString(args));
int count = sqlTemplate.update(deleteSql, args, argTypes);
log.debug("Deleted {} rows", count);
totalCount += count;
if (totalCount > 0
&& (System.currentTimeMillis() - ts > DateUtils.MILLIS_PER_MINUTE * 5)) {
log.info("Purged {} of {} rows so far using {} statements", new Object[] {
totalCount, identifier.toString().toLowerCase(), totalDeleteStmts });
ts = System.currentTimeMillis();
}
minId = maxId + 1;
}
log.info("Done purging {} of {} rows", totalCount, identifier.toString().toLowerCase());
return totalCount;
}
public long purgeIncoming(Calendar retentionCutoff, boolean force) {
long purgedRowCount = 0;
try {
if (force || clusterService.lock(ClusterConstants.PURGE_INCOMING)) {
try {
log.info("The incoming purge process is about to run");
purgedRowCount = purgeIncomingBatch(retentionCutoff);
purgedRowCount += purgeIncomingError();
purgedRowCount += purgeRegistrationRequests();
} finally {
if (!force) {
clusterService.unlock(ClusterConstants.PURGE_INCOMING);
}
log.info("The incoming purge process has completed");
}
} else {
log.debug("Could not get a lock to run an incoming purge");
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
return purgedRowCount;
}
private long purgeIncomingError() {
log.info("Purging incoming error rows");
long rowCount = sqlTemplate.update(getSql("deleteIncomingErrorsSql"));
log.info("Purged {} incoming error rows", rowCount);
return rowCount;
}
private long purgeIncomingBatch(final Calendar time) {
log.info("Getting range for incoming batch");
List<NodeBatchRange> nodeBatchRangeList = sqlTemplate.query(
getSql("selectIncomingBatchRangeSql"), new ISqlRowMapper<NodeBatchRange>() {
public NodeBatchRange mapRow(Row rs) {
return new NodeBatchRange(rs.getString("node_id"), rs.getLong("min_id"), rs
.getLong("max_id"));
}
}, time.getTime(), IncomingBatch.Status.OK.name());
int incomingBatchesPurgedCount = purgeByNodeBatchRangeList(nodeBatchRangeList);
statisticManager.incrementPurgedBatchIncomingRows(incomingBatchesPurgedCount);
return incomingBatchesPurgedCount;
}
private int purgeByNodeBatchRangeList(List<NodeBatchRange> nodeBatchRangeList) {
long ts = System.currentTimeMillis();
int totalCount = 0;
int totalDeleteStmts = 0;
log.info("About to purge incoming batch");
for (NodeBatchRange nodeBatchRange : nodeBatchRangeList) {
int maxNumOfDataIdsToPurgeInTx = parameterService
.getInt(ParameterConstants.PURGE_MAX_NUMBER_OF_BATCH_IDS);
long minBatchId = nodeBatchRange.getMinBatchId();
long purgeUpToBatchId = nodeBatchRange.getMaxBatchId();
while (minBatchId <= purgeUpToBatchId) {
totalDeleteStmts++;
long maxBatchId = minBatchId + maxNumOfDataIdsToPurgeInTx;
if (maxBatchId > purgeUpToBatchId) {
maxBatchId = purgeUpToBatchId;
}
totalCount += sqlTemplate.update(getSql("deleteIncomingBatchSql"),
new Object[] { minBatchId, maxBatchId, nodeBatchRange.getNodeId(),
IncomingBatch.Status.OK.name() });
minBatchId = maxBatchId + 1;
}
if (totalCount > 0
&& (System.currentTimeMillis() - ts > DateUtils.MILLIS_PER_MINUTE * 5)) {
log.info("Purged {} incoming batch rows so far using {} statements", new Object[] {
totalCount, totalDeleteStmts });
ts = System.currentTimeMillis();
}
}
log.info("Done purging {} incoming batch rows", totalCount);
return totalCount;
}
public void purgeStats(boolean force) {
Calendar retentionCutoff = Calendar.getInstance();
retentionCutoff.add(Calendar.MINUTE,
-parameterService.getInt(ParameterConstants.PURGE_STATS_RETENTION_MINUTES));
if (force || clusterService.lock(ClusterConstants.PURGE_STATISTICS)) {
try {
int purgedCount = sqlTemplate.update(getSql("purgeNodeHostChannelStatsSql"),
retentionCutoff.getTime());
purgedCount += sqlTemplate.update(getSql("purgeNodeHostStatsSql"),
retentionCutoff.getTime());
purgedCount += sqlTemplate.update(getSql("purgeNodeHostJobStatsSql"),
retentionCutoff.getTime());
if (purgedCount > 0) {
log.info("{} stats rows were purged", purgedCount);
}
} finally {
if (!force) {
clusterService.unlock(ClusterConstants.PURGE_STATISTICS);
}
}
}
}
class NodeBatchRange {
private String nodeId;
private long minBatchId;
private long maxBatchId;
public NodeBatchRange(String nodeId, long minBatchId, long maxBatchId) {
this.nodeId = nodeId;
this.minBatchId = minBatchId;
this.maxBatchId = maxBatchId;
}
public String getNodeId() {
return nodeId;
}
public long getMaxBatchId() {
return maxBatchId;
}
public long getMinBatchId() {
return minBatchId;
}
}
public void purgeAllIncomingEventsForNode(String nodeId) {
int count = sqlTemplate.update(getSql("deleteIncomingBatchByNodeSql"),
new Object[] { nodeId });
log.info("Purged all {} incoming batch for node {}", count, nodeId);
}
}