Search in sources :

Example 1 with TypeRegistration

use of org.apache.geode.pdx.internal.TypeRegistration in project geode by apache.

the class PdxQueryDUnitTest method testLocalMapInPeerTypePdxRegistry.

/**
   * In PeerTypeRegistration when a PdxType is updated, a local map of class => PdxTypes is
   * populated. This map is used to search a field for a class in different versions (PdxTypes) This
   * test verifies that the map is being updated by the cacheListener
   *
   * @throws CacheException
   */
@Test
public void testLocalMapInPeerTypePdxRegistry() throws CacheException {
    final Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    final VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    final String name = "/" + regionName;
    final String[] qs = { "select * from " + name + " where pdxStatus = 'active'", "select pdxStatus from " + name + " where id > 4" };
    // Start server1
    final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // Start server2
    final int port2 = (Integer) vm1.invoke(new SerializableCallable("Create Server2") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // client1 loads version 1 objects on server1
    vm2.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
            // Load version 1 objects
            for (int i = 0; i < numberOfEntries; i++) {
                PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedNewPortfolio", false);
                pdxInstanceFactory.writeInt("id", i);
                pdxInstanceFactory.writeString("pdxStatus", (i % 2 == 0 ? "active" : "inactive"));
                PdxInstance pdxInstance = pdxInstanceFactory.create();
                region.put("key-" + i, pdxInstance);
            }
            return null;
        }
    });
    // client 2 loads version 2 objects on server2
    vm3.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(vm1.getHost()), port2);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
            // Load version 2 objects
            for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
                PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PdxVersionedNewPortfolio", false);
                pdxInstanceFactory.writeInt("id", i);
                pdxInstanceFactory.writeString("status", (i % 2 == 0 ? "active" : "inactive"));
                PdxInstance pdxInstance = pdxInstanceFactory.create();
                region.put("key-" + i, pdxInstance);
            }
            return null;
        }
    });
    // on server 1 verify local map in PeerTypeRegistration has fields
    vm0.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            TypeRegistration registration = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.").getPdxRegistry().getTypeRegistration();
            Assert.assertTrue(registration instanceof PeerTypeRegistration);
            Map<String, Set<PdxType>> m = ((PeerTypeRegistration) registration).getClassToType();
            assertEquals(1, m.size());
            assertEquals("PdxVersionedNewPortfolio", m.keySet().iterator().next());
            assertEquals(2, m.values().iterator().next().size());
            for (PdxType p : m.values().iterator().next()) {
                assertEquals("PdxVersionedNewPortfolio", p.getClassName());
            }
            return null;
        }
    });
    // on server 2 verify local map in PeerTypeRegistration has fields
    vm1.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            TypeRegistration registration = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.").getPdxRegistry().getTypeRegistration();
            Assert.assertTrue(registration instanceof PeerTypeRegistration);
            Map<String, Set<PdxType>> m = ((PeerTypeRegistration) registration).getClassToType();
            assertEquals(1, m.size());
            assertEquals("PdxVersionedNewPortfolio", m.keySet().iterator().next());
            assertEquals(2, m.values().iterator().next().size());
            for (PdxType p : m.values().iterator().next()) {
                assertEquals("PdxVersionedNewPortfolio", p.getClassName());
            }
            return null;
        }
    });
    Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxType(org.apache.geode.pdx.internal.PdxType) PeerTypeRegistration(org.apache.geode.pdx.internal.PeerTypeRegistration) ClientTypeRegistration(org.apache.geode.pdx.internal.ClientTypeRegistration) TypeRegistration(org.apache.geode.pdx.internal.TypeRegistration) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) PeerTypeRegistration(org.apache.geode.pdx.internal.PeerTypeRegistration) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) Map(java.util.Map) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 2 with TypeRegistration

use of org.apache.geode.pdx.internal.TypeRegistration in project geode by apache.

the class PdxQueryDUnitTest method testClientForFieldInOtherVersion.

/**
   * 2 servers(replicated) and 2 clients. client2 puts version1 and version2 objects on server1
   * client1 had registered interest to server2, hence gets the pdx objects for both versions Test
   * local query on client1 Test if client1 fetched pdxtypes from server
   * 
   * @throws CacheException
   */
