@Ignore @Test(expected = TranslatorException.class) public void testIsImmutablePropertyFails() throws Exception { /* * Setup: * 1. requestMsg.isTransactional() must be TRUE * 2. manager.isXa() must be FALSE () * 3. command must NOT be a SELECT * 4. Then, set isImmutable to FALSE, and we should FAIL */ ConnectorManager cm = TestConnectorManager.getConnectorManager(); ((FakeConnector) cm.getExecutionFactory()).setImmutable(false); // command must not be a SELECT Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand(command); // To make the AtomicRequestMessage transactional, construct your own requestMsg.setTransactionContext( new TransactionContext() { @Override public Xid getXid() { return Mockito.mock(Xid.class); } }); new ConnectorWorkItem(requestMsg, cm); }
@Test public void testSourcHints() throws Exception { Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ command.setSourceHint(new SourceHint()); AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager cm = TestConnectorManager.getConnectorManager(); cm.registerRequest(arm); }
public ConnectorWork registerRequest(AtomicRequestMessage message) throws TeiidComponentException { checkStatus(); AtomicRequestID atomicRequestId = message.getAtomicRequestID(); LogManager.logDetail( LogConstants.CTX_CONNECTOR, new Object[] {atomicRequestId, "Create State"}); // $NON-NLS-1$ final ConnectorWorkItem item = new ConnectorWorkItem(message, this); ConnectorWork proxy = (ConnectorWork) Proxy.newProxyInstance( ConnectorWork.class.getClassLoader(), new Class[] {ConnectorWork.class}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { ClassLoader originalCL = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread() .setContextClassLoader(getExecutionFactory().getClass().getClassLoader()); return method.invoke(item, args); } catch (InvocationTargetException e) { throw e.getTargetException(); } finally { Thread.currentThread().setContextClassLoader(originalCL); } } }); Assertion.isNull( requestStates.put(atomicRequestId, proxy), "State already existed"); // $NON-NLS-1$ return proxy; }
@Test public void testSourceNotRequired() throws Exception { Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager cm = TestConnectorManager.getConnectorManager(); cm.getExecutionFactory().setSourceRequired(false); ConnectorWork synchConnectorWorkItem = cm.registerRequest(arm); synchConnectorWorkItem.execute(); synchConnectorWorkItem.close(); FakeConnector fc = (FakeConnector) cm.getExecutionFactory(); assertEquals(1, fc.getConnectionCount()); assertEquals(1, fc.getCloseCount()); }
private AtomicResultsMessage helpExecuteUpdate(boolean batch, boolean single) throws Exception, Throwable { Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ if (batch) { command = new BatchedUpdateCommand(Arrays.asList(command, command)); } AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager connectorManager = TestConnectorManager.getConnectorManager(); ((FakeConnector) connectorManager.getExecutionFactory()).setReturnSingleUpdate(single); ConnectorWorkItem synchConnectorWorkItem = new ConnectorWorkItem(arm, connectorManager); synchConnectorWorkItem.execute(); return synchConnectorWorkItem.more(); }
@Test public void testConvertIn() throws Exception { Command command = helpGetCommand( "select intkey from bqt1.smalla where stringkey in ('1', '2')", EXAMPLE_BQT); //$NON-NLS-1$ AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager cm = TestConnectorManager.getConnectorManager(); cm.getExecutionFactory().setSourceRequired(false); ConnectorWork synchConnectorWorkItem = cm.registerRequest(arm); synchConnectorWorkItem.execute(); synchConnectorWorkItem.close(); FakeConnector fc = (FakeConnector) cm.getExecutionFactory(); assertEquals( "SELECT SmallA.IntKey FROM SmallA WHERE SmallA.StringKey = '2' OR SmallA.StringKey = '1'", fc.getCommands().get(0).toString()); assertEquals(1, fc.getConnectionCount()); assertEquals(1, fc.getCloseCount()); }
static AtomicRequestMessage createNewAtomicRequestMessage(int requestid, int nodeid) throws Exception { RequestMessage rm = new RequestMessage(); DQPWorkContext workContext = RealMetadataFactory.buildWorkContext(EXAMPLE_BQT, RealMetadataFactory.exampleBQTVDB()); workContext.getSession().setSessionId(String.valueOf(1)); workContext.getSession().setUserName("foo"); // $NON-NLS-1$ AtomicRequestMessage request = new AtomicRequestMessage(rm, workContext, nodeid); request.setCommand( helpGetCommand("SELECT BQT1.SmallA.INTKEY FROM BQT1.SmallA", EXAMPLE_BQT)); // $NON-NLS-1$ request.setRequestID(new RequestID(requestid)); request.setConnectorName("testing"); // $NON-NLS-1$ request.setFetchSize(5); request.setCommandContext(new CommandContext()); return request; }
/** * Add begin point to transaction monitoring table. * * @param qr Request that contains the MetaMatrix command information in the transaction. */ void logSRCCommand( AtomicRequestMessage qr, ExecutionContext context, Event cmdStatus, Integer finalRowCnt, Long cpuTime, Object[] command) { if (!LogManager.isMessageToBeRecorded(LogConstants.CTX_COMMANDLOGGING, MessageLevel.DETAIL)) { return; } String sqlStr = null; if (cmdStatus == Event.NEW) { Command cmd = qr.getCommand(); sqlStr = cmd != null ? cmd.toString() : null; } String userName = qr.getWorkContext().getUserName(); String transactionID = null; if (qr.isTransactional()) { transactionID = qr.getTransactionContext().getTransactionId(); } String modelName = qr.getModelName(); AtomicRequestID sid = qr.getAtomicRequestID(); String principal = userName == null ? "unknown" : userName; // $NON-NLS-1$ CommandLogMessage message = null; if (cmdStatus == Event.NEW) { message = new CommandLogMessage( System.currentTimeMillis(), qr.getRequestID().toString(), sid.getNodeID(), transactionID, modelName, translatorName, qr.getWorkContext().getSessionId(), principal, sqlStr, context); } else { message = new CommandLogMessage( System.currentTimeMillis(), qr.getRequestID().toString(), sid.getNodeID(), transactionID, modelName, translatorName, qr.getWorkContext().getSessionId(), principal, finalRowCnt, cmdStatus, context, cpuTime); if (cmdStatus == Event.SOURCE) { message.setSourceCommand(command); } } LogManager.log(MessageLevel.DETAIL, LogConstants.CTX_COMMANDLOGGING, message); }
@Override public ConnectorWork registerRequest(AtomicRequestMessage message) throws TeiidComponentException { List projectedSymbols = (message.getCommand()).getProjectedSymbols(); List[] results = createResults(projectedSymbols, rows, useIntCounter); if (RelationalNodeUtil.isUpdate(message.getCommand())) { results = new List[] {Arrays.asList(1)}; } final AtomicResultsMessage msg = ConnectorWorkItem.createResultsMessage(results); msg.setFinalRow(rows); return new ConnectorWork() { boolean returnedInitial; @Override public boolean isDataAvailable() { return dataAvailable; } @Override public AtomicResultsMessage more() throws TranslatorException { if (dataNotAvailable != null) { int delay = dataNotAvailable; dataNotAvailable = null; DataNotAvailableException dnae = new DataNotAvailableException(delay); dnae.setStrict(strict); throw dnae; } if (addWarning) { msg.setWarnings(Arrays.asList(new Exception())); } if (!returnedInitial) { returnedInitial = true; return msg; } throw new RuntimeException("Should not be called"); // $NON-NLS-1$ } @Override public void execute() throws TranslatorException { executeCount.incrementAndGet(); if (sleep > 0) { try { Thread.sleep(sleep); } catch (InterruptedException e) { throw new RuntimeException(e); } } if (throwExceptionOnExecute) { throw new TranslatorException("Connector Exception"); // $NON-NLS-1$ } } @Override public void close() { closeCount.incrementAndGet(); } @Override public void cancel() {} @Override public CacheDirective getCacheDirective() { return cacheDirective; } @Override public boolean isForkable() { return true; } @Override public boolean isThreadBound() { return threadBound; } @Override public AtomicRequestID getId() { return null; } }; }
@Test public void testUnmodifibleList() throws Exception { BufferManager bm = BufferManagerFactory.getStandaloneBufferManager(); final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() { @Override public boolean isSourceRequired() { return false; } @Override public ResultSetExecution createResultSetExecution( QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException { List<String> list1 = Collections.singletonList("1"); final Iterator<List<String>> iter = Arrays.asList(list1).iterator(); return new ResultSetExecution() { @Override public void execute() throws TranslatorException {} @Override public void close() {} @Override public void cancel() throws TranslatorException {} @Override public List<?> next() throws TranslatorException, DataNotAvailableException { if (iter.hasNext()) { return iter.next(); } return null; } }; } }; ConnectorManager cm = new ConnectorManager("FakeConnector", "FakeConnector") { // $NON-NLS-1$ //$NON-NLS-2$ public ExecutionFactory getExecutionFactory() { return ef; } public Object getConnectionFactory() { return null; } }; cm.start(); AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand( helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT)); // $NON-NLS-1$ requestMsg.setBufferManager(bm); ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm); cwi.execute(); AtomicResultsMessage message = cwi.more(); List[] results = message.getResults(); assertEquals(1, results.length); List<?> tuple = results[0]; assertEquals(1, tuple.get(0)); assertEquals(1, message.getFinalRow()); }
@Test public void testLobs() throws Exception { BufferManager bm = BufferManagerFactory.getStandaloneBufferManager(); final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() { @Override public boolean isSourceRequired() { return false; } @Override public ResultSetExecution createResultSetExecution( QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException { return new ResultSetExecution() { private boolean returned; @Override public void execute() throws TranslatorException {} @Override public void close() {} @Override public void cancel() throws TranslatorException {} @Override public List<?> next() throws TranslatorException, DataNotAvailableException { if (returned) { return null; } returned = true; return Arrays.asList(AutoGenDataService.CLOB_VAL); } }; } }; ConnectorManager cm = new ConnectorManager("FakeConnector", "FakeConnector") { // $NON-NLS-1$ //$NON-NLS-2$ public ExecutionFactory getExecutionFactory() { return ef; } public Object getConnectionFactory() { return null; } }; cm.start(); ef.setCopyLobs(true); AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand( helpGetCommand("SELECT CLOB_COLUMN FROM LOB_TESTING_ONE", EXAMPLE_BQT)); // $NON-NLS-1$ requestMsg.setBufferManager(bm); ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm); cwi.execute(); AtomicResultsMessage message = cwi.more(); List[] resutls = message.getResults(); List<?> tuple = resutls[0]; ClobType clob = (ClobType) tuple.get(0); assertEquals(StorageMode.MEMORY, InputStreamFactory.getStorageMode(clob)); assertTrue(message.supportsImplicitClose()); ef.setCopyLobs(false); cwi = new ConnectorWorkItem(requestMsg, cm); cwi.execute(); message = cwi.more(); resutls = message.getResults(); tuple = resutls[0]; clob = (ClobType) tuple.get(0); assertEquals(StorageMode.OTHER, InputStreamFactory.getStorageMode(clob)); assertFalse(message.supportsImplicitClose()); }