From 4f6bd4d6608f74d11abadad4f51e61a68ad47a60 Mon Sep 17 00:00:00 2001 From: Alisherova Madinabonu <54506754+MadinaBonuAlisherova@users.noreply.github.com> Date: Wed, 26 Apr 2023 20:48:39 +0500 Subject: [PATCH 1/2] Delete-job-details changes --- .../rest/action/DeleteJobDetailsAPI.java | 138 ++++++++++++++++++ .../rest/request/DeleteJobDetailsRequest.java | 63 ++++++++ 2 files changed, 201 insertions(+) create mode 100644 src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java create mode 100644 src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java diff --git a/src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java b/src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java new file mode 100644 index 00000000..6ec64f63 --- /dev/null +++ b/src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java @@ -0,0 +1,138 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.jobscheduler.rest.action; + +import com.google.common.collect.ImmutableList; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.opensearch.action.ActionListener; +import org.opensearch.client.node.NodeClient; +import org.opensearch.core.xcontent.XContentBuilder; +import org.opensearch.core.xcontent.XContentParser; +import org.opensearch.jobscheduler.JobSchedulerPlugin; +import org.opensearch.jobscheduler.rest.request.DeleteJobDetailsRequest; +import org.opensearch.jobscheduler.rest.request.GetJobDetailsRequest; +import org.opensearch.jobscheduler.utils.JobDetailsService; +import org.opensearch.rest.BaseRestHandler; +import org.opensearch.rest.BytesRestResponse; +import org.opensearch.rest.RestRequest; +import org.opensearch.rest.RestStatus; + +import java.io.IOException; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; +import static org.opensearch.jobscheduler.rest.request.DeleteJobDetailsRequest.DOCUMENT_ID; +import static org.opensearch.rest.RestRequest.Method.DELETE; +import static org.opensearch.rest.RestRequest.Method.PUT; + +public class DeleteJobDetailsAPI extends BaseRestHandler { + + public static final String DELETE_JOB_DETAILS_API = "delete_job_details_api"; + + private final Logger logger = LogManager.getLogger(RestGetJobDetailsAction.class); + + public JobDetailsService jobDetailsService; + + public DeleteJobDetailsAPI(final JobDetailsService jobDetailsService) { + this.jobDetailsService = jobDetailsService; + } + + @Override + public String getName() { + return DELETE_JOB_DETAILS_API; + } + + @Override + public List routes() { + return ImmutableList.of( + // Delete Job Details Entry Request + new Route( + DELETE, + String.format(Locale.ROOT, "%s/%s/{%s}", JobSchedulerPlugin.JS_BASE_URI, "_job_details", DOCUMENT_ID) + ) + + ); + } + + @VisibleForTesting + @Override + protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient nodeClient) throws IOException { + XContentParser parser = restRequest.contentParser(); + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); + + DeleteJobDetailsRequest deleteJobDetailsRequest = DeleteJobDetailsRequest.parse(parser); + String documentId = restRequest.param(DOCUMENT_ID); + + CompletableFuture inProgressFuture = new CompletableFuture<>(); + + jobDetailsService.deleteJobDetails( + documentId, + new ActionListener<>() { + + @Override + public void onResponse(Boolean aBoolean) { + inProgressFuture.complete(aBoolean); + } + + @Override + public void onFailure(Exception e) { + logger.info("could not process job index", e); + inProgressFuture.completeExceptionally(e); + } + } + ); + + try { + inProgressFuture.orTimeout(JobDetailsService.TIME_OUT_FOR_REQUEST, TimeUnit.SECONDS); + } catch (CompletionException e) { + if (e.getCause() instanceof TimeoutException) { + logger.error("Delete Job Details timed out ", e); + } + if (e.getCause() instanceof RuntimeException) { + throw (RuntimeException) e.getCause(); + } else if (e.getCause() instanceof Error) { + throw (Error) e.getCause(); + } else { + throw new RuntimeException(e.getCause()); + } + } + + return channel -> { + Boolean jobDetailsResponseHolder = false; + try { + jobDetailsResponseHolder = inProgressFuture.get(); + } catch (Exception e) { + logger.error("Exception occured in get job details ", e); + } + XContentBuilder builder = channel.newBuilder(); + RestStatus restStatus = RestStatus.OK; + String restResponseString = jobDetailsResponseHolder != false ? "DELETED" : "NOT_FOUND"; + BytesRestResponse bytesRestResponse; + try { + builder.startObject(); + builder.field("response", restResponseString); + if (restResponseString.equals("DELETED")) { + builder.field(GetJobDetailsRequest.DOCUMENT_ID, jobDetailsResponseHolder); + } else { + restStatus = RestStatus.INTERNAL_SERVER_ERROR; + } + builder.endObject(); + bytesRestResponse = new BytesRestResponse(restStatus, builder); + } finally { + builder.close(); + } + + channel.sendResponse(bytesRestResponse); + }; + + } +} diff --git a/src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java b/src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java new file mode 100644 index 00000000..69750d7b --- /dev/null +++ b/src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java @@ -0,0 +1,63 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.jobscheduler.rest.request; + +import org.opensearch.action.ActionRequest; +import org.opensearch.action.ActionRequestValidationException; +import org.opensearch.common.xcontent.XContentParserUtils; +import org.opensearch.core.xcontent.XContentParser; + +import java.io.IOException; + + +public class DeleteJobDetailsRequest extends ActionRequest { + + public static String documentId; + + public static final String DOCUMENT_ID = "document_id"; + + public DeleteJobDetailsRequest( + String documentId + ) { + super(); + this.documentId = documentId; + } + + public static DeleteJobDetailsRequest parse(XContentParser parser) throws IOException { + XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); + while (parser.nextToken() != XContentParser.Token.END_OBJECT) { + String fieldName = parser.currentName(); + parser.nextToken(); + + switch (fieldName) { + case DOCUMENT_ID: + documentId = parser.textOrNull(); + break; + default: + parser.skipChildren(); + break; + } + + } + return new DeleteJobDetailsRequest(documentId); + + } + + + @Override + public ActionRequestValidationException validate() { + return null; + } + + public String getDocumentId() { + return documentId; + } + + public void setDocumentId(String documentId) { + this.documentId = documentId; + } + +} From d1292da8d39f5e77fd73eeb606fd492675f0f1e5 Mon Sep 17 00:00:00 2001 From: Alisherova Madinabonu <54506754+MadinaBonuAlisherova@users.noreply.github.com> Date: Wed, 10 May 2023 21:29:41 +0500 Subject: [PATCH 2/2] spotlesApply Signed-off-by: Alisherova Madinabonu <54506754+MadinaBonuAlisherova@users.noreply.github.com> --- .../rest/action/DeleteJobDetailsAPI.java | 38 +++++++++---------- .../rest/request/DeleteJobDetailsRequest.java | 11 +++--- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java b/src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java index 6ec64f63..4504bf13 100644 --- a/src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java +++ b/src/main/java/org/opensearch/jobscheduler/rest/action/DeleteJobDetailsAPI.java @@ -1,8 +1,11 @@ /* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. */ - package org.opensearch.jobscheduler.rest.action; import com.google.common.collect.ImmutableList; @@ -32,7 +35,6 @@ import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.opensearch.jobscheduler.rest.request.DeleteJobDetailsRequest.DOCUMENT_ID; import static org.opensearch.rest.RestRequest.Method.DELETE; -import static org.opensearch.rest.RestRequest.Method.PUT; public class DeleteJobDetailsAPI extends BaseRestHandler { @@ -54,11 +56,8 @@ public String getName() { @Override public List routes() { return ImmutableList.of( - // Delete Job Details Entry Request - new Route( - DELETE, - String.format(Locale.ROOT, "%s/%s/{%s}", JobSchedulerPlugin.JS_BASE_URI, "_job_details", DOCUMENT_ID) - ) + // Delete Job Details Entry Request + new Route(DELETE, String.format(Locale.ROOT, "%s/%s/{%s}", JobSchedulerPlugin.JS_BASE_URI, "_job_details", DOCUMENT_ID)) ); } @@ -74,22 +73,19 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient CompletableFuture inProgressFuture = new CompletableFuture<>(); - jobDetailsService.deleteJobDetails( - documentId, - new ActionListener<>() { + jobDetailsService.deleteJobDetails(documentId, new ActionListener<>() { - @Override - public void onResponse(Boolean aBoolean) { - inProgressFuture.complete(aBoolean); - } + @Override + public void onResponse(Boolean aBoolean) { + inProgressFuture.complete(aBoolean); + } - @Override - public void onFailure(Exception e) { - logger.info("could not process job index", e); - inProgressFuture.completeExceptionally(e); - } - } - ); + @Override + public void onFailure(Exception e) { + logger.info("could not process job index", e); + inProgressFuture.completeExceptionally(e); + } + }); try { inProgressFuture.orTimeout(JobDetailsService.TIME_OUT_FOR_REQUEST, TimeUnit.SECONDS); diff --git a/src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java b/src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java index 69750d7b..397b0777 100644 --- a/src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java +++ b/src/main/java/org/opensearch/jobscheduler/rest/request/DeleteJobDetailsRequest.java @@ -1,8 +1,11 @@ /* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. */ - package org.opensearch.jobscheduler.rest.request; import org.opensearch.action.ActionRequest; @@ -12,16 +15,13 @@ import java.io.IOException; - public class DeleteJobDetailsRequest extends ActionRequest { public static String documentId; public static final String DOCUMENT_ID = "document_id"; - public DeleteJobDetailsRequest( - String documentId - ) { + public DeleteJobDetailsRequest(String documentId) { super(); this.documentId = documentId; } @@ -46,7 +46,6 @@ public static DeleteJobDetailsRequest parse(XContentParser parser) throws IOExce } - @Override public ActionRequestValidationException validate() { return null;