Search in sources :

Example 1 with ResultsFuture

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

the class TestDQPCore method testLobConcurrency.

@Test
public void testLobConcurrency() throws Exception {
    RequestMessage reqMsg = exampleRequestMessage("select to_bytes(stringkey, 'utf-8') FROM BQT1.SmallA");
    reqMsg.setTxnAutoWrapMode(RequestMessage.TXN_WRAP_OFF);
    agds.setSleep(100);
    ResultsFuture<ResultsMessage> message = core.executeRequest(reqMsg.getExecutionId(), reqMsg);
    final LobThread t = new LobThread(reqMsg);
    t.start();
    message.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {

        @Override
        public void onCompletion(ResultsFuture<ResultsMessage> future) {
            try {
                final BlobType bt = (BlobType) future.get().getResultsList().get(0).get(0);
                synchronized (t) {
                    t.bt = bt;
                    t.workContext = DQPWorkContext.getWorkContext();
                    t.notify();
                }
                // give the Thread a chance to run
                Thread.sleep(100);
            } catch (Exception e) {
                t.interrupt();
                throw new RuntimeException(e);
            }
        }
    });
    message.get();
    t.join();
    assertNotNull(t.chunkFuture.get().getBytes());
}
Also used : ResultsFuture(org.teiid.client.util.ResultsFuture) BlobType(org.teiid.core.types.BlobType) ResultsMessage(org.teiid.client.ResultsMessage) RequestMessage(org.teiid.client.RequestMessage) TimeoutException(java.util.concurrent.TimeoutException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with ResultsFuture

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

the class TestPreparedStatement method testBatchedUpdateExecution.

/**
 * Verify that the <code>executeBatch()</code> method of <code>
 * MMPreparedStatement</code> is resulting in the correct command,
 * parameter values for each command of the batch, and the request type
 * are being set in the request message that would normally be sent to the
 * server.
 *
 * @throws Exception
 */
@Test
public void testBatchedUpdateExecution() throws Exception {
    // Build up a fake connection instance for use with the prepared statement
    ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
    Mockito.stub(conn.getConnectionProps()).toReturn(new Properties());
    DQP dqp = Mockito.mock(DQP.class);
    ServerConnection serverConn = Mockito.mock(ServerConnection.class);
    LogonResult logonResult = Mockito.mock(LogonResult.class);
    // stub methods
    Mockito.stub(conn.getServerConnection()).toReturn(serverConn);
    Mockito.stub(serverConn.getLogonResult()).toReturn(logonResult);
    Mockito.stub(logonResult.getTimeZone()).toReturn(TimeZone.getDefault());
    // a dummy result message that is specific to this test case
    final ResultsFuture<ResultsMessage> results = new ResultsFuture<ResultsMessage>();
    final int[] count = new int[1];
    final ResultsMessage rm = new ResultsMessage();
    Mockito.stub(dqp.executeRequest(Matchers.anyLong(), (RequestMessage) Matchers.anyObject())).toAnswer(new Answer<ResultsFuture<ResultsMessage>>() {

        @Override
        public ResultsFuture<ResultsMessage> answer(InvocationOnMock invocation) throws Throwable {
            RequestMessage requestMessage = (RequestMessage) invocation.getArguments()[1];
            count[0] += requestMessage.getParameterValues().size();
            if (count[0] == 100000) {
                rm.setException(new TeiidException());
                rm.setResults(new List<?>[] { Arrays.asList(Statement.EXECUTE_FAILED) });
            } else {
                List<?>[] vals = new List<?>[requestMessage.getParameterValues().size()];
                Arrays.fill(vals, Arrays.asList(0));
                rm.setResults(Arrays.asList(vals));
            }
            return results;
        }
    });
    rm.setUpdateResult(true);
    results.getResultsReceiver().receiveResults(rm);
    Mockito.stub(conn.getDQP()).toReturn(dqp);
    // some update SQL
    // $NON-NLS-1$
    String sqlCommand = "delete from table where col=?";
    TestableMMPreparedStatement statement = (TestableMMPreparedStatement) getMMPreparedStatement(conn, sqlCommand);
    ArrayList<ArrayList<Object>> expectedParameterValues = new ArrayList<ArrayList<Object>>(3);
    // Add some batches and their parameter values
    expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(1) })));
    statement.setInt(1, new Integer(1));
    statement.addBatch();
    expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(2) })));
    statement.setInt(1, new Integer(2));
    statement.addBatch();
    expectedParameterValues.add(new ArrayList<Object>(Arrays.asList(new Object[] { new Integer(3) })));
    statement.setInt(1, new Integer(3));
    statement.addBatch();
    // execute the batch and verify that it matches our dummy results
    // message set earlier
    assertTrue(Arrays.equals(new int[] { 0, 0, 0 }, statement.executeBatch()));
    // Now verify the statement's RequestMessage is what we expect
    // $NON-NLS-1$
    assertEquals("Command does not match", sqlCommand, statement.requestMessage.getCommandString());
    // $NON-NLS-1$
    assertEquals("Parameter values do not match", expectedParameterValues, statement.requestMessage.getParameterValues());
    // $NON-NLS-1$
    assertTrue("RequestMessage.isBatchedUpdate should be true", statement.requestMessage.isBatchedUpdate());
    // $NON-NLS-1$
    assertFalse("RequestMessage.isCallableStatement should be false", statement.requestMessage.isCallableStatement());
    // $NON-NLS-1$
    assertTrue("RequestMessage.isPreparedStatement should be true", statement.requestMessage.isPreparedStatement());
    count[0] = 0;
    // large batch handling - should split into 5
    for (int i = 0; i < 100000; i++) {
        statement.setInt(1, new Integer(1));
        statement.addBatch();
    }
    try {
        statement.executeBatch();
        fail();
    } catch (BatchUpdateException e) {
        assertEquals(100000, count[0]);
        assertEquals(95309, e.getUpdateCounts().length);
        assertEquals(Statement.EXECUTE_FAILED, e.getUpdateCounts()[95308]);
    }
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) LogonResult(org.teiid.client.security.LogonResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) RequestMessage(org.teiid.client.RequestMessage) ArrayList(java.util.ArrayList) List(java.util.List) BatchUpdateException(java.sql.BatchUpdateException) DQP(org.teiid.client.DQP) ServerConnection(org.teiid.net.ServerConnection) TeiidException(org.teiid.core.TeiidException) ResultsFuture(org.teiid.client.util.ResultsFuture) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 3 with ResultsFuture

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

