Search in sources :

Example 1 with ResultReader

use of org.apache.asterix.app.result.ResultReader in project asterixdb by apache.

the class QueryTranslator method deliverResult.

private void deliverResult(IHyracksClientConnection hcc, IHyracksDataset hdc, IStatementCompiler compiler, MetadataProvider metadataProvider, IMetadataLocker locker, ResultDelivery resultDelivery, ResultMetadata outMetadata, Stats stats, String clientContextId, IStatementExecutorContext ctx) throws Exception {
    final ResultSetId resultSetId = metadataProvider.getResultSetId();
    switch(resultDelivery) {
        case ASYNC:
            MutableBoolean printed = new MutableBoolean(false);
            executorService.submit(() -> asyncCreateAndRunJob(hcc, compiler, locker, resultDelivery, clientContextId, ctx, resultSetId, printed));
            synchronized (printed) {
                while (!printed.booleanValue()) {
                    printed.wait();
                }
            }
            break;
        case IMMEDIATE:
            createAndRunJob(hcc, null, compiler, locker, resultDelivery, id -> {
                final ResultReader resultReader = new ResultReader(hdc, id, resultSetId);
                ResultUtil.printResults(appCtx, resultReader, sessionOutput, stats, metadataProvider.findOutputRecordType());
            }, clientContextId, ctx);
            break;
        case DEFERRED:
            createAndRunJob(hcc, null, compiler, locker, resultDelivery, id -> {
                ResultUtil.printResultHandle(sessionOutput, new ResultHandle(id, resultSetId));
                if (outMetadata != null) {
                    outMetadata.getResultSets().add(Triple.of(id, resultSetId, metadataProvider.findOutputRecordType()));
                }
            }, clientContextId, ctx);
            break;
        default:
            break;
    }
}
Also used : ResultReader(org.apache.asterix.app.result.ResultReader) ResultSetId(org.apache.hyracks.api.dataset.ResultSetId) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) ResultHandle(org.apache.asterix.app.result.ResultHandle)

Example 2 with ResultReader

use of org.apache.asterix.app.result.ResultReader in project asterixdb by apache.

the class QueryStatusApiServlet method get.

@Override
protected void get(IServletRequest request, IServletResponse response) throws Exception {
    final String strHandle = localPath(request);
    final ResultHandle handle = ResultHandle.parse(strHandle);
    if (handle == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return;
    }
    IHyracksDataset hds = getHyracksDataset();
    ResultReader resultReader = new ResultReader(hds, handle.getJobId(), handle.getResultSetId());
    final DatasetJobRecord.Status resultReaderStatus = resultReader.getStatus();
    if (resultReaderStatus == null) {
        LOGGER.log(Level.INFO, "No results for: \"" + strHandle + "\"");
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        return;
    }
    ResultStatus resultStatus = resultStatus(resultReaderStatus);
    Exception ex = extractException(resultReaderStatus);
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter resultWriter = new PrintWriter(stringWriter);
    HttpUtil.setContentType(response, HttpUtil.ContentType.APPLICATION_JSON, HttpUtil.Encoding.UTF8);
    HttpResponseStatus httpStatus = HttpResponseStatus.OK;
    resultWriter.print("{\n");
    ResultUtil.printStatus(resultWriter, resultStatus, (ex != null) || ResultStatus.SUCCESS == resultStatus);
    if (ResultStatus.SUCCESS == resultStatus) {
        String servletPath = servletPath(request).replace("status", "result");
        String resHandle = "http://" + host(request) + servletPath + strHandle;
        printHandle(resultWriter, resHandle, false);
    } else if (ex != null) {
        ResultUtil.printError(resultWriter, ex, false);
    }
    resultWriter.print("}\n");
    resultWriter.flush();
    String result = stringWriter.toString();
    response.setStatus(httpStatus);
    response.writer().print(result);
    if (response.writer().checkError()) {
        LOGGER.warning("Error flushing output writer");
    }
}
Also used : ResultReader(org.apache.asterix.app.result.ResultReader) StringWriter(java.io.StringWriter) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DatasetJobRecord(org.apache.hyracks.api.dataset.DatasetJobRecord) ResultHandle(org.apache.asterix.app.result.ResultHandle) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) PrintWriter(java.io.PrintWriter)

