Search in sources :

Example 6 with ResultsFuture

use of org.teiid.client.util.ResultsFuture in project teiid by teiid.

the class DQPCore method executeQuery.

/**
 * Execute the given query asynchly. Has a hard limit of only returning max rows fetch size rows.
 * @param command
 * @param vdb
 * @param user
 * @param app
 * @param timeoutInMilli
 * @param engine
 * @param listener
 * @return
 * @throws Throwable
 */
public static ResultsFuture<?> executeQuery(final Object command, final VDBMetaData vdb, final String user, final String app, final long timeoutInMilli, final DQPCore engine, final ResultsListener listener) throws Throwable {
    final SessionMetadata session = TempTableDataManager.createTemporarySession(user, app, vdb);
    final long requestID = 0L;
    DQPWorkContext workContext = new DQPWorkContext();
    if (engine.localProfile != null) {
        workContext.setConnectionProfile(engine.localProfile);
    }
    workContext.setUseCallingThread(true);
    workContext.setSession(session);
    workContext.setAdmin(true);
    final ResultsFuture<Void> resultFuture = new ResultsFuture<Void>();
    resultFuture.addCompletionListener(new ResultsFuture.CompletionListener<Void>() {

        @SuppressWarnings("unchecked")
        @Override
        public void onCompletion(ResultsFuture<Void> future) {
            ResultsFuture<?> response;
            try {
                response = engine.closeRequest(requestID);
                response.addCompletionListener(new ResultsFuture.CompletionListener() {

                    @Override
                    public void onCompletion(ResultsFuture future) {
                        engine.terminateSession(session.getSessionId());
                    }
                });
            } catch (Exception e) {
                engine.terminateSession(session.getSessionId());
            }
        }
    });
    workContext.runInContext(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            RequestMessage request = new RequestMessage();
            if (command instanceof String) {
                request.setCommands((String) command);
            } else {
                request.setCommands(command.toString());
                request.setCommand(command);
            }
            request.setExecutionId(requestID);
            // this would limit the number of rows that are returned.
            request.setRowLimit(engine.getMaxRowsFetchSize());
            ResultsFuture<ResultsMessage> message = engine.executeRequest(requestID, request, timeoutInMilli);
            message.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {

                @Override
                public void onCompletion(ResultsFuture<ResultsMessage> future) {
                    try {
                        ResultsMessage rm = future.get();
                        if (rm.getException() != null) {
                            throw rm.getException();
                        }
                        if (rm.isUpdateResult()) {
                            // $NON-NLS-1$
                            listener.onResults(Arrays.asList("update-count"), rm.getResultsList());
                            resultFuture.getResultsReceiver().receiveResults(null);
                        } else {
                            processResult(rm);
                        }
                    } catch (Exception e) {
                        resultFuture.getResultsReceiver().exceptionOccurred(e);
                    }
                }

                private void processResult(ResultsMessage rm) throws Exception {
                    if (rm.getException() != null) {
                        throw rm.getException();
                    }
                    listener.onResults(Arrays.asList(rm.getColumnNames()), rm.getResultsList());
                    if (rm.getFinalRow() == -1 || rm.getLastRow() < rm.getFinalRow()) {
                        ResultsFuture<ResultsMessage> next = engine.processCursorRequest(requestID, rm.getLastRow() + 1, 1024);
                        next.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {

                            @Override
                            public void onCompletion(ResultsFuture<ResultsMessage> future) {
                                try {
                                    processResult(future.get());
                                } catch (Exception e) {
                                    resultFuture.getResultsReceiver().exceptionOccurred(e);
                                }
                            }
                        });
                    } else {
                        resultFuture.getResultsReceiver().receiveResults(null);
                    }
                }
            });
            return null;
        }
    });
    return resultFuture;
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata) TeiidComponentException(org.teiid.core.TeiidComponentException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) XATransactionException(org.teiid.client.xa.XATransactionException) AdminException(org.teiid.adminapi.AdminException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ResultsFuture(org.teiid.client.util.ResultsFuture) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) RequestMessage(org.teiid.client.RequestMessage)

Example 7 with ResultsFuture

use of org.teiid.client.util.ResultsFuture in project teiid by teiid.

the class DQPCore method start.

