Search in sources :

Example 26 with TeiidComponentException

use of org.teiid.core.TeiidComponentException 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)

Example 27 with TeiidComponentException

use of org.teiid.core.TeiidComponentException 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 28 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class LobManager method updateReferences.

@SuppressWarnings("unchecked")
public void updateReferences(List<?> tuple, ReferenceMode mode) throws TeiidComponentException {
    for (int i = 0; i < lobIndexes.length; i++) {
        Object anObj = tuple.get(lobIndexes[i]);
        if (!(anObj instanceof Streamable<?>)) {
            continue;
        }
        Streamable lob = (Streamable) anObj;
        String id = lob.getReferenceStreamId();
        LobHolder lobHolder = this.lobReferences.get(id);
        switch(mode) {
            case REMOVE:
                if (lobHolder != null) {
                    lobHolder.referenceCount--;
                    if (lobHolder.referenceCount < 1) {
                        this.lobReferences.remove(id);
                    }
                }
                break;
            case ATTACH:
                if (lob.getReference() == null) {
                    if (lobHolder == null) {
                        throw new TeiidComponentException(QueryPlugin.Event.TEIID30033, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30033));
                    }
                    lob.setReference(lobHolder.lob.getReference());
                }
                break;
            case CREATE:
                try {
                    StorageMode storageMode = InputStreamFactory.getStorageMode(lob);
                    if (lob.getReferenceStreamId() == null || (inlineLobs && (storageMode == StorageMode.MEMORY || (storageMode != StorageMode.FREE && lob.length() * (lob instanceof ClobType ? 2 : 1) <= maxMemoryBytes)))) {
                        lob.setReferenceStreamId(null);
                        // since this is untracked at this point, we must detach if possible
                        if (inlineLobs && storageMode == StorageMode.OTHER) {
                            persistLob(lob, null, null, true, maxMemoryBytes);
                        }
                        continue;
                    }
                } catch (SQLException e) {
                // presumably the lob is bad, but let it slide for now
                }
                if (lob.getReference() == null) {
                    throw new TeiidComponentException(QueryPlugin.Event.TEIID30034, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30034));
                }
                if (lobHolder == null) {
                    this.lobReferences.put(id, new LobHolder(lob));
                } else {
                    lobHolder.referenceCount++;
                }
                break;
        }
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) SQLException(java.sql.SQLException) TeiidComponentException(org.teiid.core.TeiidComponentException) Streamable(org.teiid.core.types.Streamable) StorageMode(org.teiid.core.types.InputStreamFactory.StorageMode)

Example 29 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class LobManager method persistLob.

public static void persistLob(final Streamable<?> lob, final FileStore store, byte[] bytes, boolean inlineLobs, int maxMemoryBytes) throws TeiidComponentException {
    long byteLength = Integer.MAX_VALUE;
    try {
        byteLength = lob.length() * (lob instanceof ClobType ? 2 : 1);
    } catch (SQLException e) {
    // just ignore for now - for a single read resource computing the length invalidates
    // TODO - inline small persisted lobs
    }
    try {
        // inline
        if (lob.getReferenceStreamId() == null || (inlineLobs && (byteLength <= maxMemoryBytes))) {
            lob.setReferenceStreamId(null);
            if (InputStreamFactory.getStorageMode(lob) == StorageMode.MEMORY) {
                return;
            }
            if (lob instanceof BlobType) {
                BlobType b = (BlobType) lob;
                byte[] blobBytes = b.getBytes(1, (int) byteLength);
                b.setReference(new SerialBlob(blobBytes));
            } else if (lob instanceof ClobType) {
                ClobType c = (ClobType) lob;
                // $NON-NLS-1$
                String s = "";
                // some clob impls return null for 0 length
                if (byteLength != 0) {
                    s = c.getSubString(1, (int) (byteLength >>> 1));
                }
                c.setReference(new ClobImpl(s));
            } else {
                XMLType x = (XMLType) lob;
                String s = x.getString();
                x.setReference(new SQLXMLImpl(s));
            }
            return;
        }
        InputStream is = null;
        if (lob instanceof BlobType) {
            is = new BlobInputStreamFactory((Blob) lob).getInputStream();
        } else if (lob instanceof ClobType) {
            is = new ClobInputStreamFactory((Clob) lob).getInputStream();
        } else {
            is = new SQLXMLInputStreamFactory((SQLXML) lob).getInputStream();
        }
        long offset = store.getLength();
        OutputStream fsos = store.createOutputStream();
        byteLength = ObjectConverterUtil.write(fsos, is, bytes, -1);
        // re-construct the new lobs based on the file store
        final long lobOffset = offset;
        final long lobLength = byteLength;
        /*
			 * Using an inner class here will hold a reference to the LobManager
			 * which prevents the removal of the FileStore until all of the
			 * lobs have been gc'd
			 */
        InputStreamFactory isf = new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return store.createInputStream(lobOffset, lobLength);
            }

            @Override
            public StorageMode getStorageMode() {
                return StorageMode.PERSISTENT;
            }
        };
        isf.setLength(byteLength);
        if (lob instanceof BlobType) {
            ((BlobType) lob).setReference(new BlobImpl(isf));
        } else if (lob instanceof ClobType) {
            long length = -1;
            try {
                length = ((ClobType) lob).length();
            } catch (SQLException e) {
            // could be streaming
            }
            ((ClobType) lob).setReference(new ClobImpl(isf, length));
        } else {
            ((XMLType) lob).setReference(new SQLXMLImpl(isf));
        }
    } catch (SQLException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30037, e);
    } catch (IOException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30036, e);
    }
}
Also used : SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SerialBlob(javax.sql.rowset.serial.SerialBlob) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) SQLXML(java.sql.SQLXML) TeiidComponentException(org.teiid.core.TeiidComponentException) Clob(java.sql.Clob) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl)

Example 30 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class BufferManagerImpl method setState.

@Override
public void setState(String state_id, InputStream istream) {
    TupleBuffer buffer = this.getTupleBuffer(state_id);
    if (buffer == null) {
        try {
            ObjectInputStream in = new ObjectInputStream(istream);
            setTupleBufferState(state_id, in);
        } catch (IOException e) {
            throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30056, e);
        } catch (ClassNotFoundException e) {
            throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30057, e);
        } catch (TeiidComponentException e) {
            throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30058, e);
        }
    }
}
Also used : TeiidComponentException(org.teiid.core.TeiidComponentException) IOException(java.io.IOException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

TeiidComponentException (org.teiid.core.TeiidComponentException)109 TeiidProcessingException (org.teiid.core.TeiidProcessingException)33 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)23 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)23 ArrayList (java.util.ArrayList)18 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)17 BlockedException (org.teiid.common.buffer.BlockedException)16 IOException (java.io.IOException)15 List (java.util.List)14 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)13 Test (org.junit.Test)12 LanguageObject (org.teiid.query.sql.LanguageObject)12 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)11 CommandContext (org.teiid.query.util.CommandContext)11 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)10 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)9 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)9 HashMap (java.util.HashMap)7 TeiidException (org.teiid.core.TeiidException)7 LogonException (org.teiid.client.security.LogonException)6