use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqServiceVsdStats method numCqsOnRegion.
/**
* This is a test method. It silently ignores exceptions and should not be used outside of unit
* tests.
* <p>
* Returns the number of CQs (active + suspended) on the given region.
*/
public long numCqsOnRegion(final InternalCache cache, String regionName) {
if (cache == null) {
return 0;
}
DefaultQueryService queryService = (DefaultQueryService) cache.getQueryService();
CqService cqService = null;
try {
cqService = queryService.getCqService();
} catch (CqException e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to get CqService {}", e.getLocalizedMessage());
}
e.printStackTrace();
// We're confused
return -1;
}
if (((CqServiceImpl) cqService).isServer()) {
// If we are on the server, look at the number of CQs in the filter profile.
try {
FilterProfile fp = cache.getFilterProfile(regionName);
if (fp == null) {
return 0;
}
return fp.getCqCount();
} catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to get serverside CQ count for region: {} {}", regionName, ex.getLocalizedMessage());
}
}
} else {
try {
CqQuery[] cqs = queryService.getCqs(regionName);
if (cqs != null) {
return cqs.length;
}
} catch (Exception ex) {
// Dont do anything.
}
}
return 0;
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqDataDUnitTest method testEventsDuringQueryExecution.
/**
* Test for events created during the CQ query execution. When CQs are executed using
* executeWithInitialResults there may be possibility that the region changes during that time may
* not be reflected in the query result set thus making the query data and region data
* inconsistent.
*
* @throws Exception
*/
@Test
public void testEventsDuringQueryExecution() throws Exception {
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client = host.getVM(1);
final String cqName = "testEventsDuringQueryExecution_0";
cqDUnitTest.createServer(server);
final int port = server.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(server.getHost());
// Initialize Client.
cqDUnitTest.createClient(client, port, host0);
// create CQ.
cqDUnitTest.createCQ(client, cqName, cqDUnitTest.cqs[0]);
final int numObjects = 200;
final int totalObjects = 500;
// initialize Region.
server.invoke(new CacheSerializableRunnable("Update Region") {
public void run2() throws CacheException {
Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
for (int i = 1; i <= numObjects; i++) {
Portfolio p = new Portfolio(i);
region.put("" + i, p);
}
}
});
// Execute CQ while update is in progress.
AsyncInvocation processCqs = client.invokeAsync(new CacheSerializableRunnable("Execute CQ") {
public void run2() throws CacheException {
QueryService cqService = getCache().getQueryService();
// Get CqQuery object.
CqQuery cq1 = cqService.getCq(cqName);
if (cq1 == null) {
fail("Failed to get CQ " + cqName);
}
SelectResults cqResults = null;
try {
cqResults = cq1.executeWithInitialResults();
} catch (Exception ex) {
AssertionError err = new AssertionError("Failed to execute CQ " + cqName);
err.initCause(ex);
throw err;
}
// getLogWriter().info("initial result size = " + cqResults.size());
CqQueryTestListener cqListener = (CqQueryTestListener) cq1.getCqAttributes().getCqListener();
// Wait for the last key to arrive.
cqListener.waitForCreated("" + totalObjects);
// Check if the events from CqListener are in order.
int oldId = 0;
for (Object cqEvent : cqListener.events.toArray()) {
int newId = new Integer(cqEvent.toString()).intValue();
if (oldId > newId) {
fail("Queued events for CQ Listener during execution with " + "Initial results is not in the order in which they are created.");
}
oldId = newId;
}
// Check if all the IDs are present as part of Select Results and CQ Events.
HashSet ids = new HashSet(cqListener.events);
for (Object o : cqResults.asList()) {
Struct s = (Struct) o;
ids.add(s.get("key"));
}
// Iterator iter = cqResults.asSet().iterator();
// while (iter.hasNext()) {
// Portfolio p = (Portfolio)iter.next();
// ids.add(p.getPk());
// //getLogWriter().info("Result set value : " + p.getPk());
// }
HashSet missingIds = new HashSet();
String key = "";
for (int i = 1; i <= totalObjects; i++) {
key = "" + i;
if (!(ids.contains(key))) {
missingIds.add(key);
}
}
if (!missingIds.isEmpty()) {
fail("Missing Keys in either ResultSet or the Cq Event list. " + " Missing keys : [size : " + missingIds.size() + "]" + missingIds + " Ids in ResultSet and CQ Events :" + ids);
}
}
});
// Keep updating region (async invocation).
server.invokeAsync(new CacheSerializableRunnable("Update Region") {
public void run2() throws CacheException {
// Wait to allow client a chance to register the cq
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
for (int i = numObjects + 1; i <= totalObjects; i++) {
Portfolio p = new Portfolio(i);
region.put("" + i, p);
}
}
});
// wait for 60 seconds for test to complete
ThreadUtils.join(processCqs, 60 * 1000);
// Close.
cqDUnitTest.closeClient(client);
cqDUnitTest.closeServer(server);
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqPerfUsingPoolDUnitTest 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(() -> CqQueryUsingPoolDUnitTest.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.CqQuery in project geode by apache.
the class CqQueryDUnitTest method mutateCQAttributes.
// Exercise CQ attributes mutator functions
private void mutateCQAttributes(VM vm, final String cqName, final int mutator_function) throws Exception {
vm.invoke(new CacheSerializableRunnable("Stop CQ :" + cqName) {
public void run2() throws CacheException {
CqQuery cq1 = null;
LogWriterUtils.getLogWriter().info("### CQ attributes mutator for ###" + cqName);
// Get CQ Service.
QueryService cqService = null;
try {
cqService = getCache().getQueryService();
} catch (Exception cqe) {
cqe.printStackTrace();
fail("Failed to getCQService.");
}
// Get CQ.
try {
cq1 = cqService.getCq(cqName);
} catch (Exception ex) {
ex.printStackTrace();
fail("Failed to get CQ " + cqName + " . " + ex.getMessage());
}
CqAttributesMutator cqAttrMutator = cq1.getCqAttributesMutator();
CqAttributes cqAttr = cq1.getCqAttributes();
CqListener[] cqListeners;
switch(mutator_function) {
case CREATE:
// Reinitialize with 2 CQ Listeners
CqListener[] cqListenersArray = { new CqQueryTestListener(getCache().getLogger()), new CqQueryTestListener(getCache().getLogger()) };
cqAttrMutator.initCqListeners(cqListenersArray);
cqListeners = cqAttr.getCqListeners();
assertEquals("CqListener count mismatch", cqListeners.length, 2);
break;
case UPDATE:
// Add 2 new CQ Listeners
CqListener newListener1 = new CqQueryTestListener(getCache().getLogger());
CqListener newListener2 = new CqQueryTestListener(getCache().getLogger());
cqAttrMutator.addCqListener(newListener1);
cqAttrMutator.addCqListener(newListener2);
cqListeners = cqAttr.getCqListeners();
assertEquals("CqListener count mismatch", cqListeners.length, 3);
break;
case DESTROY:
cqListeners = cqAttr.getCqListeners();
cqAttrMutator.removeCqListener(cqListeners[0]);
cqListeners = cqAttr.getCqListeners();
assertEquals("CqListener count mismatch", cqListeners.length, 2);
// Remove a listener and validate
cqAttrMutator.removeCqListener(cqListeners[0]);
cqListeners = cqAttr.getCqListeners();
assertEquals("CqListener count mismatch", cqListeners.length, 1);
break;
}
}
});
}
use of org.apache.geode.cache.query.CqQuery in project geode by apache.
the class CqQueryDUnitTest method waitForError.
private void waitForError(VM vm, final String cqName, final String errorMessage) {
vm.invoke(new CacheSerializableRunnable("validate cq count") {
public void run2() throws CacheException {
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[] cqListener = cqAttr.getCqListeners();
CqQueryTestListener listener = (CqQueryTestListener) cqListener[0];
listener.waitForError(errorMessage);
}
});
}
Aggregations