Search in sources :

Example 6 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqResultSetUsingPoolOptimizedExecuteDUnitTest method testCqResultsCachingWithFailOver.

/**
   * Tests CQ Result Caching with CQ Failover. When EXECUTE_QUERY_DURING_INIT is false and new
   * server calls execute during HA the results cache is not initialized.
   * 
   * @throws Exception
   */
@Override
@Test
public void testCqResultsCachingWithFailOver() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client = host.getVM(2);
    cqDUnitTest.createServer(server1);
    final int port1 = server1.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(1);
    String poolName = "testCQFailOver";
    final String cqName = "testCQFailOver_0";
    cqDUnitTest.createPool(client, poolName, new String[] { host0, host0 }, new int[] { port1, ports[0] });
    // create CQ.
    cqDUnitTest.createCQ(client, poolName, cqName, cqDUnitTest.cqs[0]);
    final int numObjects = 300;
    final int totalObjects = 500;
    // initialize Region.
    server1.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);
            }
        }
    });
    // Keep updating region (async invocation).
    server1.invokeAsync(new CacheSerializableRunnable("Update Region") {

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            // Update (totalObjects - 1) entries.
            for (int i = 1; i < totalObjects; i++) {
                // Destroy entries.
                if (i > 25 && i < 201) {
                    region.destroy("" + i);
                    continue;
                }
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
            // recreate destroyed entries.
            for (int j = 26; j < 201; j++) {
                Portfolio p = new Portfolio(j);
                region.put("" + j, p);
            }
            // Add the last key.
            Portfolio p = new Portfolio(totalObjects);
            region.put("" + totalObjects, p);
        }
    });
    // Execute CQ.
    // While region operation is in progress execute CQ.
    cqDUnitTest.executeCQ(client, cqName, true, null);
    // Verify CQ Cache results.
    server1.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqServiceImpl CqServiceImpl = null;
            try {
                CqServiceImpl = (org.apache.geode.cache.query.internal.cq.CqServiceImpl) ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqServiceImpl.", ex);
                Assert.fail("Failed to get the internal CqServiceImpl.", ex);
            }
            // Wait till all the region update is performed.
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            while (true) {
                if (region.get("" + totalObjects) == null) {
                    try {
                        Thread.sleep(50);
                    } catch (Exception ex) {
                    // ignore.
                    }
                    continue;
                }
                break;
            }
            Collection<? extends InternalCqQuery> cqs = CqServiceImpl.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getName().equals(cqName)) {
                    int size = cqQuery.getCqResultKeysSize();
                    if (size != totalObjects) {
                        LogWriterUtils.getLogWriter().info("The number of Cached events " + size + " is not equal to the expected size " + totalObjects);
                        HashSet expectedKeys = new HashSet();
                        for (int i = 1; i < totalObjects; i++) {
                            expectedKeys.add("" + i);
                        }
                        Set cachedKeys = cqQuery.getCqResultKeyCache();
                        expectedKeys.removeAll(cachedKeys);
                        LogWriterUtils.getLogWriter().info("Missing keys from the Cache : " + expectedKeys);
                    }
                    assertEquals("The number of keys cached for cq " + cqName + " is wrong.", totalObjects, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    cqDUnitTest.createServer(server2, ports[0]);
    final int thePort2 = server2.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    System.out.println("### Port on which server1 running : " + port1 + " Server2 running : " + thePort2);
    Wait.pause(3 * 1000);
    // Close server1 for CQ fail over to server2.
    cqDUnitTest.closeServer(server1);
    Wait.pause(3 * 1000);
    // Verify CQ Cache results.
    server2.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqServiceImpl CqServiceImpl = null;
            try {
                CqServiceImpl = (CqServiceImpl) ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqServiceImpl.", ex);
                Assert.fail("Failed to get the internal CqServiceImpl.", ex);
            }
            // Wait till all the region update is performed.
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            while (true) {
                if (region.get("" + totalObjects) == null) {
                    try {
                        Thread.sleep(50);
                    } catch (Exception ex) {
                    // ignore.
                    }
                    continue;
                }
                break;
            }
            Collection<? extends InternalCqQuery> cqs = CqServiceImpl.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getName().equals(cqName)) {
                    int size = cqQuery.getCqResultKeysSize();
                    assertEquals("The number of keys cached for cq " + cqName + " is wrong.", 0, size);
                }
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server2);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) CqServiceImpl(org.apache.geode.cache.query.internal.cq.CqServiceImpl) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 7 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqResultSetUsingPoolDUnitTest method testCqResultsCachingWithFailOver.

