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] Delete job document #34595

Merged
merged 3 commits into from
Oct 19, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ private void normalDeleteJob(ParentTaskAssigningClient parentTaskClient, DeleteJ

// Step 3. When the physical storage has been deleted, delete the job config document
// -------
CheckedConsumer<Boolean, Exception> deleteJobStateHandler = response -> jobConfigProvider.deleteJob(jobId,
// Don't report an error if the document has already been deleted
CheckedConsumer<Boolean, Exception> deleteJobStateHandler = response -> jobConfigProvider.deleteJob(jobId, false,
ActionListener.wrap(
deleteResponse -> apiResponseHandler.accept(Boolean.TRUE),
listener::onFailure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,37 @@ public void onFailure(Exception e) {
}

/**
* Delete the anomaly detector job config document
* Delete the anomaly detector job config document.
* {@code errorIfMissing} controls whether or not an error if return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: if return -> is returned

* if the document does not exist.
*
* @param jobId The job id
* @param errorIfMissing If the job document does not exist and this this true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: this this

* listener fails with a ResourceNotFoundException else
* the DeleteResponse is always return.
* @param actionListener Deleted job listener
*/
public void deleteJob(String jobId, ActionListener<DeleteResponse> actionListener) {
public void deleteJob(String jobId, boolean errorIfMissing, ActionListener<DeleteResponse> actionListener) {
DeleteRequest request = new DeleteRequest(AnomalyDetectorsIndex.configIndexName(),
ElasticsearchMappings.DOC_TYPE, Job.documentId(jobId));

executeAsyncWithOrigin(client, ML_ORIGIN, DeleteAction.INSTANCE, request, actionListener);
executeAsyncWithOrigin(client, ML_ORIGIN, DeleteAction.INSTANCE, request, new ActionListener<DeleteResponse>() {
@Override
public void onResponse(DeleteResponse deleteResponse) {
if (errorIfMissing) {
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
actionListener.onFailure(ExceptionsHelper.missingJobException(jobId));
return;
}
assert deleteResponse.getResult() == DocWriteResponse.Result.DELETED;
}
actionListener.onResponse(deleteResponse);
}
@Override
public void onFailure(Exception e) {
actionListener.onFailure(e);
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void testCrud() throws InterruptedException {

// Delete Job
AtomicReference<DeleteResponse> deleteJobResponseHolder = new AtomicReference<>();
blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, actionListener),
blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, true, actionListener),
deleteJobResponseHolder, exceptionHolder);
assertNull(exceptionHolder.get());
assertThat(deleteJobResponseHolder.get().getResult(), equalTo(DocWriteResponse.Result.DELETED));
Expand All @@ -172,7 +172,15 @@ public void testCrud() throws InterruptedException {
// Delete deleted job
deleteJobResponseHolder.set(null);
exceptionHolder.set(null);
blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, actionListener),
blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, true, actionListener),
deleteJobResponseHolder, exceptionHolder);
assertNull(deleteJobResponseHolder.get());
assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass());

// and again with errorIfMissing set false
deleteJobResponseHolder.set(null);
exceptionHolder.set(null);
blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, false, actionListener),
deleteJobResponseHolder, exceptionHolder);
assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteJobResponseHolder.get().getResult());
}
Expand Down