Search in sources :

Example 31 with TupleBuffer

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

the class TempTableDataManager method handleCachedProcedure.

private TupleSource handleCachedProcedure(final CommandContext context, StoredProcedure proc) throws TeiidComponentException, QueryMetadataException, TeiidProcessingException {
    String fullName = context.getMetadata().getFullName(proc.getProcedureID());
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_DQP, "processing cached procedure request for", fullName);
    LinkedList<Object> vals = new LinkedList<Object>();
    for (SPParameter param : proc.getInputParameters()) {
        vals.add(((Constant) param.getExpression()).getValue());
    }
    // collapse the hash to single byte for the key to restrict the possible results to 256
    int hash = vals.hashCode();
    hash |= (hash >>> 16);
    hash |= (hash >>> 8);
    hash &= 0x000000ff;
    final CacheID cid = new CacheID(new ParseInfo(), fullName + hash, context.getVdbName(), context.getVdbVersion(), context.getConnectionId(), context.getUserName());
    cid.setParameters(vals);
    CachedResults results = cache.get(cid);
    if (results != null) {
        TupleBuffer buffer = results.getResults();
        return buffer.createIndexedTupleSource();
    }
    // construct a query with a no cache hint
    final CacheHint hint = proc.getCacheHint();
    proc.setCacheHint(null);
    Option option = new Option();
    option.setNoCache(true);
    option.addNoCacheGroup(fullName);
    proc.setOption(option);
    StoredProcedure cloneProc = (StoredProcedure) proc.clone();
    int i = 0;
    for (SPParameter param : cloneProc.getInputParameters()) {
        param.setExpression(new Reference(i++));
    }
    final QueryProcessor qp = context.getQueryProcessorFactory().createQueryProcessor(cloneProc.toString(), fullName.toUpperCase(), context, vals.toArray());
    final BatchCollector bc = qp.createBatchCollector();
    return new ProxyTupleSource() {

        boolean success = false;

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            TupleBuffer tb = bc.collectTuples();
            CachedResults cr = new CachedResults();
            cr.setResults(tb, qp.getProcessorPlan());
            Determinism determinismLevel = qp.getContext().getDeterminismLevel();
            if (hint != null && hint.getDeterminism() != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", determinismLevel, " to ", hint.getDeterminism() });
                determinismLevel = hint.getDeterminism();
            }
            cache.put(cid, determinismLevel, cr, hint != null ? hint.getTtl() : null);
            context.setDeterminismLevel(determinismLevel);
            success = true;
            return tb.createIndexedTupleSource();
        }

        @Override
        public void closeSource() {
            super.closeSource();
            qp.closeProcessing();
            if (!success && bc.getTupleBuffer() != null) {
                bc.getTupleBuffer().remove();
            }
        }
    };
}
Also used : Determinism(org.teiid.metadata.FunctionMethod.Determinism) Reference(org.teiid.query.sql.symbol.Reference) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ParseInfo(org.teiid.query.parser.ParseInfo) LinkedList(java.util.LinkedList) CachedResults(org.teiid.dqp.internal.process.CachedResults) QueryProcessor(org.teiid.query.processor.QueryProcessor) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) BatchCollector(org.teiid.query.processor.BatchCollector)

Example 32 with TupleBuffer

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

the class TestBufferManagerImpl method testTupleBufferSessionMax.

@Test
public void testTupleBufferSessionMax() throws Exception {
    BufferManagerImpl bufferManager = new BufferManagerImpl();
    bufferManager.setCache(new MemoryStorageManager() {

        @Override
        public long getMaxStorageSpace() {
            return 64000;
        }
    });
    bufferManager.setMaxReserveKB(10);
    bufferManager.setMaxActivePlans(10);
    bufferManager.setOptions(new Options().maxSessionBufferSizeEstimate(100000));
    bufferManager.initialize();
    CommandContext context = new CommandContext();
    context.setSession(new SessionMetadata());
    CommandContext.pushThreadLocalContext(context);
    try {
        List<TupleBuffer> tupleBuffers = new ArrayList<TupleBuffer>();
        for (int i = 0; i < 36; i++) {
            TupleBuffer tb = bufferManager.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, String.class)), "x", TupleSourceType.PROCESSOR);
            try {
                for (int j = 0; j < 50; j++) {
                    tb.addTuple(Arrays.asList("a"));
                }
                tb.saveBatch();
                if (i % 2 == 0) {
                    tb.remove();
                }
            } catch (TeiidComponentException e) {
                assertEquals(34, i);
                return;
            }
            tupleBuffers.add(tb);
        }
    } finally {
        CommandContext.popThreadLocalContext();
    }
    fail();
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Options(org.teiid.query.util.Options) CommandContext(org.teiid.query.util.CommandContext) SessionMetadata(org.teiid.adminapi.impl.SessionMetadata) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ArrayList(java.util.ArrayList) TeiidComponentException(org.teiid.core.TeiidComponentException) Test(org.junit.Test)

