Search in sources :

Example 16 with CqException

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;
    }
}
Also used : RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) LoggingThreadGroup(org.apache.geode.internal.logging.LoggingThreadGroup) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) CqClosedException(org.apache.geode.cache.query.CqClosedException) CqResults(org.apache.geode.cache.query.CqResults)

Example 17 with CqException

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);
}
Also used : CacheException(org.apache.geode.cache.CacheException) CqException(org.apache.geode.cache.query.CqException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) Host(org.apache.geode.test.dunit.Host) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) VM(org.apache.geode.test.dunit.VM) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) List(java.util.List) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 18 with CqException

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);
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) QueryService(org.apache.geode.cache.query.QueryService) CqException(org.apache.geode.cache.query.CqException) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) List(java.util.List) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheException(org.apache.geode.cache.CacheException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 19 with CqException

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());
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) QueryService(org.apache.geode.cache.query.QueryService) CqException(org.apache.geode.cache.query.CqException) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) List(java.util.List) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 20 with CqException

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;
    }
}
Also used : AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) CqException(org.apache.geode.cache.query.CqException) ArrayList(java.util.ArrayList) CqService(org.apache.geode.cache.query.internal.cq.CqService) IOException(java.io.IOException) CqException(org.apache.geode.cache.query.CqException) CachedRegionHelper(org.apache.geode.internal.cache.tier.CachedRegionHelper) ClientProxyMembershipID(org.apache.geode.internal.cache.tier.sockets.ClientProxyMembershipID) CacheServerStats(org.apache.geode.internal.cache.tier.sockets.CacheServerStats) AcceptorImpl(org.apache.geode.internal.cache.tier.sockets.AcceptorImpl) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) ChunkedMessage(org.apache.geode.internal.cache.tier.sockets.ChunkedMessage)

Aggregations

CqException (org.apache.geode.cache.query.CqException)46 CacheException (org.apache.geode.cache.CacheException)20 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)20 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)20 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 Test (org.junit.Test)16 CqExistsException (org.apache.geode.cache.query.CqExistsException)15 QueryService (org.apache.geode.cache.query.QueryService)14 CqClosedException (org.apache.geode.cache.query.CqClosedException)12 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)9 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)9 IOException (java.io.IOException)8 List (java.util.List)8 CqQuery (org.apache.geode.cache.query.CqQuery)8 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)8 CqService (org.apache.geode.cache.query.internal.cq.CqService)8 Set (java.util.Set)7 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)7 QueryException (org.apache.geode.cache.query.QueryException)7 InternalCqQuery (org.apache.geode.cache.query.internal.cq.InternalCqQuery)7