Search in sources :

Example 1 with ResultHandle

use of org.apache.asterix.app.result.ResultHandle 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 2 with ResultHandle

use of org.apache.asterix.app.result.ResultHandle 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 3 with ResultHandle

use of org.apache.asterix.app.result.ResultHandle 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)

Example 4 with ResultHandle

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

the class QueryTranslator method asyncCreateAndRunJob.

private void asyncCreateAndRunJob(IHyracksClientConnection hcc, IStatementCompiler compiler, IMetadataLocker locker, ResultDelivery resultDelivery, String clientContextId, IStatementExecutorContext ctx, ResultSetId resultSetId, MutableBoolean printed) {
    Mutable<JobId> jobId = new MutableObject<>(JobId.INVALID);
    try {
        createAndRunJob(hcc, jobId, compiler, locker, resultDelivery, id -> {
            final ResultHandle handle = new ResultHandle(id, resultSetId);
            ResultUtil.printStatus(sessionOutput, AbstractQueryApiServlet.ResultStatus.RUNNING);
            ResultUtil.printResultHandle(sessionOutput, handle);
            synchronized (printed) {
                printed.setTrue();
                printed.notify();
            }
        }, clientContextId, ctx);
    } catch (Exception e) {
        if (JobId.INVALID.equals(jobId.getValue())) {
            // compilation failed
            ResultUtil.printStatus(sessionOutput, AbstractQueryApiServlet.ResultStatus.FAILED);
            ResultUtil.printError(sessionOutput.out(), e);
        } else {
            GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, resultDelivery.name() + " job with id " + jobId.getValue() + " " + "failed", e);
        }
    } finally {
        synchronized (printed) {
            if (printed.isFalse()) {
                printed.setTrue();
                printed.notify();
            }
        }
    }
}
Also used : ResultHandle(org.apache.asterix.app.result.ResultHandle) JobId(org.apache.hyracks.api.job.JobId) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) MutableObject(org.apache.commons.lang3.mutable.MutableObject)

Aggregations

ResultHandle (org.apache.asterix.app.result.ResultHandle)4 ResultReader (org.apache.asterix.app.result.ResultReader)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 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 RemoteException (java.rmi.RemoteException)1 ACIDException (org.apache.asterix.common.exceptions.ACIDException)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 CompilationException (org.apache.asterix.common.exceptions.CompilationException)1 MetadataException (org.apache.asterix.metadata.MetadataException)1 Stats (org.apache.asterix.translator.IStatementExecutor.Stats)1 SessionOutput (org.apache.asterix.translator.SessionOutput)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)1 ResultSetId (org.apache.hyracks.api.dataset.ResultSetId)1