Retry release to fix flaky tests (#216781)

Related to https://github.com/elastic/kibana/pull/216397
Closes https://github.com/elastic/kibana/issues/216763

This change ensures that we do not send the `release` request and
`extendTtl` request simultaneously in `withLock`. This caused a conflict
causing tests to fail:

```
           └-> "before all" hook for "should return the result of the callback"
             │ERROR Failed to release lock "my_lock_with_ttl_extension": version_conflict_engine_exception
             │      	Root causes:
             │      		version_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]
```

Flaky tests:
https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/8142
This commit is contained in:
Søren Louv-Jansen 2025-04-02 13:21:20 +02:00 committed by GitHub
parent 9797e95289
commit 7275d2e8bd
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}`);
}
}
}