the class TestStatement method testBatchExecution.

@Test
public void testBatchExecution() throws Exception {
    ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
    Mockito.stub(conn.getConnectionProps()).toReturn(new Properties());
    DQP dqp = Mockito.mock(DQP.class);
    ResultsFuture<ResultsMessage> results = new ResultsFuture<ResultsMessage>();
    Mockito.stub(dqp.executeRequest(Mockito.anyLong(), (RequestMessage) Mockito.anyObject())).toReturn(results);
    ResultsMessage rm = new ResultsMessage();
    rm.setResults(new List<?>[] { Arrays.asList(1), Arrays.asList(2) });
    rm.setUpdateResult(true);
    results.getResultsReceiver().receiveResults(rm);
    Mockito.stub(conn.getDQP()).toReturn(dqp);
    StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    // previously caused npe
    statement.clearBatch();
    // $NON-NLS-1$
    statement.addBatch("delete from table");
    // $NON-NLS-1$
    statement.addBatch("delete from table1");
    assertTrue(Arrays.equals(new int[] { 1, 2 }, statement.executeBatch()));
}
Also used : ResultsFuture(org.teiid.client.util.ResultsFuture) DQP(org.teiid.client.DQP) ResultsMessage(org.teiid.client.ResultsMessage) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with ResultsFuture

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

the class TestSocketRemoting method testMethodInvocation.

