Skip to content
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

[ML] Fix synching jobs from other spaces #86086

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions x-pack/plugins/ml/server/saved_objects/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ export function jobSavedObjectServiceFactory(
await savedObjectsClient.delete(ML_SAVED_OBJECT_TYPE, job.id, { force: true });
}

async function _forceDeleteJob(jobType: JobType, jobId: string, namespace: string) {
const id = savedObjectId({
job_id: jobId,
datafeed_id: null,
type: jobType,
});

await internalSavedObjectsClient.delete(ML_SAVED_OBJECT_TYPE, id, {
namespace,
force: true,
});
}

async function createAnomalyDetectionJob(jobId: string, datafeedId?: string) {
await _createJob('anomaly-detector', jobId, datafeedId);
}
Expand All @@ -122,6 +135,10 @@ export function jobSavedObjectServiceFactory(
await _deleteJob('anomaly-detector', jobId);
}

async function forceDeleteAnomalyDetectionJob(jobId: string, namespace: string) {
await _forceDeleteJob('anomaly-detector', jobId, namespace);
}

async function createDataFrameAnalyticsJob(jobId: string) {
await _createJob('data-frame-analytics', jobId);
}
Expand All @@ -130,6 +147,10 @@ export function jobSavedObjectServiceFactory(
await _deleteJob('data-frame-analytics', jobId);
}

async function forceDeleteDataFrameAnalyticsJob(jobId: string, namespace: string) {
await _forceDeleteJob('data-frame-analytics', jobId, namespace);
}

async function bulkCreateJobs(jobs: Array<{ job: JobObject; namespaces: string[] }>) {
return await _bulkCreateJobs(jobs);
}
Expand Down Expand Up @@ -325,7 +346,9 @@ export function jobSavedObjectServiceFactory(
createAnomalyDetectionJob,
createDataFrameAnalyticsJob,
deleteAnomalyDetectionJob,
forceDeleteAnomalyDetectionJob,
deleteDataFrameAnalyticsJob,
forceDeleteDataFrameAnalyticsJob,
addDatafeed,
deleteDatafeed,
filterJobsForSpace,
Expand Down
16 changes: 12 additions & 4 deletions x-pack/plugins/ml/server/saved_objects/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ export function syncSavedObjectsFactory(
results.savedObjectsDeleted[job.jobId] = { success: true };
} else {
// Delete AD saved objects for jobs which no longer exist
const jobId = job.jobId;
const { jobId, namespaces } = job;
tasks.push(async () => {
try {
await jobSavedObjectService.deleteAnomalyDetectionJob(jobId);
if (namespaces !== undefined && namespaces.length) {
await jobSavedObjectService.forceDeleteAnomalyDetectionJob(jobId, namespaces[0]);
} else {
await jobSavedObjectService.deleteAnomalyDetectionJob(jobId);
}
results.savedObjectsDeleted[job.jobId] = { success: true };
} catch (error) {
results.savedObjectsDeleted[job.jobId] = {
Expand All @@ -115,10 +119,14 @@ export function syncSavedObjectsFactory(
results.savedObjectsDeleted[job.jobId] = { success: true };
} else {
// Delete DFA saved objects for jobs which no longer exist
const jobId = job.jobId;
const { jobId, namespaces } = job;
tasks.push(async () => {
try {
await jobSavedObjectService.deleteDataFrameAnalyticsJob(jobId);
if (namespaces !== undefined && namespaces.length) {
await jobSavedObjectService.forceDeleteDataFrameAnalyticsJob(jobId, namespaces[0]);
} else {
await jobSavedObjectService.deleteDataFrameAnalyticsJob(jobId);
}
results.savedObjectsDeleted[job.jobId] = { success: true };
} catch (error) {
results.savedObjectsDeleted[job.jobId] = {
Expand Down