Example 33 with TupleBuffer

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

the class TestBufferManagerImpl method testTupleBufferMax.

@Test(expected = TeiidComponentException.class)
public void testTupleBufferMax() throws Exception {
    BufferManagerImpl bufferManager = new BufferManagerImpl();
    bufferManager.setCache(new MemoryStorageManager() {

        @Override
        public long getMaxStorageSpace() {
            return 640;
        }
    });
    bufferManager.setMaxReserveKB(10);
    bufferManager.setMaxActivePlans(20);
    bufferManager.initialize();
    TupleBuffer tb = bufferManager.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, String.class)), "x", TupleSourceType.PROCESSOR);
    // fill one batch, which should then exceed the max
    for (int i = 0; i < 1024; i++) {
        tb.addTuple(Arrays.asList("a"));
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TupleBuffer(org.teiid.common.buffer.TupleBuffer) Test(org.junit.Test)

Example 34 with TupleBuffer

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

the class TestCachedResults method testCaching.

@Test
public void testCaching() throws Exception {
    FakeBufferService fbs = new FakeBufferService(true);
    // $NON-NLS-1$
    ElementSymbol x = new ElementSymbol("x");
    x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    List<ElementSymbol> schema = Arrays.asList(x);
    // $NON-NLS-1$
    TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR);
    tb.setForwardOnly(false);
    tb.addTuple(Arrays.asList(1));
    tb.addTuple(Arrays.asList(2));
    tb.addTuple(Arrays.asList(3));
    tb.addTuple(Arrays.asList(4));
    tb.addTuple(Arrays.asList(5));
    tb.addTuple(Arrays.asList(6));
    tb.addTuple(Arrays.asList(7));
    tb.addTuple(Arrays.asList(8));
    tb.addTuple(Arrays.asList(9));
    tb.addTuple(Arrays.asList(10));
    tb.close();
    BufferManager bm = fbs.getBufferManager();
    CachedResults results = new CachedResults();
    ProcessorPlan plan = new FakeProcessorPlan(0);
    CommandContext cc = new CommandContext();
    Table t = RealMetadataFactory.exampleBQT().getGroupID("bqt1.smalla");
    cc.accessedDataObject(t);
    plan.setContext(cc);
    results.setResults(tb, plan);
    results.setCommand(new Query());
    // Cache cache = new DefaultCache("dummy"); //$NON-NLS-1$
    long ts = results.getAccessInfo().getCreationTime();
    // in cache
    for (int row = 1; row <= tb.getRowCount(); row += 4) {
    // cache.put(results.getId()+","+row, tb.getBatch(row), null); //$NON-NLS-1$
    }
    results.prepare(bm);
    // simulate distribute
    TupleBuffer distributedTb = bm.getTupleBuffer(results.getId());
    CachedResults cachedResults = UnitTestUtil.helpSerialize(results);
    RealMetadataFactory.buildWorkContext(RealMetadataFactory.exampleBQT());
    BufferManager bm2 = fbs.getBufferManager();
    bm2.distributeTupleBuffer(results.getId(), distributedTb);
    assertTrue(cachedResults.restore(bm2));
    // since restored, simulate a async cache flush
    // cache.clear();
    TupleBuffer cachedTb = cachedResults.getResults();
    assertTrue(cachedTb.isFinal());
    assertEquals(tb.getRowCount(), cachedTb.getRowCount());
    assertEquals(tb.getBatchSize(), cachedTb.getBatchSize());
    assertArrayEquals(tb.getBatch(1).getAllTuples(), cachedTb.getBatch(1).getAllTuples());
    assertArrayEquals(tb.getBatch(9).getAllTuples(), cachedTb.getBatch(9).getAllTuples());
    assertTrue(ts - cachedResults.getAccessInfo().getCreationTime() <= 5000);
// ensure that an incomplete load fails ( is this still valid use case?)
// bm2.getTupleBuffer(results.getId()).remove();
// cachedResults = UnitTestUtil.helpSerialize(results);
// assertFalse(cachedResults.restore(cache, bm2));
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Table(org.teiid.metadata.Table) CommandContext(org.teiid.query.util.CommandContext) Query(org.teiid.query.sql.lang.Query) TupleBuffer(org.teiid.common.buffer.TupleBuffer) FakeBufferService(org.teiid.dqp.service.FakeBufferService) BufferManager(org.teiid.common.buffer.BufferManager) FakeProcessorPlan(org.teiid.query.processor.FakeProcessorPlan) FakeProcessorPlan(org.teiid.query.processor.FakeProcessorPlan) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) Test(org.junit.Test)