@Test
public void testMethodInvocation() throws Exception {
    ClientServiceRegistryImpl csr = new ClientServiceRegistryImpl() {

        @Override
        public ClassLoader getCallerClassloader() {
            return getClass().getClassLoader();
        }
    };
    csr.registerClientService(ILogon.class, new ILogon() {

        public ResultsFuture<?> logoff() throws InvalidSessionException {
            ResultsFuture<?> result = new ResultsFuture<Void>();
            // $NON-NLS-1$
            result.getResultsReceiver().exceptionOccurred(new TeiidComponentException("some exception"));
            return result;
        }

        public LogonResult logon(Properties connectionProperties) throws LogonException, TeiidComponentException {
            return new LogonResult();
        }

        // tests asynch where we don't care about the result
        public ResultsFuture<?> ping() throws InvalidSessionException, TeiidComponentException {
            return null;
        }

        @Override
        public ResultsFuture<?> ping(Collection<String> sessions) throws TeiidComponentException, CommunicationException {
            return null;
        }

        @Override
        public void assertIdentity(SessionToken sessionId) throws InvalidSessionException, TeiidComponentException {
        }

        @Override
        public LogonResult neogitiateGssLogin(Properties connectionProperties, byte[] serviceToken, boolean createSession) throws LogonException {
            return null;
        }
    }, // $NON-NLS-1$
    "foo");
    // $NON-NLS-1$
    csr.registerClientService(FakeService.class, new FakeServiceImpl(), "foo");
    final FakeClientServerInstance serverInstance = new FakeClientServerInstance(csr);
    SocketServerConnection connection = createFakeConnection(serverInstance);
    ILogon logon = connection.getService(ILogon.class);
    Future<?> result = logon.ping();
    assertNull(result.get(0, TimeUnit.MILLISECONDS));
    result = logon.logoff();
    try {
        result.get(0, TimeUnit.MICROSECONDS);
        // $NON-NLS-1$
        fail("exception expected");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof TeiidComponentException);
    }
    FakeService service = connection.getService(FakeService.class);
    Future<Integer> asynchInteger = service.asynchResult();
    assertEquals(new Integer(5), asynchInteger.get(0, TimeUnit.MILLISECONDS));
    try {
        service.exceptionMethod();
        // $NON-NLS-1$
        fail("exception expected");
    } catch (TeiidProcessingException e) {
    }
    DQP dqp = connection.getService(DQP.class);
    try {
        ResultsFuture<?> future = dqp.begin();
        future.get();
        // $NON-NLS-1$
        fail("exception expected");
    } catch (Exception e) {
        // $NON-NLS-1$
        assertTrue(e.getMessage().indexOf("Component not found:") != -1);
    }
}
Also used : LogonResult(org.teiid.client.security.LogonResult) Properties(java.util.Properties) TeiidProcessingException(org.teiid.core.TeiidProcessingException) LogonException(org.teiid.client.security.LogonException) ExecutionException(java.util.concurrent.ExecutionException) InvalidSessionException(org.teiid.client.security.InvalidSessionException) DQP(org.teiid.client.DQP) CommunicationException(org.teiid.net.CommunicationException) SessionToken(org.teiid.client.security.SessionToken) ILogon(org.teiid.client.security.ILogon) InvalidSessionException(org.teiid.client.security.InvalidSessionException) TeiidComponentException(org.teiid.core.TeiidComponentException) LogonException(org.teiid.client.security.LogonException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) CommunicationException(org.teiid.net.CommunicationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) ConnectionException(org.teiid.net.ConnectionException) ResultsFuture(org.teiid.client.util.ResultsFuture) TeiidComponentException(org.teiid.core.TeiidComponentException) SocketServerConnection(org.teiid.net.socket.SocketServerConnection) Test(org.junit.Test)

Example 5 with ResultsFuture

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

the class DQPCore method executeRequest.

