[ML] Fixing job id reuse when previous job was in diffrent space (#86270)

This commit is contained in:
James Gowdy 2020-12-18 09:40:27 +00:00 committed by GitHub
parent 011849faea
commit b1d18e5d9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -77,7 +77,17 @@ export function jobSavedObjectServiceFactory(
const id = savedObjectId(job);
try {
await savedObjectsClient.delete(ML_SAVED_OBJECT_TYPE, id, { force: true });
const [existingJobObject] = await getAllJobObjectsForAllSpaces(jobType, jobId);
if (existingJobObject !== undefined) {
// a saved object for this job already exists, this may be left over from a previously deleted job
if (existingJobObject.namespaces?.length) {
// use a force delete just in case the saved object exists only in another space.
await _forceDeleteJob(jobType, jobId, existingJobObject.namespaces[0]);
} else {
// the saved object has no spaces, this is unexpected, attempt a normal delete
await savedObjectsClient.delete(ML_SAVED_OBJECT_TYPE, id, { force: true });
}
}
} catch (error) {
// the saved object may exist if a previous job with the same ID has been deleted.
// if not, this error will be throw which we ignore.
@ -168,7 +178,7 @@ export function jobSavedObjectServiceFactory(
return jobObject;
}
async function getAllJobObjectsForAllSpaces(jobType?: JobType) {
async function getAllJobObjectsForAllSpaces(jobType?: JobType, jobId?: string) {
await isMlReady();
const filterObject: JobObjectFilter = {};
@ -176,6 +186,10 @@ export function jobSavedObjectServiceFactory(
filterObject.type = jobType;
}
if (jobId !== undefined) {
filterObject.job_id = jobId;
}
const { filter, searchFields } = createSavedObjectFilter(filterObject);
const options: SavedObjectsFindOptions = {
type: ML_SAVED_OBJECT_TYPE,