Search in sources :

Example 26 with ResultsMessage

use of org.teiid.client.ResultsMessage in project teiid by teiid.

the class RequestWorkItem method sendResultsIfNeeded.

/**
 * Send results if they have been requested.  This should only be called from the processing thread.
 */
protected boolean sendResultsIfNeeded(TupleBatch batch) throws TeiidComponentException, TeiidProcessingException {
    ResultsMessage response = null;
    ResultsReceiver<ResultsMessage> receiver = null;
    boolean result = true;
    synchronized (this) {
        if (this.resultsReceiver == null) {
            if (cursorRequestExpected()) {
                if (batch != null) {
                    // $NON-NLS-1$
                    throw new AssertionError("batch has no handler");
                }
                // $NON-NLS-1$
                throw BlockedException.block(requestID, "Blocking until client is ready");
            }
            return result;
        }
        if (!this.requestMsg.getRequestOptions().isContinuous()) {
            if ((this.begin > (batch != null ? batch.getEndRow() : this.resultsBuffer.getRowCount()) && !doneProducingBatches) || (this.transactionState == TransactionState.ACTIVE) || (returnsUpdateCount && !doneProducingBatches)) {
                return result;
            }
            if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                LogManager.logDetail(LogConstants.CTX_DQP, "[RequestWorkItem.sendResultsIfNeeded] requestID:", requestID, "resultsID:", this.resultsBuffer, "done:", doneProducingBatches);
            }
            boolean fromBuffer = false;
            int count = this.end - this.begin + 1;
            if (returnsUpdateCount) {
                count = Integer.MAX_VALUE;
            }
            if (batch == null || !(batch.containsRow(this.begin) || (batch.getTerminationFlag() && batch.getEndRow() <= this.begin))) {
                if (savedBatch != null && savedBatch.containsRow(this.begin)) {
                    batch = savedBatch;
                } else {
                    batch = resultsBuffer.getBatch(begin);
                    // fetch more than 1 batch from the buffer
                    boolean first = true;
                    int rowSize = resultsBuffer.getRowSizeEstimate();
                    int batches = CLIENT_FETCH_MAX_BATCHES;
                    if (rowSize > 0) {
                        int totalSize = rowSize * resultsBuffer.getBatchSize();
                        if (schemaSize == 0) {
                            schemaSize = this.dqpCore.getBufferManager().getSchemaSize(this.originalCommand.getProjectedSymbols());
                        }
                        int multiplier = schemaSize / totalSize;
                        if (multiplier > 1) {
                            batches *= multiplier;
                        }
                    }
                    if (returnsUpdateCount) {
                        batches = Integer.MAX_VALUE;
                    }
                    for (int i = 1; i < batches && batch.getRowCount() + resultsBuffer.getBatchSize() <= count && !batch.getTerminationFlag(); i++) {
                        TupleBatch next = resultsBuffer.getBatch(batch.getEndRow() + 1);
                        if (next.getRowCount() == 0) {
                            break;
                        }
                        if (first) {
                            first = false;
                            TupleBatch old = batch;
                            batch = new TupleBatch(batch.getBeginRow(), new ResizingArrayList<List<?>>(batch.getTuples()));
                            batch.setTermination(old.getTermination());
                        }
                        batch.getTuples().addAll(next.getTuples());
                        batch.setTermination(next.getTermination());
                    }
                }
                savedBatch = null;
                fromBuffer = true;
            }
            if (batch.getRowCount() > count) {
                long beginRow = isForwardOnly() ? begin : Math.min(this.begin, batch.getEndRow() - count + 1);
                long endRow = Math.min(beginRow + count - 1, batch.getEndRow());
                boolean last = false;
                if (endRow == batch.getEndRow()) {
                    last = batch.getTerminationFlag();
                } else if (isForwardOnly()) {
                    savedBatch = batch;
                }
                List<List<?>> memoryRows = batch.getTuples();
                batch = new TupleBatch(beginRow, memoryRows.subList((int) (beginRow - batch.getBeginRow()), (int) (endRow - batch.getBeginRow() + 1)));
                batch.setTerminationFlag(last);
            } else if (!fromBuffer) {
                result = !isForwardOnly();
            }
        } else if (batch == null) {
            return result;
        } else {
            result = false;
        }
        long finalRowCount = (this.resultsBuffer.isFinal() && !this.requestMsg.getRequestOptions().isContinuous()) ? this.resultsBuffer.getRowCount() : (batch.getTerminationFlag() ? batch.getEndRow() : -1);
        if (batch.getBeginRow() > Integer.MAX_VALUE || batch.getEndRow() > Integer.MAX_VALUE) {
            throw new TeiidProcessingException(QueryPlugin.Event.TEIID31174, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31174));
        }
        response = createResultsMessage(batch.getTuples(), this.originalCommand.getProjectedSymbols());
        response.setFirstRow((int) batch.getBeginRow());
        if (batch.getTermination() == TupleBatch.ITERATION_TERMINATED) {
            response.setLastRow((int) batch.getEndRow() - 1);
        } else {
            response.setLastRow((int) batch.getEndRow());
        }
        response.setUpdateResult(this.returnsUpdateCount);
        if (this.returnsUpdateCount) {
            // batch updates can have special exceptions in addition to update count results
            Throwable t = this.processor.getContext().getBatchUpdateException();
            if (t != null) {
                t = logError(t);
                response.setException(t);
            }
            // swap the result for the generated keys
            if (this.processor.getContext().isReturnAutoGeneratedKeys() && finalRowCount == 1 && this.processor.getContext().getGeneratedKeys() != null && handleGeneratedKeys(response)) {
                finalRowCount = response.getLastRow();
            } else if (finalRowCount == 0 && response.getException() == null) {
                // anon block or other construct not setting an explicit update count
                response.setResults(Arrays.asList(Arrays.asList(0)));
                finalRowCount = 1;
            }
        }
        // set final row
        response.setFinalRow((int) Math.min(finalRowCount, Integer.MAX_VALUE));
        if (response.getLastRow() == finalRowCount) {
            response.setDelayDeserialization(false);
        }
        setWarnings(response);
        // If it is stored procedure, set parameters
        if (originalCommand instanceof StoredProcedure) {
            StoredProcedure proc = (StoredProcedure) originalCommand;
            if (proc.returnParameters()) {
                response.setParameters(getParameterInfo(proc));
            }
        }
        /*
	         * mark the results sent at this point.
	         * communication exceptions will be treated as non-recoverable 
	         */
        receiver = this.resultsReceiver;
        this.resultsReceiver = null;
    }
    cancelCancelTask();
    if ((!this.dqpWorkContext.getSession().isEmbedded() && requestMsg.isDelaySerialization() && this.requestMsg.getShowPlan() == ShowPlan.ON) || this.requestMsg.getShowPlan() == ShowPlan.DEBUG || LogManager.isMessageToBeRecorded(LogConstants.CTX_COMMANDLOGGING, MessageLevel.TRACE)) {
        int bytes;
        try {
            boolean keep = !this.dqpWorkContext.getSession().isEmbedded() && requestMsg.isDelaySerialization();
            bytes = response.serialize(keep);
            if (keep) {
                response.setDelayDeserialization(true);
            }
            dataBytes.addAndGet(bytes);
            // $NON-NLS-1$ //$NON-NLS-2$
            LogManager.logDetail(// $NON-NLS-1$ //$NON-NLS-2$
            LogConstants.CTX_DQP, // $NON-NLS-1$ //$NON-NLS-2$
            "Sending results for", // $NON-NLS-1$ //$NON-NLS-2$
            requestID, // $NON-NLS-1$ //$NON-NLS-2$
            "start row", response.getFirstRow(), "end row", response.getLastRow(), bytes, // $NON-NLS-1$ //$NON-NLS-2$
            "bytes");
        } catch (Exception e) {
        // do nothing.  there is a low level serialization error that we will let happen
        // later since it would be inconvenient here
        }
    }
    setAnalysisRecords(response);
    receiver.receiveResults(response);
    return result;
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) CacheHint(org.teiid.query.sql.lang.CacheHint) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidException(org.teiid.core.TeiidException) BlockedException(org.teiid.common.buffer.BlockedException) XATransactionException(org.teiid.client.xa.XATransactionException) ExpiredTimeSliceException(org.teiid.query.processor.QueryProcessor.ExpiredTimeSliceException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ResizingArrayList(org.teiid.client.ResizingArrayList) ResizingArrayList(org.teiid.client.ResizingArrayList) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 27 with ResultsMessage

use of org.teiid.client.ResultsMessage in project teiid by teiid.

the class TestDQPCore method helpExecute.

private ResultsMessage helpExecute(String sql, String userName, int sessionid, boolean txnAutoWrap) throws Exception {
    RequestMessage reqMsg = exampleRequestMessage(sql);
    if (txnAutoWrap) {
        reqMsg.setTxnAutoWrapMode(RequestMessage.TXN_WRAP_ON);
    }
    ResultsMessage results = execute(userName, sessionid, reqMsg);
    core.terminateSession(String.valueOf(sessionid));
    assertNull(core.getClientState(String.valueOf(sessionid), false));
    if (results.getException() != null) {
        throw results.getException();
    }
    return results;
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) RequestMessage(org.teiid.client.RequestMessage)

Example 28 with ResultsMessage

use of org.teiid.client.ResultsMessage in project teiid by teiid.

the class TestDQPCore method testRequestMaxActive.

@Test
public void testRequestMaxActive() throws Exception {
    agds.latch = new CountDownLatch(3);
    int toRun = 2;
    CountDownLatch submitted = new CountDownLatch(toRun);
    ExecutorService es = Executors.newCachedThreadPool();
    final DQPWorkContext context = DQPWorkContext.getWorkContext();
    final AtomicInteger counter = new AtomicInteger();
    es.invokeAll(Collections.nCopies(toRun, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            DQPWorkContext.setWorkContext(context);
            RequestMessage reqMsg = exampleRequestMessage("select * FROM BQT1.SmallA");
            DQPWorkContext.getWorkContext().getSession().setSessionId("1");
            DQPWorkContext.getWorkContext().getSession().setUserName("a");
            Future<ResultsMessage> message = null;
            try {
                message = core.executeRequest(counter.getAndIncrement(), reqMsg);
            } finally {
                submitted.countDown();
            }
            assertNotNull(core.getClientState("1", false));
            // after this, both will be submitted
            submitted.await();
            // allow the execution to proceed
            agds.latch.countDown();
            message.get(500000, TimeUnit.MILLISECONDS);
            return null;
        }
    }));
    assertEquals(1, this.core.getMaxWaitingPlanWatermark());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultsMessage(org.teiid.client.ResultsMessage) RequestMessage(org.teiid.client.RequestMessage) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 29 with ResultsMessage

