use of org.apache.geode.cache.query.QueryInvocationTargetException in project geode by apache.
the class PRQueryRemoteNodeExceptionDUnitTest method testCacheCloseExceptionFromLocalAndRemote2.
@Test
public void testCacheCloseExceptionFromLocalAndRemote2() throws Exception {
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Querying with PR Local/Remote Exception test Started");
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
setCacheInVMs(vm0, vm1);
List vmList = new LinkedList();
vmList.add(vm1);
vmList.add(vm0);
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Creating PR's across all VM0 , VM1");
vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRCreateLimitedBuckets(name, redundancy, numOfBuckets));
vm1.invoke(PRQHelp.getCacheSerializableRunnableForPRCreateLimitedBuckets(name, redundancy, numOfBuckets));
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Successfully Created PR on VM0 , VM1");
// creating a local region on one of the JVM's
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Creating Local Region on VM0");
vm0.invoke(PRQHelp.getCacheSerializableRunnableForLocalRegionCreation(localName, PortfolioData.class));
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Successfully Created Local Region on VM0");
// Generating portfolio object array to be populated across the PR's & Local
// Regions
final PortfolioData[] portfolio = createPortfolioData(cnt, cntDest);
// Putting the data into the accessor node
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Inserting Portfolio data through the accessor node");
vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(name, portfolio, cnt, cntDest));
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Successfully Inserted Portfolio data through the accessor node");
// Putting the same data in the local region created
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Inserting Portfolio data on local node VM0 for result Set Comparison");
vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(localName, portfolio, cnt, cntDest));
LogWriterUtils.getLogWriter().info("PRQueryRegionDestroyedDUnitTest#testPRWithLocalAndRemoteException: Successfully Inserted Portfolio data on local node VM0 for result Set Comparison");
// Insert the test hooks on local and remote node.
// Test hook on remote node will throw CacheException while Test hook on local node will throw
// QueryException.
vm1.invoke(new CacheSerializableRunnable(name) {
@Override
public void run2() throws CacheException {
class MyQueryObserver extends IndexTrackingQueryObserver {
private int noOfAccess = 0;
@Override
public void afterIterationEvaluation(Object result) {
LogWriterUtils.getLogWriter().info("Calling after IterationEvaluation :" + noOfAccess);
if (noOfAccess > 1) {
PRQHelp.getCache().getRegion(name).destroyRegion();
}
++noOfAccess;
}
}
;
QueryObserverHolder.setInstance(new MyQueryObserver());
}
;
});
vm0.invoke(new CacheSerializableRunnable(name) {
@Override
public void run2() throws CacheException {
boolean gotException = false;
Cache cache = PRQHelp.getCache();
class MyQueryObserver extends QueryObserverAdapter {
private int noOfAccess = 0;
@Override
public void afterIterationEvaluation(Object result) {
// Object region = ((DefaultQuery)query).getRegionsInQuery(null).iterator().next();
// getLogWriter().info("Region type:"+region);
int i = 0;
while (i <= 10) {
Region region = PRQHelp.getCache().getRegion(name);
if (region == null || region.isDestroyed()) {
break;
}
try {
Thread.sleep(10);
} catch (Exception ex) {
}
i++;
}
}
}
;
QueryObserverHolder.setInstance(new MyQueryObserver());
final DefaultQuery query = (DefaultQuery) cache.getQueryService().newQuery("Select * from /" + name + " p where p.ID > 0");
try {
query.execute();
} catch (Exception ex) {
gotException = true;
if (ex instanceof QueryInvocationTargetException) {
LogWriterUtils.getLogWriter().info(ex.getMessage());
LogWriterUtils.getLogWriter().info("PRQueryRemoteNodeExceptionDUnitTest: Test received Exception from remote node successfully as region.destroy happened before cache.close().");
} else {
Assert.fail("PRQueryRemoteNodeExceptionDUnitTest: Test did not receive Exception as expected from local node rather received", ex);
}
}
if (!gotException) {
fail("PRQueryRemoteNodeExceptionDUnitTest#testPRWithLocalAndRemoteException: Test did not receive Exception as expected from local as well as remote node");
}
}
});
LogWriterUtils.getLogWriter().info("PRQueryRemoteNodeExceptionDUnitTest#testPRWithLocalAndRemoteException: Querying with PR Local/Remote Exception Test ENDED");
}
use of org.apache.geode.cache.query.QueryInvocationTargetException in project geode by apache.
the class LocalRegion method query.
@Override
public SelectResults query(String predicate) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
if (predicate == null) {
throw new IllegalArgumentException("The input query predicate is null. A null predicate is not allowed.");
}
predicate = predicate.trim();
SelectResults results;
if (hasServerProxy()) {
// Trim whitespace
String queryString = constructRegionQueryString(predicate.trim());
try {
results = getServerProxy().query(queryString, null);
} catch (Exception e) {
Throwable cause = e.getCause();
if (cause == null) {
cause = e;
}
throw new QueryInvocationTargetException(e.getMessage(), cause);
}
} else {
// TODO: params size is always zero so this whole block is wasted
Object[] params = new Object[0];
QueryService qs = getGemFireCache().getLocalQueryService();
String queryStr = constructRegionQueryString(predicate.trim());
DefaultQuery query = (DefaultQuery) qs.newQuery(queryStr);
if (query.getRegionsInQuery(params).size() != 1) {
throw new QueryInvalidException("Prevent multiple region query from being executed through region.query()");
}
results = (SelectResults) query.execute(params);
}
return results;
}
use of org.apache.geode.cache.query.QueryInvocationTargetException in project geode by apache.
the class DataCommandFunction method select.
@SuppressWarnings("rawtypes")
private DataCommandResult select(Object principal, String queryString) {
InternalCache cache = getCache();
AtomicInteger nestedObjectCount = new AtomicInteger(0);
if (StringUtils.isEmpty(queryString)) {
return DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.QUERY__MSG__QUERY_EMPTY, false);
}
QueryService qs = cache.getQueryService();
// TODO : Find out if is this optimised use. Can you have something equivalent of parsed
// queries with names where name can be retrieved to avoid parsing every-time
Query query = qs.newQuery(queryString);
DefaultQuery tracedQuery = (DefaultQuery) query;
WrappedIndexTrackingQueryObserver queryObserver = null;
String queryVerboseMsg = null;
long startTime = -1;
if (tracedQuery.isTraced()) {
startTime = NanoTimer.getTime();
queryObserver = new WrappedIndexTrackingQueryObserver();
QueryObserverHolder.setInstance(queryObserver);
}
List<SelectResultRow> list = new ArrayList<>();
try {
Object results = query.execute();
if (tracedQuery.isTraced()) {
queryVerboseMsg = getLogMessage(queryObserver, startTime, queryString);
queryObserver.reset2();
}
if (results instanceof SelectResults) {
select_SelectResults((SelectResults) results, principal, list, nestedObjectCount);
} else {
select_NonSelectResults(results, list);
}
return DataCommandResult.createSelectResult(queryString, list, queryVerboseMsg, null, null, true);
} catch (FunctionDomainException | GfJsonException | QueryInvocationTargetException | NameResolutionException | TypeMismatchException e) {
logger.warn(e.getMessage(), e);
return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, e.getMessage(), false);
} finally {
if (queryObserver != null) {
QueryObserverHolder.reset();
}
}
}
use of org.apache.geode.cache.query.QueryInvocationTargetException in project geode by apache.
the class RemoteQueryDUnitTest method testBug36969.
/**
* This the dunit test for the bug no : 36969
*/
@Test
public void testBug36969() throws Exception {
final String name = this.getName();
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final int numberOfEntries = 100;
// Start server
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Properties config = new Properties();
config.setProperty(LOCATORS, "localhost[" + DistributedTestUtils.getDUnitLocatorPort() + "]");
getSystem(config);
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
final Region region = createRegion(name, factory.createRegionAttributes());
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterQueryEvaluation(Object result) {
// Destroy the region in the test
region.close();
}
});
try {
startBridgeServer(0, false);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
}
});
// Initialize server region
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
for (int i = 0; i < numberOfEntries; i++) {
region.put("key-" + i, new TestObject(i, "ibm"));
}
}
});
final int port = vm0.invoke(() -> RemoteQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
// Create client region in VM1
vm1.invoke(new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
Properties config = new Properties();
config.setProperty(MCAST_PORT, "0");
getSystem(config);
PoolManager.createFactory().addServer(host0, port).setSubscriptionEnabled(true).create("clientPool");
getCache();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setPoolName("clientPool");
createRegion(name, factory.createRegionAttributes());
}
});
// Execute client queries in VM1
vm1.invoke(new CacheSerializableRunnable("Execute queries") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
String queryStrings = "id<9";
// SelectResults results = null;
try {
region.query(queryStrings);
fail("The query should have experienced RegionDestroyedException");
} catch (QueryInvocationTargetException qte) {
// Ok test passed
} catch (Exception e) {
Assert.fail("Failed executing query " + queryStrings + " due to unexpected Excecption", e);
}
}
});
// Start server
vm0.invoke(new CacheSerializableRunnable("Create two regions") {
public void run2() throws CacheException {
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
final Region region1 = createRegion(name, factory.createRegionAttributes());
final Region region2 = createRegion(name + "_2", factory.createRegionAttributes());
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterQueryEvaluation(Object result) {
// Destroy the region in the test
region1.close();
}
});
for (int i = 0; i < numberOfEntries; i++) {
region1.put("key-" + i, new TestObject(i, "ibm"));
region2.put("key-" + i, new TestObject(i, "ibm"));
}
}
});
// Execute client queries in VM1
vm1.invoke(new CacheSerializableRunnable("Execute queries") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
String queryString = "select distinct * from /" + name;
// SelectResults results = null;
try {
region.query(queryString);
fail("The query should have experienced RegionDestroyedException");
} catch (QueryInvocationTargetException qte) {
// Ok test passed
} catch (Exception e) {
Assert.fail("Failed executing query " + queryString + " due to unexpected Excecption", e);
}
}
});
// Close client VM1
vm1.invoke(new CacheSerializableRunnable("Close client") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.close();
PoolManager.find("clientPool").destroy();
}
});
// Stop server
vm0.invoke(new SerializableRunnable("Stop CacheServer") {
public void run() {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
stopBridgeServer(getCache());
}
});
}
use of org.apache.geode.cache.query.QueryInvocationTargetException in project geode by apache.
the class ResourceManagerWithQueryMonitorDUnitTest method doCriticalMemoryHitAddResultsTestWithMultipleServers.
private void doCriticalMemoryHitAddResultsTestWithMultipleServers(final String regionName, boolean createPR, final int criticalThreshold, final boolean disabledQueryMonitorForLowMem, final int queryTimeout, final boolean hitCriticalThreshold) throws Exception {
// create region on the server
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM server2 = host.getVM(1);
final VM client = host.getVM(2);
final int numObjects = 200;
try {
final int[] port = AvailablePortHelper.getRandomAvailableTCPPorts(2);
startCacheServer(server1, port[0], criticalThreshold, disabledQueryMonitorForLowMem, queryTimeout, regionName, createPR, 0);
startCacheServer(server2, port[1], criticalThreshold, true, -1, regionName, createPR, 0);
startClient(client, server1, port[0], regionName);
populateData(server2, regionName, numObjects);
createCancelDuringAddResultsTestHook(server1);
client.invoke(new SerializableCallable("executing query to be canceled during add results") {
public Object call() {
QueryService qs = null;
try {
qs = getCache().getQueryService();
Query query = qs.newQuery("Select * From /" + regionName);
SelectResults results = (SelectResults) query.execute();
if (hitCriticalThreshold && disabledQueryMonitorForLowMem == false) {
throw new CacheException("should have hit low memory") {
};
}
} catch (Exception e) {
handleException(e, hitCriticalThreshold, disabledQueryMonitorForLowMem, queryTimeout);
}
return 0;
}
});
verifyRejectedObjects(server1, disabledQueryMonitorForLowMem, queryTimeout, hitCriticalThreshold);
// Pause for a second and then let's recover
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Recover from critical heap
if (hitCriticalThreshold) {
vmRecoversFromCriticalHeap(server1);
}
// Check to see if query execution is ok under "normal" or "healthy" conditions
client.invoke(new CacheSerializableRunnable("Executing query when system is 'Normal'") {
public void run2() {
try {
QueryService qs = getCache().getQueryService();
Query query = qs.newQuery("Select * From /" + regionName);
SelectResults results = (SelectResults) query.execute();
assertEquals(numObjects, results.size());
} catch (QueryInvocationTargetException e) {
assertFalse(true);
} catch (NameResolutionException e) {
assertFalse(true);
} catch (TypeMismatchException e) {
assertFalse(true);
} catch (FunctionDomainException e) {
assertFalse(true);
}
}
});
// Recover from critical heap
if (hitCriticalThreshold) {
vmRecoversFromCriticalHeap(server1);
}
} finally {
stopServer(server1);
stopServer(server2);
}
}
Aggregations