Search in sources :

Example 56 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable 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());
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) Host(org.apache.geode.test.dunit.Host) 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 57 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class CqDataUsingPoolDUnitTest 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.
   */
@Test
public void testEventsDuringQueryExecution() throws Exception {
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    final VM client = host.getVM(1);
    final String cqName = "testEventsDuringQueryExecution_0";
    cqDUnitTest.createServer(server);
    final int port = server.invoke(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    String poolName = "testEventsDuringQueryExecution";
    cqDUnitTest.createPool(client, poolName, host0, port);
    // create CQ.
    cqDUnitTest.createCQ(client, poolName, cqName, cqDUnitTest.cqs[0]);
    final int numObjects = 200;
    final int totalObjects = 500;
    // initialize Region.
    server.invoke(new CacheSerializableRunnable("Update Region") {

        @Override
        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);
            }
        }
    });
    // First set testhook in executeWithInitialResults so that queued events
    // are not drained before we verify there number.
    client.invoke(setTestHook());
    // Execute CQ while update is in progress.
    AsyncInvocation executeCq = client.invokeAsync(new CacheSerializableRunnable("Execute CQ AsyncInvoke") {

        @Override
        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) {
                Assert.fail("CQ execution failed", ex);
            }
            // Check num of events received during executeWithInitialResults.
            final TestHook testHook = CqQueryImpl.testHook;
            Wait.waitForCriterion(new WaitCriterion() {

                @Override
                public boolean done() {
                    return testHook.numQueuedEvents() > 0;
                }

                @Override
                public String description() {
                    return "No queued events found.";
                }
            }, 3000, 5, true);
            getCache().getLogger().fine("Queued Events Size" + testHook.numQueuedEvents());
            // Make sure CQEvents are queued during execute with initial results.
            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"));
            }
            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.invoke(new CacheSerializableRunnable("Update Region") {

        @Override
        public void run2() throws CacheException {
            Wait.pause(200);
            client.invoke(new CacheSerializableRunnable("Releasing the latch") {

                @Override
                public void run2() throws CacheException {
                    // Now release the testhook so that CQListener can proceed.
                    final TestHook testHook = CqQueryImpl.testHook;
                    testHook.ready();
                }
            });
            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);
            }
        }
    });
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server);
}
Also used : CacheException(org.apache.geode.cache.CacheException) Portfolio(org.apache.geode.cache.query.data.Portfolio) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) 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) TestHook(org.apache.geode.cache.query.internal.cq.CqQueryImpl.TestHook) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CqQuery(org.apache.geode.cache.query.CqQuery) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 58 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class CqDataDUnitTest method testCQWithEviction.

/**
   * Test for CQ when entries are evicted from region.
   * 
   * @throws Exception
   */
@Test
public void testCQWithEviction() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM client = host.getVM(2);
    final int evictionThreshold = 1;
    server1.invoke(new CacheSerializableRunnable("Create Cache Server") {

        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Create Cache Server. ###");
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            factory.setDataPolicy(DataPolicy.REPLICATE);
            // factory.setMirrorType(MirrorType.NONE);
            // setting the eviction attributes.
            EvictionAttributes evictAttrs = EvictionAttributes.createLRUEntryAttributes(evictionThreshold, EvictionAction.OVERFLOW_TO_DISK);
            factory.setEvictionAttributes(evictAttrs);
            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);
        }
    });
    final int port1 = server1.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    cqDUnitTest.createClient(client, port1, host0);
    // Create CQs.
    cqDUnitTest.createCQ(client, "testCQWithEviction_0", cqDUnitTest.cqs[0]);
    final int size = 10;
    // CREATE VALUES.
    cqDUnitTest.createValues(server1, cqDUnitTest.regions[0], size);
    cqDUnitTest.executeCQ(client, "testCQWithEviction_0", false, "CqException");
    Wait.pause(1 * 1000);
    // Update VALUES.
    cqDUnitTest.createValues(server1, cqDUnitTest.regions[0], size);
    for (int i = 1; i <= size; i++) {
        cqDUnitTest.waitForUpdated(client, "testCQWithEviction_0", cqDUnitTest.KEY + i);
    }
    cqDUnitTest.validateCQ(client, "testCQWithEviction_0", cqDUnitTest.noTest, 0, 10, 0);
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server1);
}
Also used : EvictionAttributes(org.apache.geode.cache.EvictionAttributes) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) CertifiableTestCacheListener(org.apache.geode.cache30.CertifiableTestCacheListener) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 59 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class CqDataDUnitTest method testCQWithLoad.

