Search in sources :

Example 76 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class SelectStarQueryDUnitTest method testSelectStarQueryForPdxObjectsReadSerializedTrue.

@Test
public void testSelectStarQueryForPdxObjectsReadSerializedTrue() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM client = host.getVM(3);
    // create servers and regions
    final int port = (Integer) server1.invoke(new SerializableCallable("Create Server1") {

        @Override
        public Object call() throws Exception {
            ((GemFireCacheImpl) getCache()).setReadSerialized(true);
            Region r1 = getCache().createRegionFactory(RegionShortcut.REPLICATE).create(regName);
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
            server.setPort(port);
            server.start();
            return port;
        }
    });
    // create client
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(getServerHostName(server1.getHost()), port);
            ClientCache cache = getClientCache(cf);
            cache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regName);
            Region r1 = getRootRegion(regName);
            for (int i = 0; i < 20; i++) {
                r1.put("key-" + i, new PortfolioPdx(i));
            }
            return null;
        }
    });
    // query remotely from client
    client.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            getLogWriter().info("Querying remotely from client");
            QueryService localQS = null;
            QueryService remoteQS = null;
            try {
                localQS = ((ClientCache) getCache()).getLocalQueryService();
                remoteQS = ((ClientCache) getCache()).getQueryService();
            } catch (Exception e) {
                fail("Exception getting query service ", e);
            }
            SelectResults res = null;
            SelectResults[][] sr = new SelectResults[1][2];
            for (int i = 0; i < queries.length; i++) {
                try {
                    res = (SelectResults) localQS.newQuery(queries[i]).execute();
                    sr[0][0] = res;
                    res = (SelectResults) remoteQS.newQuery(queries[i]).execute();
                    sr[0][1] = res;
                    CacheUtils.compareResultsOfWithAndWithoutIndex(sr);
                } catch (Exception e) {
                    fail("Error executing query: " + queries[i], e);
                }
                assertEquals(resultSize[i], res.size());
                if (i == 3) {
                    int cnt = ((Integer) res.iterator().next());
                    assertEquals(20, cnt);
                } else {
                    for (Object rs : res) {
                        if (rs instanceof StructImpl) {
                            for (Object obj : ((StructImpl) rs).getFieldValues()) {
                                if (obj instanceof PortfolioPdx || obj instanceof PositionPdx) {
                                } else {
                                    fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + obj.getClass());
                                }
                            }
                        } else if (rs instanceof PortfolioPdx) {
                        } else {
                            fail("Result objects for remote client query: " + queries[i] + " should be instance of PortfolioPdx and not " + rs.getClass());
                        }
                    }
                }
            }
            return null;
        }
    });
    closeCache(client);
    closeCache(server1);
}
Also used : PositionPdx(org.apache.geode.cache.query.data.PositionPdx) Host(org.apache.geode.test.dunit.Host) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) ClientCache(org.apache.geode.cache.client.ClientCache) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SelectResults(org.apache.geode.cache.query.SelectResults) StructImpl(org.apache.geode.cache.query.internal.StructImpl) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 77 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class SelectStarQueryDUnitTest method functionWithStructTypeInInnerQueryShouldNotThrowException.

@Test
public void functionWithStructTypeInInnerQueryShouldNotThrowException() throws Exception {
    final Host host = Host.getHost(0);
    final VM server1 = host.getVM(0);
    final VM client = host.getVM(3);
    PortfolioPdx[] portfolios = new PortfolioPdx[10];
    for (int i = 0; i < portfolios.length; i++) {
        portfolios[i] = new PortfolioPdx(i);
    }
    // create servers and regions
    final int port1 = startPartitionedCacheServer(server1, portfolios);
    // create client
    client.invoke(new SerializableCallable("Create client") {

        @Override
        public Object call() throws Exception {
            ClientCacheFactory cf = new ClientCacheFactory();
            cf.addPoolServer(getServerHostName(server1.getHost()), port1);
            ClientCache cache = getClientCache(cf);
            cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regName);
            return null;
        }
    });
    // put serialized PortfolioPdx objects
    client.invoke(new SerializableCallable("Put objects") {

        @Override
        public Object call() throws Exception {
            Region r1 = getRootRegion(regName);
            for (int i = 10; i < 100; i++) {
                r1.put("key-" + i, new PortfolioPdx(i));
            }
            return null;
        }
    });
    // query remotely from client
    client.invoke(new SerializableCallable("Query") {

        @Override
        public Object call() throws Exception {
            getLogWriter().info("Querying remotely from client");
            QueryService remoteQS = null;
            try {
                remoteQS = ((ClientCache) getCache()).getQueryService();
                SelectResults sr = (SelectResults) remoteQS.newQuery("select distinct oP.ID, oP.status, oP.getType from /" + regName + " oP where element(select distinct p.ID, p.status, p.getType from /" + regName + " p where p.ID = oP.ID).status = 'inactive'").execute();
                assertEquals(50, sr.size());
            } catch (Exception e) {
                fail("Exception getting query service ", e);
            }
            return null;
        }
    });
    closeCache(client);
    closeCache(server1);
}
Also used : Host(org.apache.geode.test.dunit.Host) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) ClientCache(org.apache.geode.cache.client.ClientCache) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) SelectResults(org.apache.geode.cache.query.SelectResults) 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) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 78 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class ConcurrentIndexInitOnOverflowRegionDUnitTest method testAsyncIndexInitDuringEntryPutUsingClientOnRR.

