Search in sources :

Example 56 with PortfolioData

use of org.apache.geode.cache.query.data.PortfolioData in project geode by apache.

the class InitializeIndexEntryDestroyQueryDUnitTest method testAsyncIndexInitDuringEntryDestroyAndQuery.

@Test
public void testAsyncIndexInitDuringEntryDestroyAndQuery() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    setCacheInVMs(vm0);
    name = "PartionedPortfolios";
    // Create Local Region
    vm0.invoke(new CacheSerializableRunnable("Create local region with asynchronous index maintenance") {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region localRegion = null;
            try {
                AttributesFactory attr = new AttributesFactory();
                attr.setValueConstraint(PortfolioData.class);
                attr.setScope(Scope.LOCAL);
                attr.setIndexMaintenanceSynchronous(false);
                RegionFactory regionFactory = cache.createRegionFactory(attr.create());
                localRegion = regionFactory.create(name);
            } catch (IllegalStateException ex) {
                LogWriterUtils.getLogWriter().warning("Creation caught IllegalStateException", ex);
            }
            assertNotNull("Region " + name + " not in cache", cache.getRegion(name));
            assertNotNull("Region ref null", localRegion);
            assertTrue("Region ref claims to be destroyed", !localRegion.isDestroyed());
        }
    });
    final PortfolioData[] portfolio = createPortfolioData(cnt, cntDest);
    // Putting the data into the PR's created
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(name, portfolio, cnt, cntDest));
    AsyncInvocation asyInvk0 = vm0.invokeAsync(new CacheSerializableRunnable("Create Index with Hook") {

        @Override
        public void run2() throws CacheException {
            for (int i = 0; i < cntDest; i++) {
                // Create Index first to go in hook.
                Cache cache = getCache();
                Index index = null;
                try {
                    index = cache.getQueryService().createIndex("statusIndex", "p.status", "/" + name + " p");
                } catch (Exception e1) {
                    e1.printStackTrace();
                    fail("Index creation failed");
                }
                assertNotNull(index);
                Wait.pause(100);
                getCache().getQueryService().removeIndex(index);
                Wait.pause(100);
            }
        }
    });
    // Change the value in Region
    AsyncInvocation asyInvk1 = vm0.invokeAsync(new CacheSerializableRunnable("Change value in region") {

        @Override
        public void run2() throws CacheException {
            // Do a put in region.
            Region r = getCache().getRegion(name);
            for (int i = 0, j = 0; i < 1000; i++, j++) {
                PortfolioData p = (PortfolioData) r.get(j);
                getCache().getLogger().fine("Going to destroy the value" + p);
                r.destroy(j);
                Wait.pause(100);
                // Put the value back again.
                getCache().getLogger().fine("Putting the value back" + p);
                r.put(j, p);
                // Reset j
                if (j == cntDest - 1) {
                    j = 0;
                }
            }
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Run query on region") {

        @Override
        public void run2() throws CacheException {
            // Do a put in region.
            Region r = getCache().getRegion(name);
            Query query = getCache().getQueryService().newQuery("select * from /" + name + " p where p.status = 'active'");
            // Now run the query
            SelectResults results = null;
            for (int i = 0; i < 500; i++) {
                try {
                    getCache().getLogger().fine("Querying the region");
                    results = (SelectResults) query.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for (Object obj : results) {
                    if (obj instanceof Undefined) {
                        fail("Found an undefined element" + Arrays.toString(results.toArray()));
                    }
                }
            }
        }
    });
    ThreadUtils.join(asyInvk0, 1000 * 1000);
    if (asyInvk0.exceptionOccurred()) {
        Assert.fail("asyInvk0 failed", asyInvk0.getException());
    }
    ThreadUtils.join(asyInvk1, 1000 * 1000);
    if (asyInvk1.exceptionOccurred()) {
        Assert.fail("asyInvk1 failed", asyInvk1.getException());
    }
}
Also used : Undefined(org.apache.geode.cache.query.internal.Undefined) Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) 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) RegionFactory(org.apache.geode.cache.RegionFactory) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) PortfolioData(org.apache.geode.cache.query.data.PortfolioData) Utils.createPortfolioData(org.apache.geode.cache.query.Utils.createPortfolioData) 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 57 with PortfolioData

use of org.apache.geode.cache.query.data.PortfolioData in project geode by apache.

the class InitializeIndexEntryDestroyQueryDUnitTest method testAsyncIndexInitDuringEntryDestroyAndQueryOnPR.

