Search in sources :

Example 61 with CacheSerializableRunnable

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

the class CqDataDUnitTest method testMultipleExecuteWithInitialResults.

/**
   * This test was created to test executeWithInitialResults being called multiple times.
   * Previously, the queueEvents would be overwritten and we would lose data. This test will execute
   * the method twice. The first time, the first execution will block it's own child thread (TC1).
   * The second execution will block until TC1 is completed (based on how executeWithInitialResults
   * is implemented) A third thread will be awaken and release the latch in the testhook for TC1 to
   * complete.
   * 
   * @throws Exception
   */
@Test
public void testMultipleExecuteWithInitialResults() throws Exception {
    final int numObjects = 200;
    final int totalObjects = 500;
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(1);
    client.invoke(setTestHook());
    final String cqName = "testMultiExecuteWithInitialResults";
    // initialize server and retreive host and port values
    cqDUnitTest.createServer(server);
    final int port = server.invoke(() -> CqQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    // Initialize Client.
    cqDUnitTest.createClient(client, port, host0);
    // create CQ.
    cqDUnitTest.createCQ(client, cqName, cqDUnitTest.cqs[0]);
    // initialize Region.
    server.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).
    server.invokeAsync(new CacheSerializableRunnable("Update Region") {

        public void run2() throws CacheException {
            // Wait to give client a chance to register the cq
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            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);
            }
        }
    });
    // the thread that validates all results and executes first
    AsyncInvocation processCqs = client.invokeAsync(new CacheSerializableRunnable("Execute CQ first") {

        public void run2() throws CacheException {
            SelectResults cqResults = null;
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                fail("Failed to get CQ " + cqName);
            }
            try {
                cqResults = cq1.executeWithInitialResults();
            } catch (Exception e) {
                AssertionError err = new AssertionError("Failed to execute  CQ " + cqName);
                err.initCause(e);
                throw err;
            }
            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);
            }
        }
    });
    // the second call to executeWithInitialResults. Goes to sleep hopefully
    // long enough
    // for the first call to executeWithInitialResults first
    client.invokeAsync(new CacheSerializableRunnable("Execute CQ second") {

        public void run2() throws CacheException {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            QueryService cqService = getCache().getQueryService();
            // Get CqQuery object.
            CqQuery cq1 = cqService.getCq(cqName);
            if (cq1 == null) {
                fail("Failed to get CQ " + cqName);
            }
            try {
                cq1.executeWithInitialResults();
            } catch (IllegalStateException e) {
            // we expect an error due to the cq having already being in run state
            } catch (Exception e) {
                AssertionError err = new AssertionError("test hook lock interrupted" + cqName);
                err.initCause(e);
                throw err;
            }
        }
    });
    // thread that unlatches the test hook, sleeping long enough for both
    // the other two threads to execute first
    client.invokeAsync(new CacheSerializableRunnable("Release latch") {

        public void run2() throws CacheException {
            // had a chance to invoke executeWithInitialResults
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                AssertionError err = new AssertionError("test hook lock interrupted" + cqName);
                err.initCause(e);
                throw err;
            }
            CqQueryImpl.testHook.ready();
        }
    });
    // wait for 60 seconds for test to complete
    ThreadUtils.join(processCqs, 60 * 1000);
    // 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) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) 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) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 62 with CacheSerializableRunnable

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

the class CqDataUsingPoolDUnitTest method testCQWithDestroysAndInvalidates.

/**
   * Tests propogation of invalidates and destorys to the clients. Bug 37242.
   */