/**
   * Test for CQ when region is populated with net load.
   * 
   * @throws Exception
   */
@Test
public void testCQWithLoad() 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, 0, false, MirrorType.KEYS_VALUES);
    cqDUnitTest.createServer(server2, 0, false, MirrorType.KEYS);
    final int port1 = server1.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server1.getHost());
    cqDUnitTest.createClient(client, port1, host0);
    // Create CQs.
    cqDUnitTest.createCQ(client, "testCQWithLoad_0", cqDUnitTest.cqs[0]);
    cqDUnitTest.executeCQ(client, "testCQWithLoad_0", false, null);
    Wait.pause(2 * 1000);
    final int size = 10;
    // CREATE VALUES.
    cqDUnitTest.createValues(server2, cqDUnitTest.regions[0], size);
    server1.invoke(new CacheSerializableRunnable("Load from second server") {

        public void run2() throws CacheException {
            Region region1 = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            for (int i = 1; i <= size; i++) {
                region1.get(CqQueryDUnitTest.KEY + i);
            }
        }
    });
    for (int i = 1; i <= size; i++) {
        cqDUnitTest.waitForCreated(client, "testCQWithLoad_0", CqQueryDUnitTest.KEY + i);
    }
    cqDUnitTest.validateCQ(client, "testCQWithLoad_0", CqQueryDUnitTest.noTest, size, 0, 0);
    // Close.
    cqDUnitTest.closeClient(client);
    cqDUnitTest.closeServer(server1);
    cqDUnitTest.closeServer(server2);
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 60 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable in project geode by apache.

the class CqDataDUnitTest method testCQWithDestroysAndInvalidates.

/**
   * Tests propogation of invalidates and destorys to the clients. Bug 37242.
   * 
   * @throws Exception
   */
@Test
public void testCQWithDestroysAndInvalidates() throws Exception {
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(1);
    VM producer = host.getVM(2);
    cqDUnitTest.createServer(server, 0, true);
    final int port = server.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    // Create client.
    cqDUnitTest.createClient(client, port, host0);
    // producer is not doing any thing.
    cqDUnitTest.createClient(producer, port, host0);
    final int size = 10;
    final String name = "testQuery_4";
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], size);
    cqDUnitTest.createCQ(client, name, cqDUnitTest.cqs[4]);
    cqDUnitTest.executeCQ(client, name, true, null);
    // do destroys and invalidates.
    server.invoke(new CacheSerializableRunnable("Create values") {

        public void run2() throws CacheException {
            Region region1 = getRootRegion().getSubregion(cqDUnitTest.regions[0]);
            for (int i = 1; i <= 5; i++) {
                region1.destroy(CqQueryDUnitTest.KEY + i);
            }
        }
    });
    for (int i = 1; i <= 5; i++) {
        cqDUnitTest.waitForDestroyed(client, name, CqQueryDUnitTest.KEY + i);
    }
    // recreate the key values from 1 - 5
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 5);
    // wait for all creates to arrive.
    for (int i = 1; i <= 5; i++) {
        cqDUnitTest.waitForCreated(client, name, CqQueryDUnitTest.KEY + i);
    }
    // do more puts to push first five key-value to disk.
    cqDUnitTest.createValues(server, cqDUnitTest.regions[0], 10);
    // do invalidates on fisrt five keys.
    server.invoke(new CacheSerializableRunnable("Create values") {

        public void run2() throws CacheException {
            Region region1 = getRootRegion().getSubregion(cqDUnitTest.regions[0]);
            for (int i = 1; i <= 5; i++) {
                region1.invalidate(CqQueryDUnitTest.KEY + i);
            }
        }
    });
    // wait for invalidates now.
    for (int i = 1; i <= 5; i++) {
        cqDUnitTest.waitForInvalidated(client, name, CqQueryDUnitTest.KEY + i);
    }
    // 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) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)595 CacheException (org.apache.geode.cache.CacheException)415 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)369 Test (org.junit.Test)369 Region (org.apache.geode.cache.Region)307 VM (org.apache.geode.test.dunit.VM)279 Host (org.apache.geode.test.dunit.Host)274 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)179 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)165 AttributesFactory (org.apache.geode.cache.AttributesFactory)145 IOException (java.io.IOException)135 Cache (org.apache.geode.cache.Cache)124 QueryService (org.apache.geode.cache.query.QueryService)118 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)107 LocalRegion (org.apache.geode.internal.cache.LocalRegion)106 SelectResults (org.apache.geode.cache.query.SelectResults)85 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)75 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)71 IgnoredException (org.apache.geode.test.dunit.IgnoredException)65 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)61