Search in sources :

Example 21 with BlockedException

use of org.teiid.common.buffer.BlockedException in project teiid by teiid.

the class TempTableDataManager method lookupCodeValue.

public Object lookupCodeValue(CommandContext context, String codeTableName, String returnElementName, String keyElementName, Object keyValue) throws BlockedException, TeiidComponentException, TeiidProcessingException {
    // we are not using a resolved form of a lookup, so we canonicallize with upper case
    codeTableName = codeTableName.toUpperCase();
    keyElementName = keyElementName.toUpperCase();
    returnElementName = returnElementName.toUpperCase();
    String matTableName = CODE_PREFIX + codeTableName + ElementSymbol.SEPARATOR + keyElementName + ElementSymbol.SEPARATOR + returnElementName;
    TupleSource ts = context.getCodeLookup(matTableName, keyValue);
    if (ts == null) {
        QueryMetadataInterface metadata = context.getMetadata();
        TempMetadataID id = context.getGlobalTableStore().getCodeTableMetadataId(codeTableName, returnElementName, keyElementName, matTableName);
        ElementSymbol keyElement = new ElementSymbol(keyElementName, new GroupSymbol(matTableName));
        ElementSymbol returnElement = new ElementSymbol(returnElementName, new GroupSymbol(matTableName));
        keyElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + keyElementName))));
        returnElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + returnElementName))));
        Query query = RelationalPlanner.createMatViewQuery(id, matTableName, Arrays.asList(returnElement), true);
        query.setCriteria(new CompareCriteria(keyElement, CompareCriteria.EQ, new Constant(keyValue)));
        ts = registerQuery(context, context.getTempTableStore(), query);
    }
    try {
        List<?> row = ts.nextTuple();
        Object result = null;
        if (row != null) {
            result = row.get(0);
        }
        ts.closeSource();
        return result;
    } catch (BlockedException e) {
        context.putCodeLookup(matTableName, keyValue, ts);
        throw e;
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) Constant(org.teiid.query.sql.symbol.Constant) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) BlockedException(org.teiid.common.buffer.BlockedException)

Example 22 with BlockedException

use of org.teiid.common.buffer.BlockedException in project teiid by teiid.

the class DataTierTupleSource method nextTuple.

public List<?> nextTuple() throws TeiidComponentException, TeiidProcessingException {
    if (waitUntil > 0 && waitUntil > System.currentTimeMillis()) {
        if (!this.cwi.isDataAvailable()) {
            // $NON-NLS-1$
            throw BlockedException.block(aqr.getAtomicRequestID(), "Blocking until", waitUntil);
        }
        this.waitUntil = 0;
    }
    while (true) {
        if (arm == null) {
            if (isDone()) {
                // TODO: could throw an illegal state exception
                return null;
            }
            boolean partial = false;
            AtomicResultsMessage results = null;
            boolean noResults = false;
            try {
                if (futureResult != null || !aqr.isSerial()) {
                    results = asynchGet();
                } else {
                    results = getResults();
                }
                // check for update events
                if (index == 0 && this.dtm.detectChangeEvents()) {
                    Command command = aqr.getCommand();
                    int commandIndex = 0;
                    if (RelationalNodeUtil.isUpdate(command)) {
                        long ts = System.currentTimeMillis();
                        checkForUpdates(results, command, dtm.getEventDistributor(), commandIndex, ts);
                    } else if (command instanceof BatchedUpdateCommand) {
                        long ts = System.currentTimeMillis();
                        BatchedUpdateCommand bac = (BatchedUpdateCommand) command;
                        for (Command uc : bac.getUpdateCommands()) {
                            checkForUpdates(results, uc, dtm.getEventDistributor(), commandIndex++, ts);
                        }
                    }
                }
            } catch (TranslatorException e) {
                errored = true;
                results = exceptionOccurred(e);
                partial = true;
            } catch (BlockedException e) {
                noResults = true;
                throw e;
            } catch (DataNotAvailableException e) {
                noResults = true;
                handleDataNotAvailable(e);
                continue;
            } finally {
                if (!noResults && results == null) {
                    errored = true;
                }
            }
            receiveResults(results, partial);
        }
        if (index < arm.getResults().length) {
            if (limit-- == 0) {
                this.done = true;
                arm = null;
                return null;
            }
            return this.arm.getResults()[index++];
        }
        arm = null;
        if (isDone()) {
            return null;
        }
    }
}
Also used : Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) BlockedException(org.teiid.common.buffer.BlockedException)