Example 3 with ResultReader

use of org.apache.asterix.app.result.ResultReader in project asterixdb by apache.

the class NCQueryServiceServlet method executeStatement.

@Override
protected void executeStatement(String statementsText, SessionOutput sessionOutput, IStatementExecutor.ResultDelivery delivery, IStatementExecutor.Stats stats, RequestParameters param, String handleUrl, long[] outExecStartEnd) throws Exception {
    // Running on NC -> send 'execute' message to CC
    INCServiceContext ncCtx = (INCServiceContext) serviceCtx;
    INCMessageBroker ncMb = (INCMessageBroker) ncCtx.getMessageBroker();
    IStatementExecutor.ResultDelivery ccDelivery = delivery == IStatementExecutor.ResultDelivery.IMMEDIATE ? IStatementExecutor.ResultDelivery.DEFERRED : delivery;
    ExecuteStatementResponseMessage responseMsg;
    MessageFuture responseFuture = ncMb.registerMessageFuture();
    try {
        ExecuteStatementRequestMessage requestMsg = new ExecuteStatementRequestMessage(ncCtx.getNodeId(), responseFuture.getFutureId(), queryLanguage, statementsText, sessionOutput.config(), ccDelivery, param.clientContextID, handleUrl);
        outExecStartEnd[0] = System.nanoTime();
        ncMb.sendMessageToCC(requestMsg);
        responseMsg = (ExecuteStatementResponseMessage) responseFuture.get(ExecuteStatementResponseMessage.DEFAULT_TIMEOUT_MILLIS, java.util.concurrent.TimeUnit.MILLISECONDS);
        outExecStartEnd[1] = System.nanoTime();
    } finally {
        ncMb.deregisterMessageFuture(responseFuture.getFutureId());
    }
    Throwable err = responseMsg.getError();
    if (err != null) {
        if (err instanceof Error) {
            throw (Error) err;
        } else if (err instanceof Exception) {
            throw (Exception) err;
        } else {
            throw new Exception(err.toString(), err);
        }
    }
    IStatementExecutor.ResultMetadata resultMetadata = responseMsg.getMetadata();
    if (delivery == IStatementExecutor.ResultDelivery.IMMEDIATE && !resultMetadata.getResultSets().isEmpty()) {
        for (Triple<JobId, ResultSetId, ARecordType> rsmd : resultMetadata.getResultSets()) {
            ResultReader resultReader = new ResultReader(getHyracksDataset(), rsmd.getLeft(), rsmd.getMiddle());
            ResultUtil.printResults(appCtx, resultReader, sessionOutput, stats, rsmd.getRight());
        }
    } else {
        sessionOutput.out().append(responseMsg.getResult());
    }
}
Also used : ResultReader(org.apache.asterix.app.result.ResultReader) ExecuteStatementResponseMessage(org.apache.asterix.app.message.ExecuteStatementResponseMessage) INCMessageBroker(org.apache.asterix.common.messaging.api.INCMessageBroker) ExecuteStatementRequestMessage(org.apache.asterix.app.message.ExecuteStatementRequestMessage) IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) INCServiceContext(org.apache.hyracks.api.application.INCServiceContext) ResultSetId(org.apache.hyracks.api.dataset.ResultSetId) MessageFuture(org.apache.asterix.common.messaging.api.MessageFuture) ARecordType(org.apache.asterix.om.types.ARecordType) JobId(org.apache.hyracks.api.job.JobId)

Example 4 with ResultReader

use of org.apache.asterix.app.result.ResultReader in project asterixdb by apache.

the class QueryResultApiServlet method get.

