asterixdb-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Wail Alkowaileet (Code Review)" <do-not-re...@asterixdb.incubator.apache.org>
Subject Change in asterixdb[master]: Add result location HTTP API.
Date Thu, 15 Sep 2016 17:07:22 GMT
Wail Alkowaileet has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/1178

Change subject: Add result location HTTP API.
......................................................................

Add result location HTTP API.

This addition allows other systems to connect to AsterixDB
and collect the result locations of a an async query.

Change-Id: If5d691635c6003947b4eb06291f620127de1eef8
---
A asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryResultLocationsAPIServlets.java
M asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
M asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/ServletUtil.java
3 files changed, 160 insertions(+), 0 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/78/1178/1

diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryResultLocationsAPIServlets.java
b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryResultLocationsAPIServlets.java
new file mode 100644
index 0000000..da0eaa2
--- /dev/null
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/servlet/QueryResultLocationsAPIServlets.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2009-2013 by The Regents of the University of California
+ * Licensed 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 from
+ *
+ *     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.servlet;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.asterix.app.result.ResultReader;
+import org.apache.hyracks.api.client.HyracksConnection;
+import org.apache.hyracks.api.client.IHyracksClientConnection;
+import org.apache.hyracks.api.comm.NetworkAddress;
+import org.apache.hyracks.api.dataset.DatasetDirectoryRecord;
+import org.apache.hyracks.api.dataset.DatasetJobRecord.Status;
+import org.apache.hyracks.api.dataset.IHyracksDataset;
+import org.apache.hyracks.api.dataset.IHyracksDatasetDirectoryServiceConnection;
+import org.apache.hyracks.api.dataset.ResultSetId;
+import org.apache.hyracks.api.job.JobId;
+import org.apache.hyracks.client.dataset.HyracksDataset;
+import org.apache.hyracks.client.dataset.HyracksDatasetDirectoryServiceConnection;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * A servlet to get the result locations.
+ */
+public class QueryResultLocationsAPIServlets extends HttpServlet {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final String HYRACKS_CONNECTION_ATTR = "org.apache.asterix.HYRACKS_CONNECTION";
+
+    private static final String HYRACKS_DATASET_ATTR = "org.apache.asterix.HYRACKS_DATASET";
+
+    @Override
+    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
+        response.setContentType("text/html");
+        response.setCharacterEncoding("utf-8");
+        String strHandle = request.getParameter("handle");
+        PrintWriter out = response.getWriter();
+        ServletContext context = getServletContext();
+        IHyracksClientConnection hcc;
+        IHyracksDataset hds;
+
+        try {
+            HyracksProperties hp = new HyracksProperties();
+            String strIP = hp.getHyracksIPAddress();
+            int port = hp.getHyracksPort();
+
+            synchronized (context) {
+                hcc = (IHyracksClientConnection) context.getAttribute(HYRACKS_CONNECTION_ATTR);
+                if (hcc == null) {
+                    hcc = new HyracksConnection(strIP, port);
+                    context.setAttribute(HYRACKS_CONNECTION_ATTR, hcc);
+                }
+
+                hds = (IHyracksDataset) context.getAttribute(HYRACKS_DATASET_ATTR);
+                if (hds == null) {
+                    hds = new HyracksDataset(hcc, ResultReader.FRAME_SIZE, ResultReader.NUM_READERS);
+                    context.setAttribute(HYRACKS_DATASET_ATTR, hds);
+                }
+            }
+            JSONObject handleObj = new JSONObject(strHandle);
+            JSONArray handle = handleObj.getJSONArray("handle");
+            JobId jobId = new JobId(handle.getLong(0));
+            ResultSetId rsId = new ResultSetId(handle.getLong(1));
+
+            ResultReader resultReader = new ResultReader(hds);
+            resultReader.open(jobId, rsId);
+
+            JSONObject jsonResponse = new JSONObject();
+            String status;
+            switch (resultReader.getStatus()) {
+                case RUNNING:
+                    status = "RUNNING";
+                    break;
+                case FAILED:
+                    status = "ERROR";
+                    break;
+                case SUCCESS:
+                    status = "SUCCESS";
+                    break;
+                default:
+                    status = "ERROR";
+            }
+
+            DatasetDirectoryRecord[] locs = getResultsLocations(hcc, jobId, rsId);
+            if (resultReader.getStatus() == Status.SUCCESS && isAllReady(locs)) {
+                prepareLocations(jsonResponse, locs);
+            } else if (resultReader.getStatus() == Status.SUCCESS) {
+                jsonResponse.put("status", "RUNNING");
+            } else {
+                jsonResponse.put("status", status);
+            }
+
+            out.write(jsonResponse.toString());
+
+        } catch (Exception e) {
+            out.println(e.getMessage());
+            e.printStackTrace(out);
+        } finally {
+            out.close();
+        }
+    }
+
+    private DatasetDirectoryRecord[] getResultsLocations(IHyracksClientConnection hcc, JobId
jobId,
+            ResultSetId resultSetId) throws Exception {
+        NetworkAddress ddsAddress = hcc.getDatasetDirectoryServiceInfo();
+        IHyracksDatasetDirectoryServiceConnection ddsc = new HyracksDatasetDirectoryServiceConnection(
+                ddsAddress.getAddress(), ddsAddress.getPort());
+
+        return ddsc.getDatasetResultLocations(jobId, resultSetId, null);
+
+    }
+
+    private boolean isAllReady(DatasetDirectoryRecord[] locs) {
+        for (DatasetDirectoryRecord loc : locs) {
+            if (loc == null) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    private void prepareLocations(JSONObject jsonResponse, DatasetDirectoryRecord[] locs)
throws JSONException {
+        JSONArray locsJsonArray = new JSONArray();
+        for (DatasetDirectoryRecord loc : locs) {
+            JSONObject locJsonObject = new JSONObject();
+            locJsonObject.put("address", loc.getNetworkAddress().getAddress());
+            locJsonObject.put("port", loc.getNetworkAddress().getPort());
+            locsJsonArray.put(locJsonObject);
+        }
+
+        jsonResponse.put("locations", locsJsonArray);
+    }
+
+}
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
index bd910a5..755fbf4 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/hyracks/bootstrap/CCApplicationEntryPoint.java
@@ -39,6 +39,7 @@
 import org.apache.asterix.api.http.servlet.FeedServlet;
 import org.apache.asterix.api.http.servlet.QueryAPIServlet;
 import org.apache.asterix.api.http.servlet.QueryResultAPIServlet;
+import org.apache.asterix.api.http.servlet.QueryResultLocationsAPIServlets;
 import org.apache.asterix.api.http.servlet.QueryServiceServlet;
 import org.apache.asterix.api.http.servlet.QueryStatusAPIServlet;
 import org.apache.asterix.api.http.servlet.QueryWebInterfaceServlet;
@@ -214,6 +215,7 @@
         // Other APIs.
         addServlet(context, Servlets.QUERY_STATUS);
         addServlet(context, Servlets.QUERY_RESULT);
+        addServlet(context, Servlets.QUERY_RESULT_LOCATIONS);
         addServlet(context, Servlets.QUERY_SERVICE);
         addServlet(context, Servlets.CONNECTOR);
         addServlet(context, Servlets.SHUTDOWN);
@@ -283,6 +285,8 @@
                 return new QueryStatusAPIServlet();
             case QUERY_RESULT:
                 return new QueryResultAPIServlet();
+            case QUERY_RESULT_LOCATIONS:
+                return new QueryResultLocationsAPIServlets();
             case QUERY_SERVICE:
                 return new QueryServiceServlet(ccExtensionManager.getSqlppCompilationProvider(),
                         ccExtensionManager.getQueryTranslatorFactory());
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/ServletUtil.java
b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/ServletUtil.java
index 5436dc7..3954b72 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/ServletUtil.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/utils/ServletUtil.java
@@ -31,6 +31,7 @@
         SQLPP_DDL("/ddl/sqlpp"),
         QUERY_STATUS("/query/status"),
         QUERY_RESULT("/query/result"),
+        QUERY_RESULT_LOCATIONS("/query/result/location"),
         QUERY_SERVICE("/query/service"),
         CONNECTOR("/connector"),
         SHUTDOWN("/admin/shutdown"),

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1178
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If5d691635c6003947b4eb06291f620127de1eef8
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Wail Alkowaileet <wael.y.k@gmail.com>

Mime
View raw message