use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.
the class CQJUnitTest method testValidateCQ.
/**
* Test to make sure CQs that have invalid syntax throw QueryInvalidException, and CQs that have
* unsupported CQ features throw UnsupportedOperationException
*/
@Test
public void testValidateCQ() throws Exception {
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
// The order by query computes dependency after compilation so the region has to be present
// for the query to progress further to throw UnsupportedOperationException
cache.createRegion("region", regionAttributes);
// default attributes
CqAttributes attrs = new CqAttributesFactory().create();
// valid CQ
this.qs.newCq("SELECT * FROM /region WHERE status = 'active'", attrs);
// invalid syntax
try {
this.qs.newCq("this query is garbage", attrs);
fail("should have thrown a QueryInvalidException");
} catch (QueryInvalidException e) {
// pass
}
String[] unsupportedCQs = new String[] { // not "just" a select statement
"(select * from /region where status = 'active').isEmpty", // cannot be DISTINCT
"select DISTINCT * from /region WHERE status = 'active'", // references more than one region
"select * from /region1 r1, /region2 r2 where r1 = r2", // where clause refers to a region
"select * from /region r where r.val = /region.size", // more than one iterator in FROM clause
"select * from /portfolios p1, p1.positions p2 where p2.id = 'IBM'", // first iterator in FROM clause is not just a region path
"select * from /region.entries e where e.value.id = 23", // has projections
"select id from /region where status = 'active'", // has ORDER BY
"select * from /region where status = 'active' ORDER BY id" };
for (int i = 0; i < unsupportedCQs.length; i++) {
try {
this.qs.newCq(unsupportedCQs[i], attrs);
fail("should have thrown UnsupportedOperationException for query #" + i);
} catch (UnsupportedOperationException e) {
// pass
}
}
}
use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.
the class CqDataUsingPoolDUnitTest method testGetDurableCQsFromPoolOnly.
@Test
public void testGetDurableCQsFromPoolOnly() throws Exception {
final String regionName = "regionA";
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client1 = host.getVM(1);
VM client2 = host.getVM(2);
/* Create Server and Client */
cqDUnitTest.createServer(server);
final int port = server.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(server.getHost());
final String poolName1 = "pool1";
final String poolName2 = "pool2";
cqDUnitTest.createPool(client1, poolName1, host0, port);
cqDUnitTest.createPool(client2, poolName2, host0, port);
client1.invoke(new CacheSerializableRunnable("Register cq for client 1") {
@Override
public void run2() throws CacheException {
QueryService queryService = null;
try {
queryService = (PoolManager.find(poolName1)).getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
try {
CqAttributesFactory cqAf = new CqAttributesFactory();
CqAttributes attributes = cqAf.create();
queryService.newCq("client1DCQ1", "Select * From /root/" + regionName + " where id = 1", attributes, true).execute();
queryService.newCq("client1DCQ2", "Select * From /root/" + regionName + " where id = 10", attributes, true).execute();
queryService.newCq("client1NoDC1", "Select * From /root/" + regionName, attributes, false).execute();
queryService.newCq("client1NoDC2", "Select * From /root/" + regionName + " where id = 3", attributes, false).execute();
} catch (CqException e) {
fail("failed", e);
} catch (CqExistsException e) {
fail("failed", e);
} catch (RegionNotFoundException e) {
fail("failed", e);
}
}
});
client2.invoke(new CacheSerializableRunnable("Register cq for client 2") {
@Override
public void run2() throws CacheException {
QueryService queryService = null;
try {
queryService = (PoolManager.find(poolName2)).getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
try {
CqAttributesFactory cqAf = new CqAttributesFactory();
CqAttributes attributes = cqAf.create();
queryService.newCq("client2DCQ1", "Select * From /root/" + regionName + " where id = 1", attributes, true).execute();
queryService.newCq("client2DCQ2", "Select * From /root/" + regionName + " where id = 10", attributes, true).execute();
queryService.newCq("client2DCQ3", "Select * From /root/" + regionName, attributes, true).execute();
queryService.newCq("client2DCQ4", "Select * From /root/" + regionName + " where id = 3", attributes, true).execute();
} catch (CqException e) {
fail("failed", e);
} catch (CqExistsException e) {
fail("failed", e);
} catch (RegionNotFoundException e) {
fail("failed", e);
}
}
});
client2.invoke(new CacheSerializableRunnable("test getDurableCQsFromServer for client2") {
@Override
public void run2() throws CacheException {
QueryService queryService = null;
try {
queryService = (PoolManager.find(poolName2)).getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
List<String> list = null;
try {
list = queryService.getAllDurableCqsFromServer();
} catch (CqException e) {
fail("failed", e);
}
assertEquals(4, list.size());
assertTrue(list.contains("client2DCQ1"));
assertTrue(list.contains("client2DCQ2"));
assertTrue(list.contains("client2DCQ3"));
assertTrue(list.contains("client2DCQ4"));
}
});
client1.invoke(new CacheSerializableRunnable("test getDurableCQsFromServer for client1") {
@Override
public void run2() throws CacheException {
QueryService queryService = null;
try {
queryService = (PoolManager.find(poolName1)).getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
List<String> list = null;
try {
list = queryService.getAllDurableCqsFromServer();
} catch (CqException e) {
fail("failed", e);
}
assertEquals(2, list.size());
assertTrue(list.contains("client1DCQ1"));
assertTrue(list.contains("client1DCQ2"));
}
});
cqDUnitTest.closeClient(client2);
cqDUnitTest.closeClient(client1);
cqDUnitTest.closeServer(server);
}
use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.
the class CqDataUsingPoolDUnitTest method createCq.
private CqQuery createCq(String regionName, String cqName) {
// Create CQ Attributes.
CqAttributesFactory cqAf = new CqAttributesFactory();
// Initialize and set CqListener.
CqListener[] cqListeners = { new CqListener() {
@Override
public void close() {
}
@Override
public void onEvent(CqEvent aCqEvent) {
}
@Override
public void onError(CqEvent aCqEvent) {
}
} };
cqAf.initCqListeners(cqListeners);
CqAttributes cqa = cqAf.create();
// Create cq's
// Get the query service for the Pool
QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
CqQuery query = null;
try {
query = queryService.newCq(cqName, "Select * from /" + regionName, cqa);
query.execute();
} catch (CqExistsException e) {
fail("Could not find specified region:" + regionName + ":", e);
} catch (CqException e) {
fail("Could not find specified region:" + regionName + ":", e);
} catch (RegionNotFoundException e) {
fail("Could not find specified region:" + regionName + ":", e);
}
return query;
}
use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.
the class CqPerfDUnitTest method testCQPerf.
/**
* Tests the cq performance.
*
* @throws Exception
*/
@Ignore("perf")
@Test
public void testCQPerf() throws Exception {
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client = host.getVM(1);
cqDUnitTest.createServer(server);
final int port = server.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(server.getHost());
// Create client.
cqDUnitTest.createClient(client, port, host0);
final String cqName = "testCQPerf_0";
client.invoke(new CacheSerializableRunnable("Create CQ :" + cqName) {
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
cqe.printStackTrace();
fail("Failed to getCQService.");
}
// Create CQ Attributes.
CqAttributesFactory cqf = new CqAttributesFactory();
CqListener[] cqListeners = { new CqTimeTestListener(LogWriterUtils.getLogWriter()) };
((CqTimeTestListener) cqListeners[0]).cqName = cqName;
cqf.initCqListeners(cqListeners);
CqAttributes cqa = cqf.create();
// Create and Execute CQ.
try {
CqQuery cq1 = cqService.newCq(cqName, cqDUnitTest.cqs[0], cqa);
assertTrue("newCq() state mismatch", cq1.getState().isStopped());
cq1.execute();
} catch (Exception ex) {
LogWriterUtils.getLogWriter().info("CqService is :" + cqService);
ex.printStackTrace();
AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
err.initCause(ex);
throw err;
}
}
});
final int size = 50;
// Create values.
cqDUnitTest.createValuesWithTime(client, cqDUnitTest.regions[0], size);
Wait.pause(5000);
// Update values
cqDUnitTest.createValuesWithTime(client, cqDUnitTest.regions[0], size);
client.invoke(new CacheSerializableRunnable("Validate CQs") {
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Validating CQ. ### " + cqName);
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
cqe.printStackTrace();
fail("Failed to getCqService.");
}
CqQuery cQuery = cqService.getCq(cqName);
if (cQuery == null) {
fail("Failed to get CqQuery for CQ : " + cqName);
}
// CqAttributes cqAttr = cQuery.getCqAttributes();
// CqListener cqListeners[] = cqAttr.getCqListeners();
// CqTimeTestListener listener = (CqTimeTestListener) cqListeners[0];
// Wait for all the create to arrive.
// for (int i=1; i <= size; i++) {
// listener.waitForCreated(cqDUnitTest.KEY+i);
// }
// Wait for all the update to arrive.
// for (int i=1; i <= size; i++) {
// listener.waitForUpdated(cqDUnitTest.KEY+i);
// }
// getLogWriter().info("### Time taken for Creation of " + size + " events is :" +
// listener.getTotalQueryCreateTime());
// getLogWriter().info("### Time taken for Update of " + size + " events is :" +
// listener.getTotalQueryUpdateTime());
}
});
Wait.pause(10 * 60 * 1000);
// Close.
cqDUnitTest.closeClient(client);
cqDUnitTest.closeServer(server);
}
use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.
the class PdxQueryCQDUnitTest method testCq.
/**
* Tests client-server query on PdxInstance.
*/
@Test
public void testCq() throws CacheException {
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM vm3 = host.getVM(3);
final int numberOfEntries = 10;
// where id > 5 (0-5)
final int queryLimit = 6;
// Start server1
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer();
Region region = getRootRegion().getSubregion(regionName);
for (int i = 0; i < numberOfEntries; i++) {
region.put("key-" + i, new TestObject(i, "vmware"));
}
}
});
// Start server2
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer();
Region region = getRootRegion().getSubregion(regionName);
}
});
// Client pool.
final int port0 = vm0.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
final int port1 = vm1.invoke(() -> PdxQueryCQTestBase.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
// Create client pool.
final String poolName = "testCqPool";
createPool(vm2, poolName, new String[] { host0 }, new int[] { port0 }, true);
createPool(vm3, poolName, new String[] { host0 }, new int[] { port1 }, true);
final String cqName = "testCq";
// Execute CQ
SerializableRunnable executeCq = new CacheSerializableRunnable("Execute queries") {
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
// Get CQ Service.
QueryService qService = null;
try {
qService = (PoolManager.find(poolName)).getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
// Create CQ Attributes.
CqAttributesFactory cqf = new CqAttributesFactory();
CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
((CqQueryTestListener) cqListeners[0]).cqName = cqName;
cqf.initCqListeners(cqListeners);
CqAttributes cqa = cqf.create();
// Create CQ.
try {
CqQuery cq = qService.newCq(cqName, queryString[3], cqa);
SelectResults sr = cq.executeWithInitialResults();
for (Object o : sr.asSet()) {
Struct s = (Struct) o;
Object value = s.get("value");
if (!(value instanceof TestObject)) {
fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
}
}
} catch (Exception ex) {
AssertionError err = new AssertionError("Failed to create CQ " + cqName + " . ");
err.initCause(ex);
LogWriterUtils.getLogWriter().info("QueryService is :" + qService, err);
throw err;
}
}
};
vm2.invoke(executeCq);
vm3.invoke(executeCq);
// Check for TestObject instances on Server2.
// It should be 0
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
assertEquals(0, TestObject.numInstance);
}
});
// update
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(regionName);
for (int i = 0; i < numberOfEntries * 2; i++) {
region.put("key-" + i, new TestObject(i, "vmware"));
}
// Check for TestObject instances.
assertEquals(numberOfEntries * 3, TestObject.numInstance);
}
});
// Check for TestObject instances on Server2.
// It should be 0
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
assertEquals(0, TestObject.numInstance);
}
});
SerializableRunnable validateCq = new CacheSerializableRunnable("Validate CQs") {
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Validating CQ. ### " + cqName);
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
Assert.fail("Failed to getCQService.", cqe);
}
CqQuery cQuery = cqService.getCq(cqName);
if (cQuery == null) {
fail("Failed to get CqQuery for CQ : " + cqName);
}
CqAttributes cqAttr = cQuery.getCqAttributes();
CqListener[] cqListeners = cqAttr.getCqListeners();
final CqQueryTestListener listener = (CqQueryTestListener) cqListeners[0];
// Wait for the events to show up on the client.
Wait.waitForCriterion(new WaitCriterion() {
public boolean done() {
return listener.getTotalEventCount() >= (numberOfEntries * 2 - queryLimit);
}
public String description() {
return null;
}
}, 30000, 100, false);
listener.printInfo(false);
// Check for event type.
Object[] cqEvents = listener.getEvents();
for (Object o : cqEvents) {
CqEvent cqEvent = (CqEvent) o;
Object value = cqEvent.getNewValue();
if (!(value instanceof TestObject)) {
fail("Expected type TestObject, not found in result set. Found type :" + o.getClass());
}
}
// Check for totalEvents count.
assertEquals("Total Event Count mismatch", (numberOfEntries * 2 - queryLimit), listener.getTotalEventCount());
// Check for create count.
assertEquals("Create Event mismatch", numberOfEntries, listener.getCreateEventCount());
// Check for update count.
assertEquals("Update Event mismatch", numberOfEntries - queryLimit, listener.getUpdateEventCount());
}
};
vm2.invoke(validateCq);
vm3.invoke(validateCq);
this.closeClient(vm2);
this.closeClient(vm3);
this.closeClient(vm1);
this.closeClient(vm0);
}
Aggregations