Search in sources :

Example 1 with TestQueryFunction

use of org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper.TestQueryFunction in project geode by apache.

the class PRColocatedEquiJoinDUnitTest method testNonColocatedPRLocalQuerying.

@Test
public void testNonColocatedPRLocalQuerying() throws Exception {
    IgnoredException.addIgnoredException("UnsupportedOperationException");
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    setCacheInVMs(vm0);
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Querying PR Test with DACK Started");
    // Creting PR's on the participating VM's
    // Creating DataStore node on the VM0.
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Creating the DataStore node in the PR");
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRCreate(name, redundancy, Portfolio.class));
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Successfully created the DataStore node in the PR");
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Successfully Created PR's across all VM's");
    // Creating Colocated Region DataStore node on the VM0.
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Creating the Colocated DataStore node in the PR");
    // Create second PR which is not colocated.
    vm0.invoke(new CacheSerializableRunnable(coloName) {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region partitionedregion = null;
            try {
                AttributesFactory attr = new AttributesFactory();
                attr.setValueConstraint(NewPortfolio.class);
                PartitionAttributesFactory paf = new PartitionAttributesFactory();
                PartitionAttributes prAttr = paf.setRedundantCopies(redundancy).create();
                attr.setPartitionAttributes(prAttr);
                partitionedregion = cache.createRegion(coloName, attr.create());
            } catch (IllegalStateException ex) {
                LogWriterUtils.getLogWriter().warning("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Creation caught IllegalStateException", ex);
            }
            assertNotNull("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Partitioned Region " + coloName + " not in cache", cache.getRegion(coloName));
            assertNotNull("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Partitioned Region ref null", partitionedregion);
            assertTrue("PRQueryDUnitHelper#getCacheSerializableRunnableForPRCreateWithRedundancy: Partitioned Region ref claims to be destroyed", !partitionedregion.isDestroyed());
        }
    });
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Successfully created the Colocated DataStore node in the PR");
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Successfully Created PR's across all VM's");
    // Creating local region on vm0 to compare the results of query.
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForLocalRegionCreation(localName, Portfolio.class));
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForLocalRegionCreation(coloLocalName, NewPortfolio.class));
    // Generating portfolio object array to be populated across the PR's & Local
    // Regions
    final Portfolio[] portfolio = createPortfoliosAndPositions(cntDest);
    final NewPortfolio[] newPortfolio = createNewPortfoliosAndPositions(cntDest);
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(localName, portfolio, cnt, cntDest));
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(coloLocalName, newPortfolio, cnt, cntDest));
    // Putting the data into the PR's created
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(name, portfolio, cnt, cntDest));
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(coloName, newPortfolio, cnt, cntDest));
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Inserted Portfolio data across PR's");
    // querying the VM for data and comparing the result with query result of
    // local region.
    // querying the VM for data
    vm0.invoke(new CacheSerializableRunnable("PRQuery") {

        @Override
        public void run2() throws CacheException {
            // Helper classes and function
            class TestQueryFunction extends FunctionAdapter {

                @Override
                public boolean hasResult() {
                    return true;
                }

                @Override
                public boolean isHA() {
                    return false;
                }

                private final String id;

                public TestQueryFunction(String id) {
                    super();
                    this.id = id;
                }

                @Override
                public void execute(FunctionContext context) {
                    Cache cache = CacheFactory.getAnyInstance();
                    QueryService queryService = cache.getQueryService();
                    ArrayList allQueryResults = new ArrayList();
                    String qstr = (String) context.getArguments();
                    try {
                        Query query = queryService.newQuery(qstr);
                        context.getResultSender().sendResult((ArrayList) ((SelectResults) query.execute((RegionFunctionContext) context)).asList());
                        context.getResultSender().lastResult(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new FunctionException(e);
                    }
                }

                @Override
                public String getId() {
                    return this.id;
                }
            }
            Cache cache = getCache();
            // Querying the PR region
            String[] queries = new String[] { "r1.ID = r2.id" };
            Object[][] r = new Object[queries.length][2];
            Region region = null;
            region = cache.getRegion(name);
            assertNotNull(region);
            region = cache.getRegion(coloName);
            assertNotNull(region);
            QueryService qs = getCache().getQueryService();
            Object[] params;
            try {
                for (int j = 0; j < queries.length; j++) {
                    getCache().getLogger().info("About to execute local query: " + queries[j]);
                    Function func = new TestQueryFunction("testfunction");
                    Object funcResult = FunctionService.onRegion((getCache().getRegion(name) instanceof PartitionedRegion) ? getCache().getRegion(name) : getCache().getRegion(coloName)).setArguments("Select " + (queries[j].contains("ORDER BY") ? "DISTINCT" : "") + " * from /" + name + " r1, /" + coloName + " r2 where " + queries[j]).execute(func).getResult();
                    r[j][0] = ((ArrayList) funcResult).get(0);
                }
                fail("PRQueryDUnitHelper#getCacheSerializableRunnableForPRQueryAndCompareResults: Queries Executed successfully with non-colocated region on one of the nodes");
            } catch (FunctionException e) {
                if (e.getCause() instanceof UnsupportedOperationException) {
                    LogWriterUtils.getLogWriter().info("Query received FunctionException successfully while using QueryService.");
                } else {
                    fail("UnsupportedOperationException must be thrown here");
                }
            }
        }
    });
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Querying PR's Test ENDED");
}
Also used : Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) ArrayList(java.util.ArrayList) Function(org.apache.geode.cache.execute.Function) TestQueryFunction(org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper.TestQueryFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) TestQueryFunction(org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper.TestQueryFunction) NewPortfolio(parReg.query.unittest.NewPortfolio) Portfolio(org.apache.geode.cache.query.data.Portfolio) NewPortfolio(parReg.query.unittest.NewPortfolio) FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) TestException(util.TestException) CacheException(org.apache.geode.cache.CacheException) ReplyException(org.apache.geode.distributed.internal.ReplyException) QueryException(org.apache.geode.cache.query.QueryException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Cache(org.apache.geode.cache.Cache) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 2 with TestQueryFunction

use of org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper.TestQueryFunction in project geode by apache.

the class PRColocatedEquiJoinDUnitTest method testPRRRNonLocalQueryingWithNoRROnOneNode.

/**
   * A very basic dunit test that <br>
   * 1. Creates two PR Data Stores with redundantCopies = 1. 2. Populates the region with test data.
   * 3. Fires a LOCAL query on one data store VM and verifies the result.
   * 
   * @throws Exception
   */
@Test
public void testPRRRNonLocalQueryingWithNoRROnOneNode() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    setCacheInVMs(vm0, vm1);
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Querying PR Test with DACK Started");
    // Creting PR's on the participating VM's
    // Creating DataStore node on the VM0.
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Creating the DataStore node in the PR");
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRCreate(name, 0, Portfolio.class));
    vm1.invoke(PRQHelp.getCacheSerializableRunnableForPRCreate(name, 0, Portfolio.class));
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Successfully created the DataStore node in the PR");
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Successfully Created PR's across all VM's");
    // Creating Colocated Region DataStore node on the VM0.
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Creating the Colocated DataStore node in the RR");
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForLocalRegionCreation(coloName, NewPortfolio.class));
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Successfully Created PR's across all VM's");
    final Portfolio[] portfolio = createPortfoliosAndPositions(cntDest);
    final NewPortfolio[] newPortfolio = createNewPortfoliosAndPositions(cntDest);
    // Putting the data into the PR's created
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(name, portfolio, cnt, cntDest));
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(coloName, newPortfolio, cnt, cntDest));
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Inserted Portfolio data across PR's");
    // querying the VM for data and comparing the result with query result of
    // local region.
    // querying the VM for data
    vm0.invoke(new CacheSerializableRunnable("PRQuery") {

        @Override
        public void run2() throws CacheException {
            // Helper classes and function
            class TestQueryFunction extends FunctionAdapter {

                @Override
                public boolean hasResult() {
                    return true;
                }

                @Override
                public boolean isHA() {
                    return false;
                }

                private final String id;

                public TestQueryFunction(String id) {
                    super();
                    this.id = id;
                }

                @Override
                public void execute(FunctionContext context) {
                    Cache cache = CacheFactory.getAnyInstance();
                    QueryService queryService = cache.getQueryService();
                    ArrayList allQueryResults = new ArrayList();
                    String qstr = (String) context.getArguments();
                    try {
                        Query query = queryService.newQuery(qstr);
                        context.getResultSender().sendResult((ArrayList) ((SelectResults) query.execute((RegionFunctionContext) context)).asList());
                        context.getResultSender().lastResult(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new FunctionException(e);
                    }
                }

                @Override
                public String getId() {
                    return this.id;
                }
            }
            Cache cache = getCache();
            // Querying the PR region
            String[] queries = new String[] { "r1.ID = r2.id" };
            Object[][] r = new Object[queries.length][2];
            Region region = null;
            region = cache.getRegion(name);
            assertNotNull(region);
            region = cache.getRegion(coloName);
            assertNotNull(region);
            QueryService qs = getCache().getQueryService();
            Object[] params;
            try {
                for (int j = 0; j < queries.length; j++) {
                    getCache().getLogger().info("About to execute local query: " + queries[j]);
                    Function func = new TestQueryFunction("testfunction");
                    Object funcResult = FunctionService.onRegion((getCache().getRegion(name) instanceof PartitionedRegion) ? getCache().getRegion(name) : getCache().getRegion(coloName)).setArguments("Select " + (queries[j].contains("ORDER BY") ? "DISTINCT" : "") + " * from /" + name + " r1, /" + coloName + " r2 where " + queries[j]).execute(func).getResult();
                    r[j][0] = ((ArrayList) funcResult).get(0);
                }
                fail("PRQueryDUnitHelper#getCacheSerializableRunnableForPRQueryAndCompareResults: Queries Executed successfully without RR region on one of the nodes");
            } catch (FunctionException e) {
                if (e.getCause() instanceof RegionNotFoundException) {
                    LogWriterUtils.getLogWriter().info("Query received FunctionException successfully while using QueryService.");
                } else {
                    fail("RegionNotFoundException must be thrown here");
                }
            }
        }
    });
    LogWriterUtils.getLogWriter().info("PRQBasicQueryDUnitTest#testPRBasicQuerying: Querying PR's Test ENDED");
}
Also used : Query(org.apache.geode.cache.query.Query) NewPortfolio(parReg.query.unittest.NewPortfolio) CacheException(org.apache.geode.cache.CacheException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) Portfolio(org.apache.geode.cache.query.data.Portfolio) NewPortfolio(parReg.query.unittest.NewPortfolio) ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) FunctionException(org.apache.geode.cache.execute.FunctionException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) TestException(util.TestException) CacheException(org.apache.geode.cache.CacheException) ReplyException(org.apache.geode.distributed.internal.ReplyException) QueryException(org.apache.geode.cache.query.QueryException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Function(org.apache.geode.cache.execute.Function) TestQueryFunction(org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper.TestQueryFunction) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) QueryService(org.apache.geode.cache.query.QueryService) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) TestQueryFunction(org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper.TestQueryFunction) Cache(org.apache.geode.cache.Cache) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ArrayList (java.util.ArrayList)2 Cache (org.apache.geode.cache.Cache)2 CacheClosedException (org.apache.geode.cache.CacheClosedException)2 CacheException (org.apache.geode.cache.CacheException)2 Region (org.apache.geode.cache.Region)2 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)2 Function (org.apache.geode.cache.execute.Function)2 FunctionContext (org.apache.geode.cache.execute.FunctionContext)2 FunctionException (org.apache.geode.cache.execute.FunctionException)2 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)2 Query (org.apache.geode.cache.query.Query)2 QueryException (org.apache.geode.cache.query.QueryException)2 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)2 QueryService (org.apache.geode.cache.query.QueryService)2 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)2 Portfolio (org.apache.geode.cache.query.data.Portfolio)2 TestQueryFunction (org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper.TestQueryFunction)2 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)2 ReplyException (org.apache.geode.distributed.internal.ReplyException)2 ForceReattemptException (org.apache.geode.internal.cache.ForceReattemptException)2