From ae5666b09b4c7efeacd0e191bb5a7334cd67ed5a Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 20 Feb 2023 05:51:20 -0500 Subject: [PATCH] [8.7] [ML] Fixing ML saved object cache (#151122) (#151589) # Backport This will backport the following commits from `main` to `8.7`: - [[ML] Fixing ML saved object cache (#151122)](https://github.com/elastic/kibana/pull/151122) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: James Gowdy --- .../ml/server/saved_objects/service.ts | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/ml/server/saved_objects/service.ts b/x-pack/plugins/ml/server/saved_objects/service.ts index 1c31b6d0d088d..d05972a1368ee 100644 --- a/x-pack/plugins/ml/server/saved_objects/service.ts +++ b/x-pack/plugins/ml/server/saved_objects/service.ts @@ -63,6 +63,12 @@ export function mlSavedObjectServiceFactory( (options: SavedObjectsFindOptions) => JSON.stringify(options) ); + function _clearSavedObjectsClientCache() { + if (_savedObjectsClientFindMemo.cache.clear) { + _savedObjectsClientFindMemo.cache.clear(); + } + } + async function _getJobObjects( jobType?: JobType, jobId?: string, @@ -118,6 +124,7 @@ export function mlSavedObjectServiceFactory( } else { // the saved object has no spaces, this is unexpected, attempt a normal delete await savedObjectsClient.delete(ML_JOB_SAVED_OBJECT_TYPE, id, { force: true }); + _clearSavedObjectsClientCache(); } } } catch (error) { @@ -128,11 +135,12 @@ export function mlSavedObjectServiceFactory( await savedObjectsClient.create(ML_JOB_SAVED_OBJECT_TYPE, job, { id, }); + _clearSavedObjectsClientCache(); } async function _bulkCreateJobs(jobs: Array<{ job: JobObject; namespaces: string[] }>) { await isMlReady(); - return await savedObjectsClient.bulkCreate( + const results = await savedObjectsClient.bulkCreate( jobs.map((j) => ({ type: ML_JOB_SAVED_OBJECT_TYPE, id: _jobSavedObjectId(j.job), @@ -140,6 +148,8 @@ export function mlSavedObjectServiceFactory( initialNamespaces: j.namespaces, })) ); + _clearSavedObjectsClientCache(); + return results; } function _jobSavedObjectId(job: JobObject) { @@ -154,6 +164,7 @@ export function mlSavedObjectServiceFactory( } await savedObjectsClient.delete(ML_JOB_SAVED_OBJECT_TYPE, job.id, { force: true }); + _clearSavedObjectsClientCache(); } async function _forceDeleteJob(jobType: JobType, jobId: string, namespace: string) { @@ -169,6 +180,7 @@ export function mlSavedObjectServiceFactory( namespace: namespace === '*' ? undefined : namespace, force: true, }); + _clearSavedObjectsClientCache(); } async function createAnomalyDetectionJob(jobId: string, datafeedId?: string) { @@ -250,6 +262,7 @@ export function mlSavedObjectServiceFactory( const jobObject = job.attributes; jobObject.datafeed_id = datafeedId; await savedObjectsClient.update(ML_JOB_SAVED_OBJECT_TYPE, job.id, jobObject); + _clearSavedObjectsClientCache(); } async function deleteDatafeed(datafeedId: string) { @@ -262,6 +275,7 @@ export function mlSavedObjectServiceFactory( const jobObject = job.attributes; jobObject.datafeed_id = null; await savedObjectsClient.update(ML_JOB_SAVED_OBJECT_TYPE, job.id, jobObject); + _clearSavedObjectsClientCache(); } async function _getIds(jobType: JobType, idType: keyof JobObject) { @@ -372,6 +386,8 @@ export function mlSavedObjectServiceFactory( spacesToAdd, spacesToRemove ); + _clearSavedObjectsClientCache(); + updateResult.objects.forEach(({ id: objectId, error }) => { const jobId = jobObjectIdMap.get(objectId)!; if (error) { @@ -492,6 +508,7 @@ export function mlSavedObjectServiceFactory( await savedObjectsClient.delete(ML_TRAINED_MODEL_SAVED_OBJECT_TYPE, modelId, { force: true, }); + _clearSavedObjectsClientCache(); } } } catch (error) { @@ -518,6 +535,7 @@ export function mlSavedObjectServiceFactory( ...(initialNamespaces ? { initialNamespaces } : {}), } ); + _clearSavedObjectsClientCache(); } async function _bulkCreateTrainedModel(models: TrainedModelObject[], namespaceFallback?: string) { @@ -528,7 +546,7 @@ export function mlSavedObjectServiceFactory( return acc; }, {} as Record); - return await savedObjectsClient.bulkCreate( + const results = await savedObjectsClient.bulkCreate( models.map((m) => { let initialNamespaces = m.job && namespacesPerJob[m.job.job_id]; if (!initialNamespaces?.length && namespaceFallback) { @@ -546,6 +564,8 @@ export function mlSavedObjectServiceFactory( }; }) ); + _clearSavedObjectsClientCache(); + return results; } async function getAllTrainedModelObjectsForAllSpaces(modelIds?: string[]) { @@ -577,6 +597,7 @@ export function mlSavedObjectServiceFactory( } await savedObjectsClient.delete(ML_TRAINED_MODEL_SAVED_OBJECT_TYPE, model.id, { force: true }); + _clearSavedObjectsClientCache(); } async function _forceDeleteTrainedModel(modelId: string, namespace: string) { @@ -586,6 +607,7 @@ export function mlSavedObjectServiceFactory( namespace: namespace === '*' ? undefined : namespace, force: true, }); + _clearSavedObjectsClientCache(); } async function filterTrainedModelsForSpace(list: T[], field: keyof T): Promise { @@ -729,6 +751,8 @@ export function mlSavedObjectServiceFactory( spacesToAdd, spacesToRemove ); + _clearSavedObjectsClientCache(); + updateResult.objects.forEach(({ id: objectId, error }) => { const model = trainedModelObjectIdMap.get(objectId)!; if (error) {