[NO ISSUE][API] Add NC Query Cancellation API
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
- Introduce NCQueryCancellationServlet that uses NC<->CC
messaging to cancel queries.
Change-Id: I75d326b6987ed87ccc4b58c171bd45cab33a4009
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2103
Reviewed-by: Michael Blow <mblow@apache.org>
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>
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
new file mode 100644
index 0000000..637c492
--- /dev/null
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/NCQueryCancellationServlet.java
@@ -0,0 +1,76 @@
+/*
+ * 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.api.http.server;
+
+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 java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.asterix.app.message.CancelQueryRequest;
+import org.apache.asterix.common.messaging.api.INCMessageBroker;
+import org.apache.asterix.common.messaging.api.MessageFuture;
+import org.apache.hyracks.api.application.INCServiceContext;
+import org.apache.hyracks.http.api.IServletRequest;
+import org.apache.hyracks.http.api.IServletResponse;
+
+import io.netty.handler.codec.http.HttpResponseStatus;
+
+/**
+ * The servlet provides a REST API on an NC for cancelling an on-going query.
+ */
+public class NCQueryCancellationServlet extends QueryCancellationServlet {
+ private static final Logger LOGGER = Logger.getLogger(NCQueryCancellationServlet.class.getName());
+ private final INCServiceContext serviceCtx;
+ private final INCMessageBroker messageBroker;
+
+ public NCQueryCancellationServlet(ConcurrentMap<String, Object> ctx, String... paths) {
+ super(ctx, paths);
+ this.serviceCtx = (INCServiceContext) ctx.get(ServletConstants.SERVICE_CONTEXT_ATTR);
+ messageBroker = (INCMessageBroker) serviceCtx.getMessageBroker();
+ }
+
+ @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);
+ return;
+ }
+ final MessageFuture cancelQueryFuture = messageBroker.registerMessageFuture();
+ try {
+ CancelQueryRequest cancelQueryMessage =
+ new CancelQueryRequest(serviceCtx.getNodeId(), cancelQueryFuture.getFutureId(), clientContextId);
+ messageBroker.sendMessageToCC(cancelQueryMessage);
+ cancelQueryFuture.get(DEFAULT_NC_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+ response.setStatus(HttpResponseStatus.OK);
+ } catch (Exception e) {
+ if (LOGGER.isLoggable(Level.SEVERE)) {
+ LOGGER.log(Level.SEVERE, "Unexpected exception while canceling query", e);
+ }
+ response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
+ } finally {
+ messageBroker.deregisterMessageFuture(cancelQueryFuture.getFutureId());
+ }
+ }
+}
\ No newline at end of file
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 3f07151..96ff71c 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
@@ -37,7 +37,7 @@
*/
public class QueryCancellationServlet extends AbstractServlet {
private static final Logger LOGGER = Logger.getLogger(QueryCancellationServlet.class.getName());
- private static final String CLIENT_CONTEXT_ID = "client_context_id";
+ protected static final String CLIENT_CONTEXT_ID = "client_context_id";
public QueryCancellationServlet(ConcurrentMap<String, Object> ctx, String... paths) {
super(ctx, paths);