Skip to content

Commit 2f4c770

Browse files
sorenlouvviduni94
andcommitted
[LockManager] Ensure index template are created (elastic#218901)
Closes: elastic#218944 The index template for the Lock Manager was not created, causing index mappings and settings to be incorrect. Root cause: the function responsible for creating the index template (`ensureTemplatesAndIndexCreated`) was never invoked - only during automated testing 🤦 The mappings for the lock manager index (`.kibana_locks-000001`) should match this: ```ts { mappings: { dynamic: false, properties: { token: { type: 'keyword' }, metadata: { enabled: false }, createdAt: { type: 'date' }, expiresAt: { type: 'date' }, }, }, } ``` In this test we make sure that the LockManager library can recover and fix the mappings if the existing index has invalid mappings ``` DELETE .kibana_locks-000001 DELETE _index_template/.kibana_locks-index-template DELETE _component_template/.kibana_locks-component ``` correct mappings ``` PUT .kibana_locks-000001 ``` (Restart Kibana) ``` GET .kibana_locks-000001/_mapping ``` In this test we make sure that out of the box, the LockManager library creates an index with the correct mappings ``` DELETE .kibana_locks-000001 DELETE _index_template/.kibana_locks-index-template DELETE _component_template/.kibana_locks-component ``` (Restart Kibana) ``` GET .kibana_locks-000001/_mapping ``` Related: elastic#216916 elastic#216397 --------- Co-authored-by: Viduni Wickramarachchi <[email protected]> (cherry picked from commit f684ea4)
1 parent 6e9b7cf commit 2f4c770

File tree

23 files changed

+1114
-829
lines changed

23 files changed

+1114
-829
lines changed

x-pack/platform/plugins/shared/observability_ai_assistant/server/plugin.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import { registerFunctions } from './functions';
3131
import { recallRankingEvent } from './analytics/recall_ranking';
3232
import { initLangtrace } from './service/client/instrumentation/init_langtrace';
3333
import { aiAssistantCapabilities } from '../common/capabilities';
34-
import { registerAndScheduleKbSemanticTextMigrationTask } from './service/task_manager_definitions/register_kb_semantic_text_migration_task';
35-
import { updateExistingIndexAssets } from './service/create_or_update_index_assets';
34+
import { populateMissingSemanticTextFieldMigration } from './service/startup_migrations/populate_missing_semantic_text_field_migration';
35+
import { updateExistingIndexAssets } from './service/startup_migrations/create_or_update_index_assets';
3636

3737
export class ObservabilityAIAssistantPlugin
3838
implements
@@ -128,23 +128,19 @@ export class ObservabilityAIAssistantPlugin
128128
}));
129129

130130
// Update existing index assets (mappings, templates, etc). This will not create assets if they do not exist.
131-
const indexAssetsUpdatedPromise = updateExistingIndexAssets({
132-
logger: this.logger.get('index_assets'),
133-
core,
134-
}).catch((e) => this.logger.error(`Index assets could not be updated: ${e.message}`));
135-
136-
// register task to migrate knowledge base entries to include semantic_text field
137-
registerAndScheduleKbSemanticTextMigrationTask({
138-
core,
139-
taskManager: plugins.taskManager,
140-
logger: this.logger.get('kb_semantic_text_migration_task'),
141-
config: this.config,
142-
indexAssetsUpdatedPromise,
143-
}).catch((e) =>
144-
this.logger.error(
145-
`Knowledge base semantic_text migration task could not be registered: ${e.message}`
131+
updateExistingIndexAssets({ logger: this.logger, core })
132+
.then(() =>
133+
populateMissingSemanticTextFieldMigration({
134+
core,
135+
logger: this.logger,
136+
config: this.config,
137+
})
146138
)
147-
);
139+
.catch((e) =>
140+
this.logger.error(
141+
`Error during knowledge base migration in AI Assistant plugin startup: ${e.message}`
142+
)
143+
);
148144

149145
service.register(registerFunctions);
150146

x-pack/platform/plugins/shared/observability_ai_assistant/server/routes/knowledge_base/route.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,23 @@ const resetKnowledgeBase = createObservabilityAIAssistantServerRoute({
100100
},
101101
});
102102

