Search in sources :

Example 1 with IStatementExecutorContext

use of org.apache.asterix.translator.IStatementExecutorContext in project asterixdb by apache.

the class QueryCancellationServletTest method testDelete.

@Test
public void testDelete() throws Exception {
    // Creates a query cancellation servlet.
    QueryCancellationServlet cancellationServlet = new QueryCancellationServlet(new ConcurrentHashMap<>(), new String[] { "/" });
    // Adds mocked Hyracks client connection into the servlet context.
    IHyracksClientConnection mockHcc = mock(IHyracksClientConnection.class);
    cancellationServlet.ctx().put(ServletConstants.HYRACKS_CONNECTION_ATTR, mockHcc);
    // Adds a query context into the servlet context.
    IStatementExecutorContext queryCtx = new StatementExecutorContext();
    cancellationServlet.ctx().put(ServletConstants.RUNNING_QUERIES_ATTR, queryCtx);
    // Tests the case that query is not in the map.
    IServletRequest mockRequest = mockRequest("1");
    IServletResponse mockResponse = mock(IServletResponse.class);
    cancellationServlet.handle(mockRequest, mockResponse);
    verify(mockResponse, times(1)).setStatus(HttpResponseStatus.NOT_FOUND);
    // Tests the case that query is in the map.
    queryCtx.put("1", new JobId(1));
    cancellationServlet.handle(mockRequest, mockResponse);
    verify(mockResponse, times(1)).setStatus(HttpResponseStatus.OK);
    // Tests the case the client_context_id is not provided.
    mockRequest = mockRequest(null);
    cancellationServlet.handle(mockRequest, mockResponse);
    verify(mockResponse, times(1)).setStatus(HttpResponseStatus.BAD_REQUEST);
    // Tests the case that the job cancellation hit some exception from Hyracks.
    queryCtx.put("2", new JobId(2));
    Mockito.doThrow(new Exception()).when(mockHcc).cancelJob(any());
    mockRequest = mockRequest("2");
    cancellationServlet.handle(mockRequest, mockResponse);
    verify(mockResponse, times(1)).setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
Also used : StatementExecutorContext(org.apache.asterix.api.http.ctx.StatementExecutorContext) IStatementExecutorContext(org.apache.asterix.translator.IStatementExecutorContext) IHyracksClientConnection(org.apache.hyracks.api.client.IHyracksClientConnection) QueryCancellationServlet(org.apache.asterix.api.http.server.QueryCancellationServlet) IStatementExecutorContext(org.apache.asterix.translator.IStatementExecutorContext) IServletResponse(org.apache.hyracks.http.api.IServletResponse) JobId(org.apache.hyracks.api.job.JobId) IServletRequest(org.apache.hyracks.http.api.IServletRequest) Test(org.junit.Test)

Example 2 with IStatementExecutorContext

use of org.apache.asterix.translator.IStatementExecutorContext in project asterixdb by apache.

the class QueryCancellationServlet method delete.

@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;
    }
    // Retrieves the corresponding Hyracks job id.
    IStatementExecutorContext runningQueries = (IStatementExecutorContext) ctx.get(ServletConstants.RUNNING_QUERIES_ATTR);
    IHyracksClientConnection hcc = (IHyracksClientConnection) ctx.get(ServletConstants.HYRACKS_CONNECTION_ATTR);
    JobId jobId = runningQueries.getJobIdFromClientContextId(clientContextId);
    if (jobId == null) {
        // response: NOT FOUND
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        return;
    }
    try {
        // Cancels the on-going job.
        hcc.cancelJob(jobId);
        // Removes the cancelled query from the map activeQueries.
        runningQueries.removeJobIdFromClientContextId(clientContextId);
        // response: OK
        response.setStatus(HttpResponseStatus.OK);
    } catch (Exception e) {
        if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
        }
        // response: INTERNAL SERVER ERROR
        response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : IHyracksClientConnection(org.apache.hyracks.api.client.IHyracksClientConnection) IStatementExecutorContext(org.apache.asterix.translator.IStatementExecutorContext) JobId(org.apache.hyracks.api.job.JobId) IOException(java.io.IOException)

Example 3 with IStatementExecutorContext

use of org.apache.asterix.translator.IStatementExecutorContext in project asterixdb by apache.

the class ExecuteStatementRequestMessage method handle.

