[LockManager] Fix flaky API test (#217470)

Related: https://github.com/elastic/kibana/pull/216916

The flaky test runner is sometimes throwing this error:

```

└-: withLock API
--
  | └-> "before all" hook: beforeTestSuite.trigger in "withLock API"
  | └-> "before all" hook in "withLock API"
  | └- ✖ fail: Stateful Observability - Deployment-agnostic API integration tests observability AI Assistant LockManager withLock API "before all" hook in "withLock API"
  | │       ResponseError: {"took":1,"timed_out":false,"total":1,"deleted":0,"batches":1,"version_conflicts":1,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1,"throttled_until_millis":0,"failures":[{"index":".kibana_locks-000001","id":"my_lock_with_token_fencing","cause":{"type":"version_conflict_engine_exception","reason":"[my_lock_with_token_fencing]: version conflict, required seqNo [117], primary term[1]. but no document was found","index_uuid":"F_O5sNfQSLqtyPeLexG_Qw","shard":"0","index":".kibana_locks-000001"},"status":409}]}
  | │       at SniffingTransport._request (node_modules/@elastic/elasticsearch/node_modules/@elastic/transport/src/Transport.ts:605:17)
  | │       at processTicksAndRejections (node:internal/process/task_queues:95:5)
  | │       at /opt/buildkite-agent/builds/bk-agent-prod-gcp-1744099176361402458/elastic/kibana-flaky-test-suite-runner/kibana/node_modules/@elastic/elasticsearch/node_modules/@elastic/transport/src/Transport.ts:711:22
  | │       at SniffingTransport.request (node_modules/@elastic/elasticsearch/node_modules/@elastic/transport/src/Transport.ts:708:14)
  | │       at Client.DeleteByQueryApi [as deleteByQuery] (node_modules/@elastic/src/api/api/delete_by_query.ts:143:10)
  | │       at Context.<anonymous> (distributed_lock_manager.spec.ts:444:9)
  | │       at Object.apply (wrap_function.js:74:16)
```

The error happens when `release` and `clearAllLocks` simultaneously
tries to delete the same document. This PR ensures that `clearAllLocks`
will not throw an exception on conflict
This commit is contained in:
Søren Louv-Jansen 2025-04-08 16:06:05 +02:00 committed by GitHub
parent 2a01722cfa
commit a5f3c0ad03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,7 +37,7 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon
before(async () => {
await ensureTemplatesAndIndexCreated(es);
await createLocksWriteIndex(es);
await clearAllLocks(es);
await clearAllLocks(es, log);
});
describe('Basic lock operations', () => {
@ -412,7 +412,7 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon
expect(lock!.token).to.be(newToken);
// cleanup
await clearAllLocks(es);
await clearAllLocks(es, log);
});
it('should use a fresh token on subsequent acquisitions', async () => {
@ -441,7 +441,7 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon
before(async () => {
await ensureTemplatesAndIndexCreated(es);
await createLocksWriteIndex(es);
await clearAllLocks(es);
await clearAllLocks(es, log);
});
const LOCK_ID = 'my_lock_with_lock';
@ -645,15 +645,21 @@ export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderCon
});
}
function clearAllLocks(es: Client) {
return es.deleteByQuery(
{
index: LOCKS_CONCRETE_INDEX_NAME,
query: { match_all: {} },
refresh: true,
},
{ ignore: [404] }
);
function clearAllLocks(es: Client, log: ToolingLog) {
try {
return es.deleteByQuery(
{
index: LOCKS_CONCRETE_INDEX_NAME,
query: { match_all: {} },
refresh: true,
conflicts: 'proceed',
},
{ ignore: [404] }
);
} catch (e) {
log.error(`Failed to clear locks: ${e.message}`);
log.debug(e);
}
}
async function getLocks(es: Client) {