public void start(DQPConfiguration theConfig) {
    this.config = theConfig;
    this.authorizationValidator = config.getAuthorizationValidator();
    this.chunkSize = config.getLobChunkSizeInKB() * 1024;
    this.processWorkerPool = config.getTeiidExecutor();
    // we don't want cancellations waiting on normal processing, so they get a small dedicated pool
    // TODO: overflow to the worker pool
    // $NON-NLS-1$
    timeoutExecutor = ExecutorUtils.newFixedThreadPool(3, "Server Side Timeout");
    this.cancellationTimer = new EnhancedTimer(timeoutExecutor, timeoutExecutor);
    this.maxActivePlans = config.getMaxActivePlans();
    if (this.maxActivePlans > config.getMaxThreads()) {
        LogManager.logWarning(LogConstants.CTX_DQP, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30006, this.maxActivePlans, config.getMaxThreads()));
        this.maxActivePlans = config.getMaxThreads();
    }
    // for now options are scoped to the engine - vdb scoping is a todo
    options = new Options();
    options.setAssumeMatchingCollation(false);
    options.setProperties(config.getProperties());
    // $NON-NLS-1$
    PropertiesUtils.setBeanProperties(options, options.getProperties(), "org.teiid", true);
    this.bufferManager.setOptions(options);
    // hack to set the max active plans
    this.bufferManager.setMaxActivePlans(this.maxActivePlans);
    try {
        this.bufferManager.initialize();
    } catch (TeiidComponentException e) {
        throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30496, e);
    }
    this.userRequestSourceConcurrency = config.getUserRequestSourceConcurrency();
    if (this.userRequestSourceConcurrency < 1) {
        this.userRequestSourceConcurrency = Math.min(config.getMaxThreads(), 2 * config.getMaxThreads() / this.maxActivePlans);
    }
    DataTierManagerImpl processorDataManager = new DataTierManagerImpl(this, this.bufferManager, this.config.isDetectingChangeEvents());
    processorDataManager.setEventDistributor(eventDistributor);
    dataTierMgr = new TempTableDataManager(processorDataManager, this.bufferManager, this.rsCache);
    dataTierMgr.setExecutor(new TempTableDataManager.RequestExecutor() {

        @Override
        public void execute(String command, List<?> parameters) {
            final String sessionId = DQPWorkContext.getWorkContext().getSessionId();
            RequestMessage request = new RequestMessage(command);
            request.setParameterValues(parameters);
            request.setStatementType(StatementType.PREPARED);
            ResultsFuture<ResultsMessage> result;
            try {
                result = executeRequest(0, request);
            } catch (TeiidProcessingException e) {
                throw new TeiidRuntimeException(e);
            }
            result.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {

                @Override
                public void onCompletion(ResultsFuture<ResultsMessage> future) {
                    terminateSession(sessionId);
                }
            });
        }

        @Override
        public boolean isShutdown() {
            return shutdown;
        }
    });
    dataTierMgr.setEventDistributor(eventDistributor);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    LogManager.logDetail(LogConstants.CTX_DQP, "DQPCore started maxThreads", this.config.getMaxThreads(), "maxActivePlans", this.maxActivePlans, "source concurrency", this.userRequestSourceConcurrency);
}
Also used : Options(org.teiid.query.util.Options) EnhancedTimer(org.teiid.jdbc.EnhancedTimer) TempTableDataManager(org.teiid.query.tempdata.TempTableDataManager) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ResultsFuture(org.teiid.client.util.ResultsFuture) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) RequestMessage(org.teiid.client.RequestMessage) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 8 with ResultsFuture

use of org.teiid.client.util.ResultsFuture in project teiid by teiid.

the class TestSQLXMLProcessing method executeStreaming.

private void executeStreaming(String sql, final List<?>[] expected, int batchSize) throws Throwable {
    final CommandContext cc = createCommandContext();
    if (batchSize != -1) {
        cc.setBufferManager(BufferManagerFactory.getTestBufferManager(0, 1));
    }
    final ResultsFuture<Runnable> r = new ResultsFuture<Runnable>();
    Executor ex = new Executor() {

        @Override
        public void execute(Runnable command) {
            r.getResultsReceiver().receiveResults(command);
        }
    };
    cc.setExecutor(ex);
    final ProcessorPlan plan = helpGetPlan(helpParse(sql), RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder(), cc);
    final ResultsFuture<Void> result = new ResultsFuture<Void>();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                doProcess(plan, dataManager, expected, cc);
                result.getResultsReceiver().receiveResults(null);
            } catch (Throwable e) {
                result.getResultsReceiver().exceptionOccurred(e);
            }
        }
    };
    t.start();
    Runnable runnable = r.get();
    runnable.run();
    try {
        result.get();
    } catch (ExecutionException e) {
        if (e.getCause() != null) {
            throw e.getCause();
        }
        throw e;
    }
}
Also used : ResultsFuture(org.teiid.client.util.ResultsFuture) Executor(java.util.concurrent.Executor) CommandContext(org.teiid.query.util.CommandContext) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with ResultsFuture

use of org.teiid.client.util.ResultsFuture in project teiid by teiid.

the class TestAsynch method testAsynchPlaning.

