Search in sources :

Example 6 with QueryTestUtils

use of org.apache.geode.cache.query.QueryTestUtils in project geode by apache.

the class CopyOnReadQueryJUnitTest method setUp.

@Before
public void setUp() throws java.lang.Exception {
    utils = new QueryTestUtils();
    utils.createCache(null);
    utils.getCache().setCopyOnRead(true);
    Portfolio.resetInstanceCount();
}
Also used : QueryTestUtils(org.apache.geode.cache.query.QueryTestUtils) Before(org.junit.Before)

Example 7 with QueryTestUtils

use of org.apache.geode.cache.query.QueryTestUtils in project geode by apache.

the class CopyOnReadIndexDUnitTest method helpTestTransactionsOnReplicatedRegion.

public void helpTestTransactionsOnReplicatedRegion(final String queryString, final int numPortfolios, final int numExpectedResults, final boolean hasIndex) throws Exception {
    resetInstanceCount(vm0);
    resetInstanceCount(vm1);
    resetInstanceCount(vm2);
    createReplicatedRegion(vm0, "portfolios");
    createReplicatedRegion(vm1, "portfolios");
    createReplicatedRegion(vm2, "portfolios");
    // counts
    if (hasIndex) {
        vm0.invoke(new SerializableCallable() {

            public Object call() throws Exception {
                QueryTestUtils utils = new QueryTestUtils();
                utils.createHashIndex("idIndex", "p.ID", "/portfolios p");
                return null;
            }
        });
        // let's not create index on vm1 to check different scenarios
        vm2.invoke(new SerializableCallable() {

            public Object call() throws Exception {
                QueryTestUtils utils = new QueryTestUtils();
                utils.createHashIndex("idIndex", "p.ID", "/portfolios p");
                return null;
            }
        });
    }
    vm0.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            for (int i = 0; i < numPortfolios; i++) {
                Portfolio p = new Portfolio(i);
                p.status = "testStatus";
                p.positions = new HashMap();
                p.positions.put("" + i, new Position("" + i, 20));
                region.put("key " + i, p);
            }
            // We should have the same number of portfolio objects that we created for the put
            Wait.waitForCriterion(verifyPortfolioCount(numPortfolios), 5000, 200, true);
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            // At this point, we should only have serialized values in this vm
            Region region = getCache().getRegion("/portfolios");
            Wait.waitForCriterion(verifyPortfolioCount(0), 0, 200, true);
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            // There is an index for vm2, so we should have deserialized values at this point,
            Region region = getCache().getRegion("/portfolios");
            if (hasIndex) {
                Wait.waitForCriterion(verifyPortfolioCount(numPortfolios), 0, 200, true);
            } else {
                Wait.waitForCriterion(verifyPortfolioCount(0), 0, 200, true);
            }
            return null;
        }
    });
    // start transaction
    // execute query
    // modify results
    // check instance count
    vm0.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            CacheTransactionManager txManager = region.getCache().getCacheTransactionManager();
            try {
                txManager.begin();
                QueryService qs = getCache().getQueryService();
                Query query = qs.newQuery(queryString);
                SelectResults results = (SelectResults) query.execute();
                assertEquals(numExpectedResults, results.size());
                for (Object o : results) {
                    if (o instanceof Portfolio) {
                        Portfolio p = (Portfolio) o;
                        p.status = "discardStatus";
                    } else {
                        Struct struct = (Struct) o;
                        Portfolio p = (Portfolio) struct.getFieldValues()[0];
                        p.status = "discardStatus";
                    }
                }
                txManager.commit();
            } catch (CommitConflictException conflict) {
                Assert.fail("commit conflict exception", conflict);
            }
            // We have created puts from our previous callable
            // Now we have copied the results from the query
            Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults + numPortfolios), 0, 200, true);
            return null;
        }
    });
    // Check objects in cache on vm1
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            QueryService qs = getCache().getQueryService();
            Query query = qs.newQuery(queryString);
            SelectResults results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                }
            }
            // first it must deserialize the portfolios in the replicated region
            // then we do a copy on read of these deserialized objects for the final result set
            Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults + numPortfolios), 0, 200, true);
            results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                }
            }
            // we never created index on vm1
            // so in this case, we always have to deserialize the value from the region
            Wait.waitForCriterion(verifyPortfolioCount(numPortfolios * 2 + numExpectedResults * 2), 0, 200, true);
            return null;
        }
    });
    // Check objects in cache on vm2
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            QueryService qs = getCache().getQueryService();
            Query query = qs.newQuery(queryString);
            SelectResults results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                    p.status = "discardStatus";
                }
            }
            // with or without index, the values had to have been deserialized at one point
            Wait.waitForCriterion(verifyPortfolioCount(numPortfolios + numExpectedResults), 0, 200, true);
            results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                }
            }
            if (hasIndex) {
                // we have an index, so the values are already deserialized
                // total is now our original deserialization amount : numPortfolios
                // two query results copied.
                Wait.waitForCriterion(verifyPortfolioCount(numPortfolios + numExpectedResults * 2), 0, 200, true);
            } else {
                // we never created index on vm1
                // so in this case, we always have to deserialize the value from the region
                Wait.waitForCriterion(verifyPortfolioCount(numPortfolios * 2 + numExpectedResults * 2), 0, 200, true);
            }
            return null;
        }
    });
    // Check objects in cache on vm0
    vm0.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region region = getCache().getRegion("/portfolios");
            QueryService qs = getCache().getQueryService();
            Query query = qs.newQuery(queryString);
            SelectResults results = (SelectResults) query.execute();
            assertEquals(numExpectedResults, results.size());
            for (Object o : results) {
                if (o instanceof Portfolio) {
                    Portfolio p = (Portfolio) o;
                    assertEquals("status should not have been changed", "testStatus", p.status);
                } else {
                    Struct struct = (Struct) o;
                    Portfolio p = (Portfolio) struct.getFieldValues()[0];
                    assertEquals("status should not have been changed", "testStatus", p.status);
                }
            }
            // with or without index, the values we put in the region were already deserialized values
            Wait.waitForCriterion(verifyPortfolioCount(numExpectedResults * 2 + numPortfolios), 0, 200, true);
            return null;
        }
    });
    destroyRegion("portfolio", vm0);
}
Also used : Query(org.apache.geode.cache.query.Query) HashMap(java.util.HashMap) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) CacheException(org.apache.geode.cache.CacheException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Struct(org.apache.geode.cache.query.Struct) CommitConflictException(org.apache.geode.cache.CommitConflictException) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryTestUtils(org.apache.geode.cache.query.QueryTestUtils) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 8 with QueryTestUtils

use of org.apache.geode.cache.query.QueryTestUtils in project geode by apache.

the class CopyOnReadIndexDUnitTest method testPRQueryOnLocalNode.

// test different queries against partitioned region
@Test
public void testPRQueryOnLocalNode() throws Exception {
    QueryTestUtils utils = new QueryTestUtils();
    configureServers();
    helpTestPRQueryOnLocalNode(utils.queries.get("545"), 100, 100, true);
    helpTestPRQueryOnLocalNode(utils.queries.get("546"), 100, 100, true);
    helpTestPRQueryOnLocalNode(utils.queries.get("543"), 100, 100, true);
    helpTestPRQueryOnLocalNode(utils.queries.get("544"), 100, 100, true);
    helpTestPRQueryOnLocalNode("select * from /portfolios p where p.ID = 1", 100, 1, true);
    helpTestPRQueryOnLocalNode(utils.queries.get("545"), 100, 100, false);
    helpTestPRQueryOnLocalNode(utils.queries.get("546"), 100, 100, false);
    helpTestPRQueryOnLocalNode(utils.queries.get("543"), 100, 100, false);
    helpTestPRQueryOnLocalNode(utils.queries.get("544"), 100, 100, false);
    helpTestPRQueryOnLocalNode("select * from /portfolios p where p.ID = 1", 100, 1, false);
}
Also used : QueryTestUtils(org.apache.geode.cache.query.QueryTestUtils) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

QueryTestUtils (org.apache.geode.cache.query.QueryTestUtils)8 Properties (java.util.Properties)3 HashMap (java.util.HashMap)2 CacheException (org.apache.geode.cache.CacheException)2 CommitConflictException (org.apache.geode.cache.CommitConflictException)2 Region (org.apache.geode.cache.Region)2 Query (org.apache.geode.cache.query.Query)2 QueryService (org.apache.geode.cache.query.QueryService)2 SelectResults (org.apache.geode.cache.query.SelectResults)2 Struct (org.apache.geode.cache.query.Struct)2 Portfolio (org.apache.geode.cache.query.data.Portfolio)2 Position (org.apache.geode.cache.query.data.Position)2 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)2 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)2 Host (org.apache.geode.test.dunit.Host)2 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 Before (org.junit.Before)2 Test (org.junit.Test)2