/**
   * Tests CQ Result Caching with CQ Failover.
   * 
   * @throws Exception
   */
// GEODE-1251
@Category(FlakyTest.class)
@Test
public void testCqResultsCachingWithFailOver() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client = host.getVM(2);
    cqDUnitTest.createServer(server1);
    final int port1 = server1.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(1);
    String poolName = "testCQFailOver";
    final String cqName = "testCQFailOver_0";
    cqDUnitTest.createPool(client, poolName, new String[] { host0, host0 }, new int[] { port1, ports[0] });
    // create CQ.
    cqDUnitTest.createCQ(client, poolName, cqName, cqDUnitTest.cqs[0]);
    final int numObjects = 300;
    final int totalObjects = 500;
    // initialize Region.
    server1.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);
            }
        }
    });
    // Keep updating region (async invocation).
    server1.invokeAsync(new CacheSerializableRunnable("Update Region") {

        public void run2() throws CacheException {
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            // Update (totalObjects - 1) entries.
            for (int i = 1; i < totalObjects; i++) {
                // Destroy entries.
                if (i > 25 && i < 201) {
                    region.destroy("" + i);
                    continue;
                }
                Portfolio p = new Portfolio(i);
                region.put("" + i, p);
            }
            // recreate destroyed entries.
            for (int j = 26; j < 201; j++) {
                Portfolio p = new Portfolio(j);
                region.put("" + j, p);
            }
            // Add the last key.
            Portfolio p = new Portfolio(totalObjects);
            region.put("" + totalObjects, p);
        }
    });
    // Execute CQ.
    // While region operation is in progress execute CQ.
    cqDUnitTest.executeCQ(client, cqName, true, null);
    // Verify CQ Cache results.
    server1.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            // Wait till all the region update is performed.
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            while (true) {
                if (region.get("" + totalObjects) == null) {
                    try {
                        Thread.sleep(50);
                    } catch (Exception ex) {
                    // ignore.
                    }
                    continue;
                }
                break;
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getName().equals(cqName)) {
                    int size = cqQuery.getCqResultKeysSize();
                    if (size != totalObjects) {
                        LogWriterUtils.getLogWriter().info("The number of Cached events " + size + " is not equal to the expected size " + totalObjects);
                        HashSet expectedKeys = new HashSet();
                        for (int i = 1; i < totalObjects; i++) {
                            expectedKeys.add("" + i);
                        }
                        Set cachedKeys = cqQuery.getCqResultKeyCache();
                        expectedKeys.removeAll(cachedKeys);
                        LogWriterUtils.getLogWriter().info("Missing keys from the Cache : " + expectedKeys);
                    }
                    assertEquals("The number of keys cached for cq " + cqName + " is wrong.", totalObjects, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    cqDUnitTest.createServer(server2, ports[0]);
    final int thePort2 = server2.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    System.out.println("### Port on which server1 running : " + port1 + " Server2 running : " + thePort2);
    Wait.pause(3 * 1000);
    // Close server1 for CQ fail over to server2.
    cqDUnitTest.closeServer(server1);
    Wait.pause(3 * 1000);
    // Verify CQ Cache results.
    server2.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            // Wait till all the region update is performed.
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            while (true) {
                if (region.get("" + totalObjects) == null) {
                    try {
                        Thread.sleep(50);
                    } catch (Exception ex) {
                    // ignore.
                    }
                    continue;
                }
                break;
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getName().equals(cqName)) {
                    int size = cqQuery.getCqResultKeysSize();
                    if (size != totalObjects) {
                        LogWriterUtils.getLogWriter().info("The number of Cached events " + size + " is not equal to the expected size " + totalObjects);
                        HashSet expectedKeys = new HashSet();
                        for (int i = 1; i < totalObjects; i++) {
                            expectedKeys.add("" + i);
                        }
                        Set cachedKeys = cqQuery.getCqResultKeyCache();
                        expectedKeys.removeAll(cachedKeys);
                        LogWriterUtils.getLogWriter().info("Missing keys from the Cache : " + expectedKeys);
                    }
                    assertEquals("The number of keys cached for cq " + cqName + " is wrong.", totalObjects, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server2);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) CqService(org.apache.geode.cache.query.internal.cq.CqService) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) HashSet(java.util.HashSet) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqResultSetUsingPoolDUnitTest method testCqResultsCachingForDestroyEventsOnPR.

/**
   * Tests CQ Result Set caching for destroy events.
   * 
   * @throws Exception
   */
@Test
public void testCqResultsCachingForDestroyEventsOnPR() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client = host.getVM(2);
    cqDUnitTest.createServerWithPR(server1, 0, false, 0);
    cqDUnitTest.createServerWithPR(server2, 0, false, 0);
    final int port = server1.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    String poolName = "testCqResults";
    final String cqName = "testCqResultsCachingForDestroyEventsOnPR_0";
    cqDUnitTest.createPool(client, poolName, host0, port);
    // Create client.
    cqDUnitTest.createClient(client, port, host0);
    // create CQ.
    cqDUnitTest.createCQ(client, poolName, cqName, cqDUnitTest.cqs[0]);
    // Execute CQ.
    cqDUnitTest.executeCQ(client, cqName, true, null);
    final int numObjects = 50;
    // initialize Region.
    server1.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);
            }
        }
    });
    // Update from server2.
    server2.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);
            }
        }
    });
    // Destroy entries.
    server2.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.destroy("" + i);
            }
        }
    });
    // Wait for events to be sent.
    cqDUnitTest.waitForDestroyed(client, cqName, "" + numObjects);
    // Verify CQ Cache results.
    server1.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getCqResultKeysSize() > 0) {
                    fail("The CQ Result Cache on PR should have been empty for CQ :" + cqName + " keys=" + cqQuery.getCqResultKeyCache());
                }
            }
        }
    });
    server2.invoke(new CacheSerializableRunnable("Verify CQ Cache results") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                if (cqQuery.getCqResultKeysSize() > 0) {
                    fail("The CQ Result Cache on PR should have been empty for CQ :" + cqName);
                }
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server1);
    cqDUnitTest.closeServer(server2);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) CqService(org.apache.geode.cache.query.internal.cq.CqService) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 9 with ServerCQImpl