use of org.teiid.client.ResultsMessage in project teiid by teiid.

the class TestDQPCore method testXmlTableStreamingWithLimit.

@Test
public void testXmlTableStreamingWithLimit() throws Exception {
    // $NON-NLS-1$
    String sql = "select * from xmltable('/a/b' passing xmlparse(document '<a x=''1''><b>foo</b><b>bar</b><b>zed</b></a>') columns y string path '.') as x limit 2";
    ResultsMessage rm = execute("A", 1, exampleRequestMessage(sql));
    assertNull(rm.getException());
    assertEquals(2, rm.getResultsList().size());
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) Test(org.junit.Test)

Example 30 with ResultsMessage

use of org.teiid.client.ResultsMessage in project teiid by teiid.

the class TestDQPCore method testDataAvailable.

@Test
public void testDataAvailable() throws Exception {
    agds.dataNotAvailable = -1;
    agds.dataAvailable = true;
    RequestMessage reqMsg = exampleRequestMessage("select * FROM BQT1.SmallA");
    ResultsMessage results = execute("A", 1, reqMsg);
    if (results.getException() != null) {
        throw results.getException();
    }
}
Also used : ResultsMessage(org.teiid.client.ResultsMessage) RequestMessage(org.teiid.client.RequestMessage) Test(org.junit.Test)

Aggregations

ResultsMessage (org.teiid.client.ResultsMessage)54 Test (org.junit.Test)30 RequestMessage (org.teiid.client.RequestMessage)23 ResultsFuture (org.teiid.client.util.ResultsFuture)13 List (java.util.List)6 DQP (org.teiid.client.DQP)6 ArrayList (java.util.ArrayList)5 Properties (java.util.Properties)5 BufferManagerImpl (org.teiid.common.buffer.impl.BufferManagerImpl)5 ExecutionException (java.util.concurrent.ExecutionException)4 SQLException (java.sql.SQLException)3 TimeoutException (java.util.concurrent.TimeoutException)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ParameterInfo (org.teiid.client.metadata.ParameterInfo)3 TeiidComponentException (org.teiid.core.TeiidComponentException)3 TeiidException (org.teiid.core.TeiidException)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3 BigInteger (java.math.BigInteger)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)2