Test for job existence before updating its state (elastic/elasticsearch#532)

* Test for job existence before updating its state

* Add unit tests covering expected missing job exceptions

Original commit: elastic/x-pack-elasticsearch@bcd270dafd
This commit is contained in:
David Kyle 2016-12-13 09:24:16 +00:00 committed by GitHub
parent bca06f0ad7
commit 9f73f047eb
4 changed files with 17 additions and 4 deletions

View File

@ -172,8 +172,6 @@ public class CloseJobAction extends Action<CloseJobAction.Request, CloseJobActio
@Override
protected void masterOperation(Request request, ClusterState state, ActionListener<Response> listener) throws Exception {
jobManager.getJobOrThrowIfUnknown(request.getJobId());
UpdateJobStatusAction.Request updateStatusRequest = new UpdateJobStatusAction.Request(request.getJobId(), JobStatus.CLOSING);
ActionListener<UpdateJobStatusAction.Response> delegateListener = ActionListener.wrap(
response -> respondWhenJobIsClosed(request.getJobId(), listener), listener::onFailure);

View File

@ -192,8 +192,6 @@ public class OpenJobAction extends Action<OpenJobAction.Request, OpenJobAction.R
@Override
protected void masterOperation(Request request, ClusterState state, ActionListener<Response> listener) throws Exception {
jobManager.getJobOrThrowIfUnknown(request.getJobId());
ActionListener<Response> delegateListener = ActionListener.wrap(response -> respondWhenJobIsOpened(request, listener),
listener::onFailure);
jobManager.openJob(request, delegateListener);

View File

@ -348,6 +348,10 @@ public class PrelertMetadata implements MetaData.Custom {
}
public Builder updateStatus(String jobId, JobStatus jobStatus, @Nullable String reason) {
if (jobs.containsKey(jobId) == false) {
throw ExceptionsHelper.missingJobException(jobId);
}
Allocation previous = allocations.get(jobId);
if (previous == null) {
throw new IllegalStateException("[" + jobId + "] no allocation exist to update the status to [" + jobStatus + "]");
@ -367,6 +371,10 @@ public class PrelertMetadata implements MetaData.Custom {
}
public Builder setIgnoreDowntime(String jobId) {
if (jobs.containsKey(jobId) == false) {
throw ExceptionsHelper.missingJobException(jobId);
}
Allocation allocation = allocations.get(jobId);
if (allocation == null) {
throw new IllegalStateException("[" + jobId + "] no allocation to ignore downtime");

View File

@ -201,4 +201,13 @@ public class PrelertMetadataTests extends AbstractSerializingTestCase<PrelertMet
assertThat(prelertMetadata.getJobs().get("_job_id").getFinishedTime(), notNullValue());
}
public void testUpdateStatus_failBecauseJobDoesNotExist() {
PrelertMetadata.Builder builder = new PrelertMetadata.Builder();
expectThrows(ResourceNotFoundException.class, () -> builder.updateStatus("missing-job", JobStatus.CLOSED, "for testting"));
}
public void testSetIgnoreDowntime_failBecauseJobDoesNotExist() {
PrelertMetadata.Builder builder = new PrelertMetadata.Builder();
expectThrows(ResourceNotFoundException.class, () -> builder.setIgnoreDowntime("missing-job"));
}
}