Example 23 with BlockedException

use of org.teiid.common.buffer.BlockedException in project teiid by teiid.

the class RequestWorkItem method processNew.

protected void processNew() throws TeiidProcessingException, TeiidComponentException {
    planningStart = System.currentTimeMillis();
    SessionAwareCache<CachedResults> rsCache = dqpCore.getRsCache();
    boolean cachable = false;
    CacheID cacheId = null;
    if (rsCache != null) {
        boolean canUseCache = true;
        if (requestMsg.getRequestOptions().isContinuous()) {
            canUseCache = false;
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Command is continuous, result set caching will not be used");
        } else if (!requestMsg.useResultSetCache() && getCacheHint() == null) {
            canUseCache = false;
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Command has no cache hint and result set cache mode is not on.");
        }
        if (canUseCache) {
            ParseInfo pi = Request.createParseInfo(requestMsg, this.dqpWorkContext.getSession());
            cacheId = new CacheID(this.dqpWorkContext, pi, requestMsg.getCommandString());
            cachable = cacheId.setParameters(requestMsg.getParameterValues());
            if (cachable) {
                // allow cache to be transactionally aware
                if (rsCache.isTransactional()) {
                    TransactionContext tc = request.getTransactionContext(false);
                    if (tc != null && tc.getTransactionType() != Scope.NONE) {
                        initTransactionState(tc);
                        resume();
                    }
                }
                CachedResults cr = rsCache.get(cacheId);
                // TODO: possibly ignore max rows for caching
                if (cr != null && (cr.getRowLimit() == 0 || (requestMsg.getRowLimit() != 0 && requestMsg.getRowLimit() <= cr.getRowLimit()))) {
                    request.initMetadata();
                    this.originalCommand = cr.getCommand(requestMsg.getCommandString(), request.metadata, pi);
                    if (!request.validateAccess(requestMsg.getCommands(), this.originalCommand, CommandType.CACHED)) {
                        // $NON-NLS-1$
                        LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Using result set cached results", cacheId);
                        this.resultsBuffer = cr.getResults();
                        doneProducingBatches();
                        return;
                    }
                    // $NON-NLS-1$
                    LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Cached result command to be modified, will not use the cached results", cacheId);
                }
            } else {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Parameters are not serializable - cache cannot be used for", cacheId);
            }
        }
    } else {
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Result set caching is disabled.");
    }
    try {
        request.processRequest();
    } finally {
        analysisRecord = request.analysisRecord;
    }
    originalCommand = request.userCommand;
    if (cachable && (requestMsg.useResultSetCache() || originalCommand.getCacheHint() != null) && rsCache != null && originalCommand.areResultsCachable()) {
        this.cid = cacheId;
        // turn on the collection of data objects used
        request.processor.getContext().setDataObjects(new HashSet<Object>(4));
    }
    request.processor.getContext().setWorkItem(this);
    processor = request.processor;
    planningEnd = System.currentTimeMillis();
    this.dqpCore.logMMCommand(this, Event.PLAN, null, null);
    collector = new BatchCollector(processor, processor.getBufferManager(), this.request.context, isForwardOnly()) {

        int maxRows = 0;

        @Override
        protected void flushBatchDirect(TupleBatch batch, boolean add) throws TeiidComponentException, TeiidProcessingException {
            resultsBuffer = getTupleBuffer();
            if (maxRows == 0) {
                maxRows = OUTPUT_BUFFER_MAX_BATCHES * resultsBuffer.getBatchSize();
            }
            if (cid != null) {
                super.flushBatchDirect(batch, add);
            }
            synchronized (lobStreams) {
                if (cid == null && resultsBuffer.isLobs()) {
                    super.flushBatchDirect(batch, false);
                }
                if (batch.getTerminationFlag()) {
                    done();
                }
                add = sendResultsIfNeeded(batch);
                if (cid != null) {
                    return;
                }
                super.flushBatchDirect(batch, add);
                if (!add && !processor.hasBuffer()) {
                    resultsBuffer.setRowCount(batch.getEndRow());
                }
                if (transactionState != TransactionState.ACTIVE && (requestMsg.getRequestOptions().isContinuous() || (useCallingThread && isForwardOnly()))) {
                    synchronized (this) {
                        if (resultsReceiver == null) {
                            // $NON-NLS-1$
                            throw BlockedException.block(requestID, "Blocking to allow asynch processing");
                        }
                    }
                    if (add && !returnsUpdateCount) {
                        // $NON-NLS-1$
                        throw new AssertionError("Should not add batch to buffer");
                    }
                }
                if (add) {
                    flowControl(batch);
                }
            }
        }

        private void flowControl(TupleBatch batch) throws BlockedException {
            if (processor.hasBuffer() || batch.getTerminationFlag() || transactionState == TransactionState.ACTIVE) {
                return;
            }
            synchronized (this) {
                if (!isForwardOnly() && resultsReceiver != null && begin > resultsBuffer.getRowCount()) {
                    // a valid request beyond the processed range
                    return;
                }
            }
            if (resultsBuffer.getManagedRowCount() < maxRows) {
                // continue to buffer
                return;
            }
            int timeOut = 500;
            if (!connectorInfo.isEmpty()) {
                if (explicitSourceClose) {
                    for (DataTierTupleSource ts : getConnectorRequests()) {
                        if (!ts.isExplicitClose()) {
                            timeOut = 100;
                            break;
                        }
                    }
                } else {
                    timeOut = 100;
                }
            }
            if (dqpCore.blockOnOutputBuffer(RequestWorkItem.this)) {
                if (moreWorkTask != null) {
                    moreWorkTask.cancel(false);
                    moreWorkTask = null;
                }
                if (getThreadState() != ThreadState.MORE_WORK) {
                    // we schedule the work to ensure that an idle client won't just indefinitely hold resources
                    moreWorkTask = scheduleWork(timeOut);
                }
                throw // $NON-NLS-1$
                BlockedException.block(// $NON-NLS-1$
                requestID, // $NON-NLS-1$
                "Blocking due to full results TupleBuffer", this.getTupleBuffer(), "rows", this.getTupleBuffer().getManagedRowCount(), "batch size", // $NON-NLS-1$ //$NON-NLS-2$
                this.getTupleBuffer().getBatchSize());
            }
            if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, requestID, "Exceeding buffer limit since there are pending active plans or this is using the calling thread.");
            }
        }
    };
    if (!request.addedLimit && this.requestMsg.getRowLimit() > 0 && this.requestMsg.getRowLimit() < Integer.MAX_VALUE) {
        // covers maxrows for commands that already have a limit, are prepared, or are a stored procedure
        this.collector.setRowLimit(this.requestMsg.getRowLimit());
        this.collector.setSaveLastRow(request.isReturingParams());
    }
    this.resultsBuffer = collector.getTupleBuffer();
    if (this.resultsBuffer == null) {
        // This is just a dummy result it will get replaced by collector source
        resultsBuffer = this.processor.getBufferManager().createTupleBuffer(this.originalCommand.getProjectedSymbols(), this.request.context.getConnectionId(), TupleSourceType.FINAL);
    } else if (this.requestMsg.getRequestOptions().isContinuous()) {
        // TODO: this is based upon continuous being an embedded connection otherwise we have to do something like
        // forcing inlining, but truncating or erroring over a given size (similar to odbc handling)
        resultsBuffer.removeLobTracking();
    }
    initTransactionState(request.transactionContext);
    if (requestMsg.isNoExec()) {
        doneProducingBatches();
        resultsBuffer.close();
        this.cid = null;
    }
    this.returnsUpdateCount = request.returnsUpdateCount;
    if (this.returnsUpdateCount && this.requestMsg.getRequestOptions().isContinuous()) {
        // $NON-NLS-1$
        throw new IllegalStateException("Continuous requests are not allowed to be updates.");
    }
    request = null;
}
Also used : ParseInfo(org.teiid.query.parser.ParseInfo) BlockedException(org.teiid.common.buffer.BlockedException) CacheHint(org.teiid.query.sql.lang.CacheHint) TeiidProcessingException(org.teiid.core.TeiidProcessingException) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) TransactionContext(org.teiid.dqp.service.TransactionContext) TeiidComponentException(org.teiid.core.TeiidComponentException) BatchCollector(org.teiid.query.processor.BatchCollector) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 24 with BlockedException

