use of org.apache.geode.cache.query.CqException in project geode by apache.
the class ClientCQImpl method executeWithInitialResults.
/**
* Start or resume executing the query. Gets or updates the CQ results and returns them.
*/
@Override
public <E> CqResults<E> executeWithInitialResults() throws CqClosedException, RegionNotFoundException, CqException {
synchronized (queuedEventsSynchObject) {
// until first call is completed.
while (queuedEvents != null) {
try {
queuedEventsSynchObject.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// At this point we know queuedEvents is null and no one is adding to queuedEvents yet.
this.queuedEvents = new ConcurrentLinkedQueue<CqEventImpl>();
}
if (CqQueryImpl.testHook != null) {
testHook.pauseUntilReady();
}
// Send CQ request to servers.
// If an exception is thrown, we need to clean up the queuedEvents
// or else client will hang on next executeWithInitialResults
CqResults initialResults;
try {
initialResults = (CqResults) executeCqOnRedundantsAndPrimary(true);
} catch (RegionNotFoundException | CqException | RuntimeException e) {
queuedEvents = null;
throw e;
}
// initial results can be added to the queue.
synchronized (queuedEventsSynchObject) {
// Invoke the CQ Listeners with the received CQ Events.
try {
if (!this.queuedEvents.isEmpty()) {
try {
Runnable r = new Runnable() {
@Override
public void run() {
Object[] eventArray = null;
if (CqQueryImpl.testHook != null) {
testHook.setEventCount(queuedEvents.size());
}
// Synchronization for the executer thread.
synchronized (queuedEventsSynchObject) {
try {
eventArray = queuedEvents.toArray();
// Process through the events
for (Object cqEvent : eventArray) {
cqService.invokeListeners(cqName, ClientCQImpl.this, (CqEventImpl) cqEvent);
stats.decQueuedCqListenerEvents();
}
} finally {
// Make sure that we notify waiting threads or else possible dead lock
queuedEvents.clear();
queuedEvents = null;
queuedEventsSynchObject.notify();
}
}
}
};
final LoggingThreadGroup group = LoggingThreadGroup.createThreadGroup("CQEventHandler", logger);
Thread thread = new Thread(group, r, "CQEventHandler For " + cqName);
thread.setDaemon(true);
thread.start();
} catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Exception while invoking the CQ Listener with queued events.", ex);
}
}
} else {
queuedEvents = null;
}
} finally {
queuedEventsSynchObject.notify();
}
return initialResults;
}
}
use of org.apache.geode.cache.query.CqException 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.CqException in project geode by apache.
the class CqDataUsingPoolDUnitTest method testGetDurableCQsFromServerWithEmptyList.
@Test
public void testGetDurableCQsFromServerWithEmptyList() throws Exception {
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client1 = host.getVM(1);
/* 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";
cqDUnitTest.createPool(client1, poolName1, host0, port);
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(0, list.size());
assertFalse(list.contains("client1DCQ1"));
assertFalse(list.contains("client1DCQ2"));
}
});
cqDUnitTest.closeClient(client1);
cqDUnitTest.closeServer(server);
}
use of org.apache.geode.cache.query.CqException in project geode by apache.
the class CqDataUsingPoolDUnitTest method testGetDurableCqsFromServer.
@Test
public void testGetDurableCqsFromServer() {
disconnectAllFromDS();
final String regionName = "testGetAllDurableCqsFromServer";
final String cq1Name = "testCq1";
final Host host = Host.getHost(0);
VM server = host.getVM(0);
VM client1 = host.getVM(1);
VM client2 = host.getVM(2);
// 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));
// Start client 2
client2.invoke(() -> CacheServerTestUtil.createClientCache(getClientPool(NetworkUtils.getServerHostName(client2.getHost()), server1Port), regionName));
createClient1CqsAndDurableCqs(client1, regionName);
createClient2CqsAndDurableCqs(client2, regionName);
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 GetDurableCQs 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);
if (logger.isDebugEnabled()) {
logger.debug("{}: Received {} request from {}", serverConnection.getName(), MessageType.getString(clientMessage.getMessageType()), serverConnection.getSocketString());
}
DefaultQueryService qService = null;
CqService cqServiceForExec = null;
try {
qService = (DefaultQueryService) crHelper.getCache().getLocalQueryService();
this.securityService.authorizeClusterRead();
// Authorization check
AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
if (authzRequest != null) {
authzRequest.getDurableCQsAuthorize();
}
cqServiceForExec = qService.getCqService();
List<String> durableCqs = cqServiceForExec.getAllDurableClientCqs(id);
ChunkedMessage chunkedResponseMsg = serverConnection.getChunkedResponseMessage();
chunkedResponseMsg.setMessageType(MessageType.RESPONSE);
chunkedResponseMsg.setTransactionId(clientMessage.getTransactionId());
chunkedResponseMsg.sendHeader();
List durableCqList = new ArrayList(MAXIMUM_CHUNK_SIZE);
final boolean isTraceEnabled = logger.isTraceEnabled();
for (Iterator it = durableCqs.iterator(); it.hasNext(); ) {
Object durableCqName = it.next();
durableCqList.add(durableCqName);
if (isTraceEnabled) {
logger.trace("{}: getDurableCqsResponse <{}>; list size was {}", serverConnection.getName(), durableCqName, durableCqList.size());
}
if (durableCqList.size() == MAXIMUM_CHUNK_SIZE) {
// Send the chunk and clear the list
sendDurableCqsResponseChunk(durableCqList, false, serverConnection);
durableCqList.clear();
}
}
// Send the last chunk even if the list is of zero size.
sendDurableCqsResponseChunk(durableCqList, true, serverConnection);
} catch (CqException cqe) {
sendCqResponse(MessageType.CQ_EXCEPTION_TYPE, "", clientMessage.getTransactionId(), cqe, serverConnection);
return;
} catch (Exception e) {
writeChunkedException(clientMessage, e, serverConnection);
return;
}
}
Aggregations