[NO ISSUE][API] Return Not Found on NC Cancellation Requests
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
- Return not found on cancellation requests with already
terminated/missing jobs.
Change-Id: I50b05168001b6999d4aab04ca1604ffe6b07de24
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2423
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/NCQueryCancellationServlet.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/NCQueryCancellationServlet.java
index c3e02af..da621d2 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/NCQueryCancellationServlet.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/NCQueryCancellationServlet.java
@@ -20,13 +20,14 @@
import static org.apache.asterix.app.message.ExecuteStatementRequestMessage.DEFAULT_NC_TIMEOUT_MILLIS;
-import java.io.IOException;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import org.apache.asterix.app.message.CancelQueryRequest;
+import org.apache.asterix.app.message.CancelQueryResponse;
import org.apache.asterix.common.messaging.api.INCMessageBroker;
import org.apache.asterix.common.messaging.api.MessageFuture;
+import org.apache.asterix.common.utils.RequestStatus;
import org.apache.hyracks.api.application.INCServiceContext;
import org.apache.hyracks.http.api.IServletRequest;
import org.apache.hyracks.http.api.IServletResponse;
@@ -51,7 +52,7 @@
}
@Override
- protected void delete(IServletRequest request, IServletResponse response) throws IOException {
+ protected void delete(IServletRequest request, IServletResponse response) {
// gets the parameter client_context_id from the request.
String clientContextId = request.getParameter(CLIENT_CONTEXT_ID);
if (clientContextId == null) {
@@ -64,8 +65,18 @@
new CancelQueryRequest(serviceCtx.getNodeId(), cancelQueryFuture.getFutureId(), clientContextId);
// TODO(mblow): multicc -- need to send cancellation to the correct cc
messageBroker.sendMessageToPrimaryCC(cancelQueryMessage);
- cancelQueryFuture.get(DEFAULT_NC_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
- response.setStatus(HttpResponseStatus.OK);
+ CancelQueryResponse cancelResponse =
+ (CancelQueryResponse) cancelQueryFuture.get(DEFAULT_NC_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+ final RequestStatus status = cancelResponse.getStatus();
+ switch (status) {
+ case SUCCESS:
+ case FAILED:
+ case NOT_FOUND:
+ response.setStatus(status.toHttpResponse());
+ break;
+ default:
+ throw new IllegalStateException("Unrecognized status: " + status);
+ }
} catch (Exception e) {
LOGGER.log(Level.ERROR, "Unexpected exception while canceling query", e);
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryCancellationServlet.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryCancellationServlet.java
index 8fd40b3..f8655ad 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryCancellationServlet.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryCancellationServlet.java
@@ -46,7 +46,6 @@
@Override
protected void delete(IServletRequest request, IServletResponse response) throws IOException {
- // gets the parameter client_context_id from the request.
String clientContextId = request.getParameter(CLIENT_CONTEXT_ID);
if (clientContextId == null) {
response.setStatus(HttpResponseStatus.BAD_REQUEST);
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryRequest.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryRequest.java
index 7e76fc1..a1c87d8 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryRequest.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryRequest.java
@@ -20,6 +20,7 @@
import org.apache.asterix.common.dataflow.ICcApplicationContext;
import org.apache.asterix.common.messaging.api.ICcAddressedMessage;
+import org.apache.asterix.common.utils.RequestStatus;
import org.apache.asterix.hyracks.bootstrap.CCApplication;
import org.apache.asterix.messaging.CCMessageBroker;
import org.apache.asterix.translator.IStatementExecutorContext;
@@ -51,19 +52,23 @@
CCApplication application = (CCApplication) ccs.getApplication();
IStatementExecutorContext executorsCtx = application.getStatementExecutorContext();
JobId jobId = executorsCtx.getJobIdFromClientContextId(contextId);
+ RequestStatus status;
if (jobId == null) {
LOGGER.log(Level.WARN, "No job found for context id " + contextId);
+ status = RequestStatus.NOT_FOUND;
} else {
try {
IHyracksClientConnection hcc = application.getHcc();
hcc.cancelJob(jobId);
executorsCtx.removeJobIdFromClientContextId(contextId);
+ status = RequestStatus.SUCCESS;
} catch (Exception e) {
LOGGER.log(Level.WARN, "unexpected exception thrown from cancel", e);
+ status = RequestStatus.FAILED;
}
}
- CancelQueryResponse response = new CancelQueryResponse(reqId);
+ CancelQueryResponse response = new CancelQueryResponse(reqId, status);
CCMessageBroker messageBroker = (CCMessageBroker) appCtx.getServiceContext().getMessageBroker();
try {
messageBroker.sendApplicationMessageToNC(response, nodeId);
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryResponse.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryResponse.java
index 4fbcf22..d65ae31 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryResponse.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/message/CancelQueryResponse.java
@@ -21,6 +21,7 @@
import org.apache.asterix.common.api.INcApplicationContext;
import org.apache.asterix.common.messaging.api.INcAddressedMessage;
import org.apache.asterix.common.messaging.api.MessageFuture;
+import org.apache.asterix.common.utils.RequestStatus;
import org.apache.asterix.messaging.NCMessageBroker;
import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -28,9 +29,11 @@
private static final long serialVersionUID = 1L;
private final long reqId;
+ private final RequestStatus status;
- public CancelQueryResponse(long reqId) {
+ public CancelQueryResponse(long reqId, RequestStatus status) {
this.reqId = reqId;
+ this.status = status;
}
@Override
@@ -41,4 +44,9 @@
future.complete(this);
}
}
+
+ public RequestStatus getStatus() {
+ return status;
+ }
+
}
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/RequestStatus.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/RequestStatus.java
new file mode 100644
index 0000000..52dfe90
--- /dev/null
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/RequestStatus.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.asterix.common.utils;
+
+import io.netty.handler.codec.http.HttpResponseStatus;
+
+public enum RequestStatus {
+ SUCCESS,
+ FAILED,
+ NOT_FOUND;
+
+ public HttpResponseStatus toHttpResponse() {
+ switch (this) {
+ case SUCCESS:
+ return HttpResponseStatus.OK;
+ case FAILED:
+ return HttpResponseStatus.INTERNAL_SERVER_ERROR;
+ case NOT_FOUND:
+ return HttpResponseStatus.NOT_FOUND;
+ default:
+ throw new IllegalStateException("Unrecognized status: " + this);
+ }
+ }
+}
\ No newline at end of file