Search in sources :

Example 1 with CacheDirective

use of org.teiid.translator.CacheDirective in project teiid by teiid.

the class TestDataTierManager method testCaching.

@Test
public void testCaching() throws Exception {
    assertEquals(0, connectorManager.getExecuteCount().get());
    QueryMetadataInterface metadata = RealMetadataFactory.exampleBQTCached();
    CacheDirective cd = new CacheDirective();
    this.connectorManager.cacheDirective = cd;
    helpSetupDataTierManager();
    Command command = helpSetupRequest("SELECT stringkey from bqt1.smalla", 1, metadata).getCommand();
    RegisterRequestParameter rrp = new RegisterRequestParameter();
    rrp.connectorBindingId = "x";
    TupleSource ts = dtm.registerRequest(context, command, "foo", rrp);
    assertTrue(ts instanceof CachingTupleSource);
    assertEquals(10, pullTuples(ts, -1));
    assertEquals(1, connectorManager.getExecuteCount().get());
    assertFalse(rrp.doNotCache);
    assertFalse(((CachingTupleSource) ts).dtts.errored);
    assertNull(((CachingTupleSource) ts).dtts.scope);
    ts.closeSource();
    assertEquals(1, this.rm.getRsCache().getCachePutCount());
    assertEquals(1, this.rm.getRsCache().getTotalCacheEntries());
    // same session, should be cached
    command = helpSetupRequest("SELECT stringkey from bqt1.smalla", 1, metadata).getCommand();
    rrp = new RegisterRequestParameter();
    rrp.connectorBindingId = "x";
    ts = dtm.registerRequest(context, command, "foo", rrp);
    assertFalse(ts instanceof CachingTupleSource);
    assertEquals(10, pullTuples(ts, -1));
    assertEquals(1, connectorManager.getExecuteCount().get());
    assertTrue(rrp.doNotCache);
    // switch sessions
    command = helpSetupRequest("SELECT stringkey from bqt1.smalla", 1, metadata).getCommand();
    this.context.getSession().setSessionId("different");
    rrp = new RegisterRequestParameter();
    rrp.connectorBindingId = "x";
    ts = dtm.registerRequest(context, command, "foo", rrp);
    assertTrue(ts instanceof CachingTupleSource);
    assertEquals(9, pullTuples(ts, 9));
    assertEquals(2, connectorManager.getExecuteCount().get());
    assertFalse(rrp.doNotCache);
    // should force read all
    ts.closeSource();
    assertFalse(((CachingTupleSource) ts).dtts.errored);
    assertNull(((CachingTupleSource) ts).dtts.scope);
    assertEquals(2, this.rm.getRsCache().getCachePutCount());
    assertEquals(2, this.rm.getRsCache().getTotalCacheEntries());
    // proactive invalidation, removes immediately
    command = helpSetupRequest("SELECT stringkey from bqt1.smalla", 1, metadata).getCommand();
    cd.setInvalidation(Invalidation.IMMEDIATE);
    rrp = new RegisterRequestParameter();
    rrp.connectorBindingId = "x";
    ts = dtm.registerRequest(context, command, "foo", rrp);
    assertTrue(ts instanceof CachingTupleSource);
    assertEquals(10, pullTuples(ts, -1));
    assertEquals(3, connectorManager.getExecuteCount().get());
    assertFalse(rrp.doNotCache);
}
Also used : CacheDirective(org.teiid.translator.CacheDirective) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) TupleSource(org.teiid.common.buffer.TupleSource) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) RegisterRequestParameter(org.teiid.query.processor.RegisterRequestParameter) Test(org.junit.Test)

Example 2 with CacheDirective

use of org.teiid.translator.CacheDirective 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)

Example 3 with CacheDirective

use of org.teiid.translator.CacheDirective in project teiid by teiid.

the class TestDataTierManager method testCancelWithCaching.

@Test
public void testCancelWithCaching() throws Exception {
    QueryMetadataInterface metadata = RealMetadataFactory.exampleBQTCached();
    CacheDirective cd = new CacheDirective();
    this.connectorManager.cacheDirective = cd;
    helpSetupDataTierManager();
    Command command = helpSetupRequest("SELECT stringkey from bqt1.smalla", 1, metadata).getCommand();
    this.context.getSession().setSessionId("different");
    RegisterRequestParameter rrp = new RegisterRequestParameter();
    rrp.connectorBindingId = "x";
    TupleSource ts = dtm.registerRequest(context, command, "foo", rrp);
    assertTrue(ts instanceof CachingTupleSource);
    assertEquals(4, pullTuples(ts, 4));
    ((CachingTupleSource) ts).item.requestCancel("");
    assertEquals(1, connectorManager.getExecuteCount().get());
    assertFalse(rrp.doNotCache);
    // should force read all
    ts.closeSource();
    assertFalse(((CachingTupleSource) ts).dtts.errored);
    assertNull(((CachingTupleSource) ts).dtts.scope);
    assertEquals(0, this.rm.getRsCache().getCachePutCount());
}
Also used : CacheDirective(org.teiid.translator.CacheDirective) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) TupleSource(org.teiid.common.buffer.TupleSource) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) RegisterRequestParameter(org.teiid.query.processor.RegisterRequestParameter) Test(org.junit.Test)

Aggregations

TupleSource (org.teiid.common.buffer.TupleSource)3 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)3 CacheDirective (org.teiid.translator.CacheDirective)3 Test (org.junit.Test)2 RegisterRequestParameter (org.teiid.query.processor.RegisterRequestParameter)2 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)2 Command (org.teiid.query.sql.lang.Command)2 TupleBuffer (org.teiid.common.buffer.TupleBuffer)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 ConnectorManager (org.teiid.dqp.internal.datamgr.ConnectorManager)1 ConnectorManagerRepository (org.teiid.dqp.internal.datamgr.ConnectorManagerRepository)1 ConnectorWork (org.teiid.dqp.internal.datamgr.ConnectorWork)1 CacheID (org.teiid.dqp.internal.process.SessionAwareCache.CacheID)1 CachableVisitor (org.teiid.dqp.internal.process.TupleSourceCache.CachableVisitor)1 CopyOnReadTupleSource (org.teiid.dqp.internal.process.TupleSourceCache.CopyOnReadTupleSource)1 AtomicRequestMessage (org.teiid.dqp.message.AtomicRequestMessage)1 TempMetadataID (org.teiid.query.metadata.TempMetadataID)1 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)1 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)1 TranslatorException (org.teiid.translator.TranslatorException)1