/**
  *
  */
@Test
public void testAsyncIndexInitDuringEntryPutUsingClientOnRR() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    IgnoredException.addIgnoredException("Unexpected IOException:");
    IgnoredException.addIgnoredException("java.net.SocketException");
    name = "PartionedPortfoliosPR";
    // Create Overflow Persistent Partition Region
    vm0.invoke(new CacheSerializableRunnable("Create local region with synchronous index maintenance") {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region partitionRegion = null;
            IndexManager.testHook = null;
            try {
                CacheServer bridge = cache.addCacheServer();
                bridge.setPort(0);
                bridge.start();
                bridgeServerPort = bridge.getPort();
                DiskStore ds = cache.findDiskStore("disk");
                if (ds == null) {
                    ds = cache.createDiskStoreFactory().setDiskDirs(getDiskDirs()).create("disk");
                }
                AttributesFactory attr = new AttributesFactory();
                attr.setValueConstraint(PortfolioData.class);
                attr.setIndexMaintenanceSynchronous(true);
                EvictionAttributesImpl evicAttr = new EvictionAttributesImpl().setAction(EvictionAction.OVERFLOW_TO_DISK);
                evicAttr.setAlgorithm(EvictionAlgorithm.LRU_ENTRY).setMaximum(1);
                attr.setEvictionAttributes(evicAttr);
                attr.setDataPolicy(DataPolicy.REPLICATE);
                // attr.setPartitionAttributes(new
                // PartitionAttributesFactory().setTotalNumBuckets(1).create());
                attr.setDiskStoreName("disk");
                RegionFactory regionFactory = cache.createRegionFactory(attr.create());
                partitionRegion = regionFactory.create(name);
            } catch (IllegalStateException ex) {
                LogWriterUtils.getLogWriter().warning("Creation caught IllegalStateException", ex);
            } catch (IOException e) {
                e.printStackTrace();
            }
            assertNotNull("Region " + name + " not in cache", cache.getRegion(name));
            assertNotNull("Region ref null", partitionRegion);
            assertTrue("Region ref claims to be destroyed", !partitionRegion.isDestroyed());
            // Create Indexes
            try {
                Index index = cache.getQueryService().createIndex("idIndex", "p.ID", "/" + name + " p");
                assertNotNull(index);
            } catch (Exception e1) {
                e1.printStackTrace();
                fail("Index creation failed");
            }
        }
    });
    final int port = vm0.invoke(() -> ConcurrentIndexInitOnOverflowRegionDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Start changing the value in Region which should turn into a deadlock if
    // the fix is not there
    vm1.invoke(new CacheSerializableRunnable("Change value in region") {

        @Override
        public void run2() throws CacheException {
            disconnectFromDS();
            ClientCache clientCache = new ClientCacheFactory().addPoolServer(host0, port).create();
            // Do a put in region.
            Region r = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(name);
            for (int i = 0; i < 100; i++) {
                r.put(i, new PortfolioData(i));
            }
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Set Test Hook") {

        @Override
        public void run2() throws CacheException {
            // Set test hook before client operation
            assertNull(IndexManager.testHook);
            IndexManager.testHook = new IndexManagerTestHook();
        }
    });
    AsyncInvocation asyncInv1 = vm1.invokeAsync(new CacheSerializableRunnable("Change value in region") {

        @Override
        public void run2() throws CacheException {
            ClientCache clientCache = ClientCacheFactory.getAnyInstance();
            // Do a put in region.
            Region r = clientCache.getRegion(name);
            // Destroy one of the values.
            clientCache.getLogger().fine("Destroying the value");
            r.destroy(1);
        }
    });
    AsyncInvocation asyncInv2 = vm0.invokeAsync(new CacheSerializableRunnable("Run query on region") {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            while (!hooked) {
                Wait.pause(100);
            }
            // Create Indexes
            try {
                Index index = cache.getQueryService().createIndex("statusIndex", "p.status", "/" + name + " p");
                assertNotNull(index);
            } catch (Exception e1) {
                e1.printStackTrace();
                fail("Index creation failed");
            }
        }
    });
    // If we take more than 30 seconds then its a deadlock.
    ThreadUtils.join(asyncInv2, 30 * 1000);
    ThreadUtils.join(asyncInv1, 30 * 1000);
    vm0.invoke(new CacheSerializableRunnable("Set Test Hook") {

        @Override
        public void run2() throws CacheException {
            assertNotNull(IndexManager.testHook);
            IndexManager.testHook = null;
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) IOException(java.io.IOException) ClientCache(org.apache.geode.cache.client.ClientCache) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) DiskStore(org.apache.geode.cache.DiskStore) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) RegionFactory(org.apache.geode.cache.RegionFactory) EvictionAttributesImpl(org.apache.geode.internal.cache.EvictionAttributesImpl) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) PortfolioData(org.apache.geode.cache.query.data.PortfolioData) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 79 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class ProductUseLogDUnitTest method testMembershipMonitoring.

