From 9c8b63a878162e1c13110883fba3adc5067eb091 Mon Sep 17 00:00:00 2001 From: Jordan <51442161+JordanSh@users.noreply.github.com> Date: Thu, 6 Jul 2023 19:02:39 +0300 Subject: [PATCH] [Cloud Security] Restarting transform on failure (#161192) --- .../create_transforms.test.ts | 18 +++++++++++++++++- .../create_transforms/create_transforms.ts | 18 +++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.test.ts b/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.test.ts index e701e7c94cada..980c47c2e6c4a 100644 --- a/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.test.ts +++ b/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.test.ts @@ -82,7 +82,7 @@ describe('startTransformIfNotStarted', () => { jest.resetAllMocks(); }); - ['failed', 'stopping', 'started', 'aborting', 'indexing'].forEach((state) => + ['stopping', 'started', 'aborting', 'indexing'].forEach((state) => it(`expect not to start if state is ${state}`, async () => { mockEsClient.transform.getTransformStats.mockResolvedValue({ transforms: [getTransformWithState(state)], @@ -125,4 +125,20 @@ describe('startTransformIfNotStarted', () => { transform_id: latestFindingsTransform.transform_id, }); }); + + it('expect to attempt restart if state is failed', async () => { + mockEsClient.transform.getTransformStats.mockResolvedValue({ + transforms: [getTransformWithState('failed')], + count: 1, + }); + await startTransformIfNotStarted(mockEsClient, latestFindingsTransform.transform_id, logger); + expect(mockEsClient.transform.getTransformStats).toHaveBeenCalledTimes(1); + expect(mockEsClient.transform.getTransformStats).toHaveBeenCalledWith({ + transform_id: latestFindingsTransform.transform_id, + }); + expect(mockEsClient.transform.startTransform).toHaveBeenCalledTimes(1); + expect(mockEsClient.transform.startTransform).toHaveBeenCalledWith({ + transform_id: latestFindingsTransform.transform_id, + }); + }); }); diff --git a/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.ts b/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.ts index 83430d1d9f507..e93692d9e1a58 100644 --- a/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.ts +++ b/x-pack/plugins/cloud_security_posture/server/create_transforms/create_transforms.ts @@ -85,23 +85,27 @@ export const startTransformIfNotStarted = async ( const transformStats = await esClient.transform.getTransformStats({ transform_id: transformId, }); + if (transformStats.count <= 0) { logger.error(`Failed starting transform ${transformId}: couldn't find transform`); return; } + const fetchedTransformStats = transformStats.transforms[0]; - if (fetchedTransformStats.state === 'stopped') { + + // trying to restart the transform in case it comes to a full stop or failure + if (fetchedTransformStats.state === 'stopped' || fetchedTransformStats.state === 'failed') { try { return await esClient.transform.startTransform({ transform_id: transformId }); } catch (startErr) { const startError = transformError(startErr); - logger.error(`Failed starting transform ${transformId}: ${startError.message}`); + logger.error( + `Failed to start transform ${transformId}. Transform State: Transform State: ${fetchedTransformStats.state}. Error: ${startError.message}` + ); } - } else if ( - fetchedTransformStats.state === 'stopping' || - fetchedTransformStats.state === 'aborting' || - fetchedTransformStats.state === 'failed' - ) { + } + + if (fetchedTransformStats.state === 'stopping' || fetchedTransformStats.state === 'aborting') { logger.error( `Not starting transform ${transformId} since it's state is: ${fetchedTransformStats.state}` );