Example 35 with TupleBuffer

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

the class TestBatchIterator method testNoSaveForwardOnly.

@Test
public void testNoSaveForwardOnly() throws Exception {
    BatchIterator bi = new BatchIterator(new FakeRelationalNode(1, new List[] { Arrays.asList(1), Arrays.asList(1), Arrays.asList(1), Arrays.asList(1) }, 2) {

        @Override
        public TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
            TupleBatch tb = super.nextBatchDirect();
            tb.setRowOffset(tb.getBeginRow() + 3);
            return tb;
        }
    });
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    TupleBuffer tb = bm.createTupleBuffer(Arrays.asList(new ElementSymbol("x", null, DataTypeManager.DefaultDataClasses.INTEGER)), "test", TupleSourceType.PROCESSOR);
    tb.setForwardOnly(true);
    // $NON-NLS-1$
    bi.setBuffer(tb, false);
    tb.addTuple(Arrays.asList(2));
    tb.addTuple(Arrays.asList(2));
    tb.addTuple(Arrays.asList(2));
    assertEquals(3, bi.getBuffer().getManagedRowCount());
    bi.nextTuple();
    // pull the first batch
    assertEquals(2, bi.available());
    assertEquals(0, bi.getBuffer().getManagedRowCount());
    for (int i = 0; i < 2; i++) {
        assertNotNull(bi.nextTuple());
        assertEquals(0, bi.getBuffer().getManagedRowCount());
    }
    bi.readAhead(3);
    assertEquals(2, bi.getBuffer().getManagedRowCount());
    for (int i = 0; i < 4; i++) {
        assertNotNull(bi.nextTuple());
        assertEquals(0, bi.getBuffer().getManagedRowCount());
    }
    assertNull(bi.nextTuple());
    assertEquals(0, bi.getBuffer().getManagedRowCount());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TupleBuffer(org.teiid.common.buffer.TupleBuffer) List(java.util.List) TeiidComponentException(org.teiid.core.TeiidComponentException) FakeRelationalNode(org.teiid.query.processor.relational.FakeRelationalNode) BufferManager(org.teiid.common.buffer.BufferManager) BlockedException(org.teiid.common.buffer.BlockedException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TupleBatch(org.teiid.common.buffer.TupleBatch) Test(org.junit.Test)

Aggregations

TupleBuffer (org.teiid.common.buffer.TupleBuffer)43 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)19 BufferManager (org.teiid.common.buffer.BufferManager)16 List (java.util.List)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)11 BlockedException (org.teiid.common.buffer.BlockedException)10 TupleSource (org.teiid.common.buffer.TupleSource)10 CommandContext (org.teiid.query.util.CommandContext)7 TeiidProcessingException (org.teiid.core.TeiidProcessingException)6 FakeRelationalNode (org.teiid.query.processor.relational.FakeRelationalNode)6 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TupleBatch (org.teiid.common.buffer.TupleBatch)4 Map (java.util.Map)3 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)3 Expression (org.teiid.query.sql.symbol.Expression)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 IndexedTupleSource (org.teiid.common.buffer.IndexedTupleSource)2