Skip to content

Commit

Permalink
[Entity Analytics] [Entity Store] Fix Asset Criticality index issue w…
Browse files Browse the repository at this point in the history
…hen setting up entity engines concurrently (elastic#199486)

## Summary

If the Entity Engine setup API was called in parallel for the host and
user engine, there was an issue where both calls attempt to create the
asset criticality index causing 'resource_already_exists_exception' to
be thrown.

This pull request fixes this by ignoring the
`resource_already_exists_exception` when creating indices in our util.

To test, setup both engines concurrently:

```
(curl -H 'Content-Type: application/json' -X POST -H 'kbn-xsrf: true' -H 'elastic-api-version: 2023-10-31' http://elastic:changeme@localhost:5601/api/entity_store/engines/host/init -d '{}' & curl -H 'Content-Type: application/json' -X POST -H 'kbn-xsrf: true' -H 'elastic-api-version: 2023-10-31' http://elastic:changeme@localhost:5601/api/entity_store/engines/user/init -d '{}' & wait)
```
  • Loading branch information
hop-dev authored Nov 11, 2024
1 parent c19551f commit ff588ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { ElasticsearchClient, Logger } from '@kbn/core/server';
import type { EntityType } from '../../../../../common/api/entity_analytics';
import { getEntitiesIndexName } from '../utils';
import { createOrUpdateIndex } from '../../utils/create_or_update_index';

interface Options {
entityType: EntityType;
Expand All @@ -17,18 +18,13 @@ interface Options {
}

export const createEntityIndex = async ({ entityType, esClient, namespace, logger }: Options) => {
try {
await esClient.indices.create({
await createOrUpdateIndex({
esClient,
logger,
options: {
index: getEntitiesIndexName(entityType, namespace),
body: {},
});
} catch (e) {
if (e.meta.body.error.type === 'resource_already_exists_exception') {
logger.debug(`Index for ${entityType} already exists, skipping creation.`);
} else {
throw e;
}
}
},
});
};

export const deleteEntityIndex = ({ entityType, esClient, namespace }: Options) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ export const createOrUpdateIndex = async ({
);
}
} else {
return esClient.indices.create(options);
try {
await esClient.indices.create(options);
} catch (err) {
// If the index already exists, we can ignore the error
if (err?.meta?.body?.error?.type === 'resource_already_exists_exception') {
logger.info(`${options.index} already exists`);
} else {
throw err;
}
}
}
} catch (err) {
const error = transformError(err);
Expand Down

0 comments on commit ff588ff

Please sign in to comment.