Search in sources :

Example 6 with CacheID

use of org.teiid.dqp.internal.process.SessionAwareCache.CacheID in project teiid by teiid.

the class TestPreparedPlanCache method helpPutPreparedPlans.

// ====Help methods====//
private void helpPutPreparedPlans(SessionAwareCache<PreparedPlan> cache, DQPWorkContext session, int start, int count) {
    for (int i = 0; i < count; i++) {
        Command dummy;
        try {
            dummy = QueryParser.getQueryParser().parseCommand(EXAMPLE_QUERY + (start + i));
        } catch (QueryParserException e) {
            throw new RuntimeException(e);
        }
        CacheID id = new CacheID(session, pi, dummy.toString());
        PreparedPlan pPlan = new PreparedPlan();
        cache.put(id, Determinism.SESSION_DETERMINISTIC, pPlan, null);
        pPlan.setCommand(dummy);
        pPlan.setPlan(new RelationalPlan(new ProjectNode(i)), new CommandContext());
        AnalysisRecord analysisRecord = new AnalysisRecord(true, false);
        pPlan.setAnalysisRecord(analysisRecord);
        ArrayList<Reference> refs = new ArrayList<Reference>();
        refs.add(new Reference(1));
        pPlan.setReferences(refs);
    }
}
Also used : QueryParserException(org.teiid.api.exception.query.QueryParserException) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) CommandContext(org.teiid.query.util.CommandContext) Reference(org.teiid.query.sql.symbol.Reference) ArrayList(java.util.ArrayList) RelationalPlan(org.teiid.query.processor.relational.RelationalPlan) Command(org.teiid.query.sql.lang.Command) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ProjectNode(org.teiid.query.processor.relational.ProjectNode)

Example 7 with CacheID

use of org.teiid.dqp.internal.process.SessionAwareCache.CacheID in project teiid by teiid.

the class TestPreparedPlanCache method testPutPreparedPlan.

// ====Tests====//
@Test
public void testPutPreparedPlan() {
    SessionAwareCache<PreparedPlan> cache = new SessionAwareCache<PreparedPlan>("preparedplan", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.PREPAREDPLAN, 0);
    CacheID id = new CacheID(token, pi, EXAMPLE_QUERY + 1);
    // No PreparedPlan at the begining
    assertNull(cache.get(id));
    // create one
    cache.put(id, Determinism.SESSION_DETERMINISTIC, new PreparedPlan(), null);
    // should have one now
    // $NON-NLS-1$
    assertNotNull("Unable to get prepared plan from cache", cache.get(id));
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) Test(org.junit.Test)

Example 8 with CacheID

use of org.teiid.dqp.internal.process.SessionAwareCache.CacheID in project teiid by teiid.

the class PreparedStatementRequest method generatePlan.

/**
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 * @see org.teiid.dqp.internal.process.Request#generatePlan()
 */
@Override
protected void generatePlan(boolean addLimit) throws TeiidComponentException, TeiidProcessingException {
    createCommandContext();
    String sqlQuery = requestMsg.getCommands()[0];
    if (this.preParser != null) {
        sqlQuery = this.preParser.preParse(sqlQuery, this.context);
    }
    CacheID id = new CacheID(this.workContext, Request.createParseInfo(this.requestMsg, this.workContext.getSession()), sqlQuery);
    prepPlan = prepPlanCache.get(id);
    if (prepPlan != null) {
        // already in cache. obtain the values from cache
        analysisRecord = prepPlan.getAnalysisRecord();
        ProcessorPlan cachedPlan = prepPlan.getPlan();
        this.userCommand = prepPlan.getCommand();
        if (validateAccess(requestMsg.getCommands(), userCommand, CommandType.PREPARED)) {
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_DQP, requestId, "AuthorizationValidator indicates that the prepared plan for command will not be used");
            prepPlan = null;
            analysisRecord = null;
        } else {
            // $NON-NLS-1$
            LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Query exist in cache: ", sqlQuery });
            processPlan = cachedPlan.clone();
        }
    }
    if (prepPlan == null) {
        // if prepared plan does not exist, create one
        prepPlan = new PreparedPlan();
        // $NON-NLS-1$
        LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Query does not exist in cache: ", sqlQuery });
        super.generatePlan(true);
        prepPlan.setCommand(this.userCommand);
        // there's no need to cache the plan if it's a stored procedure, since we already do that in the optimizer
        boolean cache = !(this.userCommand instanceof StoredProcedure);
        // Defect 13751: Clone the plan in its current state (i.e. before processing) so that it can be used for later queries
        prepPlan.setPlan(cache ? processPlan.clone() : processPlan, this.context);
        prepPlan.setAnalysisRecord(analysisRecord);
        if (cache) {
            Determinism determinismLevel = this.context.getDeterminismLevel();
            if (userCommand.getCacheHint() != null && userCommand.getCacheHint().getDeterminism() != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", this.context.getDeterminismLevel(), " to ", determinismLevel });
                determinismLevel = userCommand.getCacheHint().getDeterminism();
            }
            this.prepPlanCache.put(id, determinismLevel, prepPlan, userCommand.getCacheHint() != null ? userCommand.getCacheHint().getTtl() : null);
        }
    }
    if (requestMsg.isBatchedUpdate()) {
        handlePreparedBatchUpdate();
    } else {
        List<Reference> params = prepPlan.getReferences();
        List<?> values = requestMsg.getParameterValues();
        PreparedStatementRequest.resolveParameterValues(params, values, this.context, this.metadata);
    }
}
Also used : Determinism(org.teiid.metadata.FunctionMethod.Determinism) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) Reference(org.teiid.query.sql.symbol.Reference) ProcessorPlan(org.teiid.query.processor.ProcessorPlan)