@Test
public void testAsynchPlaning() throws Exception {
    Statement stmt = this.internalConnection.createStatement();
    TeiidStatement ts = stmt.unwrap(TeiidStatement.class);
    ef.addData("SELECT someTable.col FROM someTable", Arrays.asList(Arrays.asList(1)));
    final ResultsFuture<Integer> result = new ResultsFuture<Integer>();
    ts.submitExecute("select * from someTable", new StatementCallback() {

        int rowCount;

        @Override
        public void onRow(Statement s, ResultSet rs) {
            try {
                rowCount++;
                if (rowCount == 3) {
                    s.close();
                }
            } catch (SQLException e) {
                result.getResultsReceiver().exceptionOccurred(e);
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onException(Statement s, Exception e) {
            result.getResultsReceiver().exceptionOccurred(e);
        }

        @Override
        public void onComplete(Statement s) {
            result.getResultsReceiver().receiveResults(rowCount);
        }
    }, new RequestOptions().continuous(true));
    assertEquals(3, result.get().intValue());
    assertEquals(3, partIds.size());
    assertEquals(partIds.get(0), partIds.get(1));
    assertEquals(partIds.get(1), partIds.get(2));
}
Also used : SQLException(java.sql.SQLException) RequestOptions(org.teiid.jdbc.RequestOptions) TeiidStatement(org.teiid.jdbc.TeiidStatement) Statement(java.sql.Statement) TeiidStatement(org.teiid.jdbc.TeiidStatement) AsynchPositioningException(org.teiid.jdbc.AsynchPositioningException) SQLException(java.sql.SQLException) TranslatorException(org.teiid.translator.TranslatorException) ResultsFuture(org.teiid.client.util.ResultsFuture) ContinuousStatementCallback(org.teiid.jdbc.ContinuousStatementCallback) StatementCallback(org.teiid.jdbc.StatementCallback) ResultSet(java.sql.ResultSet) TeiidResultSet(org.teiid.jdbc.TeiidResultSet) Test(org.junit.Test)

Example 10 with ResultsFuture

use of org.teiid.client.util.ResultsFuture in project teiid by teiid.

the class TestAsynch method testAsynchContinuousEmpty.

@Test
public void testAsynchContinuousEmpty() throws Exception {
    Statement stmt = this.internalConnection.createStatement();
    TeiidStatement ts = stmt.unwrap(TeiidStatement.class);
    final ResultsFuture<Integer> result = new ResultsFuture<Integer>();
    ts.submitExecute("select * from SYS.Schemas where 1 = 0", new ContinuousStatementCallback() {

        int execCount;

        @Override
        public void onRow(Statement s, ResultSet rs) throws SQLException {
            fail();
        }

        @Override
        public void onException(Statement s, Exception e) {
            result.getResultsReceiver().exceptionOccurred(e);
        }

        @Override
        public void onComplete(Statement s) {
            result.getResultsReceiver().receiveResults(execCount);
        }

        @Override
        public void beforeNextExecution(Statement s) throws SQLException {
            execCount++;
            assertEquals(-1, s.getResultSet().unwrap(TeiidResultSet.class).available());
            if (execCount == 1024) {
                s.close();
            }
        }
    }, new RequestOptions().continuous(true));
    assertEquals(1024, result.get().intValue());
}
Also used : SQLException(java.sql.SQLException) RequestOptions(org.teiid.jdbc.RequestOptions) TeiidStatement(org.teiid.jdbc.TeiidStatement) Statement(java.sql.Statement) TeiidStatement(org.teiid.jdbc.TeiidStatement) AsynchPositioningException(org.teiid.jdbc.AsynchPositioningException) SQLException(java.sql.SQLException) TranslatorException(org.teiid.translator.TranslatorException) ResultsFuture(org.teiid.client.util.ResultsFuture) ContinuousStatementCallback(org.teiid.jdbc.ContinuousStatementCallback) ResultSet(java.sql.ResultSet) TeiidResultSet(org.teiid.jdbc.TeiidResultSet) TeiidResultSet(org.teiid.jdbc.TeiidResultSet) Test(org.junit.Test)

Aggregations

ResultsFuture (org.teiid.client.util.ResultsFuture)34 Test (org.junit.Test)21 SQLException (java.sql.SQLException)17 ResultsMessage (org.teiid.client.ResultsMessage)13 TranslatorException (org.teiid.translator.TranslatorException)11 ResultSet (java.sql.ResultSet)10 Statement (java.sql.Statement)10 RequestOptions (org.teiid.jdbc.RequestOptions)10 TeiidStatement (org.teiid.jdbc.TeiidStatement)10 AsynchPositioningException (org.teiid.jdbc.AsynchPositioningException)9 ContinuousStatementCallback (org.teiid.jdbc.ContinuousStatementCallback)9 TeiidResultSet (org.teiid.jdbc.TeiidResultSet)9 Properties (java.util.Properties)8 ExecutionException (java.util.concurrent.ExecutionException)8 StatementCallback (org.teiid.jdbc.StatementCallback)8 DQP (org.teiid.client.DQP)7 RequestMessage (org.teiid.client.RequestMessage)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4