use of org.apache.geode.cache.query.CqException in project geode by apache.
the class ExecuteCQ method cmdExecute.
@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
AcceptorImpl acceptor = serverConnection.getAcceptor();
CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
ClientProxyMembershipID id = serverConnection.getProxyID();
CacheServerStats stats = serverConnection.getCacheServerStats();
serverConnection.setAsTrue(REQUIRES_RESPONSE);
serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
// Retrieve the data from the message parts
String cqName = clientMessage.getPart(0).getString();
String cqQueryString = clientMessage.getPart(1).getString();
int cqState = clientMessage.getPart(2).getInt();
Part isDurablePart = clientMessage.getPart(3);
byte[] isDurableByte = isDurablePart.getSerializedForm();
boolean isDurable = (isDurableByte == null || isDurableByte[0] == 0) ? false : true;
if (logger.isDebugEnabled()) {
logger.debug("{}: Received {} request from {} CqName: {} queryString: {}", serverConnection.getName(), MessageType.getString(clientMessage.getMessageType()), serverConnection.getSocketString(), cqName, cqQueryString);
}
DefaultQueryService qService = null;
CqService cqServiceForExec = null;
Query query = null;
Set cqRegionNames = null;
ExecuteCQOperationContext executeCQContext = null;
ServerCQ cqQuery = null;
try {
qService = (DefaultQueryService) crHelper.getCache().getLocalQueryService();
// Authorization check
AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
if (authzRequest != null) {
query = qService.newQuery(cqQueryString);
cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
executeCQContext = authzRequest.executeCQAuthorize(cqName, cqQueryString, cqRegionNames);
String newCqQueryString = executeCQContext.getQuery();
if (!cqQueryString.equals(newCqQueryString)) {
query = qService.newQuery(newCqQueryString);
cqQueryString = newCqQueryString;
cqRegionNames = executeCQContext.getRegionNames();
if (cqRegionNames == null) {
cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
}
}
}
cqServiceForExec = qService.getCqService();
cqQuery = cqServiceForExec.executeCq(cqName, cqQueryString, cqState, id, acceptor.getCacheClientNotifier(), isDurable, false, 0, null);
} catch (CqException cqe) {
sendCqResponse(MessageType.CQ_EXCEPTION_TYPE, "", clientMessage.getTransactionId(), cqe, serverConnection);
return;
} catch (Exception e) {
writeChunkedException(clientMessage, e, serverConnection);
return;
}
long oldstart = start;
boolean sendResults = false;
boolean successQuery = false;
if (clientMessage.getMessageType() == MessageType.EXECUTECQ_WITH_IR_MSG_TYPE) {
sendResults = true;
}
// Execute the query and send the result-set to client.
try {
if (query == null) {
query = qService.newQuery(cqQueryString);
cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
}
((DefaultQuery) query).setIsCqQuery(true);
successQuery = processQuery(clientMessage, query, cqQueryString, cqRegionNames, start, cqQuery, executeCQContext, serverConnection, sendResults);
// Update the CQ statistics.
cqQuery.getVsdStats().setCqInitialResultsTime((DistributionStats.getStatTime()) - oldstart);
stats.incProcessExecuteCqWithIRTime((DistributionStats.getStatTime()) - oldstart);
// logger.fine("Time spent in execute with initial results :" +
// DistributionStats.getStatTime() + ", " + oldstart);
} finally {
// If failure to execute the query, close the CQ.
if (!successQuery) {
try {
cqServiceForExec.closeCq(cqName, id);
} catch (Exception ex) {
// Ignore.
}
}
}
if (!sendResults && successQuery) {
// Send OK to client
sendCqResponse(MessageType.REPLY, LocalizedStrings.ExecuteCQ_CQ_CREATED_SUCCESSFULLY.toLocalizedString(), clientMessage.getTransactionId(), null, serverConnection);
long start2 = DistributionStats.getStatTime();
stats.incProcessCreateCqTime(start2 - oldstart);
}
serverConnection.setAsTrue(RESPONDED);
}
use of org.apache.geode.cache.query.CqException in project geode by apache.
the class ExecuteCQ61 method cmdExecute.
@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
AcceptorImpl acceptor = serverConnection.getAcceptor();
CachedRegionHelper crHelper = serverConnection.getCachedRegionHelper();
ClientProxyMembershipID id = serverConnection.getProxyID();
CacheServerStats stats = serverConnection.getCacheServerStats();
serverConnection.setAsTrue(REQUIRES_RESPONSE);
serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
// Retrieve the data from the message parts
String cqName = clientMessage.getPart(0).getString();
String cqQueryString = clientMessage.getPart(1).getString();
int cqState = clientMessage.getPart(2).getInt();
Part isDurablePart = clientMessage.getPart(3);
byte[] isDurableByte = isDurablePart.getSerializedForm();
boolean isDurable = (isDurableByte == null || isDurableByte[0] == 0) ? false : true;
// region data policy
Part regionDataPolicyPart = clientMessage.getPart(clientMessage.getNumberOfParts() - 1);
byte[] regionDataPolicyPartBytes = regionDataPolicyPart.getSerializedForm();
if (logger.isDebugEnabled()) {
logger.debug("{}: Received {} request from {} CqName: {} queryString: {}", serverConnection.getName(), MessageType.getString(clientMessage.getMessageType()), serverConnection.getSocketString(), cqName, cqQueryString);
}
// Check if the Server is running in NotifyBySubscription=true mode.
CacheClientNotifier ccn = acceptor.getCacheClientNotifier();
if (ccn != null) {
CacheClientProxy proxy = ccn.getClientProxy(id);
if (proxy != null && !proxy.isNotifyBySubscription()) {
// This should have been taken care at the client.
String err = LocalizedStrings.ExecuteCQ_SERVER_NOTIFYBYSUBSCRIPTION_MODE_IS_SET_TO_FALSE_CQ_EXECUTION_IS_NOT_SUPPORTED_IN_THIS_MODE.toLocalizedString();
sendCqResponse(MessageType.CQDATAERROR_MSG_TYPE, err, clientMessage.getTransactionId(), null, serverConnection);
return;
}
}
DefaultQueryService qService = null;
CqServiceImpl cqServiceForExec = null;
Query query = null;
Set cqRegionNames = null;
ExecuteCQOperationContext executeCQContext = null;
ServerCQImpl cqQuery = null;
try {
qService = (DefaultQueryService) crHelper.getCache().getLocalQueryService();
// Authorization check
AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
if (authzRequest != null) {
query = qService.newQuery(cqQueryString);
cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
executeCQContext = authzRequest.executeCQAuthorize(cqName, cqQueryString, cqRegionNames);
String newCqQueryString = executeCQContext.getQuery();
if (!cqQueryString.equals(newCqQueryString)) {
query = qService.newQuery(newCqQueryString);
cqQueryString = newCqQueryString;
cqRegionNames = executeCQContext.getRegionNames();
if (cqRegionNames == null) {
cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
}
}
}
if (CqServiceProvider.VMOTION_DURING_CQ_REGISTRATION_FLAG) {
VMotionObserver vmo = VMotionObserverHolder.getInstance();
vmo.vMotionBeforeCQRegistration();
}
cqServiceForExec = (CqServiceImpl) qService.getCqService();
// registering cq with serverConnection so that when CCP will require auth info it can access
// that
// registering cq auth before as possibility that you may get event
serverConnection.setCq(cqName, isDurable);
cqQuery = (ServerCQImpl) cqServiceForExec.executeCq(cqName, cqQueryString, cqState, id, ccn, isDurable, true, regionDataPolicyPartBytes[0], null);
} catch (CqException cqe) {
sendCqResponse(MessageType.CQ_EXCEPTION_TYPE, "", clientMessage.getTransactionId(), cqe, serverConnection);
serverConnection.removeCq(cqName, isDurable);
return;
} catch (Exception e) {
writeChunkedException(clientMessage, e, serverConnection);
serverConnection.removeCq(cqName, isDurable);
return;
}
long oldstart = start;
boolean sendResults = false;
boolean successQuery = false;
if (clientMessage.getMessageType() == MessageType.EXECUTECQ_WITH_IR_MSG_TYPE) {
sendResults = true;
}
// if it is a non PR query with execute query and maintain keys flags set
if (sendResults || (CqServiceImpl.EXECUTE_QUERY_DURING_INIT && CqServiceProvider.MAINTAIN_KEYS && !cqQuery.isPR())) {
// Execute the query and send the result-set to client.
try {
if (query == null) {
query = qService.newQuery(cqQueryString);
cqRegionNames = ((DefaultQuery) query).getRegionsInQuery(null);
}
((DefaultQuery) query).setIsCqQuery(true);
successQuery = processQuery(clientMessage, query, cqQueryString, cqRegionNames, start, cqQuery, executeCQContext, serverConnection, sendResults);
// Update the CQ statistics.
cqQuery.getVsdStats().setCqInitialResultsTime((DistributionStats.getStatTime()) - oldstart);
stats.incProcessExecuteCqWithIRTime((DistributionStats.getStatTime()) - oldstart);
// logger.fine("Time spent in execute with initial results :" +
// DistributionStats.getStatTime() + ", " + oldstart);
} finally {
// If failure to execute the query, close the CQ.
if (!successQuery) {
try {
cqServiceForExec.closeCq(cqName, id);
} catch (Exception ex) {
// Ignore.
}
}
}
} else {
// Don't execute query for cq.execute and
// if it is a PR query with execute query and maintain keys flags not set
cqQuery.cqResultKeysInitialized = true;
successQuery = true;
}
if (!sendResults && successQuery) {
// Send OK to client
sendCqResponse(MessageType.REPLY, LocalizedStrings.ExecuteCQ_CQ_CREATED_SUCCESSFULLY.toLocalizedString(), clientMessage.getTransactionId(), null, serverConnection);
long start2 = DistributionStats.getStatTime();
stats.incProcessCreateCqTime(start2 - oldstart);
}
serverConnection.setAsTrue(RESPONDED);
}
use of org.apache.geode.cache.query.CqException in project geode by apache.
the class CqDataUsingPoolDUnitTest method testCQWithEviction.
/**
* Test for CQ when entries are evicted from region.
*/
@Test
public void testCQWithEviction() throws Exception {
final Host host = Host.getHost(0);
VM server1 = host.getVM(0);
VM server2 = host.getVM(1);
VM client = host.getVM(2);
final int evictionThreshold = 5;
server1.invoke(new CacheSerializableRunnable("Create Cache Server") {
@Override
public void run2() throws CacheException {
LogWriterUtils.getLogWriter().info("### Create Cache Server. ###");
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setMirrorType(MirrorType.NONE);
// setting the eviction attributes.
factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(evictionThreshold));
for (int i = 0; i < cqDUnitTest.regions.length; i++) {
Region region = createRegion(cqDUnitTest.regions[i], factory.createRegionAttributes());
// Set CacheListener.
region.getAttributesMutator().setCacheListener(new CertifiableTestCacheListener(LogWriterUtils.getLogWriter()));
}
Wait.pause(2000);
try {
cqDUnitTest.startBridgeServer(0, true);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
Wait.pause(2000);
}
});
cqDUnitTest.createServer(server2, 0, false, MirrorType.NONE);
final int port1 = server1.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(server1.getHost());
String poolName = "testCQWithEviction";
cqDUnitTest.createPool(client, poolName, host0, port1);
// cqDUnitTest.createClient(client, port1, host0);
// Create CQs.
cqDUnitTest.createCQ(client, poolName, "testCQWithEviction_0", cqDUnitTest.cqs[0]);
// There is a bug37966 filed on this.
try {
cqDUnitTest.executeCQ(client, "testCQWithEviction_0", false, "CqException");
fail("Should have thrown exception, cq not supported on Non-replicated region.");
} catch (Exception expected) {
// Ignore expected.
}
Wait.pause(1 * 1000);
final int size = 10;
// CREATE VALUES.
cqDUnitTest.createValues(server2, cqDUnitTest.regions[0], size);
server1.invoke(new CacheSerializableRunnable("Load from second server") {
@Override
public void run2() throws CacheException {
Region region1 = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
for (int i = 1; i <= size; i++) {
region1.get(CqQueryUsingPoolDUnitTest.KEY + i);
}
}
});
Wait.pause(2 * 1000);
server1.invoke(new CacheSerializableRunnable("validate destroy") {
@Override
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(cqDUnitTest.regions[0]);
assertNotNull(region);
Set keys = region.entrySet();
int keyCnt = size - evictionThreshold;
assertEquals("Mismatch, number of keys in local region is not equal to the expected size", keyCnt, keys.size());
CertifiableTestCacheListener ctl = (CertifiableTestCacheListener) region.getAttributes().getCacheListener();
for (int i = 1; i <= keyCnt; i++) {
ctl.waitForDestroyed(CqQueryUsingPoolDUnitTest.KEY + i);
assertNull(region.getEntry(CqQueryUsingPoolDUnitTest.KEY + i));
}
}
});
// Close.
cqDUnitTest.closeClient(client);
cqDUnitTest.closeServer(server1);
cqDUnitTest.closeServer(server2);
}
use of org.apache.geode.cache.query.CqException in project geode by apache.
the class CqDataUsingPoolDUnitTest method testGetDurableCqsFromServerCycleClients.
@Test
public void testGetDurableCqsFromServerCycleClients() {
disconnectAllFromDS();
final String regionName = "testGetAllDurableCqsFromServerCycleClients";
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client1 = host.getVM(1);
VM client2 = host.getVM(2);
int timeout = 60000;
// Start server 1
final int server1Port = ((Integer) server.invoke(() -> CacheServerTestUtil.createCacheServer(regionName, new Boolean(true)))).intValue();
// Start client 1
client1.invoke(() -> CacheServerTestUtil.createClientCache(getClientPool(NetworkUtils.getServerHostName(client1.getHost()), server1Port), regionName, getDurableClientProperties("client1_dc", timeout)));
// Start client 2
client2.invoke(() -> CacheServerTestUtil.createClientCache(getClientPool(NetworkUtils.getServerHostName(client2.getHost()), server1Port), regionName, getDurableClientProperties("client2_dc", timeout)));
createClient1CqsAndDurableCqs(client1, regionName);
createClient2CqsAndDurableCqs(client2, regionName);
cycleDurableClient(client1, "client1_dc", server1Port, regionName, timeout);
cycleDurableClient(client2, "client2_dc", server1Port, regionName, timeout);
client2.invoke(new CacheSerializableRunnable("check durable cqs for client 2") {
@Override
public void run2() throws CacheException {
QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
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("check durable cqs for client 1") {
@Override
public void run2() throws CacheException {
QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
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"));
}
});
client1.invoke(() -> CacheServerTestUtil.closeCache());
client2.invoke(() -> CacheServerTestUtil.closeCache());
server.invoke(() -> CacheServerTestUtil.closeCache());
}
use of org.apache.geode.cache.query.CqException in project geode by apache.
the class CqDataUsingPoolDUnitTest method testGetDurableCqsFromServerCycleClientsAndMoreCqs.
@Test
public void testGetDurableCqsFromServerCycleClientsAndMoreCqs() {
final String regionName = "testGetAllDurableCqsFromServerCycleClients";
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client1 = host.getVM(1);
VM client2 = host.getVM(2);
int timeout = 60000;
// Start server 1
final int server1Port = ((Integer) server.invoke(() -> CacheServerTestUtil.createCacheServer(regionName, new Boolean(true)))).intValue();
// Start client 1
client1.invoke(() -> CacheServerTestUtil.createClientCache(getClientPool(NetworkUtils.getServerHostName(client1.getHost()), server1Port), regionName, getDurableClientProperties("client1_dc", timeout)));
// Start client 2
client2.invoke(() -> CacheServerTestUtil.createClientCache(getClientPool(NetworkUtils.getServerHostName(client2.getHost()), server1Port), regionName, getDurableClientProperties("client2_dc", timeout)));
// create the test cqs
createClient1CqsAndDurableCqs(client1, regionName);
createClient2CqsAndDurableCqs(client2, regionName);
cycleDurableClient(client1, "client1_dc", server1Port, regionName, timeout);
cycleDurableClient(client2, "client2_dc", server1Port, regionName, timeout);
client1.invoke(new CacheSerializableRunnable("Register more cq for client 1") {
@Override
public void run2() throws CacheException {
// register the cq's
QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
CqAttributesFactory cqAf = new CqAttributesFactory();
CqAttributes attributes = cqAf.create();
try {
queryService.newCq("client1MoreDCQ1", "Select * From /" + regionName + " where id = 1", attributes, true).execute();
queryService.newCq("client1MoreDCQ2", "Select * From /" + regionName + " where id = 10", attributes, true).execute();
queryService.newCq("client1MoreNoDC1", "Select * From /" + regionName, attributes, false).execute();
queryService.newCq("client1MoreNoDC2", "Select * From /" + regionName + " where id = 3", attributes, false).execute();
} catch (RegionNotFoundException e) {
fail("failed", e);
} catch (CqException e) {
fail("failed", e);
} catch (CqExistsException e) {
fail("failed", e);
}
}
});
client2.invoke(new CacheSerializableRunnable("Register more cq for client 2") {
@Override
public void run2() throws CacheException {
// register the cq's
QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
CqAttributesFactory cqAf = new CqAttributesFactory();
CqAttributes attributes = cqAf.create();
try {
queryService.newCq("client2MoreDCQ1", "Select * From /" + regionName + " where id = 1", attributes, true).execute();
queryService.newCq("client2MoreDCQ2", "Select * From /" + regionName + " where id = 10", attributes, true).execute();
queryService.newCq("client2MoreDCQ3", "Select * From /" + regionName, attributes, true).execute();
queryService.newCq("client2MoreDCQ4", "Select * From /" + regionName + " where id = 3", attributes, true).execute();
} catch (RegionNotFoundException e) {
fail("failed", e);
} catch (CqException e) {
fail("failed", e);
} catch (CqExistsException e) {
fail("failed", e);
}
}
});
// Cycle clients a second time
cycleDurableClient(client1, "client1_dc", server1Port, regionName, timeout);
cycleDurableClient(client2, "client2_dc", server1Port, regionName, timeout);
client2.invoke(new CacheSerializableRunnable("check durable cqs for client 2") {
@Override
public void run2() throws CacheException {
QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
List<String> list = null;
try {
list = queryService.getAllDurableCqsFromServer();
} catch (CqException e) {
fail("failed", e);
}
assertEquals(8, list.size());
assertTrue(list.contains("client2DCQ1"));
assertTrue(list.contains("client2DCQ2"));
assertTrue(list.contains("client2DCQ3"));
assertTrue(list.contains("client2DCQ4"));
assertTrue(list.contains("client2MoreDCQ1"));
assertTrue(list.contains("client2MoreDCQ2"));
assertTrue(list.contains("client2MoreDCQ3"));
assertTrue(list.contains("client2MoreDCQ4"));
}
});
client1.invoke(new CacheSerializableRunnable("check durable cqs for client 1") {
@Override
public void run2() throws CacheException {
QueryService queryService = CacheServerTestUtil.getCache().getQueryService();
List<String> list = null;
try {
list = queryService.getAllDurableCqsFromServer();
} catch (CqException e) {
fail("failed", e);
}
assertEquals(4, list.size());
assertTrue(list.contains("client1DCQ1"));
assertTrue(list.contains("client1DCQ2"));
assertTrue(list.contains("client1MoreDCQ1"));
assertTrue(list.contains("client1MoreDCQ2"));
}
});
client1.invoke(() -> CacheServerTestUtil.closeCache());
client2.invoke(() -> CacheServerTestUtil.closeCache());
server.invoke(() -> CacheServerTestUtil.closeCache());
}
Aggregations