[8.x] Retry release to fix flaky tests (#216781) (#216798)

# Backport

This will backport the following commits from `main` to `8.x`:
- [Retry release to fix flaky tests
(#216781)](https://github.com/elastic/kibana/pull/216781)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Søren
Louv-Jansen","email":"soren.louv@elastic.co"},"sourceCommit":{"committedDate":"2025-04-02T11:21:20Z","message":"Retry
release to fix flaky tests (#216781)\n\nRelated to
https://github.com/elastic/kibana/pull/216397\nCloses
https://github.com/elastic/kibana/issues/216763\n\nThis change ensures
that we do not send the `release` request and\n`extendTtl` request
simultaneously in `withLock`. This caused a conflict\ncausing tests to
fail:\n\n```\n └-> \"before all\" hook for \"should return the result of
the callback\"\n │ERROR Failed to release lock
\"my_lock_with_ttl_extension\": version_conflict_engine_exception\n │
\tRoot causes:\n │ \t\tversion_conflict_engine_exception:
[my_lock_with_ttl_extension]: version conflict, required seqNo [43],
primary term [1]. current document has seqNo [44] and primary term
[1]\n```\n\nFlaky
tests:\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8142","sha":"7275d2e8bd834303898e3f95c5fd1ab734e92947","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Obs
AI Assistant","backport:version","v9.1.0","v8.19.0"],"title":"Retry
release to fix flaky
tests","number":216781,"url":"https://github.com/elastic/kibana/pull/216781","mergeCommit":{"message":"Retry
release to fix flaky tests (#216781)\n\nRelated to
https://github.com/elastic/kibana/pull/216397\nCloses
https://github.com/elastic/kibana/issues/216763\n\nThis change ensures
that we do not send the `release` request and\n`extendTtl` request
simultaneously in `withLock`. This caused a conflict\ncausing tests to
fail:\n\n```\n └-> \"before all\" hook for \"should return the result of
the callback\"\n │ERROR Failed to release lock
\"my_lock_with_ttl_extension\": version_conflict_engine_exception\n │
\tRoot causes:\n │ \t\tversion_conflict_engine_exception:
[my_lock_with_ttl_extension]: version conflict, required seqNo [43],
primary term [1]. current document has seqNo [44] and primary term
[1]\n```\n\nFlaky
tests:\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8142","sha":"7275d2e8bd834303898e3f95c5fd1ab734e92947"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/216781","number":216781,"mergeCommit":{"message":"Retry
release to fix flaky tests (#216781)\n\nRelated to
https://github.com/elastic/kibana/pull/216397\nCloses
https://github.com/elastic/kibana/issues/216763\n\nThis change ensures
that we do not send the `release` request and\n`extendTtl` request
simultaneously in `withLock`. This caused a conflict\ncausing tests to
fail:\n\n```\n └-> \"before all\" hook for \"should return the result of
the callback\"\n │ERROR Failed to release lock
\"my_lock_with_ttl_extension\": version_conflict_engine_exception\n │
\tRoot causes:\n │ \t\tversion_conflict_engine_exception:
[my_lock_with_ttl_extension]: version conflict, required seqNo [43],
primary term [1]. current document has seqNo [44] and primary term
[1]\n```\n\nFlaky
tests:\nhttps://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8142","sha":"7275d2e8bd834303898e3f95c5fd1ab734e92947"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Søren Louv-Jansen <soren.louv@elastic.co>
This commit is contained in:
Kibana Machine 2025-04-02 15:13:31 +02:00 committed by GitHub
parent 766a251242
commit b421fe725b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -285,20 +285,26 @@ export async function withLock<T>(
`Lock "${lockId}" acquired. Extending TTL every ${prettyMilliseconds(extendInterval)}`
);
let extendTTlPromise = Promise.resolve(true);
const intervalId = setInterval(() => {
lockManager.extendTtl().catch((err) => {
logger.error(`Failed to extend lock "${lockId}":`, err);
});
// wait for the previous extendTtl request to finish before sending the next one. This is to avoid flooding ES with extendTtl requests in cases where ES is slow to respond.
extendTTlPromise = extendTTlPromise
.then(() => lockManager.extendTtl())
.catch((err) => {
logger.error(`Failed to extend lock "${lockId}":`, err);
return false;
});
}, extendInterval);
try {
return await callback();
} finally {
clearInterval(intervalId);
try {
clearInterval(intervalId);
await extendTTlPromise;
await lockManager.release();
} catch (error) {
logger.error(`Failed to release lock "${lockId}": ${error.message}`);
logger.error(`Failed to release lock "${lockId}" in withLock: ${error.message}`);
}
}
}