@Override
protected void get(IServletRequest request, IServletResponse response) throws Exception {
    // TODO this seems wrong ...
    HttpUtil.setContentType(response, HttpUtil.ContentType.TEXT_HTML, HttpUtil.Encoding.UTF8);
    PrintWriter out = response.writer();
    final String strHandle = localPath(request);
    final ResultHandle handle = ResultHandle.parse(strHandle);
    if (handle == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return;
    }
    IHyracksDataset hds = getHyracksDataset();
    ResultReader resultReader = new ResultReader(hds, handle.getJobId(), handle.getResultSetId());
    try {
        DatasetJobRecord.Status status = resultReader.getStatus();
        final HttpResponseStatus httpStatus;
        if (status == null) {
            httpStatus = HttpResponseStatus.NOT_FOUND;
        } else {
            switch(status.getState()) {
                case SUCCESS:
                    httpStatus = HttpResponseStatus.OK;
                    break;
                case RUNNING:
                case IDLE:
                case FAILED:
                    httpStatus = HttpResponseStatus.NOT_FOUND;
                    break;
                default:
                    httpStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
                    break;
            }
        }
        response.setStatus(httpStatus);
        if (httpStatus != HttpResponseStatus.OK) {
            return;
        }
        // QQQ The output format is determined by the initial
        // query and cannot be modified here, so calling back to
        // initResponse() is really an error. We need to find a
        // way to send the same OutputFormat value here as was
        // originally determined there. Need to save this value on
        // some object that we can obtain here.
        SessionOutput sessionOutput = RestApiServlet.initResponse(request, response);
        ResultUtil.printResults(appCtx, resultReader, sessionOutput, new Stats(), null);
    } catch (HyracksDataException e) {
        final int errorCode = e.getErrorCode();
        if (ErrorCode.NO_RESULTSET == errorCode) {
            LOGGER.log(Level.INFO, "No results for: \"" + strHandle + "\"");
            response.setStatus(HttpResponseStatus.NOT_FOUND);
            return;
        }
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        out.println(e.getMessage());
        LOGGER.log(Level.WARNING, "Error retrieving result for \"" + strHandle + "\"", e);
    } catch (Exception e) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        LOGGER.log(Level.WARNING, "Error retrieving result for \"" + strHandle + "\"", e);
    }
    if (out.checkError()) {
        LOGGER.warning("Error flushing output writer for \"" + strHandle + "\"");
    }
}
Also used : ResultReader(org.apache.asterix.app.result.ResultReader) SessionOutput(org.apache.asterix.translator.SessionOutput) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DatasetJobRecord(org.apache.hyracks.api.dataset.DatasetJobRecord) Stats(org.apache.asterix.translator.IStatementExecutor.Stats) ResultHandle(org.apache.asterix.app.result.ResultHandle) IHyracksDataset(org.apache.hyracks.api.dataset.IHyracksDataset) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) PrintWriter(java.io.PrintWriter)

Aggregations

ResultReader (org.apache.asterix.app.result.ResultReader)4 ResultHandle (org.apache.asterix.app.result.ResultHandle)3 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 PrintWriter (java.io.PrintWriter)2 DatasetJobRecord (org.apache.hyracks.api.dataset.DatasetJobRecord)2 IHyracksDataset (org.apache.hyracks.api.dataset.IHyracksDataset)2 ResultSetId (org.apache.hyracks.api.dataset.ResultSetId)2 StringWriter (java.io.StringWriter)1 ExecuteStatementRequestMessage (org.apache.asterix.app.message.ExecuteStatementRequestMessage)1 ExecuteStatementResponseMessage (org.apache.asterix.app.message.ExecuteStatementResponseMessage)1 INCMessageBroker (org.apache.asterix.common.messaging.api.INCMessageBroker)1 MessageFuture (org.apache.asterix.common.messaging.api.MessageFuture)1 ARecordType (org.apache.asterix.om.types.ARecordType)1 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)1 Stats (org.apache.asterix.translator.IStatementExecutor.Stats)1 SessionOutput (org.apache.asterix.translator.SessionOutput)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1 INCServiceContext (org.apache.hyracks.api.application.INCServiceContext)1 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)1 JobId (org.apache.hyracks.api.job.JobId)1