@Test
public void testMembershipMonitoring() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    // use a locator so we will monitor server load and record member->server mappings
    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    Properties p = new Properties();
    p.put(START_LOCATOR, "localhost[" + locatorPort + "],peer=false");
    p.put(USE_CLUSTER_CONFIGURATION, "false");
    InternalDistributedSystem system = getSystem(p);
    InternalLocator locator = (InternalLocator) Locator.getLocator();
    // server location is forced on for product use logging
    assertTrue(locator.isServerLocator());
    File logFile = new File("locator" + locatorPort + "views.log");
    // the locator should have already created this file
    assertTrue(logFile.exists());
    assertTrue(logFile.exists());
    int serverPort = (Integer) vm0.invoke(new SerializableCallable("get system") {

        public Object call() {
            getSystem();
            Cache cache = getCache();
            cache.createRegionFactory(RegionShortcut.REPLICATE).create("myregion");
            CacheServer server = cache.addCacheServer();
            server.setPort(0);
            try {
                server.start();
            } catch (IOException e) {
                Assert.fail("failed to start server", e);
            }
            return server.getPort();
        }
    });
    vm1.invoke(new SerializableRunnable("create a client") {

        public void run() {
            ClientCache clientCache = new ClientCacheFactory().setPoolSubscriptionEnabled(true).addPoolServer("localhost", serverPort).create();
            Region r = clientCache.createClientRegionFactory(ClientRegionShortcut.PROXY).create("myregion");
            r.registerInterest(".*");
            r.put("somekey", "somevalue");
        }
    });
    vm0.invoke(new SerializableRunnable("check region") {

        public void run() {
            Region r = getCache().getRegion("myregion");
            Assert.assertNotNull(r.get("somekey"));
        }
    });
    // wait for the server info to be received and logged
    Thread.sleep(2 * CacheServer.DEFAULT_LOAD_POLL_INTERVAL);
    system.disconnect();
    String logContents = readFile(logFile);
    assertTrue("expected " + logFile + " to contain a View", logContents.contains("View"));
    assertTrue("expected " + logFile + " to have a server count of 1", logContents.contains("server count: 1"));
    assertTrue("expected " + logFile + " to have a client count of 1", logContents.contains("client count: 1"));
    assertTrue("expected " + logFile + " to have a queue count of 1", logContents.contains("queue count: 1"));
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) ClientCache(org.apache.geode.cache.client.ClientCache) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheServer(org.apache.geode.cache.server.CacheServer) Region(org.apache.geode.cache.Region) File(java.io.File) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 80 with ClientCacheFactory

use of org.apache.geode.cache.client.ClientCacheFactory in project geode by apache.

the class PutAllWithIndexPerfDUnitTest method testPutAllWithIndexes.