@Test
public void testClientForFieldInOtherVersion() throws CacheException {
    final Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    final VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    final String name = "/" + regionName;
    final String[] qs = { "select pdxStatus from " + name + " where pdxStatus = 'active'", "select status from " + name + " where id > 8 and id < 14" };
    // Start server1
    final int port1 = (Integer) vm0.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // Start server2
    final int port2 = (Integer) vm1.invoke(new SerializableCallable("Create Server2") {

        @Override
        public Object call() throws Exception {
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // client 1 registers interest for server2
    vm2.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.setPoolSubscriptionEnabled(true);
            cf.addPoolServer(NetworkUtils.getServerHostName(vm1.getHost()), port2);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
            region.registerInterest("ALL_KEYS");
            return null;
        }
    });
    // client2 loads both version objects on server1
    vm3.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(NetworkUtils.getServerHostName(vm0.getHost()), port1);
            ClientCache cache = getClientCache(cf);
            Region region = cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regionName);
            // Load version 1 objects
            for (int i = 0; i < numberOfEntries; i++) {
                PdxInstanceFactory pdxFactory = cache.createPdxInstanceFactory("PdxPortfolio");
                pdxFactory.writeString("pdxStatus", (i % 2 == 0 ? "active" : "inactive"));
                pdxFactory.writeInt("id", i);
                PdxInstance pdxInstance = pdxFactory.create();
                region.put("key-" + i, pdxInstance);
            }
            ;
            // Load version 2 objects
            for (int i = numberOfEntries; i < numberOfEntries * 2; i++) {
                PdxInstanceFactory pdxFactory = cache.createPdxInstanceFactory("PdxPortfolio");
                pdxFactory.writeString("status", i % 2 == 0 ? "active" : "inactive");
                pdxFactory.writeInt("id", i);
                PdxInstance pdxInstance = pdxFactory.create();
                region.put("key-" + i, pdxInstance);
            }
            return null;
        }
    });
    // query locally on client 1 which has registered interest
    vm2.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
            cache.setReadSerialized(true);
            QueryService localQueryService = null;
            // Execute query remotely
            try {
                localQueryService = ((ClientCache) getCache()).getLocalQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            for (int i = 0; i < qs.length; i++) {
                try {
                    SelectResults sr = (SelectResults) localQueryService.newQuery(qs[i]).execute();
                    assertEquals(5, sr.size());
                    if (i == 1) {
                        for (Object o : sr) {
                            if (o == null) {
                            } else if (o instanceof String) {
                            } else {
                                fail("Result should be either null or String and not " + o.getClass());
                            }
                        }
                    }
                } catch (Exception e) {
                    Assert.fail("Failed executing " + qs[i], e);
                }
            }
            // check if the types registered on server are fetched by the client
            TypeRegistration registration = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.").getPdxRegistry().getTypeRegistration();
            Assert.assertTrue(registration instanceof ClientTypeRegistration);
            Map<Integer, PdxType> m = ((ClientTypeRegistration) registration).types();
            assertEquals(2, m.size());
            for (PdxType type : m.values()) {
                assertEquals("PdxPortfolio", type.getClassName());
            }
            return null;
        }
    });
    Invoke.invokeInEveryVM("Disconnecting from the Distributed system", () -> disconnectFromDS());
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxType(org.apache.geode.pdx.internal.PdxType) PeerTypeRegistration(org.apache.geode.pdx.internal.PeerTypeRegistration) ClientTypeRegistration(org.apache.geode.pdx.internal.ClientTypeRegistration) TypeRegistration(org.apache.geode.pdx.internal.TypeRegistration) Host(org.apache.geode.test.dunit.Host) ClientCache(org.apache.geode.cache.client.ClientCache) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SelectResults(org.apache.geode.cache.query.SelectResults) PdxInstance(org.apache.geode.pdx.PdxInstance) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) ClientTypeRegistration(org.apache.geode.pdx.internal.ClientTypeRegistration) Map(java.util.Map) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

Map (java.util.Map)2 CacheException (org.apache.geode.cache.CacheException)2 Region (org.apache.geode.cache.Region)2 ClientCache (org.apache.geode.cache.client.ClientCache)2 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)2 CacheServer (org.apache.geode.cache.server.CacheServer)2 PdxInstance (org.apache.geode.pdx.PdxInstance)2 PdxInstanceFactory (org.apache.geode.pdx.PdxInstanceFactory)2 ClientTypeRegistration (org.apache.geode.pdx.internal.ClientTypeRegistration)2 PdxType (org.apache.geode.pdx.internal.PdxType)2 PeerTypeRegistration (org.apache.geode.pdx.internal.PeerTypeRegistration)2 TypeRegistration (org.apache.geode.pdx.internal.TypeRegistration)2 Host (org.apache.geode.test.dunit.Host)2 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)2 VM (org.apache.geode.test.dunit.VM)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 Test (org.junit.Test)2 QueryService (org.apache.geode.cache.query.QueryService)1 SelectResults (org.apache.geode.cache.query.SelectResults)1 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)1