public ResultsFuture<ResultsMessage> executeRequest(long reqID, RequestMessage requestMsg, Long queryTimeout) throws TeiidProcessingException {
    DQPWorkContext workContext = DQPWorkContext.getWorkContext();
    checkActive(workContext);
    RequestID requestID = workContext.getRequestID(reqID);
    requestMsg.setFetchSize(Math.min(requestMsg.getFetchSize(), this.config.getMaxRowsFetchSize()));
    Request request = null;
    if (requestMsg.isPreparedStatement() || requestMsg.isCallableStatement() || requestMsg.getRequestOptions().isContinuous()) {
        request = new PreparedStatementRequest(prepPlanCache);
    } else {
        request = new Request();
    }
    ClientState state = this.getClientState(workContext.getSessionId(), true);
    if (state.session == null) {
        state.session = workContext.getSession();
    }
    request.initialize(requestMsg, bufferManager, dataTierMgr, transactionService, state.sessionTables, workContext, this.prepPlanCache);
    request.setOptions(options);
    request.setExecutor(this.processWorkerPool);
    request.setResultSetCacheEnabled(this.rsCache != null);
    request.setAuthorizationValidator(this.authorizationValidator);
    final PreParser preparser = workContext.getVDB().getAttachment(PreParser.class);
    if (preparser != null) {
        if (this.config.getPreParser() != null) {
            // chain the preparsing effect
            request.setPreParser(new PreParser() {

                @Override
                public String preParse(String command, org.teiid.CommandContext context) {
                    String preParse = config.getPreParser().preParse(command, context);
                    return preparser.preParse(preParse, context);
                }
            });
        } else {
            request.setPreParser(preparser);
        }
    } else {
        request.setPreParser(this.config.getPreParser());
    }
    request.setUserRequestConcurrency(this.getUserRequestSourceConcurrency());
    ResultsFuture<ResultsMessage> resultsFuture = new ResultsFuture<ResultsMessage>();
    final RequestWorkItem workItem = new RequestWorkItem(this, requestMsg, request, resultsFuture.getResultsReceiver(), requestID, workContext);
    logMMCommand(workItem, Event.NEW, null, null);
    addRequest(requestID, workItem, state);
    long timeout = workContext.getVDB().getQueryTimeout();
    timeout = Math.min(timeout > 0 ? timeout : Long.MAX_VALUE, config.getQueryTimeout() > 0 ? config.getQueryTimeout() : Long.MAX_VALUE);
    if (queryTimeout != null && queryTimeout > 0) {
        timeout = Math.min(timeout > 0 ? timeout : Long.MAX_VALUE, queryTimeout);
    }
    if (timeout < Long.MAX_VALUE) {
        final long finalTimeout = timeout;
        workItem.setCancelTask(this.cancellationTimer.add(new Runnable() {

            WeakReference<RequestWorkItem> workItemRef = new WeakReference<RequestWorkItem>(workItem);

            @Override
            public void run() {
                try {
                    RequestWorkItem wi = workItemRef.get();
                    if (wi != null) {
                        String reason = QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31096, finalTimeout);
                        wi.requestCancel(reason);
                    }
                } catch (TeiidComponentException e) {
                    LogManager.logError(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30018));
                }
            }
        }, timeout));
    }
    boolean runInThread = requestMsg.isSync();
    synchronized (waitingPlans) {
        if (runInThread || currentlyActivePlans < maxActivePlans) {
            startActivePlan(workItem, !runInThread);
        } else {
            if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, workItem.requestID, "Queuing plan, since max plans has been reached.");
            }
            waitingPlans.add(workItem);
            maxWaitingPlans = Math.max(this.maxWaitingPlans, waitingPlans.size());
        }
    }
    if (runInThread) {
        workItem.useCallingThread = true;
        workItem.run();
    }
    return resultsFuture;
}
Also used : RequestID(org.teiid.dqp.message.RequestID) ResultsMessage(org.teiid.client.ResultsMessage) PreParser(org.teiid.PreParser) ResultsFuture(org.teiid.client.util.ResultsFuture) WeakReference(java.lang.ref.WeakReference) TeiidComponentException(org.teiid.core.TeiidComponentException)

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