-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[LockManager] Ensure index template are created #218901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sorenlouv
merged 6 commits into
elastic:main
from
sorenlouv:create-lock-manager-index-assets
Apr 23, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
02b9415
[LockManager] Ensure index template are created
sorenlouv 8a2fe3d
Merge branch 'main' into create-lock-manager-index-assets
sorenlouv 2769d21
Add migration that deletes the index if the mappings are incorrect
sorenlouv 98aec20
Add test to verify that index is not deleted if it has the right mapp…
sorenlouv 2ba560f
Improve log messages
sorenlouv 7879f17
Update x-pack/platform/plugins/shared/observability_ai_assistant/serv…
sorenlouv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
97 changes: 97 additions & 0 deletions
97
...vability_ai_assistant/server/service/distributed_lock_manager/setup_lock_manager_index.ts
This file contains hidden or 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,97 @@ | ||
| /* | ||
| * 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 { errors } from '@elastic/elasticsearch'; | ||
| import { ElasticsearchClient, Logger } from '@kbn/core/server'; | ||
| import { IndicesGetMappingResponse } from '@elastic/elasticsearch/lib/api/types'; | ||
|
|
||
| const LOCKS_INDEX_ALIAS = '.kibana_locks'; | ||
| export const LOCKS_CONCRETE_INDEX_NAME = `${LOCKS_INDEX_ALIAS}-000001`; | ||
| export const LOCKS_COMPONENT_TEMPLATE_NAME = `${LOCKS_INDEX_ALIAS}-component`; | ||
| export const LOCKS_INDEX_TEMPLATE_NAME = `${LOCKS_INDEX_ALIAS}-index-template`; | ||
|
|
||
| export async function removeLockIndexWithIncorrectMappings( | ||
| esClient: ElasticsearchClient, | ||
| logger: Logger | ||
| ) { | ||
| let res: IndicesGetMappingResponse; | ||
| try { | ||
| res = await esClient.indices.getMapping({ index: LOCKS_CONCRETE_INDEX_NAME }); | ||
| } catch (error) { | ||
| const isNotFoundError = error instanceof errors.ResponseError && error.statusCode === 404; | ||
| if (!isNotFoundError) { | ||
| logger.error( | ||
| `Failed to get mapping for lock index "${LOCKS_CONCRETE_INDEX_NAME}": ${error.message}` | ||
| ); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const { mappings } = res[LOCKS_CONCRETE_INDEX_NAME]; | ||
| const hasIncorrectMappings = | ||
| mappings.properties?.token?.type !== 'keyword' || | ||
| mappings.properties?.expiresAt?.type !== 'date'; | ||
|
|
||
| if (hasIncorrectMappings) { | ||
| logger.warn(`Lock index "${LOCKS_CONCRETE_INDEX_NAME}" has incorrect mappings.`); | ||
| try { | ||
| await esClient.indices.delete({ index: LOCKS_CONCRETE_INDEX_NAME }); | ||
| logger.info(`Lock index "${LOCKS_CONCRETE_INDEX_NAME}" removed successfully.`); | ||
| } catch (error) { | ||
| logger.error(`Failed to remove lock index "${LOCKS_CONCRETE_INDEX_NAME}": ${error.message}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function ensureTemplatesAndIndexCreated( | ||
| esClient: ElasticsearchClient, | ||
| logger: Logger | ||
| ): Promise<void> { | ||
| const INDEX_PATTERN = `${LOCKS_INDEX_ALIAS}*`; | ||
|
|
||
| await esClient.cluster.putComponentTemplate({ | ||
| name: LOCKS_COMPONENT_TEMPLATE_NAME, | ||
| template: { | ||
| mappings: { | ||
| dynamic: false, | ||
| properties: { | ||
| token: { type: 'keyword' }, | ||
| metadata: { enabled: false }, | ||
| createdAt: { type: 'date' }, | ||
| expiresAt: { type: 'date' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| logger.info( | ||
| `Component template ${LOCKS_COMPONENT_TEMPLATE_NAME} created or updated successfully.` | ||
| ); | ||
|
|
||
| await esClient.indices.putIndexTemplate({ | ||
| name: LOCKS_INDEX_TEMPLATE_NAME, | ||
| index_patterns: [INDEX_PATTERN], | ||
| composed_of: [LOCKS_COMPONENT_TEMPLATE_NAME], | ||
| priority: 500, | ||
| template: { | ||
| settings: { | ||
| number_of_shards: 1, | ||
| auto_expand_replicas: '0-1', | ||
| hidden: true, | ||
| }, | ||
| }, | ||
| }); | ||
| logger.info(`Index template ${LOCKS_INDEX_TEMPLATE_NAME} created or updated successfully.`); | ||
|
|
||
| await esClient.indices.create({ index: LOCKS_CONCRETE_INDEX_NAME }, { ignore: [400] }); | ||
| logger.info(`Index ${LOCKS_CONCRETE_INDEX_NAME} created or updated successfully.`); | ||
| } | ||
|
|
||
| export async function setuplockManagerIndex(esClient: ElasticsearchClient, logger: Logger) { | ||
| await removeLockIndexWithIncorrectMappings(esClient, logger); // TODO: should be removed in the future (after 9.1). See https://github.com/elastic/kibana/issues/218944 | ||
| await ensureTemplatesAndIndexCreated(esClient, logger); | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.