-
Notifications
You must be signed in to change notification settings - Fork 982
Expand file tree
/
Copy pathdeletionManagerJob.js
More file actions
445 lines (414 loc) · 17.9 KB
/
deletionManagerJob.js
File metadata and controls
445 lines (414 loc) · 17.9 KB
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
const common = require('../utils/common.js');
const log = require("../utils/log.js")("job:deletionManager");
const Job = require("../../jobServer/Job.js");
const deletionManager = require('../utils/deletionManager.js');
let clickHouseRunner;
try {
clickHouseRunner = require('../../plugins/clickhouse/api/queries/clickhouseCoreQueries.js');
}
catch {
//
}
let chHealth = null;
try {
chHealth = require('../../plugins/clickhouse/api/health.js');
}
catch {
//
}
// Constants for managing stale tasks and retries
const STALE_MS = 24 * 60 * 60 * 1000; // 24h - consider tasks running longer than this as stale
const RETRY_DELAY_MS = 30 * 60 * 1000; // 30m - delay before retrying a failed task
const MAX_RETRIES = 3; // Max number of retries before marking a task as failed
const VALIDATION_INTERVAL_MS = 3 * 60 * 1000; // 3m - interval between mutation status checks
const BATCH_LIMIT = 10; // Number of tasks to process in one run
/** Class for the deletion manager job **/
class DeletionManagerJob extends Job {
/**
* Get schedule for the job
* @returns {GetScheduleConfig} Schedule configuration objectd
*/
getSchedule() {
return {
type: "schedule",
value: "* * * * *" // Every minute
};
}
/**
* Get concurrency for the job - do not allow run in parallel
* @returns {number} Concurrency level
*/
getConcurrency() {
return 1;
}
/**
* Run the job
* @param {done} done callback
*/
async run() {
const now = Date.now();
const batchId = common.db.ObjectID() + '';
const summary = [];
try {
if (deletionManager.isClickhouseEnabled()) {
const pre = await this.shouldDeferDueToClickhousePressure();
if (pre && pre.defer) {
log.d("Run deferred due to ClickHouse pressure", pre || {});
return [{ status: pre.reason || "deferred_due_to_ch_pressure", metrics: pre.metrics, utilization: pre.utilization, thresholds: pre.thresholds }];
}
else if (pre && (pre.risk_level === 'moderate')) {
log.d('ClickHouse pressure moderate; proceeding', { utilization: pre.utilization, thresholds: pre.thresholds });
}
}
}
catch (e) {
log.e("Global CH precheck failed; proceeding", e?.message || e + "");
}
// Reset stale tasks (running > 24h)
await this.resetStaleTasks();
// Reserve a batch of records to process
await common.db.collection("deletion_manager").aggregate([
{
$match: {
running: false,
status: deletionManager.DELETION_STATUS.QUEUED,
$or: [{ retry_at: { $exists: false } }, { retry_at: null }, { retry_at: { $lte: now } }]
}
},
{ $sort: { ts: 1 } },
{ $limit: BATCH_LIMIT },
{ $set: { running: true, status: deletionManager.DELETION_STATUS.RUNNING, hb: now, error: null, batch_id: batchId } },
{
$merge: {
into: "deletion_manager",
on: "_id",
whenMatched: "merge",
whenNotMatched: "discard"
}
}
]).toArray();
// Fetch and process the reserved batch
const toProcess = await common.db.collection("deletion_manager")
.find({ batch_id: batchId, running: true, status: deletionManager.DELETION_STATUS.RUNNING })
.sort({ ts: 1 })
.toArray();
for (const task of toProcess) {
try {
if (task.db === "countly_drill" && task.collection === "drill_events") {
const mongoOk = await this.deleteMongo(task);
let chScheduledOk = true;
const clickhouseEnabled = deletionManager.isClickhouseEnabled();
const hasClickhouse = clickhouseEnabled && !!(clickHouseRunner && clickHouseRunner.deleteGranularDataByQuery);
if (hasClickhouse) {
chScheduledOk = await this.deleteClickhouse(task);
}
if (mongoOk && hasClickhouse && chScheduledOk) {
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: {
running: false,
status: deletionManager.DELETION_STATUS.AWAITING_CH_MUTATION_VALIDATION,
hb: Date.now(),
retry_at: Date.now() + VALIDATION_INTERVAL_MS
},
$unset: { batch_id: "" }
}
);
summary.push({ query: task.query, status: "awaiting_validation" });
}
else if (mongoOk && !hasClickhouse) {
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: {
running: false,
status: deletionManager.DELETION_STATUS.DELETED,
hb: Date.now(),
deletion_completion_ts: new Date()
},
$unset: { batch_id: "" }
}
);
summary.push({ query: task.query, status: "deleted_marked" });
}
else {
summary.push({ query: task.query, status: "error" });
}
}
}
catch (e) {
await this.markFailedOrRetry(task, (e && e.message) || e);
summary.push({ task: task.query, status: "error", error: (e && e.message) || e });
log.e("Deletion task failed", task._id, e);
}
}
if (deletionManager.isClickhouseEnabled()) {
await this.processAwaitingValidation(summary);
}
return summary;
}
/**
* Processes a batch of tasks awaiting ClickHouse mutation validation.
* Reserves a batch, checks mutation status via system.mutations and updates/deletes tasks accordingly.
* @param {Array} summary - Summary array to push status logs
* @returns {Promise<void>} Resolves when awaiting validation batch is processed
*/
async processAwaitingValidation(summary) {
const validationBatchId = common.db.ObjectID() + '';
const nowTs = Date.now();
await common.db.collection("deletion_manager").aggregate([
{
$match: {
running: false,
status: deletionManager.DELETION_STATUS.AWAITING_CH_MUTATION_VALIDATION,
$or: [{ retry_at: { $exists: false } }, { retry_at: null }, { retry_at: { $lte: nowTs } }]
}
},
{ $sort: { ts: 1 } },
{ $limit: BATCH_LIMIT },
{ $set: { running: true, hb: nowTs, batch_id: validationBatchId } },
{
$merge: {
into: "deletion_manager",
on: "_id",
whenMatched: "merge",
whenNotMatched: "discard"
}
}
]).toArray();
const awaiting = await common.db.collection("deletion_manager")
.find({ batch_id: validationBatchId, running: true, status: deletionManager.DELETION_STATUS.AWAITING_CH_MUTATION_VALIDATION })
.sort({ ts: 1 })
.toArray();
for (const task of awaiting) {
try {
if (task.db === "countly_drill" && task.collection === "drill_events" && chHealth && typeof chHealth.getMutationStatus === 'function') {
const status = await chHealth.getMutationStatus({ validation_command_id: task.validation_command_id, table: task.collection, database: task.db });
if (status && status.is_done) {
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: {
running: false,
status: deletionManager.DELETION_STATUS.DELETED,
hb: Date.now(),
deletion_completion_ts: new Date()
},
$unset: { batch_id: "" }
}
);
summary.push({ query: task.query, status: "validated_deleted_marked" });
}
else if (status && (status.is_killed || status.latest_fail_reason)) {
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: { running: false, status: deletionManager.DELETION_STATUS.FAILED, hb: Date.now(), error: status.latest_fail_reason || "mutation_killed" },
$unset: { batch_id: "" }
}
);
summary.push({ query: task.query, status: "failed", error: status.latest_fail_reason || "mutation_killed" });
}
else {
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: { running: false, status: deletionManager.DELETION_STATUS.AWAITING_CH_MUTATION_VALIDATION, hb: Date.now(), retry_at: Date.now() + VALIDATION_INTERVAL_MS },
$unset: { batch_id: "" }
}
);
summary.push({ query: task.query, status: "mutation_pending_recheck_scheduled" });
}
}
}
catch (e) {
log.e("Validation step failed", task._id, e?.message || e + "");
await this.markFailedOrRetry(task, "ch_process_awating_validation: " + e?.message || e + "");
}
}
}
/**
* Reset stale deletion tasks that have been running longer than STALE_MS.
*/
async resetStaleTasks() {
try {
const now = Date.now();
await common.db.collection("deletion_manager").updateMany(
{
running: true,
$expr: { $lt: [{ $ifNull: ["$hb", "$ts"] }, now - STALE_MS] }
},
{
$set: { running: false, status: deletionManager.DELETION_STATUS.QUEUED, hb: now, error: "stale_reset" },
$unset: { batch_id: "" }
}
);
}
catch (e) {
log.e("resetStaleTasks failed", e?.message || String(e));
}
}
/**
* Delete documents from MongoDB
* @param {Object} task - The deletion task
*/
async deleteMongo(task) {
if (!task.query || !Object.keys(task.query).length) {
await this.markFailedOrRetry(task, "empty_mongo_query");
return false;
}
let res;
const start = Date.now();
try {
res = await common.drillDb.collection(task.collection).deleteMany(task.query || {});
}
catch (err) {
const duration = Date.now() - start;
log.e("Mongo deletion failed", { taskId: task._id, durationMs: duration, error: (err?.message || err + "") });
await this.markFailedOrRetry(task, "mongo_delete_error: " + (err?.message || err + ""));
return false;
}
const duration = Date.now() - start;
log.d("Mongo deletion done", { taskId: task._id, deletedCount: res?.deletedCount || 0, durationMs: duration });
return true;
}
/**
* Deletes data from Clickhouse based on provided parameters.
* @param {object} task - Task object containing deletion parameters.
* @returns {Object} query runner result
*/
async deleteClickhouse(task) {
if (!common.queryRunner) {
log.e("queryRunner not available, skipping ClickHouse delete", { taskId: task._id });
await this.markFailedOrRetry(task, "queryRunner_unavailable");
return false;
}
if (!task.query || !Object.keys(task.query).length) {
log.e("Skipping ClickHouse delete (empty query)", { taskId: task._id });
await this.markFailedOrRetry(task, "empty_ch_query");
return false;
}
const queryDef = {
name: 'DELETE_GRANULAR_DATA',
adapters: {
clickhouse: {
handler: clickHouseRunner.deleteGranularDataByQuery
}
}
};
try {
const retryIndex = Number(task.fail_count || 0);
const commandId = `dm_${String(task._id)}_${retryIndex}`;
await common.queryRunner.executeQuery(
queryDef,
{ queryObj: task.query, targetTable: task.collection, validation_command_id: commandId },
{}
);
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{ $set: { validation_command_id: commandId } }
);
log.d("ClickHouse deletion scheduled (runner)", { taskId: task._id, commandId });
return true;
}
catch (err) {
log.e("ClickHouse deletion failed (runner)", {
taskId: task._id,
error: err && err.message ? err.message : String(err)
});
await this.markFailedOrRetry(task, "clickhouse_delete_error: " + (err?.message || err + ""));
return false;
}
}
/**
* Marks a task as failed or schedules it for a retry based on the number of previous failures.
* @param {Object} task - The task object to update.
* @param {string} message - The error message to log
*/
async markFailedOrRetry(task, message) {
try {
const now = Date.now();
const failCount = (task.fail_count || 0) + 1;
if (failCount >= MAX_RETRIES) {
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: { running: false, status: deletionManager.DELETION_STATUS.FAILED, fail_count: failCount, error: message, hb: now },
$unset: { batch_id: "" }
}
);
}
else {
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: { running: false, status: deletionManager.DELETION_STATUS.QUEUED, fail_count: failCount, error: message, retry_at: now + RETRY_DELAY_MS, hb: now },
$unset: { batch_id: "" }
}
);
}
}
catch (e) {
log.e("markFailedOrRetry fallback", { taskId: task._id, err: (e && e.message) || e });
await common.db.collection("deletion_manager").updateOne(
{ _id: task._id },
{
$set: { running: false, status: deletionManager.DELETION_STATUS.FAILED, error: message, hb: Date.now() },
$unset: { batch_id: "" }
}
);
}
}
/**
* Fetches ClickHouse load metrics to determine if deletion should be deferred.
* @returns {Promise<Object|null>} Object with metrics or null if fetch failed
*/
async getClickhouseLoadMetrics() {
if (!chHealth || typeof chHealth.getOperationalSnapshot !== 'function') {
return null;
}
try {
const snapshot = await chHealth.getOperationalSnapshot({ database: 'countly_drill', table: 'drill_events' });
return snapshot ? {
max_parts_per_partition: snapshot.max_parts_per_partition,
total_merge_tree_parts: snapshot.total_merge_tree_parts
} : null;
}
catch (e) {
log.e('CH health metrics fetch failed', e?.message || e + "");
return null;
}
}
/**
* Fetches ClickHouse load metrics to determine if a task should be deferred.
* @returns {object} Whether to defer due to ClickHouse pressure
*/
async shouldDeferDueToClickhousePressure() {
const m = await this.getClickhouseLoadMetrics();
if (!m) {
return { defer: false };
}
if (chHealth && typeof chHealth.getBackpressureSnapshot === 'function') {
try {
const bp = await chHealth.getBackpressureSnapshot({
max_parts_per_partition: m.max_parts_per_partition,
total_merge_tree_parts: m.total_merge_tree_parts
});
const defer = bp.deferred_due_to_clickhouse || bp.risk_level === 'high';
return {
defer,
reason: bp.defer_reason || (bp.risk_level && `risk_${bp.risk_level}`) || undefined,
metrics: m,
utilization: bp.utilization,
thresholds: bp.thresholds,
risk_level: bp.risk_level
};
}
catch (e) {
log.e('Backpressure snapshot failed.', e?.message || e + "");
}
}
}
}
module.exports = DeletionManagerJob;