use of org.apache.geode.cache.query.internal.cq.ServerCQImpl in project geode by apache.

the class CqPerfUsingPoolDUnitTest method testKeyMaintenance.

/**
   * Test for maintaining keys for update optimization.
   * 
   * @throws Exception
   */
@Test
public void testKeyMaintenance() 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());
    // cqDUnitTest.createClient(client, port, host0);
    String poolName = "testKeyMaintainance";
    cqDUnitTest.createPool(client, poolName, host0, port);
    // HashSet for caching purpose will be created for cqs.
    final int cqSize = 2;
    // Cq1
    cqDUnitTest.createCQ(client, poolName, "testKeyMaintainance_0", cqDUnitTest.cqs[0]);
    cqDUnitTest.executeCQ(client, "testKeyMaintainance_0", false, null);
    // Cq2
    cqDUnitTest.createCQ(client, poolName, "testKeyMaintainance_1", cqDUnitTest.cqs[10]);
    cqDUnitTest.executeCQ(client, "testKeyMaintainance_1", false, null);
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 1);
    cqDUnitTest.waitForCreated(client, "testKeyMaintainance_0", CqQueryUsingPoolDUnitTest.KEY + 1);
    // Entry is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 0
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeys1") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 1, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 0, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Update 1.
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 10);
    cqDUnitTest.waitForCreated(client, "testKeyMaintainance_0", CqQueryDUnitTest.KEY + 10);
    // Entry/check is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 1
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate1") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 10, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 5, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Update.
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 12);
    cqDUnitTest.waitForCreated(client, "testKeyMaintainance_0", CqQueryDUnitTest.KEY + 12);
    // Entry/check is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 1
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 12, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 6, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // Delete.
    cqDUnitTest.deleteValues(server, cqDUnitTest.regions[0], 6);
    cqDUnitTest.waitForDestroyed(client, "testKeyMaintainance_0", CqQueryDUnitTest.KEY + 6);
    // Entry/check is made into the CQs cache hashset.
    // testKeyMaintainance_0 with 1 entry and testKeyMaintainance_1 with 1
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 6, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 3, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // This should still needs to process the events so that Results are uptodate.
    cqDUnitTest.stopCQ(client, "testKeyMaintainance_1");
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 12);
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 12, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_1 is wrong.", 6, cqQuery.getCqResultKeysSize());
                }
            }
        }
    });
    // This should re-start the caching for this CQ.
    cqDUnitTest.executeCQ(client, "testKeyMaintainance_1", false, null);
    // This will remove the caching for this CQ.
    cqDUnitTest.closeCQ(client, "testKeyMaintainance_1");
    server.invoke(new CacheSerializableRunnable("LookForCachedEventKeysAfterUpdate2") {

        public void run2() throws CacheException {
            CqService cqService = null;
            try {
                cqService = ((DefaultQueryService) getCache().getQueryService()).getCqService();
            } catch (Exception ex) {
                LogWriterUtils.getLogWriter().info("Failed to get the internal CqService.", ex);
                Assert.fail("Failed to get the internal CqService.", ex);
            }
            Collection<? extends InternalCqQuery> cqs = cqService.getAllCqs();
            for (InternalCqQuery cq : cqs) {
                ServerCQImpl cqQuery = (ServerCQImpl) cq;
                String serverCqName = (String) cqQuery.getServerCqName();
                if (serverCqName.startsWith("testKeyMaintainance_0")) {
                    assertEquals("The number of keys cached for cq testKeyMaintainance_0 is wrong.", 12, cqQuery.getCqResultKeysSize());
                } else if (serverCqName.startsWith("testKeyMaintainance_1")) {
                    fail("The key maintainance should not be present for this CQ.");
                }
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server);
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Collection(java.util.Collection) Host(org.apache.geode.test.dunit.Host) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqService(org.apache.geode.cache.query.internal.cq.CqService) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) ServerCQImpl(org.apache.geode.cache.query.internal.cq.ServerCQImpl) CacheException(org.apache.geode.cache.CacheException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ServerCQImpl (org.apache.geode.cache.query.internal.cq.ServerCQImpl)9 Collection (java.util.Collection)8 CacheException (org.apache.geode.cache.CacheException)8 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)8 InternalCqQuery (org.apache.geode.cache.query.internal.cq.InternalCqQuery)8 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)8 Host (org.apache.geode.test.dunit.Host)8 VM (org.apache.geode.test.dunit.VM)8 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)8 Test (org.junit.Test)8 CqService (org.apache.geode.cache.query.internal.cq.CqService)7 Region (org.apache.geode.cache.Region)6 Portfolio (org.apache.geode.cache.query.data.Portfolio)6 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)6 Set (java.util.Set)5 HashSet (java.util.HashSet)4 CqServiceImpl (org.apache.geode.cache.query.internal.cq.CqServiceImpl)2 IOException (java.io.IOException)1 ExecuteCQOperationContext (org.apache.geode.cache.operations.ExecuteCQOperationContext)1 CqException (org.apache.geode.cache.query.CqException)1