Search in sources :

Example 96 with AttributesFactory

use of org.apache.geode.cache.AttributesFactory in project geode by apache.

the class CacheUtils method createRegion.

public static Region createRegion(String regionName, Class valueConstraint, boolean indexMaintenanceSynchronous) {
    try {
        AttributesFactory attributesFactory = new AttributesFactory();
        attributesFactory.setValueConstraint(valueConstraint);
        attributesFactory.setIndexMaintenanceSynchronous(indexMaintenanceSynchronous);
        RegionAttributes regionAttributes = attributesFactory.create();
        return cache.createRegion(regionName, regionAttributes);
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) RegionExistsException(org.apache.geode.cache.RegionExistsException) TimeoutException(org.apache.geode.cache.TimeoutException) GatewayException(org.apache.geode.cache.GatewayException) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 97 with AttributesFactory

use of org.apache.geode.cache.AttributesFactory in project geode by apache.

the class CacheUtils method createRegion.

public static Region createRegion(Region parentRegion, String regionName, Class valueConstraint) {
    try {
        AttributesFactory attributesFactory = new AttributesFactory();
        if (valueConstraint != null)
            attributesFactory.setValueConstraint(valueConstraint);
        RegionAttributes regionAttributes = attributesFactory.create();
        return parentRegion.createSubregion(regionName, regionAttributes);
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) RegionExistsException(org.apache.geode.cache.RegionExistsException) TimeoutException(org.apache.geode.cache.TimeoutException) GatewayException(org.apache.geode.cache.GatewayException) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 98 with AttributesFactory

use of org.apache.geode.cache.AttributesFactory in project geode by apache.

the class QueryJUnitTest method test007UndefinedResults.

@Test
public void test007UndefinedResults() {
    CacheUtils.log("testQueryExceptionLogMessage");
    Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
    region.put("1", new Portfolio(1));
    region.put("2", new Portfolio(0));
    String queryStr = "SELECT DISTINCT * FROM /Portfolios.ketset";
    Query q = CacheUtils.getQueryService().newQuery(queryStr);
    Object results = null;
    try {
        results = q.execute();
    } catch (Exception e) {
        fail("Query execution failed " + e);
    }
    assertEquals(0, ((SelectResults) results).size());
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    AttributesFactory af = new AttributesFactory();
    af.setPartitionAttributes(paf.create());
    region = CacheUtils.createRegion("PortfoliosPR", af.create(), false);
    region.put("1", new Portfolio(1));
    region.put("2", new Portfolio(0));
    queryStr = "SELECT DISTINCT * FROM /PortfoliosPR.ketset";
    q = CacheUtils.getQueryService().newQuery(queryStr);
    try {
        results = q.execute();
    } catch (Exception e) {
        fail("Query execution failed " + e);
    }
    assertEquals(0, ((SelectResults) results).size());
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Portfolio(org.apache.geode.cache.query.data.Portfolio) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 99 with AttributesFactory

use of org.apache.geode.cache.AttributesFactory in project geode by apache.

the class PdxQueryDUnitTest method testPutAllWithIndexes.

/**
   * This test creates 3 cache servers with a PR and one client which puts some PDX values in PR and
   * runs a query. This was failing randomly in a POC.
   */
@Test
public void testPutAllWithIndexes() {
    final String name = "testRegion";
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final Properties config = new Properties();
    config.setProperty("locators", "localhost[" + DistributedTestUtils.getDUnitLocatorPort() + "]");
    // Start server
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Cache cache = new CacheFactory(config).create();
            AttributesFactory factory = new AttributesFactory();
            PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
            prfactory.setRedundantCopies(0);
            factory.setPartitionAttributes(prfactory.create());
            cache.createRegionFactory(factory.create()).create(name);
            try {
                startCacheServer(0, false);
            } catch (Exception ex) {
                Assert.fail("While starting CacheServer", ex);
            }
            // Create Index on empty region
            try {
                cache.getQueryService().createIndex("myFuncIndex", "intId", "/" + name);
            } catch (Exception e) {
                Assert.fail("index creation failed", e);
            }
        }
    });
    // Start server
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Cache cache = new CacheFactory(config).create();
            AttributesFactory factory = new AttributesFactory();
            PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
            prfactory.setRedundantCopies(0);
            factory.setPartitionAttributes(prfactory.create());
            cache.createRegionFactory(factory.create()).create(name);
            try {
                startCacheServer(0, false);
            } catch (Exception ex) {
                Assert.fail("While starting CacheServer", ex);
            }
        }
    });
    // Start server
    vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Cache cache = new CacheFactory(config).create();
            AttributesFactory factory = new AttributesFactory();
            PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
            prfactory.setRedundantCopies(0);
            factory.setPartitionAttributes(prfactory.create());
            cache.createRegionFactory(factory.create()).create(name);
            try {
                startCacheServer(0, false);
            } catch (Exception ex) {
                Assert.fail("While starting CacheServer", ex);
            }
        }
    });
    // Create client region
    final int port = vm0.invoke(() -> PdxQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm2.getHost());
    vm3.invoke(new CacheSerializableRunnable("Create region") {

        public void run2() throws CacheException {
            Properties config = new Properties();
            config.setProperty("mcast-port", "0");
            ClientCache cache = new ClientCacheFactory(config).addPoolServer(host0, port).setPoolPRSingleHopEnabled(true).setPoolSubscriptionEnabled(true).create();
            AttributesFactory factory = new AttributesFactory();
            cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(name);
        }
    });
    vm3.invoke(new CacheSerializableRunnable("putAll() test") {

        @Override
        public void run2() throws CacheException {
            try {
                ClientCache cache = new ClientCacheFactory().create();
                Region region = cache.getRegion(name);
                QueryService queryService = cache.getQueryService();
                String k;
                for (int x = 0; x < 285; x++) {
                    k = Integer.valueOf(x).toString();
                    PortfolioPdx v = new PortfolioPdx(x, x);
                    region.put(k, v);
                }
                Query q = queryService.newQuery("SELECT DISTINCT * from /" + name + " WHERE ID = 2");
                SelectResults qResult = (SelectResults) q.execute();
                for (Object o : qResult.asList()) {
                    System.out.println("o = " + o);
                }
            } catch (Exception e) {
                Assert.fail("Querying failed: ", e);
            }
        }
    });
    Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
