Search in sources :

Example 61 with TeiidRuntimeException

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

the class BufferManagerImpl method reserveBuffersBlocking.

@Override
public int reserveBuffersBlocking(int count, long[] val, boolean force) throws BlockedException {
    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_BUFFER_MGR, MessageLevel.TRACE)) {
        // $NON-NLS-1$
        LogManager.logTrace(LogConstants.CTX_BUFFER_MGR, "Reserving buffer space", count, force);
    }
    assert count >= 0;
    if (count == 0) {
        return 0;
    }
    int result = 0;
    int count_orig = count;
    CommandContext context = CommandContext.getThreadLocalContext();
    long reserved = 0;
    if (context != null) {
        reserved = context.addAndGetReservedBuffers(0);
    // TODO: in theory we have to check the whole stack as we could be
    // issuing embedded queries back to ourselves
    }
    count = Math.min(count, (int) Math.min(Integer.MAX_VALUE, nominalProcessingMemoryMax - reserved));
    if (count_orig != count && !force) {
        // is not possible to reserve the desired amount
        return 0;
    }
    result = noWaitReserve(count, true, context);
    if (result == 0) {
        if (val[0]++ == 0) {
            val[1] = System.currentTimeMillis();
        }
        if (val[1] > 1) {
            long last = val[1];
            val[1] = System.currentTimeMillis();
            try {
                lock.lock();
                if (val[1] - last < 10) {
                    // if the time difference is too close, then wait to prevent tight spins
                    // but we can't wait too long as we don't want to thread starve the system
                    batchesFreed.await(20, TimeUnit.MILLISECONDS);
                }
                if ((val[0] << (force ? 16 : 18)) > count) {
                    // TOOD: ideally we should be using a priority queue and better scheduling
                    if (!force) {
                        return 0;
                    }
                    reserve(count_orig, context);
                    result = count_orig;
                } else {
                    int min = 0;
                    if (force) {
                        min = 2 * count / 3;
                    } else {
                        min = 4 * count / 5;
                    }
                    // if a sample looks good proceed
                    if (reserveBatchBytes.get() > min) {
                        reserve(count_orig, context);
                        result = count_orig;
                    }
                }
            } catch (InterruptedException e) {
                throw new TeiidRuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
        if (result == 0) {
            if (context != null) {
                RequestWorkItem workItem = context.getWorkItem();
                if (workItem != null) {
                    // if we have a workitem (non-test scenario) then before
                    // throwing blocked on memory to indicate there's more work
                    workItem.moreWork();
                }
            }
            throw BlockedException.BLOCKED_ON_MEMORY_EXCEPTION;
        }
    }
    if (force && result < count_orig) {
        reserve(count_orig - result, context);
        result = count_orig;
    }
    val[0] = 0;
    persistBatchReferences(result);
    return result;
}
Also used : RequestWorkItem(org.teiid.dqp.internal.process.RequestWorkItem) CommandContext(org.teiid.query.util.CommandContext) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException)

Example 62 with TeiidRuntimeException

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

the class SPage method clone.

public SPage clone(STree tree) {
    try {
        if (this.managedBatch != null && trackingObject == null) {
            this.trackingObject = new Object();
            CleanupReference managedBatchReference = new CleanupReference(trackingObject, managedBatch, stree.getBatchManager(children == null).getBatchManagerReference());
            REFERENCES.put(managedBatch, managedBatchReference);
        }
        SPage clone = (SPage) super.clone();
        clone.stree = tree;
        if (children != null) {
            clone.children = new ResizingArrayList<SPage>(children);
        }
        if (values != null) {
            clone.values = new ResizingArrayList<List<?>>(values);
        }
        return clone;
    } catch (CloneNotSupportedException e) {
        throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30038, e);
    }
}
Also used : List(java.util.List) ResizingArrayList(org.teiid.client.ResizingArrayList) LinkedList(java.util.LinkedList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException)

Example 63 with TeiidRuntimeException

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

the class STree method clone.

