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;
}
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);
}
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;
}
}
}
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);
}
}
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);
}
}
}
Aggregations