// GEODE-1036: uses PRQueryDUnitHelper, time sensitive, async actions,
@Category(FlakyTest.class)
// sleeps
@Test
public void testAsyncIndexInitDuringEntryDestroyAndQueryOnPR() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    setCacheInVMs(vm0);
    name = "PartionedPortfoliosPR";
    // Create Local Region
    vm0.invoke(new CacheSerializableRunnable("Create local region with asynchronous index maintenance") {

        @Override
        public void run2() throws CacheException {
            Cache cache = getCache();
            Region partitionRegion = null;
            try {
                AttributesFactory attr = new AttributesFactory();
                attr.setValueConstraint(PortfolioData.class);
                attr.setIndexMaintenanceSynchronous(false);
                attr.setPartitionAttributes(new PartitionAttributesFactory().create());
                RegionFactory regionFactory = cache.createRegionFactory(attr.create());
                partitionRegion = regionFactory.create(name);
            } catch (IllegalStateException ex) {
                LogWriterUtils.getLogWriter().warning("Creation caught IllegalStateException", ex);
            }
            assertNotNull("Region " + name + " not in cache", cache.getRegion(name));
            assertNotNull("Region ref null", partitionRegion);
            assertTrue("Region ref claims to be destroyed", !partitionRegion.isDestroyed());
        }
    });
    final PortfolioData[] portfolio = createPortfolioData(cnt, cntDest);
    // Putting the data into the PR's created
    vm0.invoke(PRQHelp.getCacheSerializableRunnableForPRPuts(name, portfolio, cnt, cntDest));
    AsyncInvocation asyInvk0 = vm0.invokeAsync(new CacheSerializableRunnable("Create Index with Hook") {

        @Override
        public void run2() throws CacheException {
            for (int i = 0; i < cntDest; i++) {
                // Create Index first to go in hook.
                Cache cache = getCache();
                Index index = null;
                try {
                    index = cache.getQueryService().createIndex("statusIndex", "p.status", "/" + name + " p");
                } catch (Exception e1) {
                    e1.printStackTrace();
                    Assert.fail("Index creation failed", e1);
                }
                assertNotNull(index);
                getCache().getQueryService().removeIndex(index);
            }
        }
    });
    // Change the value in Region
    AsyncInvocation asyInvk1 = vm0.invokeAsync(new CacheSerializableRunnable("Change value in region") {

        @Override
        public void run2() throws CacheException {
            // Do a put in region.
            Region r = getCache().getRegion(name);
            for (int i = 0, j = 0; i < 1000; i++, j++) {
                PortfolioData p = (PortfolioData) r.get(j);
                getCache().getLogger().fine("Going to destroy the value" + p);
                r.destroy(j);
                Wait.pause(20);
                // Put the value back again.
                getCache().getLogger().fine("Putting the value back" + p);
                r.put(j, p);
                // Reset j
                if (j == cntDest - 1) {
                    j = 0;
                }
            }
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Run query on region") {

        @Override
        public void run2() throws CacheException {
            // Do a put in region.
            Query query = getCache().getQueryService().newQuery("select * from /" + name + " p where p.status = 'active'");
            // Now run the query
            SelectResults results = null;
            for (int i = 0; i < 500; i++) {
                try {
                    getCache().getLogger().fine("Querying the region");
                    results = (SelectResults) query.execute();
                } catch (Exception e) {
                    // TODO: eats exceptions
                    e.printStackTrace();
                }
                for (Object obj : results) {
                    if (obj instanceof Undefined) {
                        fail("Found an undefined element" + Arrays.toString(results.toArray()));
                    }
                }
            }
        }
    });
    // TODO: this is way too long: 16.67 minutes!
    ThreadUtils.join(asyInvk0, 1000 * 1000);
    if (asyInvk0.exceptionOccurred()) {
        Assert.fail("asyInvk0 failed", asyInvk0.getException());
    }
    // TODO: this is way too long: 16.67 minutes!
    ThreadUtils.join(asyInvk1, 1000 * 1000);
    if (asyInvk1.exceptionOccurred()) {
        Assert.fail("asyInvk1 failed", asyInvk1.getException());
    }
}
Also used : Undefined(org.apache.geode.cache.query.internal.Undefined) Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) Index(org.apache.geode.cache.query.Index) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheException(org.apache.geode.cache.CacheException) 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) RegionFactory(org.apache.geode.cache.RegionFactory) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) PortfolioData(org.apache.geode.cache.query.data.PortfolioData) Utils.createPortfolioData(org.apache.geode.cache.query.Utils.createPortfolioData) Cache(org.apache.geode.cache.Cache) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

PortfolioData (org.apache.geode.cache.query.data.PortfolioData)57 Test (org.junit.Test)57 Host (org.apache.geode.test.dunit.Host)52 VM (org.apache.geode.test.dunit.VM)52 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)52 Cache (org.apache.geode.cache.Cache)18 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)18 Region (org.apache.geode.cache.Region)17 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)17 CacheException (org.apache.geode.cache.CacheException)16 SelectResults (org.apache.geode.cache.query.SelectResults)14 Index (org.apache.geode.cache.query.Index)13 Query (org.apache.geode.cache.query.Query)12 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)12 ArrayList (java.util.ArrayList)11 AttributesFactory (org.apache.geode.cache.AttributesFactory)11 RegionFactory (org.apache.geode.cache.RegionFactory)11 LinkedList (java.util.LinkedList)9 List (java.util.List)9 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)9