@Override
public void handle(ICcApplicationContext ccAppCtx) throws HyracksDataException, InterruptedException {
    ICCServiceContext ccSrvContext = ccAppCtx.getServiceContext();
    ClusterControllerService ccSrv = (ClusterControllerService) ccSrvContext.getControllerService();
    CCApplication ccApp = (CCApplication) ccSrv.getApplication();
    CCMessageBroker messageBroker = (CCMessageBroker) ccSrvContext.getMessageBroker();
    CCExtensionManager ccExtMgr = (CCExtensionManager) ccAppCtx.getExtensionManager();
    ILangCompilationProvider compilationProvider = ccExtMgr.getCompilationProvider(lang);
    IStorageComponentProvider storageComponentProvider = ccAppCtx.getStorageComponentProvider();
    IStatementExecutorFactory statementExecutorFactory = ccApp.getStatementExecutorFactory();
    IStatementExecutorContext statementExecutorContext = ccApp.getStatementExecutorContext();
    ccSrv.getExecutor().submit(() -> {
        ExecuteStatementResponseMessage responseMsg = new ExecuteStatementResponseMessage(requestMessageId);
        try {
            final IClusterManagementWork.ClusterState clusterState = ClusterStateManager.INSTANCE.getState();
            if (clusterState != IClusterManagementWork.ClusterState.ACTIVE) {
                throw new IllegalStateException("Cannot execute request, cluster is " + clusterState);
            }
            IParser parser = compilationProvider.getParserFactory().createParser(statementsText);
            List<Statement> statements = parser.parse();
            StringWriter outWriter = new StringWriter(256);
            PrintWriter outPrinter = new PrintWriter(outWriter);
            SessionOutput.ResultDecorator resultPrefix = ResultUtil.createPreResultDecorator();
            SessionOutput.ResultDecorator resultPostfix = ResultUtil.createPostResultDecorator();
            SessionOutput.ResultAppender appendHandle = ResultUtil.createResultHandleAppender(handleUrl);
            SessionOutput.ResultAppender appendStatus = ResultUtil.createResultStatusAppender();
            SessionOutput sessionOutput = new SessionOutput(sessionConfig, outPrinter, resultPrefix, resultPostfix, appendHandle, appendStatus);
            IStatementExecutor.ResultMetadata outMetadata = new IStatementExecutor.ResultMetadata();
            MetadataManager.INSTANCE.init();
            IStatementExecutor translator = statementExecutorFactory.create(ccAppCtx, statements, sessionOutput, compilationProvider, storageComponentProvider);
            translator.compileAndExecute(ccAppCtx.getHcc(), null, delivery, outMetadata, new IStatementExecutor.Stats(), clientContextID, statementExecutorContext);
            outPrinter.close();
            responseMsg.setResult(outWriter.toString());
            responseMsg.setMetadata(outMetadata);
        } catch (AlgebricksException | HyracksException | TokenMgrError | org.apache.asterix.aqlplus.parser.TokenMgrError pe) {
            // we trust that "our" exceptions are serializable and have a comprehensible error message
            GlobalConfig.ASTERIX_LOGGER.log(Level.WARNING, pe.getMessage(), pe);
            responseMsg.setError(pe);
        } catch (Exception e) {
            GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, "Unexpected exception", e);
            responseMsg.setError(new Exception(e.toString()));
        }
        try {
            messageBroker.sendApplicationMessageToNC(responseMsg, requestNodeId);
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, e.toString(), e);
        }
    });
}
Also used : ICCServiceContext(org.apache.hyracks.api.application.ICCServiceContext) CCApplication(org.apache.asterix.hyracks.bootstrap.CCApplication) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) StringWriter(java.io.StringWriter) ILangCompilationProvider(org.apache.asterix.compiler.provider.ILangCompilationProvider) IStatementExecutorFactory(org.apache.asterix.translator.IStatementExecutorFactory) PrintWriter(java.io.PrintWriter) IStorageComponentProvider(org.apache.asterix.common.context.IStorageComponentProvider) IStatementExecutorContext(org.apache.asterix.translator.IStatementExecutorContext) Statement(org.apache.asterix.lang.common.base.Statement) CCMessageBroker(org.apache.asterix.messaging.CCMessageBroker) IClusterManagementWork(org.apache.asterix.common.api.IClusterManagementWork) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) TokenMgrError(org.apache.asterix.lang.aql.parser.TokenMgrError) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) CCExtensionManager(org.apache.asterix.app.cc.CCExtensionManager) IStatementExecutor(org.apache.asterix.translator.IStatementExecutor) SessionOutput(org.apache.asterix.translator.SessionOutput) ClusterControllerService(org.apache.hyracks.control.cc.ClusterControllerService) IParser(org.apache.asterix.lang.common.base.IParser)

Aggregations

IStatementExecutorContext (org.apache.asterix.translator.IStatementExecutorContext)3 IHyracksClientConnection (org.apache.hyracks.api.client.IHyracksClientConnection)2 JobId (org.apache.hyracks.api.job.JobId)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 StatementExecutorContext (org.apache.asterix.api.http.ctx.StatementExecutorContext)1 QueryCancellationServlet (org.apache.asterix.api.http.server.QueryCancellationServlet)1 CCExtensionManager (org.apache.asterix.app.cc.CCExtensionManager)1 IClusterManagementWork (org.apache.asterix.common.api.IClusterManagementWork)1 IStorageComponentProvider (org.apache.asterix.common.context.IStorageComponentProvider)1 ILangCompilationProvider (org.apache.asterix.compiler.provider.ILangCompilationProvider)1 CCApplication (org.apache.asterix.hyracks.bootstrap.CCApplication)1 TokenMgrError (org.apache.asterix.lang.aql.parser.TokenMgrError)1 IParser (org.apache.asterix.lang.common.base.IParser)1 Statement (org.apache.asterix.lang.common.base.Statement)1 CCMessageBroker (org.apache.asterix.messaging.CCMessageBroker)1 IStatementExecutor (org.apache.asterix.translator.IStatementExecutor)1 IStatementExecutorFactory (org.apache.asterix.translator.IStatementExecutorFactory)1 SessionOutput (org.apache.asterix.translator.SessionOutput)1