103+
const reIndexKnowledgeBase = createObservabilityAIAssistantServerRoute({
104+
endpoint: 'POST /internal/observability_ai_assistant/kb/reindex',
105+
security: {
106+
authz: {
107+
requiredPrivileges: ['ai_assistant'],
108+
},
109+
},
110+
handler: async (resources): Promise<{ result: boolean }> => {
111+
const client = await resources.service.getClient({ request: resources.request });
112+
const result = await client.reIndexKnowledgeBaseWithLock();
113+
return { result };
114+
},
115+
});
116+
103117
const semanticTextMigrationKnowledgeBase = createObservabilityAIAssistantServerRoute({
104-
endpoint: 'POST /internal/observability_ai_assistant/kb/migrations/kb_semantic_text',
118+
endpoint:
119+
'POST /internal/observability_ai_assistant/kb/migrations/populate_missing_semantic_text_field',
105120
security: {
106121
authz: {
107122
requiredPrivileges: ['ai_assistant'],
@@ -320,6 +335,7 @@ const importKnowledgeBaseEntries = createObservabilityAIAssistantServerRoute({
320335
});
321336

322337
export const knowledgeBaseRoutes = {
338+
...reIndexKnowledgeBase,
323339
...semanticTextMigrationKnowledgeBase,
324340
...setupKnowledgeBase,
325341
...resetKnowledgeBase,

x-pack/platform/plugins/shared/observability_ai_assistant/server/routes/top_level/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 2.0.
66
*/
77

8-
import { createOrUpdateIndexAssets } from '../../service/create_or_update_index_assets';
8+
import { createOrUpdateIndexAssets } from '../../service/startup_migrations/create_or_update_index_assets';
99
import { createObservabilityAIAssistantServerRoute } from '../create_observability_ai_assistant_server_route';
1010

1111
const createOrUpdateIndexAssetsRoute = createObservabilityAIAssistantServerRoute({

x-pack/platform/plugins/shared/observability_ai_assistant/server/service/client/index.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@ import { continueConversation } from './operators/continue_conversation';
6767
import { convertInferenceEventsToStreamingEvents } from './operators/convert_inference_events_to_streaming_events';
6868
import { extractMessages } from './operators/extract_messages';
6969
import { getGeneratedTitle } from './operators/get_generated_title';
70-
import {
71-
reIndexKnowledgeBaseAndPopulateSemanticTextField,
72-
scheduleKbSemanticTextMigrationTask,
73-
} from '../task_manager_definitions/register_kb_semantic_text_migration_task';
70+
import { populateMissingSemanticTextFieldMigration } from '../startup_migrations/populate_missing_semantic_text_field_migration';
7471
import { ObservabilityAIAssistantPluginStartDependencies } from '../../types';
7572
import { ObservabilityAIAssistantConfig } from '../../config';
7673
import { getElserModelId } from '../knowledge_base_service/get_elser_model_id';
7774
import { apmInstrumentation } from './operators/apm_instrumentation';
75+
import { reIndexKnowledgeBaseWithLock } from '../knowledge_base_service/reindex_knowledge_base';
7876

7977
const MAX_FUNCTION_CALLS = 8;
8078

@@ -684,14 +682,15 @@ export class ObservabilityAIAssistantClient {
684682
// setup the knowledge base
685683
const res = await knowledgeBaseService.setup(esClient, modelId);
686684

687-
core
688-
.getStartServices()
689-
.then(([_, pluginsStart]) =>
690-
scheduleKbSemanticTextMigrationTask({ taskManager: pluginsStart.taskManager, logger })
691-
)
692-
.catch((error) => {
693-
logger.error(`Failed to schedule semantic text migration task: ${error}`);
694-
});
685+
populateMissingSemanticTextFieldMigration({
686+
core,
687+
logger,
688+
config: this.dependencies.config,
689+
}).catch((e) => {
690+
this.dependencies.logger.error(
691+
`Failed to populate missing semantic text fields: ${e.message}`
692+
);
693+
});
695694

696695
return res;
697696
};
@@ -701,14 +700,21 @@ export class ObservabilityAIAssistantClient {
701700
return this.dependencies.knowledgeBaseService.reset(esClient);
702701
};
703702

704-
reIndexKnowledgeBaseAndPopulateSemanticTextField = () => {
705-
return reIndexKnowledgeBaseAndPopulateSemanticTextField({
703+
reIndexKnowledgeBaseWithLock = () => {
704+
return reIndexKnowledgeBaseWithLock({
705+
core: this.dependencies.core,
706706
esClient: this.dependencies.esClient,
707707
logger: this.dependencies.logger,
708-
config: this.dependencies.config,
709708
});
710709
};
711710

711+
reIndexKnowledgeBaseAndPopulateSemanticTextField = () => {
712+
return populateMissingSemanticTextFieldMigration({
713+
core: this.dependencies.core,
714+
logger: this.dependencies.logger,
715+
config: this.dependencies.config,
716+
});
717+
};
712718
addUserInstruction = async ({
713719
entry,
714720
}: {

0 commit comments

Comments
 (0)