// }
}
Also used : Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) ClientCache(org.apache.geode.cache.client.ClientCache) Properties(java.util.Properties) CacheException(org.apache.geode.cache.CacheException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) 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) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 100 with AttributesFactory

use of org.apache.geode.cache.AttributesFactory in project geode by apache.

the class PdxQueryDUnitTest method testClientServerCountQuery.

/**
   * Tests client-server query on PdxInstance.
   */
@Test
public void testClientServerCountQuery() throws CacheException {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int numberOfEntries = 10;
    final String queryStr = "SELECT COUNT(*) FROM " + regName + " WHERE id >= 0";
    // Start server1
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer();
            Region region = getRootRegion().getSubregion(regionName);
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
        }
    });
    // Start server2
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer();
            Region region = getRootRegion().getSubregion(regionName);
        }
    });
    // Client pool.
    final int port0 = vm0.invoke(() -> PdxQueryDUnitTest.getCacheServerPort());
    final int port1 = vm1.invoke(() -> PdxQueryDUnitTest.getCacheServerPort());
    final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
    // Create client pool.
    final String poolName = "testClientServerQueryPool";
    createPool(vm2, poolName, new String[] { host0 }, new int[] { port0 }, true);
    createPool(vm3, poolName, new String[] { host0 }, new int[] { port1 }, true);
    // Create client region
    vm3.invoke(new CacheSerializableRunnable("Create region") {

        public void run2() throws CacheException {
            QueryService localQueryService = null;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            ClientServerTestCase.configureConnectionPool(factory, host0, port1, -1, true, -1, -1, null);
            Region region = createRegion(regionName, rootRegionName, factory.create());
            for (int i = 0; i < numberOfEntries; i++) {
                region.put("key-" + i, new TestObject(i, "vmware"));
            }
            // Execute query locally.
            try {
                localQueryService = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            try {
                Query query = localQueryService.newQuery(queryStr);
                SelectResults results = (SelectResults) query.execute();
                assertEquals(numberOfEntries, ((Integer) results.asList().get(0)).intValue());
            } catch (Exception e) {
                Assert.fail("Failed executing " + queryStr, e);
            }
        }
    });
    // Execute client queries
    SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {

        public void run2() throws CacheException {
            QueryService remoteQueryService = null;
            QueryService localQueryService = null;
            SelectResults[][] rs = new SelectResults[1][2];
            try {
                remoteQueryService = (PoolManager.find(poolName)).getQueryService();
                localQueryService = getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            try {
                logger.info("### Executing Query on server:" + queryStr);
                Query query = remoteQueryService.newQuery(queryStr);
                rs[0][0] = (SelectResults) query.execute();
                assertEquals(numberOfEntries, ((Integer) rs[0][0].asList().get(0)).intValue());
                logger.info("### Executing Query locally:" + queryStr);
                query = localQueryService.newQuery(queryStr);
                rs[0][1] = (SelectResults) query.execute();
                assertEquals(numberOfEntries, ((Integer) rs[0][1].asList().get(0)).intValue());
                // Compare local and remote query results.
                if (!CacheUtils.compareResultsOfWithAndWithoutIndex(rs)) {
                    fail("Local and Remote Query Results are not matching for query :" + queryStr);
                }
            } catch (Exception e) {
                Assert.fail("Failed executing " + queryStr, e);
            }
            assertEquals(numberOfEntries, TestObject.numInstance);
        }
    };
    vm3.invoke(executeQueries);
    // Check for TestObject instances on Server2.
    // It should be 0
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            assertEquals(0, TestObject.numInstance);
        }
    });
    this.closeClient(vm2);
    this.closeClient(vm3);
    this.closeClient(vm1);
    this.closeClient(vm0);
}
Also used : Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) 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) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

AttributesFactory (org.apache.geode.cache.AttributesFactory)1156 Region (org.apache.geode.cache.Region)565 Test (org.junit.Test)550 RegionAttributes (org.apache.geode.cache.RegionAttributes)471 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)468 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)356 VM (org.apache.geode.test.dunit.VM)304 Host (org.apache.geode.test.dunit.Host)288 Properties (java.util.Properties)244 CacheException (org.apache.geode.cache.CacheException)243 Cache (org.apache.geode.cache.Cache)229 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)206 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)201 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)199 LocalRegion (org.apache.geode.internal.cache.LocalRegion)173 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)156 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)139 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)129 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)126 IgnoredException (org.apache.geode.test.dunit.IgnoredException)125