forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Advanced Settings] Add retry for 409 conflicts in API integration te…
…sts (elastic#189813) Fixes elastic#176445 Flaky test runner: https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/6679 This PR fixes the advanced settings API integration tests that seem to be flaky. The reason for the occasional failures is most likely a `version_conflict_engine_exception` which is thrown when another node indexes the same documents. This can happen when we save an advanced setting since the settings API uses saved objects under the hood, and in CI, multiple nodes can try to save an advanced setting simultaneously. The solution in this PR is to retry the request if we encounter a 409 error. This is adapted from the solution in elastic#174185 which resolves a similar failure. (cherry picked from commit cc29eea)
- Loading branch information
1 parent
dfeb32f
commit a209845
Showing
3 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
x-pack/test/api_integration/apis/management/advanced_settings/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { retryRequestIfConflicts } from './retry_if_conflicts'; |
46 changes: 46 additions & 0 deletions
46
x-pack/test/api_integration/apis/management/advanced_settings/utils/retry_if_conflicts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { ToolingLog } from '@kbn/tooling-log'; | ||
|
||
// Number of times to retry when conflicts occur | ||
const RETRY_ATTEMPTS = 2; | ||
|
||
// Delay between retries when conflicts occur | ||
const RETRY_DELAY = 200; | ||
|
||
/* | ||
* Retry a request if it runs into 409 Conflicts, | ||
* up to a maximum number of attempts. | ||
*/ | ||
export const retryRequestIfConflicts = async ( | ||
logger: ToolingLog, | ||
name: string, | ||
sendRequest: () => Promise<any>, | ||
retries: number = RETRY_ATTEMPTS, | ||
retryDelay: number = RETRY_DELAY | ||
): Promise<any> => { | ||
const response = await sendRequest(); | ||
if (response.statusCode !== 409) { | ||
return response; | ||
} | ||
|
||
// If no retries left, throw it | ||
if (retries <= 0) { | ||
logger.error(`${name} conflict, exceeded retries`); | ||
throw new Error(`${name} conflict, exceeded retries`); | ||
} | ||
|
||
// Otherwise, delay a bit before retrying | ||
logger.debug(`${name} conflict, retrying ...`); | ||
await waitBeforeNextRetry(retryDelay); | ||
return await retryRequestIfConflicts(logger, name, sendRequest, retries - 1); | ||
}; | ||
|
||
async function waitBeforeNextRetry(retryDelay: number): Promise<void> { | ||
await new Promise((resolve) => setTimeout(resolve, retryDelay)); | ||
} |