public STree clone() {
    updateLock.lock();
    try {
        STree clone = (STree) super.clone();
        if (lobManager != null) {
            clone.lobManager = lobManager.clone();
        }
        clone.updateLock = new ReentrantLock();
        clone.rowCount = new AtomicLong(rowCount.get());
        // clone the pages
        clone.pages = new HashMap<Long, SPage>(pages);
        for (Map.Entry<Long, SPage> entry : clone.pages.entrySet()) {
            entry.setValue(entry.getValue().clone(clone));
        }
        // reset the pointers
        for (Map.Entry<Long, SPage> entry : clone.pages.entrySet()) {
            SPage clonePage = entry.getValue();
            clonePage.next = clone.getPage(clonePage.next);
            clonePage.prev = clone.getPage(clonePage.prev);
            if (clonePage.children != null) {
                for (int i = 0; i < clonePage.children.size(); i++) {
                    clonePage.children.set(i, clone.getPage(clonePage.children.get(i)));
                }
            }
        }
        clone.header = Arrays.copyOf(header, header.length);
        for (int i = 0; i < header.length; i++) {
            clone.header[i] = clone.pages.get(header[i].getId());
        }
        return clone;
    } catch (CloneNotSupportedException e) {
        throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30039, e);
    } finally {
        updateLock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 64 with TeiidRuntimeException

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

the class NativeMetadataRepository method getMetadata.

private void getMetadata(MetadataFactory factory, ExecutionFactory executionFactory, Object connectionFactory) throws TranslatorException {
    Object connection = null;
    try {
        connection = executionFactory.getConnection(connectionFactory, null);
    } catch (Throwable e) {
        // if security pass through is enabled the connection creation may fail at the startup
        if (executionFactory.isSourceRequiredForMetadata()) {
            throw new TranslatorException(QueryPlugin.Event.TEIID31178, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31178, factory.getName()));
        }
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Exception getting connection for metadata, but no connection is required");
    }
    Object unwrapped = null;
    if (connection instanceof WrappedConnection) {
        try {
            unwrapped = ((WrappedConnection) connection).unwrap();
        } catch (ResourceException e) {
            if (executionFactory.isSourceRequiredForMetadata()) {
                throw new TranslatorException(QueryPlugin.Event.TEIID30477, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30477, factory.getName()));
            }
            connection = null;
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Could not unwrap exception to get metadata, but no connection is required");
        }
    }
    try {
        executionFactory.getMetadata(factory, (unwrapped == null) ? connection : unwrapped);
    } finally {
        executionFactory.closeConnection(connection, connectionFactory);
    }
    if (PropertiesUtils.getBooleanProperty(factory.getModelProperties(), IMPORT_PUSHDOWN_FUNCTIONS, false)) {
        // $NON-NLS-1$
        List<FunctionMethod> functions = executionFactory.getPushDownFunctions();
        // create a copy and add to the schema
        if (!functions.isEmpty()) {
            try {
                AccessibleByteArrayOutputStream baos = new AccessibleByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(functions);
                oos.close();
                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.getBuffer(), 0, baos.getCount()));
                functions = (List<FunctionMethod>) ois.readObject();
                for (FunctionMethod functionMethod : functions) {
                    factory.addFunction(functionMethod);
                    functionMethod.setProperty(FunctionMethod.SYSTEM_NAME, functionMethod.getName());
                }
            } catch (IOException e) {
                throw new TeiidRuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new TeiidRuntimeException(e);
            }
        }
    }
}
Also used : AccessibleByteArrayOutputStream(org.teiid.core.util.AccessibleByteArrayOutputStream) IOException(java.io.IOException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) WrappedConnection(org.teiid.resource.spi.WrappedConnection) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FunctionMethod(org.teiid.metadata.FunctionMethod) TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) ObjectInputStream(java.io.ObjectInputStream)

Example 65 with TeiidRuntimeException

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

the class SystemMetadata method loadSchema.

private MetadataFactory loadSchema(VDBMetaData vdb, Properties p, String name, QueryParser parser) {
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName(name);
    vdb.addModel(mmd);
    // $NON-NLS-1$ //$NON-NLS-2$
    InputStream is = SystemMetadata.class.getClassLoader().getResourceAsStream("org/teiid/metadata/" + name + ".sql");
    try {
        MetadataFactory factory = new MetadataFactory(vdb.getName(), vdb.getVersion(), name, typeMap, p, null);
        // $NON-NLS-1$
        parser.parseDDL(factory, new InputStreamReader(is, Charset.forName("UTF-8")));
        for (Table t : factory.getSchema().getTables().values()) {
            t.setSystem(true);
        }
        return factory;
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            throw new TeiidRuntimeException(e);
        }
    }
}
Also used : Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData)

Aggregations

TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)103 IOException (java.io.IOException)27 TeiidComponentException (org.teiid.core.TeiidComponentException)22 TeiidException (org.teiid.core.TeiidException)22 ArrayList (java.util.ArrayList)20 TeiidProcessingException (org.teiid.core.TeiidProcessingException)17 SQLException (java.sql.SQLException)11 ObjectInputStream (java.io.ObjectInputStream)9 HashMap (java.util.HashMap)9 InputStream (java.io.InputStream)7 Map (java.util.Map)7 Test (org.junit.Test)7 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)7 ObjectOutputStream (java.io.ObjectOutputStream)6 List (java.util.List)6 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)6 XMLStreamException (javax.xml.stream.XMLStreamException)5 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 JsonObject (com.couchbase.client.java.document.json.JsonObject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4