From 7445556dda7b5e3a3a5f8f2383a589e722e10161 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Wed, 31 May 2023 14:21:38 +0200 Subject: [PATCH 1/3] Set retry timeouts randomise false for es initialisation --- x-pack/plugins/event_log/server/es/init.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/event_log/server/es/init.ts b/x-pack/plugins/event_log/server/es/init.ts index da98a18e9d758..87d623050cb53 100644 --- a/x-pack/plugins/event_log/server/es/init.ts +++ b/x-pack/plugins/event_log/server/es/init.ts @@ -52,7 +52,7 @@ function getRetryOptions(esContext: EsContext, operation: string) { maxTimeout: MAX_RETRY_DELAY, retries: 4, factor: 2, - randomize: true, + randomize: false, onFailedAttempt: (err: FailedAttemptError) => { const message = `eventLog initialization operation failed and will be retried: ${operation}; ${err.retriesLeft} more times; error: ${err.message}`; logger.warn(message); From 6beb17f28e8cbec1aba61c1fe3d370b8becf8e7c Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Wed, 31 May 2023 15:43:43 +0200 Subject: [PATCH 2/3] [Event Log] Make retryDelay in mock 1ms less to cover JS time difference calculation discrepancy --- x-pack/plugins/event_log/server/es/context.mock.ts | 2 +- x-pack/plugins/event_log/server/es/init.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/event_log/server/es/context.mock.ts b/x-pack/plugins/event_log/server/es/context.mock.ts index c974a63a6407f..15d532c2154ec 100644 --- a/x-pack/plugins/event_log/server/es/context.mock.ts +++ b/x-pack/plugins/event_log/server/es/context.mock.ts @@ -25,7 +25,7 @@ const createContextMock = () => { waitTillReady: jest.fn(async () => true), esAdapter: clusterClientAdapterMock.create(), initialized: true, - retryDelay: MOCK_RETRY_DELAY, + retryDelay: MOCK_RETRY_DELAY - 1, }; return mock; }; diff --git a/x-pack/plugins/event_log/server/es/init.ts b/x-pack/plugins/event_log/server/es/init.ts index 87d623050cb53..da98a18e9d758 100644 --- a/x-pack/plugins/event_log/server/es/init.ts +++ b/x-pack/plugins/event_log/server/es/init.ts @@ -52,7 +52,7 @@ function getRetryOptions(esContext: EsContext, operation: string) { maxTimeout: MAX_RETRY_DELAY, retries: 4, factor: 2, - randomize: false, + randomize: true, onFailedAttempt: (err: FailedAttemptError) => { const message = `eventLog initialization operation failed and will be retried: ${operation}; ${err.retriesLeft} more times; error: ${err.message}`; logger.warn(message); From d5b9b53114e42665795d72cf5718b8252da0baf9 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Wed, 31 May 2023 15:54:09 +0200 Subject: [PATCH 3/3] Use performance.now() rather than Date.now() to calculate elapsed time in tests. --- .../plugins/event_log/server/es/context.mock.ts | 2 +- x-pack/plugins/event_log/server/es/init.test.ts | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/event_log/server/es/context.mock.ts b/x-pack/plugins/event_log/server/es/context.mock.ts index 15d532c2154ec..c974a63a6407f 100644 --- a/x-pack/plugins/event_log/server/es/context.mock.ts +++ b/x-pack/plugins/event_log/server/es/context.mock.ts @@ -25,7 +25,7 @@ const createContextMock = () => { waitTillReady: jest.fn(async () => true), esAdapter: clusterClientAdapterMock.create(), initialized: true, - retryDelay: MOCK_RETRY_DELAY - 1, + retryDelay: MOCK_RETRY_DELAY, }; return mock; }; diff --git a/x-pack/plugins/event_log/server/es/init.test.ts b/x-pack/plugins/event_log/server/es/init.test.ts index 7280849a8cac0..137bf17247f6e 100644 --- a/x-pack/plugins/event_log/server/es/init.test.ts +++ b/x-pack/plugins/event_log/server/es/init.test.ts @@ -455,8 +455,7 @@ describe('parseIndexAliases', () => { }); }); -// FLAKY: https://github.com/elastic/kibana/issues/156061 -describe.skip('retries', () => { +describe('retries', () => { let esContext = contextMock.create(); // set up context APIs to return defaults indicating already created beforeEach(() => { @@ -472,9 +471,9 @@ describe.skip('retries', () => { test('createIlmPolicyIfNotExists with 1 retry', async () => { esContext.esAdapter.doesIlmPolicyExist.mockRejectedValueOnce(new Error('retry 1')); - const timeStart = Date.now(); + const timeStart = performance.now(); await initializeEs(esContext); - const timeElapsed = Date.now() - timeStart; + const timeElapsed = performance.now() - timeStart; expect(timeElapsed).toBeGreaterThanOrEqual(MOCK_RETRY_DELAY); @@ -492,9 +491,9 @@ describe.skip('retries', () => { esContext.esAdapter.doesIndexTemplateExist.mockRejectedValueOnce(new Error('retry 2a')); esContext.esAdapter.doesIndexTemplateExist.mockRejectedValueOnce(new Error('retry 2b')); - const timeStart = Date.now(); + const timeStart = performance.now(); await initializeEs(esContext); - const timeElapsed = Date.now() - timeStart; + const timeElapsed = performance.now() - timeStart; expect(timeElapsed).toBeGreaterThanOrEqual(MOCK_RETRY_DELAY * (1 + 2)); @@ -518,9 +517,9 @@ describe.skip('retries', () => { // make sure it only tries 5 times - this one should not be reported esContext.esAdapter.doesAliasExist.mockRejectedValueOnce(new Error('retry 5f')); - const timeStart = Date.now(); + const timeStart = performance.now(); await initializeEs(esContext); - const timeElapsed = Date.now() - timeStart; + const timeElapsed = performance.now() - timeStart; expect(timeElapsed).toBeGreaterThanOrEqual(MOCK_RETRY_DELAY * (1 + 2 + 4 + 8));