@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(() -> CqQueryUsingPoolDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(server.getHost());
    String poolName = "testCQWithDestroysAndInvalidates";
    cqDUnitTest.createPool(client, poolName, host0, port);
    // 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, poolName, name, cqDUnitTest.cqs[4]);
    cqDUnitTest.executeCQ(client, name, true, null);
    // do destroys and invalidates.
    server.invoke(new CacheSerializableRunnable("Create values") {

        @Override
        public void run2() throws CacheException {
            Region region1 = getRootRegion().getSubregion(cqDUnitTest.regions[0]);
            for (int i = 1; i <= 5; i++) {
                region1.destroy(CqQueryUsingPoolDUnitTest.KEY + i);
            }
        }
    });
    for (int i = 1; i <= 5; i++) {
        cqDUnitTest.waitForDestroyed(client, name, CqQueryUsingPoolDUnitTest.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, CqQueryUsingPoolDUnitTest.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") {

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

Example 63 with CacheSerializableRunnable

use of org.apache.geode.cache30.CacheSerializableRunnable 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 64 with CacheSerializableRunnable

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

the class CqDataUsingPoolDUnitTest method testCqStatInitializationTimingIssue.

@Test
public void testCqStatInitializationTimingIssue() {
    disconnectAllFromDS();
    // The async close can cause this exception on the server
    IgnoredException.addIgnoredException("java.net.SocketException: Broken pipe");
    final String regionName = "testCqStatInitializationTimingIssue";
    final String cq1Name = "testCq1";
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = 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 a client
    client.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(NetworkUtils.getServerHostName(client.getHost()), server1Port), regionName));
    // Start a pub client
    client2.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(NetworkUtils.getServerHostName(client2.getHost()), server1Port), regionName));
    // client has thread that invokes new and remove cq over and over
    client.invokeAsync(new CacheSerializableRunnable("Register cq") {

        @Override
        public void run2() throws CacheException {
            for (int i = 0; i < 10000; i++) {
                CqQuery query = createCq(regionName, cq1Name);
                if (query != null) {
                    try {
                        query.close();
                    } catch (Exception e) {
                        fail("exception while closing cq:", e);
                    }
                }
            }
        }
    });
    client2.invokeAsync(new CacheSerializableRunnable("pub updates") {

        @Override
        public void run2() throws CacheException {
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            while (true) {
                for (int i = 0; i < 50000; i++) {
                    region.put("" + i, "" + Math.random());
                }
            }
        }
    });
    server.invokeAsync(new CacheSerializableRunnable("pub updates") {

        @Override
        public void run2() throws CacheException {
            Region region = CacheServerTestUtil.getCache().getRegion(regionName);
            while (true) {
                for (int i = 0; i < 50000; i++) {
                    region.put("" + i, "" + Math.random());
                }
            }
        }
    });
    // client has another thread that retrieves cq map and checks stat over and over
    client.invoke(new CacheSerializableRunnable("Check Stats") {

        @Override
        public void run2() throws CacheException {
            for (int i = 0; i < 10000; i++) {
                checkCqStats(cq1Name);
            }
        }
    });
    client.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) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) CqQuery(org.apache.geode.cache.query.CqQuery) 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 65 with CacheSerializableRunnable

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

the class CqDataUsingPoolDUnitTest method testRegionEvents.

/**
   * Test for: Region destroy, calls close on the server. Region clear triggers cqEvent with query
   * op region clear. Region invalidate triggers cqEvent with query op region invalidate.
   */
@Test
public void testRegionEvents() 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());
    String poolName = "testRegionEvents";
    cqDUnitTest.createPool(client, poolName, host0, port);
    // cqDUnitTest.createClient(client, port, host0);
    // Create CQ on regionA
    cqDUnitTest.createCQ(client, poolName, "testRegionEvents_0", cqDUnitTest.cqs[0]);
    cqDUnitTest.executeCQ(client, "testRegionEvents_0", false, null);
    // Create CQ on regionB
    cqDUnitTest.createCQ(client, poolName, "testRegionEvents_1", cqDUnitTest.cqs[2]);
    cqDUnitTest.executeCQ(client, "testRegionEvents_1", false, null);
    // Test for Event on Region Clear.
    server.invoke(new CacheSerializableRunnable("testRegionEvents") {

        @Override
        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Clearing the region on the server ###");
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            for (int i = 1; i <= 5; i++) {
                region.put(CqQueryUsingPoolDUnitTest.KEY + i, new Portfolio(i));
            }
            region.clear();
        }
    });
    cqDUnitTest.waitForRegionClear(client, "testRegionEvents_0");
    // Test for Event on Region invalidate.
    server.invoke(new CacheSerializableRunnable("testRegionEvents") {

        @Override
        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Invalidate the region on the server ###");
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[0]);
            for (int i = 1; i <= 5; i++) {
                region.put(CqQueryUsingPoolDUnitTest.KEY + i, new Portfolio(i));
            }
            region.invalidateRegion();
        }
    });
    cqDUnitTest.waitForRegionInvalidate(client, "testRegionEvents_0");
    // Test for Event on Region destroy.
    server.invoke(new CacheSerializableRunnable("testRegionEvents") {

        @Override
        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Destroying the region on the server ###");
            Region region = getCache().getRegion("/root/" + cqDUnitTest.regions[1]);
            for (int i = 1; i <= 5; i++) {
                region.put(CqQueryUsingPoolDUnitTest.KEY + i, new Portfolio(i));
            }
            // this should close one cq on client.
            region.destroyRegion();
        }
    });
    // wait for cq to close becuse of region destroy on server.
    Wait.pause(1000);
    // cqDUnitTest.waitForClose(client,"testRegionEvents_1");
    cqDUnitTest.validateCQCount(client, 1);
    // 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) Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

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