Example 9 with CacheID

use of org.teiid.dqp.internal.process.SessionAwareCache.CacheID in project teiid by teiid.

the class CommandContext method getPlan.

public PreparedPlan getPlan(String key) {
    if (this.globalState.planCache == null) {
        return null;
    }
    CacheID id = new CacheID(new ParseInfo(), key, getVdbName(), getVdbVersion(), getConnectionId(), getUserName());
    PreparedPlan pp = this.globalState.planCache.get(id);
    if (pp != null) {
        if (id.getSessionId() != null) {
            setDeterminismLevel(Determinism.USER_DETERMINISTIC);
        } else if (id.getUserName() != null) {
            setDeterminismLevel(Determinism.SESSION_DETERMINISTIC);
        }
        return pp;
    }
    return null;
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan) ParseInfo(org.teiid.query.parser.ParseInfo)

Example 10 with CacheID

use of org.teiid.dqp.internal.process.SessionAwareCache.CacheID in project teiid by teiid.

the class DataTierManagerImpl method registerRequest.

public TupleSource registerRequest(CommandContext context, Command command, String modelName, final RegisterRequestParameter parameterObject) throws TeiidComponentException, TeiidProcessingException {
    RequestWorkItem workItem = context.getWorkItem();
    Assertion.isNotNull(workItem);
    if (CoreConstants.SYSTEM_MODEL.equals(modelName) || CoreConstants.SYSTEM_ADMIN_MODEL.equals(modelName)) {
        return processSystemQuery(context, command, workItem.getDqpWorkContext());
    }
    AtomicRequestMessage aqr = createRequest(workItem, command, modelName, parameterObject.connectorBindingId, parameterObject.nodeID);
    aqr.setCommandContext(context);
    if (parameterObject.fetchSize > 0) {
        aqr.setFetchSize(2 * parameterObject.fetchSize);
    }
    if (parameterObject.limit > 0) {
        aqr.setFetchSize(Math.min(parameterObject.limit, aqr.getFetchSize()));
    }
    aqr.setCopyStreamingLobs(parameterObject.copyStreamingLobs);
    Collection<GroupSymbol> accessedGroups = null;
    if (context.getDataObjects() != null) {
        QueryMetadataInterface metadata = context.getMetadata();
        accessedGroups = GroupCollectorVisitor.getGroupsIgnoreInlineViews(command, false);
        boolean usedModel = false;
        for (GroupSymbol gs : accessedGroups) {
            context.accessedDataObject(gs.getMetadataID());
            // check the source/tables/procs for determinism level
            Object mid = gs.getMetadataID();
            if (mid instanceof TempMetadataID) {
                TempMetadataID tid = (TempMetadataID) mid;
                if (tid.getOriginalMetadataID() != null) {
                    mid = tid.getOriginalMetadataID();
                }
            }
            String specificProp = metadata.getExtensionProperty(mid, AbstractMetadataRecord.RELATIONAL_URI + DDLConstants.DETERMINISM, false);
            if (specificProp == null) {
                if (!usedModel) {
                    Object modelId = metadata.getModelID(mid);
                    String prop = metadata.getExtensionProperty(modelId, AbstractMetadataRecord.RELATIONAL_URI + DDLConstants.DETERMINISM, false);
                    if (prop != null) {
                        usedModel = true;
                        // set model property
                        context.setDeterminismLevel(Determinism.valueOf(prop.toUpperCase()));
                    }
                }
                continue;
            }
            context.setDeterminismLevel(Determinism.valueOf(specificProp.toUpperCase()));
        }
    }
    ConnectorManagerRepository cmr = workItem.getDqpWorkContext().getVDB().getAttachment(ConnectorManagerRepository.class);
    ConnectorManager connectorManager = cmr.getConnectorManager(aqr.getConnectorName());
    if (connectorManager == null) {
        // can happen if sources are removed
        if (RelationalNodeUtil.hasOutputParams(command)) {
            // $NON-NLS-1$
            throw new AssertionError("A source is required to execute a procedure returning parameters");
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        LogManager.logDetail(LogConstants.CTX_DQP, "source", aqr.getConnectorName(), "no longer exists, returning dummy results");
        return CollectionTupleSource.createNullTupleSource();
    }
    ConnectorWork work = connectorManager.registerRequest(aqr);
    if (!work.isForkable()) {
        aqr.setSerial(true);
    }
    CacheID cid = null;
    CacheDirective cd = null;
    if (workItem.getRsCache() != null && command.areResultsCachable()) {
        CachableVisitor cv = new CachableVisitor();
        PreOrPostOrderNavigator.doVisit(command, cv, PreOrPostOrderNavigator.PRE_ORDER, true);
        if (cv.cacheable) {
            try {
                cd = work.getCacheDirective();
            } catch (TranslatorException e) {
                // $NON-NLS-1$
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30504, e, aqr.getConnectorName() + ": " + e.getMessage());
            }
            if (cd != null) {
                if (cd.getScope() == Scope.NONE) {
                    parameterObject.doNotCache = true;
                } else {
                    String cmdString = command.toString();
                    if (cmdString.length() < 100000) {
                        // TODO: this check won't be needed if keys aren't exclusively held in memory
                        cid = new CacheID(workItem.getDqpWorkContext(), ParseInfo.DEFAULT_INSTANCE, cmdString);
                        cid.setParameters(cv.parameters);
                        if (cd.getInvalidation() == null || cd.getInvalidation() == Invalidation.NONE) {
                            CachedResults cr = workItem.getRsCache().get(cid);
                            if (cr != null && (cr.getRowLimit() == 0 || (parameterObject.limit > 0 && cr.getRowLimit() >= parameterObject.limit))) {
                                parameterObject.doNotCache = true;
                                // $NON-NLS-1$
                                LogManager.logDetail(LogConstants.CTX_DQP, "Using cache entry for", cid);
                                work.close();
                                return cr.getResults().createIndexedTupleSource();
                            }
                        } else if (cd.getInvalidation() == Invalidation.IMMEDIATE) {
                            workItem.getRsCache().remove(cid, CachingTupleSource.getDeterminismLevel(cd.getScope()));
                        }
                    }
                }
            } else {
                // $NON-NLS-1$
                LogManager.logTrace(LogConstants.CTX_DQP, aqr.getAtomicRequestID(), "no cache directive");
            }
        } else {
            // $NON-NLS-1$
            LogManager.logTrace(LogConstants.CTX_DQP, aqr.getAtomicRequestID(), "command not cachable");
        }
    }
    DataTierTupleSource dtts = new DataTierTupleSource(aqr, workItem, work, this, parameterObject.limit);
    TupleSource result = dtts;
    TupleBuffer tb = null;
    if (cid != null) {
        tb = getBufferManager().createTupleBuffer(aqr.getCommand().getProjectedSymbols(), aqr.getCommandContext().getConnectionId(), TupleSourceType.PROCESSOR);
        result = new CachingTupleSource(this, tb, (DataTierTupleSource) result, cid, parameterObject, cd, accessedGroups, workItem);
    }
    if (work.isThreadBound()) {
        result = handleThreadBound(workItem, aqr, work, cid, result, dtts, tb);
    } else if (!aqr.isSerial()) {
        dtts.addWork();
    }
    return result;
}
Also used : CachableVisitor(org.teiid.dqp.internal.process.TupleSourceCache.CachableVisitor) ConnectorManagerRepository(org.teiid.dqp.internal.datamgr.ConnectorManagerRepository) TempMetadataID(org.teiid.query.metadata.TempMetadataID) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ConnectorManager(org.teiid.dqp.internal.datamgr.ConnectorManager) TeiidProcessingException(org.teiid.core.TeiidProcessingException) CacheDirective(org.teiid.translator.CacheDirective) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) CopyOnReadTupleSource(org.teiid.dqp.internal.process.TupleSourceCache.CopyOnReadTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TranslatorException(org.teiid.translator.TranslatorException) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) ConnectorWork(org.teiid.dqp.internal.datamgr.ConnectorWork) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage)

Aggregations

CacheID (org.teiid.dqp.internal.process.SessionAwareCache.CacheID)16 ParseInfo (org.teiid.query.parser.ParseInfo)11 Test (org.junit.Test)8 Cachable (org.teiid.cache.Cachable)6 Reference (org.teiid.query.sql.symbol.Reference)4 TupleBuffer (org.teiid.common.buffer.TupleBuffer)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 Determinism (org.teiid.metadata.FunctionMethod.Determinism)2 BatchCollector (org.teiid.query.processor.BatchCollector)2 ProjectNode (org.teiid.query.processor.relational.ProjectNode)2 RelationalPlan (org.teiid.query.processor.relational.RelationalPlan)2 Command (org.teiid.query.sql.lang.Command)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 QueryParserException (org.teiid.api.exception.query.QueryParserException)1 BlockedException (org.teiid.common.buffer.BlockedException)1 TupleBatch (org.teiid.common.buffer.TupleBatch)1 TupleSource (org.teiid.common.buffer.TupleSource)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1 ConnectorManager (org.teiid.dqp.internal.datamgr.ConnectorManager)1