From c4df2049a8f8c5e9cbc69f634b0a5747e0677376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 6 Jan 2023 11:42:58 +0100 Subject: [PATCH] fix: Stop OOM crashes in Execution Data pruning (#5095) * fix: Stop OOM crashed in Execution Data pruning Currently while pruning execution data, we are loading all the data in memory. For instances where there are thousands of executions, this causes the container to run out of memory. Since ids is all we need, we should only query for ids. * query for Executions only when ids are actually needed for pruning binary data in default mode the binary data is in the database, and will get pruned along with the executions. --- .../cli/src/WorkflowExecuteAdditionalData.ts | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/WorkflowExecuteAdditionalData.ts b/packages/cli/src/WorkflowExecuteAdditionalData.ts index e94193200b..8a56ce8688 100644 --- a/packages/cli/src/WorkflowExecuteAdditionalData.ts +++ b/packages/cli/src/WorkflowExecuteAdditionalData.ts @@ -64,7 +64,6 @@ import * as WorkflowHelpers from '@/WorkflowHelpers'; import { getWorkflowOwner } from '@/UserManagement/UserManagementHelper'; import { findSubworkflowStart } from '@/utils'; import { PermissionChecker } from './UserManagement/PermissionChecker'; -import { eventBus } from './eventbus'; import { WorkflowsService } from './workflows/workflows.services'; const ERROR_TRIGGER_TYPE = config.getEnv('nodes.errorTriggerType'); @@ -204,18 +203,24 @@ async function pruneExecutionData(this: WorkflowHooks): Promise { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const utcDate = DateUtils.mixedDateToUtcDatetimeString(date); + const toPrune = { stoppedAt: LessThanOrEqual(utcDate) }; + const isBinaryModeDefaultMode = config.getEnv('binaryDataManager.mode') === 'default'; try { - const executions = await Db.collections.Execution.find({ - stoppedAt: LessThanOrEqual(utcDate), - }); - await Db.collections.Execution.delete({ stoppedAt: LessThanOrEqual(utcDate) }); + const executions = isBinaryModeDefaultMode + ? [] + : await Db.collections.Execution.find({ + select: ['id'], + where: toPrune, + }); + await Db.collections.Execution.delete(toPrune); setTimeout(() => { throttling = false; }, timeout * 1000); // Mark binary data for deletion for all executions - await BinaryDataManager.getInstance().markDataForDeletionByExecutionIds( - executions.map(({ id }) => id), - ); + if (!isBinaryModeDefaultMode) + await BinaryDataManager.getInstance().markDataForDeletionByExecutionIds( + executions.map(({ id }) => id), + ); } catch (error) { ErrorReporter.error(error); throttling = false;