use of org.teiid.common.buffer.BlockedException in project teiid by teiid.

the class Evaluator method evaluateIsDistinct.

private Boolean evaluateIsDistinct(IsDistinctCriteria idc, List<?> tuple) throws AssertionError, ExpressionEvaluationException, BlockedException, TeiidComponentException {
    RowValue left = getRowValue(tuple, idc.getLeftRowValue());
    RowValue right = getRowValue(tuple, idc.getRightRowValue());
    if (left.length() != right.length()) {
        return !idc.isNegated();
    }
    boolean result = false;
    for (int i = 0; i < left.length(); i++) {
        Object l = left.get(i);
        Object r = right.get(i);
        if (l == null) {
            if (r != null) {
                result = true;
                break;
            }
            continue;
        } else if (r == null) {
            result = true;
            break;
        }
        try {
            Boolean b = compare(CompareCriteria.EQ, l, r);
            if (b == null) {
                // shouldn't happen
                continue;
            }
            if (!b) {
                result = true;
                break;
            }
        } catch (Exception e) {
            // we'll consider this a difference
            // more than likely they are different types
            result = true;
            break;
        }
    }
    if (idc.isNegated()) {
        return !result;
    }
    return result;
}
Also used : LanguageObject(org.teiid.query.sql.LanguageObject) TeiidComponentException(org.teiid.core.TeiidComponentException) ComponentNotFoundException(org.teiid.core.ComponentNotFoundException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) BlockedException(org.teiid.common.buffer.BlockedException) SQLException(java.sql.SQLException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException)

