use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class QueryUsingPoolDUnitTest method executeQueries.
private void executeQueries(String regionName, String poolName1) {
String queryString;
SelectResults results = null;
QueryService qService = null;
try {
qService = (PoolManager.find(poolName1)).getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
queryString = "SELECT DISTINCT itr.value FROM " + regionName + ".entries itr where itr.key = 'key-1'";
try {
Query query = qService.newQuery(queryString);
results = (SelectResults) query.execute();
} catch (Exception e) {
Assert.fail("Failed executing " + queryString, e);
}
assertEquals(1, results.size());
assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
assertTrue(results.asList().get(0) instanceof TestObject);
queryString = "SELECT DISTINCT itr.key FROM " + regionName + ".entries itr where itr.key = 'key-1'";
try {
Query query = qService.newQuery(queryString);
results = (SelectResults) query.execute();
} catch (Exception e) {
Assert.fail("Failed executing " + queryString, e);
}
assertEquals(1, results.size());
assertTrue(!results.getCollectionType().allowsDuplicates() && !results.getCollectionType().getElementType().isStructType());
assertEquals("key-1", results.asList().get(0));
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class QueryUsingPoolDUnitTest method testRemoteJoinRegionQueries.
/**
* Tests remote join query execution.
*/
@Test
public void testRemoteJoinRegionQueries() throws CacheException {
final String name = this.getName();
final String rootRegionName = "root";
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final int numberOfEntries = 100;
// Start server
vm0.invoke("Create Bridge Server", () -> {
createAndStartBridgeServer();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
createRegion(name + "1", factory.create());
createRegion(name + "2", factory.create());
});
// Initialize server region
vm0.invoke("Create Bridge Server", () -> {
Region region1 = getRootRegion().getSubregion(name + "1");
for (int i = 0; i < numberOfEntries; i++) {
region1.put("key-" + i, new TestObject(i, "ibm"));
}
Region region2 = getRootRegion().getSubregion(name + "2");
for (int i = 0; i < numberOfEntries; i++) {
region2.put("key-" + i, new TestObject(i, "ibm"));
}
});
// Create client region
final int port = vm0.invoke("getCacheServerPort", () -> QueryUsingPoolDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
final String regionName1 = "/" + rootRegionName + "/" + name + "1";
final String regionName2 = "/" + rootRegionName + "/" + name + "2";
// Create client pool.
final String poolName = "testRemoteJoinRegionQueries";
vm1.invoke("createPool", () -> createPool(poolName, host0, port));
// Execute client queries
vm1.invoke("Execute queries", () -> {
String queryString = null;
SelectResults results = null;
QueryService qService = null;
try {
qService = (PoolManager.find(poolName)).getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
queryString = "select distinct a, b.price from " + regionName1 + " a, " + regionName2 + " b where a.price = b.price";
try {
Query query = qService.newQuery(queryString);
results = (SelectResults) query.execute();
} catch (Exception e) {
Assert.fail("Failed executing " + queryString, e);
}
assertEquals(numberOfEntries, results.size());
assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
queryString = "select distinct a, b.price from " + regionName1 + " a, " + regionName2 + " b where a.price = b.price and a.price = 50";
try {
Query query = qService.newQuery(queryString);
results = (SelectResults) query.execute();
} catch (Exception e) {
Assert.fail("Failed executing " + queryString, e);
}
assertEquals(1, results.size());
assertTrue(!results.getCollectionType().allowsDuplicates() && results.getCollectionType().getElementType().isStructType());
});
// Stop server
vm0.invoke("Stop CacheServer", () -> stopBridgeServer(getCache()));
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class QueryUsingPoolDUnitTest method testClientServerCompiledQueryCleanup.
/**
* Tests client-server compiled query register and cleanup. It creates the client connections
* without the subscription enabled. This doesn't create any client proxy on the server.
*/
@Test
public void testClientServerCompiledQueryCleanup() throws CacheException {
final String name = this.getName();
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
final int numberOfEntries = 100;
final Object[][] params = new Object[][] { // 0
{ "key-1" }, // 1
{ 101 }, // 2
{ 101 }, // 3
{ 101 }, // 4
{ 50, "ibm" }, // 5
{ 50, "ibm" } };
// Start server
final int port = vm0.invoke("Create Bridge Server", () -> {
setupBridgeServerAndCreateData(name, numberOfEntries);
QueryService queryService = getCache().getQueryService();
queryService.newQuery("Select * from " + regName);
DefaultQuery.setTestCompiledQueryClearTime(2 * 1000);
return getCacheServerPort();
});
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
// Create client pool.
final String poolName = "testClientServerQueriesWithParams";
final boolean subscriptiuonEnabled = false;
vm1.invoke("createPool", () -> createPool(poolName, host0, port, subscriptiuonEnabled));
vm2.invoke("createPool", () -> createPool(poolName, host0, port, subscriptiuonEnabled));
// Execute client queries
vm1.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
// Execute client queries
vm2.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
// Validate maintained compiled queries.
vm0.invoke("validate Compiled query", () -> validateCompiledQuery(0));
// Recreate compiled queries.
// Execute client queries
vm1.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
// Execute client queries
// The client2 will be using the queries.
vm2.invokeAsync("Execute queries", () -> {
for (int i = 0; i < 10; i++) {
Wait.pause(10);
executeCompiledQueries(poolName, params);
}
});
// Validate maintained compiled queries.
vm0.invoke("validate Compiled query", () -> validateCompiledQuery(queryString.length));
// Close clients
// Let the compiled queries to be idle (not used).
// pause(2 * 1000);
// Validate maintained compiled queries.
vm0.invoke("validate Compiled query", () -> validateCompiledQuery(0));
// Recreate compiled queries.
// Execute client queries
vm1.invoke("Execute queries", () -> executeCompiledQueries(poolName, params));
// Validate maintained compiled queries.
vm0.invoke("validate compiled query.", () -> {
long compiledQueryCount = CacheClientNotifier.getInstance().getStats().getCompiledQueryCount();
assertEquals(queryString.length, compiledQueryCount);
});
// Close clients
vm2.invoke("closeClient", () -> closeClient());
vm1.invoke("closeClient", () -> closeClient());
// Validate maintained compiled queries.
// since not used it should get cleaned up after sometime.
vm0.invoke("validate Compiled query", () -> validateCompiledQuery(0));
// Stop server
vm0.invoke("Stop CacheServer", () -> stopBridgeServer(getCache()));
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class QueryUsingPoolDUnitTest method testUnSupportedOps.
@Test
public void testUnSupportedOps() throws Exception {
final String name = this.getName();
final String rootRegionName = "root";
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
final int numberOfEntries = 100;
// Start server
vm0.invoke("Create Bridge Server", () -> {
createAndStartBridgeServer();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
final Region region1 = createRegion(name, factory.createRegionAttributes());
final Region region2 = createRegion(name + "_2", factory.createRegionAttributes());
for (int i = 0; i < numberOfEntries; i++) {
region1.put("key-" + i, new TestObject(i, "ibm"));
region2.put("key-" + i, new TestObject(i, "ibm"));
}
});
final int port = vm0.invoke("getCacheServerPort", () -> QueryUsingPoolDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
final String regionName1 = "/" + rootRegionName + "/" + name;
// Create client pool.
final String poolName = "testUnSupportedOps";
vm1.invoke("createPool", () -> createPool(poolName, host0, port));
// Execute client queries in VM1
vm1.invoke("Execute queries", () -> {
final Region region1 = ((ClientCache) getCache()).createClientRegionFactory(ClientRegionShortcut.LOCAL).create(name);
String queryString = "select distinct * from " + regionName1 + " where ticker = $1";
Object[] params = new Object[] { new String("ibm") };
// SelectResults results = null;
QueryService qService = null;
try {
qService = (PoolManager.find(poolName)).getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
// Testing Remote Query with params.
try {
Query query = qService.newQuery(queryString);
query.execute(params);
} catch (UnsupportedOperationException e) {
// Expected behavior.
} catch (Exception e) {
Assert.fail("Failed with UnExpected Exception.", e);
}
// Test with Index.
try {
qService.createIndex("test", IndexType.FUNCTIONAL, "ticker", regionName1);
} catch (UnsupportedOperationException e) {
// Expected behavior.
} catch (Exception e) {
Assert.fail("Failed with UnExpected Exception.", e);
}
try {
String importString = "import org.apache.geode.admin.QueryUsingPoolDUnitTest.TestObject;";
qService.createIndex("test", IndexType.FUNCTIONAL, "ticker", regionName1, importString);
} catch (UnsupportedOperationException e) {
// Expected behavior.
} catch (Exception e) {
Assert.fail("Failed with UnExpected Exception.", e);
}
try {
qService.getIndex(region1, "test");
} catch (UnsupportedOperationException e) {
// Expected behavior.
}
try {
qService.getIndexes(region1);
} catch (UnsupportedOperationException e) {
// Expected behavior.
}
try {
qService.getIndexes(region1, IndexType.FUNCTIONAL);
} catch (UnsupportedOperationException e) {
// Expected behavior.
}
try {
qService.removeIndexes(region1);
} catch (UnsupportedOperationException e) {
// Expected behavior.
}
try {
qService.removeIndexes();
} catch (UnsupportedOperationException e) {
// Expected behavior.
}
});
// Stop server
vm0.invoke("Stop CacheServer", () -> {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
stopBridgeServer(getCache());
});
}
use of org.apache.geode.cache.query.QueryService 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