Search in sources :

Example 1 with AsyncInvocation

use of org.apache.geode.test.dunit.AsyncInvocation in project geode by apache.

the class PutAllCSDUnitTest method testPartialKeyInPR.

/**
   * Tests partial key putAll to 2 PR servers, because putting data at server side is different
   * between PR and LR. PR does it in postPutAll. It's not running in singleHop putAll
   */
@Test
public void testPartialKeyInPR() throws CacheException, InterruptedException {
    final String title = "testPartialKeyInPR:";
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    VM client1 = host.getVM(2);
    VM client2 = host.getVM(3);
    final String regionName = getUniqueName();
    final String serverHost = NetworkUtils.getServerHostName(server1.getHost());
    // set <true, false> means <PR=true, notifyBySubscription=false> to test local-invalidates
    final int serverPort1 = createBridgeServer(server1, regionName, 0, true, 0, "ds1");
    final int serverPort2 = createBridgeServer(server2, regionName, 0, true, 0, "ds1");
    createClient(client1, regionName, serverHost, new int[] { serverPort1, serverPort2 }, -1, -1, false, false, true);
    createClient(client2, regionName, serverHost, new int[] { serverPort1, serverPort2 }, -1, -1, false, false, true);
    server1.invoke(addExceptionTag1(expectedExceptions));
    server2.invoke(addExceptionTag1(expectedExceptions));
    client1.invoke(addExceptionTag1(expectedExceptions));
    client2.invoke(addExceptionTag1(expectedExceptions));
    server1.invoke(new CacheSerializableRunnable(title + "server1 add slow listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(true));
        }
    });
    final SharedCounter sc_server2 = new SharedCounter("server2");
    server2.invoke(new CacheSerializableRunnable(title + "server2 add slow listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(server2, true, sc_server2, 10));
        }
    });
    client2.invoke(new CacheSerializableRunnable(title + "client2 add listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
            region.registerInterest("ALL_KEYS");
            LogWriterUtils.getLogWriter().info("client2 registerInterest ALL_KEYS at " + region.getFullPath());
        }
    });
    AsyncInvocation async1 = client1.invokeAsync(new CacheSerializableRunnable(title + "client1 add listener and putAll") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
            region.registerInterest("ALL_KEYS");
            // create keys
            try {
                doPutAll(regionName, title, numberOfEntries);
                fail("Expect ServerOperationException caused by PutAllParitialResultException");
            } catch (ServerOperationException soe) {
                if (!(soe.getCause() instanceof PartitionOfflineException)) {
                    throw soe;
                }
                if (!soe.getMessage().contains(LocalizedStrings.Region_PutAll_Applied_PartialKeys_At_Server_0.toLocalizedString(region.getFullPath()))) {
                    throw soe;
                }
            }
        }
    });
    // server2 will closeCache after created 10 keys
    ThreadUtils.join(async1, 30 * 1000);
    if (async1.exceptionOccurred()) {
        Assert.fail("Aync1 get exceptions:", async1.getException());
    }
    int client1Size = getRegionSize(client1, regionName);
    // client2Size maybe more than client1Size
    int client2Size = getRegionSize(client2, regionName);
    int server1Size = getRegionSize(server1, regionName);
    LogWriterUtils.getLogWriter().info("region sizes: " + client1Size + "," + client2Size + "," + server1Size);
    // restart server2
    createBridgeServer(server2, regionName, serverPort2, true, 0, "ds1");
    server1Size = getRegionSize(server1, regionName);
    int server2Size = getRegionSize(server2, regionName);
    LogWriterUtils.getLogWriter().info("region sizes after server2 restarted: " + client1Size + "," + client2Size + "," + server1Size + ":" + server2Size);
    assertEquals(client2Size, server1Size);
    assertEquals(client2Size, server2Size);
    // close a server to re-run the test
    closeCache(server2);
    server1Size = getRegionSize(server1, regionName);
    client1.invoke(new CacheSerializableRunnable(title + "client1 does putAll again") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // create keys
            try {
                doPutAll(regionName, title + "again:", numberOfEntries);
                fail("Expect ServerOperationException caused by PutAllParitialResultException");
            } catch (ServerOperationException soe) {
                assertTrue(soe.getMessage().contains(LocalizedStrings.Region_PutAll_Applied_PartialKeys_At_Server_0.toLocalizedString(region.getFullPath())));
                assertTrue(soe.getCause() instanceof PartitionOfflineException);
            }
        }
    });
    int new_server1Size = getRegionSize(server1, regionName);
    int new_client1Size = getRegionSize(client1, regionName);
    int new_client2Size = getRegionSize(client2, regionName);
    LogWriterUtils.getLogWriter().info("region sizes after re-run the putAll: " + new_client1Size + "," + new_client2Size + "," + new_server1Size);
    assertEquals(server1Size + numberOfEntries / 2, new_server1Size);
    assertEquals(client1Size + numberOfEntries / 2, new_client1Size);
    assertEquals(client2Size + numberOfEntries / 2, new_client2Size);
    // restart server2
    createBridgeServer(server2, regionName, serverPort2, true, 0, "ds1");
    server1Size = getRegionSize(server1, regionName);
    server2Size = getRegionSize(server2, regionName);
    LogWriterUtils.getLogWriter().info("region sizes after restart server2: " + server1Size + "," + server2Size);
    assertEquals(server1Size, server2Size);
    // add a cacheWriter for server to stop after created 15 keys
    server1.invoke(new CacheSerializableRunnable(title + "server1 execute P2P putAll") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            // let the server to trigger exception after created 15 keys
            region.getAttributesMutator().setCacheWriter(new MyWriter(15));
        }
    });
    // p2p putAll on PR and expect exception
    server2.invoke(new CacheSerializableRunnable(title + "server2 add listener and putAll") {

        @Override
        public void run2() throws CacheException {
            // create keys
            try {
                doPutAll(regionName, title + "once again:", numberOfEntries);
                fail("Expected a CacheWriterException to be thrown by test");
            } catch (CacheWriterException rte) {
                assertTrue(rte.getMessage().contains("Triggered exception as planned, created 15 keys"));
            }
        }
    });
    new_server1Size = getRegionSize(server1, regionName);
    int new_server2Size = getRegionSize(server2, regionName);
    LogWriterUtils.getLogWriter().info("region sizes after restart server2: " + new_server1Size + "," + new_server2Size);
    assertEquals(server1Size + 15, new_server1Size);
    assertEquals(server2Size + 15, new_server2Size);
    server1.invoke(removeExceptionTag1(expectedExceptions));
    server2.invoke(removeExceptionTag1(expectedExceptions));
    client1.invoke(removeExceptionTag1(expectedExceptions));
    client2.invoke(removeExceptionTag1(expectedExceptions));
    // Stop server
    stopBridgeServers(getCache());
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) PartitionOfflineException(org.apache.geode.cache.persistence.PartitionOfflineException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheWriterException(org.apache.geode.cache.CacheWriterException) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 2 with AsyncInvocation

use of org.apache.geode.test.dunit.AsyncInvocation in project geode by apache.

the class PutAllCSDUnitTest method testOneServer.

/**
   * Tests putAll to one server.
   */
@Test
public void testOneServer() throws CacheException, InterruptedException {
    final String title = "testOneServer:";
    final Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client1 = host.getVM(2);
    VM client2 = host.getVM(3);
    final String regionName = getUniqueName();
    final String serverHost = NetworkUtils.getServerHostName(server.getHost());
    // set <false, true> means <PR=false, notifyBySubscription=true> to enable registerInterest and
    // CQ
    final int serverPort = createBridgeServer(server, regionName, 0, false, 0, null);
    createClient(client1, regionName, serverHost, new int[] { serverPort }, -1, -1, false, true, true);
    createClient(client2, regionName, serverHost, new int[] { serverPort }, -1, -1, false, true, true);
    server.invoke(new CacheSerializableRunnable(title + "server add listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
        }
    });
    client2.invoke(new CacheSerializableRunnable(title + "client2 registerInterest and add listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
            // registerInterest for ALL_KEYS
            region.registerInterest("ALL_KEYS");
            LogWriterUtils.getLogWriter().info("client2 registerInterest ALL_KEYS at " + region.getFullPath());
        }
    });
    client1.invoke(new CacheSerializableRunnable(title + "client1 create local region and run putAll") {

        @Override
        public void run2() throws CacheException {
            AttributesFactory factory2 = new AttributesFactory();
            factory2.setScope(Scope.LOCAL);
            factory2.addCacheListener(new MyListener(false));
            createRegion("localsave", factory2.create());
            Region region = doPutAll(regionName, "key-", numberOfEntries);
            assertEquals(numberOfEntries, region.size());
        }
    });
    AsyncInvocation async1 = client1.invokeAsync(new CacheSerializableRunnable(title + "client1 create CQ") {

        @Override
        public void run2() throws CacheException {
            // create a CQ for key 10-20
            Region localregion = getRootRegion().getSubregion("localsave");
            CqAttributesFactory cqf1 = new CqAttributesFactory();
            EOCQEventListener EOCQListener = new EOCQEventListener(localregion);
            cqf1.addCqListener(EOCQListener);
            CqAttributes cqa1 = cqf1.create();
            String cqName1 = "EOInfoTracker";
            String queryStr1 = "SELECT ALL * FROM /root/" + regionName + " ii WHERE ii.getTicker() >= '10' and ii.getTicker() < '20'";
            LogWriterUtils.getLogWriter().info("Query String: " + queryStr1);
            try {
                QueryService cqService = getCache().getQueryService();
                CqQuery EOTracker = cqService.newCq(cqName1, queryStr1, cqa1);
                SelectResults rs1 = EOTracker.executeWithInitialResults();
                List list1 = rs1.asList();
                for (int i = 0; i < list1.size(); i++) {
                    Struct s = (Struct) list1.get(i);
                    TestObject o = (TestObject) s.get("value");
                    LogWriterUtils.getLogWriter().info("InitialResult:" + i + ":" + o);
                    localregion.put("key-" + i, o);
                }
                if (localregion.size() > 0) {
                    LogWriterUtils.getLogWriter().info("CQ is ready");
                    synchronized (lockObject) {
                        lockObject.notify();
                    }
                }
                waitTillNotify(lockObject2, 20000, (EOCQListener.num_creates == 5 && EOCQListener.num_updates == 5));
                EOTracker.close();
            } catch (CqClosedException e) {
                Assert.fail("CQ", e);
            } catch (RegionNotFoundException e) {
                Assert.fail("CQ", e);
            } catch (QueryInvalidException e) {
                Assert.fail("CQ", e);
            } catch (CqExistsException e) {
                Assert.fail("CQ", e);
            } catch (CqException e) {
                Assert.fail("CQ", e);
            }
        }
    });
    server.invoke(new CacheSerializableRunnable(title + "verify Bridge Server") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            assertEquals(numberOfEntries, region.size());
            for (int i = 0; i < numberOfEntries; i++) {
                TestObject obj = (TestObject) region.getEntry("key-" + i).getValue();
                assertEquals(i, obj.getPrice());
            }
        }
    });
    // verify CQ is ready
    client1.invoke(new CacheSerializableRunnable(title + "verify CQ is ready") {

        @Override
        public void run2() throws CacheException {
            Region localregion = getRootRegion().getSubregion("localsave");
            waitTillNotify(lockObject, 10000, (localregion.size() > 0));
            assertTrue(localregion.size() > 0);
        }
    });
    // verify registerInterest result at client2
    client2.invoke(new CacheSerializableRunnable(title + "verify client2") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(regionName);
            checkRegionSize(region, numberOfEntries);
            for (int i = 0; i < numberOfEntries; i++) {
                TestObject obj = (TestObject) region.getEntry("key-" + i).getValue();
                assertEquals(i, obj.getPrice());
            }
            // then do update for key 10-20 to trigger CQ at server2
            // destroy key 10-14 to simulate create/update mix case
            region.removeAll(Arrays.asList("key-10", "key-11", "key-12", "key-13", "key-14"), "removeAllCallback");
            assertEquals(null, region.get("key-10"));
            assertEquals(null, region.get("key-11"));
            assertEquals(null, region.get("key-12"));
            assertEquals(null, region.get("key-13"));
            assertEquals(null, region.get("key-14"));
        }
    });
    // verify CQ result at client1
    client1.invoke(new CacheSerializableRunnable(title + "Verify client1") {

        @Override
        public void run2() throws CacheException {
            Region localregion = getRootRegion().getSubregion("localsave");
            for (int i = 10; i < 15; i++) {
                TestObject obj = null;
                int cnt = 0;
                while (cnt < 100) {
                    obj = (TestObject) localregion.get("key-" + i);
                    if (obj != null) {
                        // wait for the key to be destroyed
                        Wait.pause(100);
                        if (LogWriterUtils.getLogWriter().fineEnabled()) {
                            LogWriterUtils.getLogWriter().info("Waiting 100ms(" + cnt + ") for key-" + i + " to be destroyed");
                        }
                        cnt++;
                    } else {
                        break;
                    }
                }
            }
        }
    });
    client2.invoke(new CacheSerializableRunnable(title + "verify client2") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(regionName);
            LinkedHashMap map = new LinkedHashMap();
            for (int i = 10; i < 20; i++) {
                map.put("key-" + i, new TestObject(i * 10));
            }
            region.putAll(map, "putAllCallback");
        }
    });
    // verify CQ result at client1
    client1.invoke(new CacheSerializableRunnable(title + "Verify client1") {

        @Override
        public void run2() throws CacheException {
            Region localregion = getRootRegion().getSubregion("localsave");
            for (int i = 10; i < 20; i++) {
                TestObject obj = null;
                int cnt = 0;
                while (cnt < 100) {
                    obj = (TestObject) localregion.get("key-" + i);
                    if (obj == null || obj.getPrice() != i * 10) {
                        Wait.pause(100);
                        LogWriterUtils.getLogWriter().info("Waiting 100ms(" + cnt + ") for obj.getPrice() == i*10 at entry " + i);
                        cnt++;
                    } else {
                        break;
                    }
                }
                assertEquals(i * 10, obj.getPrice());
            }
            synchronized (lockObject2) {
                lockObject2.notify();
            }
        }
    });
    ThreadUtils.join(async1, 30 * 1000);
    // verify stats for client putAll into distributed region
    // 1. verify client staus
    /*
     * server2.invoke(new CacheSerializableRunnable("server2 execute putAll") { public void run2()
     * throws CacheException { try { DistributedSystemConfig config =
     * AdminDistributedSystemFactory.defineDistributedSystem(system, null); AdminDistributedSystem
     * ads = AdminDistributedSystemFactory.getDistributedSystem(config); ads.connect();
     * DistributedMember distributedMember = system.getDistributedMember(); SystemMember member =
     * ads.lookupSystemMember(distributedMember);
     * 
     * StatisticResource[] resources = member.getStats(); for (int i=0; i<resources.length; i++) {
     * System.out.println("GGG:"+resources[i].getType()); if
     * (resources[i].getType().equals("CacheServerClientStats")) { Statistic[] stats =
     * resources[i].getStatistics(); for (int j=0; i<stats.length; i++) { if
     * (stats[j].getName().equals("putAll")) {
     * System.out.println("GGG:"+stats[j].getName()+":"+stats[j].getValue()); } else if
     * (stats[j].getName().equals("sendPutAllTime")) {
     * System.out.println("GGG:"+stats[j].getName()+":"+stats[j].getValue()); } } } } } catch
     * (AdminException e) { fail("Failed while creating AdminDS", e); } } });
     */
    // Test Exception handling
    // verify CQ is ready
    client1.invoke(new CacheSerializableRunnable(title + "test exception handling") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            Map m = null;
            boolean NPEthrowed = false;
            try {
                region.putAll(m, "putAllCallback");
                fail("Should have thrown NullPointerException");
            } catch (NullPointerException ex) {
                NPEthrowed = true;
            }
            assertTrue(NPEthrowed);
            region.localDestroyRegion();
            boolean RDEthrowed = false;
            try {
                m = new HashMap();
                for (int i = 1; i < 21; i++) {
                    m.put(new Integer(i), Integer.toString(i));
                }
                region.putAll(m, "putAllCallback");
                fail("Should have thrown RegionDestroyedException");
            } catch (RegionDestroyedException ex) {
                RDEthrowed = true;
            }
            assertTrue(RDEthrowed);
            try {
                region.removeAll(Arrays.asList("key-10", "key-11"), "removeAllCallback");
                fail("Should have thrown RegionDestroyedException");
            } catch (RegionDestroyedException expected) {
            }
        }
    });
    // Stop server
    stopBridgeServers(getCache());
}
Also used : CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) CqClosedException(org.apache.geode.cache.query.CqClosedException) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) Struct(org.apache.geode.cache.query.Struct) LinkedHashMap(java.util.LinkedHashMap) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) List(java.util.List) ArrayList(java.util.ArrayList) CqQuery(org.apache.geode.cache.query.CqQuery) CqException(org.apache.geode.cache.query.CqException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) Host(org.apache.geode.test.dunit.Host) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CqAttributes(org.apache.geode.cache.query.CqAttributes) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) CqExistsException(org.apache.geode.cache.query.CqExistsException) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 3 with AsyncInvocation