Example 25 with BlockedException

use of org.teiid.common.buffer.BlockedException in project teiid by teiid.

the class Evaluator method evaluate.

private Object evaluate(List<?> tuple, ExceptionExpression ee) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    String msg = (String) internalEvaluate(ee.getMessage(), tuple);
    String sqlState = ee.getDefaultSQLState();
    if (ee.getSqlState() != null) {
        sqlState = (String) internalEvaluate(ee.getSqlState(), tuple);
    }
    Integer errorCode = null;
    if (ee.getErrorCode() != null) {
        errorCode = (Integer) internalEvaluate(ee.getErrorCode(), tuple);
    }
    Exception parent = null;
    if (ee.getParent() != null) {
        parent = (Exception) internalEvaluate(ee.getParent(), tuple);
    }
    Exception result = new TeiidSQLException(parent, msg, sqlState, errorCode != null ? errorCode : 0);
    result.setStackTrace(SourceWarning.EMPTY_STACK_TRACE);
    return result;
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) TeiidComponentException(org.teiid.core.TeiidComponentException) ComponentNotFoundException(org.teiid.core.ComponentNotFoundException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) BlockedException(org.teiid.common.buffer.BlockedException) SQLException(java.sql.SQLException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException)

Aggregations

BlockedException (org.teiid.common.buffer.BlockedException)45 TupleBatch (org.teiid.common.buffer.TupleBatch)16 ArrayList (java.util.ArrayList)15 List (java.util.List)15 TeiidProcessingException (org.teiid.core.TeiidProcessingException)15 TeiidComponentException (org.teiid.core.TeiidComponentException)13 TupleBuffer (org.teiid.common.buffer.TupleBuffer)10 CommandContext (org.teiid.query.util.CommandContext)10 TupleSource (org.teiid.common.buffer.TupleSource)8 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)8 Test (org.junit.Test)6 BufferManager (org.teiid.common.buffer.BufferManager)6 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)5 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)5 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)5 Command (org.teiid.query.sql.lang.Command)5 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)5 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)4 SQLException (java.sql.SQLException)3 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)3