@Ignore("TODO: test is disabled")
@Test
public void testPutAllWithIndexes() {
    final String name = "testRegion";
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final int numberOfEntries = 10000;
    // Start server
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Properties config = new Properties();
            config.put(LOCATORS, "localhost[" + DistributedTestUtils.getDUnitLocatorPort() + "]");
            Cache cache = new CacheFactory(config).create();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            cache.createRegionFactory(factory.create()).create(name);
            try {
                startBridgeServer(0, false);
            } catch (Exception ex) {
                Assert.fail("While starting CacheServer", ex);
            }
            // Create Index on empty region
            try {
                cache.getQueryService().createIndex("idIndex", "ID", "/" + name);
            } catch (Exception e) {
                Assert.fail("index creation failed", e);
            }
        }
    });
    // Create client region
    final int port = vm0.invoke(() -> PutAllWithIndexPerfDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    vm1.invoke(new CacheSerializableRunnable("Create region") {

        public void run2() throws CacheException {
            Properties config = new Properties();
            config.setProperty(MCAST_PORT, "0");
            ClientCache cache = new ClientCacheFactory().addPoolServer(host0, port).create();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(name);
        }
    });
    vm1.invoke(new CacheSerializableRunnable("putAll() test") {

        @Override
        public void run2() throws CacheException {
            Region exampleRegion = ClientCacheFactory.getAnyInstance().getRegion(name);
            Map warmupMap = new HashMap();
            Map data = new HashMap();
            for (int i = 0; i < 10000; i++) {
                Object p = new PortfolioPdx(i);
                if (i < 1000)
                    warmupMap.put(i, p);
                data.put(i, p);
            }
            for (int i = 0; i < 10; i++) {
                exampleRegion.putAll(warmupMap);
            }
            long start = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                exampleRegion.putAll(data);
            }
            long end = System.currentTimeMillis();
            timeWithoutStructTypeIndex = ((end - start) / 10);
            System.out.println("Total putall time for 10000 objects is: " + ((end - start) / 10) + "ms");
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Remove Index and create new one") {

        @Override
        public void run2() throws CacheException {
            try {
                Cache cache = CacheFactory.getAnyInstance();
                cache.getQueryService().removeIndexes();
                cache.getRegion(name).clear();
                cache.getQueryService().createIndex("idIndex", "p.ID", "/" + name + " p");
            } catch (Exception e) {
                Assert.fail("index creation failed", e);
            }
        }
    });
    vm1.invoke(new CacheSerializableRunnable("putAll() test") {

        @Override
        public void run2() throws CacheException {
            Region exampleRegion = ClientCacheFactory.getAnyInstance().getRegion(name);
            exampleRegion.clear();
            Map warmupMap = new HashMap();
            Map data = new HashMap();
            for (int i = 0; i < 10000; i++) {
                Object p = new PortfolioPdx(i);
                if (i < 1000)
                    warmupMap.put(i, p);
                data.put(i, p);
            }
            for (int i = 0; i < 10; i++) {
                exampleRegion.putAll(warmupMap);
            }
            long start = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                exampleRegion.putAll(data);
            }
            long end = System.currentTimeMillis();
            timeWithStructTypeIndex = ((end - start) / 10);
            System.out.println("Total putall time for 10000 objects is: " + ((end - start) / 10) + "ms");
        }
    });
    if (timeWithoutStructTypeIndex > timeWithStructTypeIndex) {
        fail("putAll took more time without struct type index than simple index");
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) Host(org.apache.geode.test.dunit.Host) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) ClientCache(org.apache.geode.cache.client.ClientCache) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) HashMap(java.util.HashMap) Map(java.util.Map) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)87 ClientCache (org.apache.geode.cache.client.ClientCache)65 Test (org.junit.Test)45 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)44 VM (org.apache.geode.test.dunit.VM)42 Host (org.apache.geode.test.dunit.Host)40 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)40 Region (org.apache.geode.cache.Region)37 Properties (java.util.Properties)23 QueryService (org.apache.geode.cache.query.QueryService)22 SelectResults (org.apache.geode.cache.query.SelectResults)22 CacheServer (org.apache.geode.cache.server.CacheServer)22 Cache (org.apache.geode.cache.Cache)19 CacheException (org.apache.geode.cache.CacheException)14 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)13 IOException (java.io.IOException)12 IgnoredException (org.apache.geode.test.dunit.IgnoredException)12 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)11 PdxInstance (org.apache.geode.pdx.PdxInstance)10