use of org.apache.geode.test.dunit.AsyncInvocation in project geode by apache.

the class PutAllCSDUnitTest method testPartialKeyInPRSingleHop.

/**
   * Tests partial key putAll to 2 PR servers, because putting data at server side is different
   * between PR and LR. PR does it in postPutAll. This is a singlehop putAll test.
   */
@Test
public void testPartialKeyInPRSingleHop() throws CacheException, InterruptedException {
    final String title = "testPartialKeyInPRSingleHop_";
    final int cacheWriterAllowedKeyNum = 16;
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM client1 = host.getVM(2);
    final VM client2 = host.getVM(3);
    final String regionName = getUniqueName();
    final String serverHost = NetworkUtils.getServerHostName(server1.getHost());
    // set <true, false> means <PR=true, notifyBySubscription=false> to test local-invalidates
    final int serverPort1 = createBridgeServer(server1, regionName, 0, true, 0, "ds1");
    final int serverPort2 = createBridgeServer(server2, regionName, 0, true, 0, "ds1");
    createClient(client1, regionName, serverHost, new int[] { serverPort1, serverPort2 }, -1, -1, false, false, true, false);
    createClient(client2, regionName, serverHost, new int[] { serverPort1, serverPort2 }, -1, -1, false, false, true);
    server1.invoke(addExceptionTag1(expectedExceptions));
    server2.invoke(addExceptionTag1(expectedExceptions));
    client1.invoke(addExceptionTag1(expectedExceptions));
    client2.invoke(addExceptionTag1(expectedExceptions));
    try {
        client2.invoke(new CacheSerializableRunnable(title + "client2 add listener") {

            @Override
            public void run2() throws CacheException {
                Region region = getRootRegion().getSubregion(regionName);
                region.getAttributesMutator().addCacheListener(new MyListener(false));
                region.registerInterest("ALL_KEYS");
                LogWriterUtils.getLogWriter().info("client2 registerInterest ALL_KEYS at " + region.getFullPath());
            }
        });
        client1.invoke(new CacheSerializableRunnable(title + "do some putAll to get ClientMetaData for future putAll") {

            @Override
            public void run2() throws CacheException {
                doPutAll(regionName, "key-", numberOfEntries);
            }
        });
        WaitCriterion waitForSizes = new WaitCriterion() {

            @Override
            public String description() {
                return "waiting for conditions to be met";
            }

            @Override
            public boolean done() {
                int c1Size = getRegionSize(client1, regionName);
                int c2Size = getRegionSize(client2, regionName);
                int s1Size = getRegionSize(server1, regionName);
                int s2Size = getRegionSize(server2, regionName);
                LogWriterUtils.getLogWriter().info("region sizes: " + c1Size + "," + c2Size + "," + s1Size + "," + s2Size);
                if (c1Size != numberOfEntries) {
                    LogWriterUtils.getLogWriter().info("waiting for client1 to get all updates");
                    return false;
                }
                if (c2Size != numberOfEntries) {
                    LogWriterUtils.getLogWriter().info("waiting for client2 to get all updates");
                    return false;
                }
                if (s1Size != numberOfEntries) {
                    LogWriterUtils.getLogWriter().info("waiting for server1 to get all updates");
                    return false;
                }
                if (s2Size != numberOfEntries) {
                    LogWriterUtils.getLogWriter().info("waiting for server2 to get all updates");
                    return false;
                }
                return true;
            }
        };
        Wait.waitForCriterion(waitForSizes, 10000, 1000, true);
        server1.invoke(new CacheSerializableRunnable(title + "server1 add slow listener") {

            @Override
            public void run2() throws CacheException {
                Region region = getRootRegion().getSubregion(regionName);
                region.getAttributesMutator().addCacheListener(new MyListener(true));
            }
        });
        // add a listener that will close the cache at the 10th update
        final SharedCounter sc_server2 = new SharedCounter("server2");
        server2.invoke(new CacheSerializableRunnable(title + "server2 add slow listener") {

            @Override
            public void run2() throws CacheException {
                Region region = getRootRegion().getSubregion(regionName);
                region.getAttributesMutator().addCacheListener(new MyListener(server2, true, sc_server2, 10));
            }
        });
        AsyncInvocation async1 = client1.invokeAsync(new CacheSerializableRunnable(title + "client1 add listener and putAll") {

            @Override
            public void run2() throws CacheException {
                Region region = getRootRegion().getSubregion(regionName);
                region.getAttributesMutator().addCacheListener(new MyListener(false));
                // create keys
                try {
                    doPutAll(regionName, title, numberOfEntries);
                    fail("Expect ServerOperationException caused by PutAllParitialResultException");
                } catch (ServerOperationException soe) {
                    assertTrue(soe.getMessage().contains(LocalizedStrings.Region_PutAll_Applied_PartialKeys_At_Server_0.toLocalizedString(region.getFullPath())));
                }
            }
        });
        // server2 will closeCache after creating 10 keys
        ThreadUtils.join(async1, 30 * 1000);
        if (async1.exceptionOccurred()) {
            Assert.fail("putAll client threw an exception", async1.getException());
        }
        // restart server2
        System.out.println("restarting server 2");
        createBridgeServer(server2, regionName, serverPort2, true, 0, "ds1");
        // Test Case1: Trigger singleHop putAll. Stop server2 in middle.
        // numberOfEntries/2 + X keys will be created at servers. i.e. X keys at server2,
        // numberOfEntries/2 keys at server1.
        // The client should receive a PartialResultException due to PartitionOffline
        // close a server to re-run the test
        closeCache(server2);
        client1.invoke(new CacheSerializableRunnable(title + "client1 does putAll again") {

            @Override
            public void run2() throws CacheException {
                Region region = getRootRegion().getSubregion(regionName);
                // create keys
                try {
                    doPutAll(regionName, title + "again:", numberOfEntries);
                    fail("Expect ServerOperationException caused by PutAllParitialResultException");
                } catch (ServerOperationException soe) {
                    assertTrue(soe.getMessage().contains(LocalizedStrings.Region_PutAll_Applied_PartialKeys_At_Server_0.toLocalizedString(region.getFullPath())));
                }
            }
        });
        // Test Case 2: based on case 1, but this time, there should be no X keys
        // created on server2.
        // restart server2
        createBridgeServer(server2, regionName, serverPort2, true, 0, "ds1");
        // add a cacheWriter for server to fail putAll after it created cacheWriterAllowedKeyNum keys
        server1.invoke(new CacheSerializableRunnable(title + "server1 add cachewriter to throw exception after created some keys") {

            @Override
            public void run2() throws CacheException {
                Region region = getRootRegion().getSubregion(regionName);
                region.getAttributesMutator().setCacheWriter(new MyWriter(cacheWriterAllowedKeyNum));
            }
        });
        client1.invoke(new CacheSerializableRunnable(title + "client1 does putAll once more") {

            @Override
            public void run2() throws CacheException {
                Region region = getRootRegion().getSubregion(regionName);
                // create keys
                try {
                    doPutAll(regionName, title + "once more:", numberOfEntries);
                    fail("Expect ServerOperationException caused by PutAllParitialResultException");
                } catch (ServerOperationException soe) {
                    assertTrue(soe.getMessage().contains(LocalizedStrings.Region_PutAll_Applied_PartialKeys_At_Server_0.toLocalizedString(region.getFullPath())));
                }
            }
        });
    } finally {
        server1.invoke(removeExceptionTag1(expectedExceptions));
        server2.invoke(removeExceptionTag1(expectedExceptions));
        client1.invoke(removeExceptionTag1(expectedExceptions));
        client2.invoke(removeExceptionTag1(expectedExceptions));
        // Stop server
        stopBridgeServers(getCache());
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 4 with AsyncInvocation

use of org.apache.geode.test.dunit.AsyncInvocation in project geode by apache.

the class PutAllCSDUnitTest method testEventIdMisorderInPRSingleHop.

/**
   * Tests bug 41403: let 2 sub maps both failed with partial key applied. This is a singlehop
   * putAll test.
   */
@Test
public void testEventIdMisorderInPRSingleHop() throws CacheException, InterruptedException {
    final String title = "testEventIdMisorderInPRSingleHop_";
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM server3 = host.getVM(2);
    final VM client1 = host.getVM(3);
    final String regionName = getUniqueName();
    final int[] serverPorts = new int[3];
    final String serverHost = NetworkUtils.getServerHostName(server1.getHost());
    final SharedCounter sc_server1 = new SharedCounter("server1");
    final SharedCounter sc_server2 = new SharedCounter("server2");
    final SharedCounter sc_server3 = new SharedCounter("server3");
    final SharedCounter sc_client2 = new SharedCounter("client2");
    // set <true, false> means <PR=true, notifyBySubscription=false> to test local-invalidates
    serverPorts[0] = createBridgeServer(server1, regionName, 0, true, 0, null);
    serverPorts[1] = createBridgeServer(server2, regionName, 0, true, 0, null);
    serverPorts[2] = createBridgeServer(server3, regionName, 0, true, 0, null);
    createClient(client1, regionName, serverHost, serverPorts, -1, -1, false, true, true, false);
    {
        // Create local region
        Properties config = new Properties();
        config.setProperty(MCAST_PORT, "0");
        config.setProperty(LOCATORS, "");
        getSystem(config);
        // Create Region
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.LOCAL);
        try {
            getCache();
            ClientServerTestCase.configureConnectionPool(factory, serverHost, serverPorts, true, -1, -1, null);
            createRegion(regionName, factory.create());
            assertNotNull(getRootRegion().getSubregion(regionName));
        } catch (CacheException ex) {
            Assert.fail("While creating Region on Edge", ex);
        }
    }
    server1.invoke(addExceptionTag1(expectedExceptions));
    server2.invoke(addExceptionTag1(expectedExceptions));
    server3.invoke(addExceptionTag1(expectedExceptions));
    client1.invoke(addExceptionTag1(expectedExceptions));
    addExceptionTag1(expectedExceptions);
    client1.invoke(new CacheSerializableRunnable(title + "do some putAll to get ClientMetaData for future putAll") {

        @Override
        public void run2() throws CacheException {
            doPutAll(regionName, "key-", numberOfEntries);
        }
    });
    // register interest and add listener
    MyListener myListener = new MyListener(false, sc_client2);
    Region region = getRootRegion().getSubregion(regionName);
    region.getAttributesMutator().addCacheListener(myListener);
    region.registerInterest("ALL_KEYS");
    server1.invoke(new CacheSerializableRunnable(title + "server1 add slow listener") {

        @Override
        public void run2() throws CacheException {
            Region r = getRootRegion().getSubregion(regionName);
            r.getAttributesMutator().addCacheListener(new MyListener(server1, true, sc_server1, 10));
        }
    });
    server2.invoke(new CacheSerializableRunnable(title + "server2 add slow listener") {

        @Override
        public void run2() throws CacheException {
            Region r = getRootRegion().getSubregion(regionName);
            r.getAttributesMutator().addCacheListener(new MyListener(server2, true, sc_server2, 10));
        }
    });
    server3.invoke(new CacheSerializableRunnable(title + "server3 add slow listener") {

        @Override
        public void run2() throws CacheException {
            Region r = getRootRegion().getSubregion(regionName);
            r.getAttributesMutator().addCacheListener(new MyListener(true, sc_server3));
        }
    });
    int client1Size = getRegionSize(client1, regionName);
    int server1Size = getRegionSize(server1, regionName);
    int server2Size = getRegionSize(server2, regionName);
    int server3Size = getRegionSize(server2, regionName);
    LogWriterUtils.getLogWriter().info("region sizes: " + client1Size + "," + server1Size + "," + server2Size + "," + server3Size);
    AsyncInvocation async1 = client1.invokeAsync(new CacheSerializableRunnable(title + "client1 add listener and putAll") {

        @Override
        public void run2() throws CacheException {
            Region r = getRootRegion().getSubregion(regionName);
            r.getAttributesMutator().addCacheListener(new MyListener(false));
            doPutAll(regionName, title, numberOfEntries);
        }
    });
    // server1 and server2 will closeCache after created 10 keys
    ThreadUtils.join(async1, 30 * 1000);
    if (async1.exceptionOccurred()) {
        Assert.fail("Aync1 get exceptions:", async1.getException());
    }
    server3.invoke(new CacheSerializableRunnable(title + "server3 print counter") {

        @Override
        public void run2() throws CacheException {
            Region r = getRootRegion().getSubregion(regionName);
            MyListener l = (MyListener) r.getAttributes().getCacheListeners()[0];
            LogWriterUtils.getLogWriter().info("event counters : " + l.sc);
            assertEquals(numberOfEntries, l.sc.num_create_event);
            assertEquals(0, l.sc.num_update_event);
        }
    });
    LogWriterUtils.getLogWriter().info("event counters : " + myListener.sc);
    assertEquals(numberOfEntries, myListener.sc.num_create_event);
    assertEquals(0, myListener.sc.num_update_event);
    server1.invoke(removeExceptionTag1(expectedExceptions));
    server2.invoke(removeExceptionTag1(expectedExceptions));
    server3.invoke(removeExceptionTag1(expectedExceptions));
    client1.invoke(removeExceptionTag1(expectedExceptions));
    removeExceptionTag1(expectedExceptions);
    // Stop server
    stopBridgeServers(getCache());
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 5 with AsyncInvocation

use of org.apache.geode.test.dunit.AsyncInvocation in project geode by apache.

the class PutAllCSDUnitTest method testPartialKeyInPRSingleHopWithRedundency.

/**
   * Set redundancy=1 to see if retry succeeded after PRE This is a singlehop putAll test.
   */
@Test
public void testPartialKeyInPRSingleHopWithRedundency() throws CacheException, InterruptedException {
    final String title = "testPartialKeyInPRSingleHopWithRedundency_";
    int client1Size;
    int client2Size;
    int server1Size;
    int server2Size;
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM server2 = host.getVM(1);
    final VM client1 = host.getVM(2);
    final VM client2 = host.getVM(3);
    final String regionName = getUniqueName();
    final String serverHost = NetworkUtils.getServerHostName(server1.getHost());
    // set <true, false> means <PR=true, notifyBySubscription=false> to test local-invalidates
    final int serverPort1 = createBridgeServer(server1, regionName, 0, true, 1, "ds1");
    final int serverPort2 = createBridgeServer(server2, regionName, 0, true, 1, "ds1");
    createClient(client1, regionName, serverHost, new int[] { serverPort1, serverPort2 }, -1, -1, false, false, true, false);
    createClient(client2, regionName, serverHost, new int[] { serverPort1, serverPort2 }, -1, -1, false, false, true);
    server1.invoke(addExceptionTag1(expectedExceptions));
    server2.invoke(addExceptionTag1(expectedExceptions));
    client1.invoke(addExceptionTag1(expectedExceptions));
    client2.invoke(addExceptionTag1(expectedExceptions));
    client2.invoke(new CacheSerializableRunnable(title + "client2 add listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
            region.registerInterest("ALL_KEYS");
            LogWriterUtils.getLogWriter().info("client2 registerInterest ALL_KEYS at " + region.getFullPath());
        }
    });
    client1.invoke(new CacheSerializableRunnable(title + "do some putAll to get ClientMetaData for future putAll") {

        @Override
        public void run2() throws CacheException {
            doPutAll(regionName, "key-", numberOfEntries);
        }
    });
    WaitCriterion waitForSizes = new WaitCriterion() {

        @Override
        public String description() {
            return "waiting for conditions to be met";
        }

        @Override
        public boolean done() {
            int c1Size = getRegionSize(client1, regionName);
            int c2Size = getRegionSize(client2, regionName);
            int s1Size = getRegionSize(server1, regionName);
            int s2Size = getRegionSize(server2, regionName);
            LogWriterUtils.getLogWriter().info("region sizes: " + c1Size + "," + c2Size + "," + s1Size + "," + s2Size);
            if (c1Size != numberOfEntries) {
                LogWriterUtils.getLogWriter().info("waiting for client1 to get all updates");
                return false;
            }
            if (c2Size != numberOfEntries) {
                LogWriterUtils.getLogWriter().info("waiting for client2 to get all updates");
                return false;
            }
            if (s1Size != numberOfEntries) {
                LogWriterUtils.getLogWriter().info("waiting for server1 to get all updates");
                return false;
            }
            if (s2Size != numberOfEntries) {
                LogWriterUtils.getLogWriter().info("waiting for server2 to get all updates");
                return false;
            }
            return true;
        }
    };
    Wait.waitForCriterion(waitForSizes, 10000, 1000, true);
    client1Size = getRegionSize(client1, regionName);
    client2Size = getRegionSize(client2, regionName);
    server1Size = getRegionSize(server1, regionName);
    server2Size = getRegionSize(server2, regionName);
    server1.invoke(new CacheSerializableRunnable(title + "server1 add slow listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(true));
        }
    });
    final SharedCounter sc_server2 = new SharedCounter("server2");
    server2.invoke(new CacheSerializableRunnable(title + "server2 add slow listener") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(server2, true, sc_server2, 10));
        }
    });
    AsyncInvocation async1 = client1.invokeAsync(new CacheSerializableRunnable(title + "client1 add listener and putAll") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            region.getAttributesMutator().addCacheListener(new MyListener(false));
            region.registerInterest("ALL_KEYS");
            // create keys
            doPutAll(regionName, title, numberOfEntries);
        }
    });
    // server2 will closeCache after created 10 keys
    ThreadUtils.join(async1, 30 * 1000);
    if (async1.exceptionOccurred()) {
        Assert.fail("Aync1 get exceptions:", async1.getException());
    }
    client1Size = getRegionSize(client1, regionName);
    // client2Size maybe more than client1Size
    client2Size = getRegionSize(client2, regionName);
    server1Size = getRegionSize(server1, regionName);
    // putAll should succeed after retry
    LogWriterUtils.getLogWriter().info("region sizes: " + client1Size + "," + client2Size + "," + server1Size);
    assertEquals(server1Size, client1Size);
    assertEquals(server1Size, client2Size);
    // restart server2
    createBridgeServer(server2, regionName, serverPort2, true, 1, "ds1");
    server1Size = getRegionSize(server1, regionName);
    server2Size = getRegionSize(server2, regionName);
    LogWriterUtils.getLogWriter().info("region sizes after server2 restarted: " + client1Size + "," + client2Size + "," + server1Size);
    assertEquals(client2Size, server1Size);
    assertEquals(client2Size, server2Size);
    // close a server to re-run the test
    closeCache(server2);
    server1Size = getRegionSize(server1, regionName);
    client1.invoke(new CacheSerializableRunnable(title + "client1 does putAll again") {

        @Override
        public void run2() throws CacheException {
            doPutAll(regionName, title + "again:", numberOfEntries);
        }
    });
    int new_server1Size = getRegionSize(server1, regionName);
    int new_client1Size = getRegionSize(client1, regionName);
    int new_client2Size = getRegionSize(client2, regionName);
    // putAll should succeed, all the numbers should match
    LogWriterUtils.getLogWriter().info("region sizes after re-run the putAll: " + new_client1Size + "," + new_client2Size + "," + new_server1Size);
    assertEquals(new_server1Size, new_client1Size);
    assertEquals(new_server1Size, new_client2Size);
    // Stop server
    stopBridgeServers(getCache());
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) 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) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Aggregations

AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)314 Test (org.junit.Test)280 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)275 VM (org.apache.geode.test.dunit.VM)186 Host (org.apache.geode.test.dunit.Host)175 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)139 Region (org.apache.geode.cache.Region)87 CacheException (org.apache.geode.cache.CacheException)65 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)65 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)61 Cache (org.apache.geode.cache.Cache)44 AttributesFactory (org.apache.geode.cache.AttributesFactory)41 IgnoredException (org.apache.geode.test.dunit.IgnoredException)35 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)28 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)26 LocalRegion (org.apache.geode.internal.cache.LocalRegion)26 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)25 Ignore (org.junit.Ignore)25 Category (org.junit.experimental.categories.Category)25