Search in sources :

Example 1 with AbstractRegion

use of org.apache.geode.internal.cache.AbstractRegion in project geode by apache.

the class RegionAlterFunction method alterRegion.

private <K, V> Region<?, ?> alterRegion(Cache cache, RegionFunctionArgs regionAlterArgs) {
    final String regionPathString = regionAlterArgs.getRegionPath();
    RegionPath regionPath = new RegionPath(regionPathString);
    AbstractRegion region = (AbstractRegion) cache.getRegion(regionPathString);
    if (region == null) {
        throw new IllegalArgumentException(CliStrings.format(CliStrings.ALTER_REGION__MSG__REGION_DOESNT_EXIST_0, new Object[] { regionPath }));
    }
    AttributesMutator mutator = region.getAttributesMutator();
    if (regionAlterArgs.isCloningEnabled() != null) {
        mutator.setCloningEnabled(regionAlterArgs.isCloningEnabled());
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cloning");
        }
    }
    if (regionAlterArgs.getEvictionMax() != null) {
        mutator.getEvictionAttributesMutator().setMaximum(regionAlterArgs.getEvictionMax());
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - eviction attributes max");
        }
    }
    // Alter expiration attributes
    final RegionFunctionArgs.ExpirationAttrs newEntryExpirationIdleTime = regionAlterArgs.getEntryExpirationIdleTime();
    if (newEntryExpirationIdleTime != null) {
        mutator.setEntryIdleTimeout(parseExpirationAttributes(newEntryExpirationIdleTime, region.getEntryIdleTimeout()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - entry idle timeout");
        }
    }
    final RegionFunctionArgs.ExpirationAttrs newEntryExpirationTTL = regionAlterArgs.getEntryExpirationTTL();
    if (newEntryExpirationTTL != null) {
        mutator.setEntryTimeToLive(parseExpirationAttributes(newEntryExpirationTTL, region.getEntryTimeToLive()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - entry TTL");
        }
    }
    final RegionFunctionArgs.ExpirationAttrs newRegionExpirationIdleTime = regionAlterArgs.getRegionExpirationIdleTime();
    if (newRegionExpirationIdleTime != null) {
        mutator.setRegionIdleTimeout(parseExpirationAttributes(newRegionExpirationIdleTime, region.getRegionIdleTimeout()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - region idle timeout");
        }
    }
    final RegionFunctionArgs.ExpirationAttrs newRegionExpirationTTL = regionAlterArgs.getRegionExpirationTTL();
    if (newRegionExpirationTTL != null) {
        mutator.setRegionTimeToLive(parseExpirationAttributes(newRegionExpirationTTL, region.getRegionTimeToLive()));
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - region TTL");
        }
    }
    // Alter Gateway Sender Ids
    final Set<String> newGatewaySenderIds = regionAlterArgs.getGatewaySenderIds();
    if (newGatewaySenderIds != null) {
        // Remove old gateway sender ids that aren't in the new list
        Set<String> oldGatewaySenderIds = region.getGatewaySenderIds();
        if (!oldGatewaySenderIds.isEmpty()) {
            for (String gatewaySenderId : oldGatewaySenderIds) {
                if (!newGatewaySenderIds.contains(gatewaySenderId)) {
                    mutator.removeGatewaySenderId(gatewaySenderId);
                }
            }
        }
        // Add new gateway sender ids that don't already exist
        for (String gatewaySenderId : newGatewaySenderIds) {
            if (!oldGatewaySenderIds.contains(gatewaySenderId)) {
                mutator.addGatewaySenderId(gatewaySenderId);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - gateway sender IDs");
        }
    }
    // Alter Async Queue Ids
    final Set<String> newAsyncEventQueueIds = regionAlterArgs.getAsyncEventQueueIds();
    if (newAsyncEventQueueIds != null) {
        // Remove old async event queue ids that aren't in the new list
        Set<String> oldAsyncEventQueueIds = region.getAsyncEventQueueIds();
        if (!oldAsyncEventQueueIds.isEmpty()) {
            for (String asyncEventQueueId : oldAsyncEventQueueIds) {
                if (!newAsyncEventQueueIds.contains(asyncEventQueueId)) {
                    mutator.removeAsyncEventQueueId(asyncEventQueueId);
                }
            }
        }
        // Add new async event queue ids that don't already exist
        for (String asyncEventQueueId : newAsyncEventQueueIds) {
            if (!oldAsyncEventQueueIds.contains(asyncEventQueueId)) {
                mutator.addAsyncEventQueueId(asyncEventQueueId);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - async event queue IDs");
        }
    }
    // Alter Cache Listeners
    final Set<String> newCacheListenerNames = regionAlterArgs.getCacheListeners();
    if (newCacheListenerNames != null) {
        // Remove old cache listeners that aren't in the new list
        CacheListener[] oldCacheListeners = region.getCacheListeners();
        for (CacheListener oldCacheListener : oldCacheListeners) {
            if (!newCacheListenerNames.contains(oldCacheListener.getClass().getName())) {
                mutator.removeCacheListener(oldCacheListener);
            }
        }
        // Add new cache listeners that don't already exist
        for (String newCacheListenerName : newCacheListenerNames) {
            if (newCacheListenerName.isEmpty()) {
                continue;
            }
            boolean nameFound = false;
            for (CacheListener oldCacheListener : oldCacheListeners) {
                if (oldCacheListener.getClass().getName().equals(newCacheListenerName)) {
                    nameFound = true;
                    break;
                }
            }
            if (!nameFound) {
                Class<CacheListener<K, V>> cacheListenerKlass = forName(newCacheListenerName, CliStrings.ALTER_REGION__CACHELISTENER);
                mutator.addCacheListener(newInstance(cacheListenerKlass, CliStrings.ALTER_REGION__CACHELISTENER));
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cache listeners");
        }
    }
    final String cacheLoader = regionAlterArgs.getCacheLoader();
    if (cacheLoader != null) {
        if (cacheLoader.isEmpty()) {
            mutator.setCacheLoader(null);
        } else {
            Class<CacheLoader<K, V>> cacheLoaderKlass = forName(cacheLoader, CliStrings.ALTER_REGION__CACHELOADER);
            mutator.setCacheLoader(newInstance(cacheLoaderKlass, CliStrings.ALTER_REGION__CACHELOADER));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cache loader");
        }
    }
    final String cacheWriter = regionAlterArgs.getCacheWriter();
    if (cacheWriter != null) {
        if (cacheWriter.isEmpty()) {
            mutator.setCacheWriter(null);
        } else {
            Class<CacheWriter<K, V>> cacheWriterKlass = forName(cacheWriter, CliStrings.ALTER_REGION__CACHEWRITER);
            mutator.setCacheWriter(newInstance(cacheWriterKlass, CliStrings.ALTER_REGION__CACHEWRITER));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Region successfully altered - cache writer");
        }
    }
    return region;
}
Also used : AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) CacheListener(org.apache.geode.cache.CacheListener) RegionPath(org.apache.geode.management.internal.cli.util.RegionPath) CacheWriter(org.apache.geode.cache.CacheWriter) CacheLoader(org.apache.geode.cache.CacheLoader) AttributesMutator(org.apache.geode.cache.AttributesMutator)

Example 2 with AbstractRegion

use of org.apache.geode.internal.cache.AbstractRegion in project geode by apache.

the class RegionReliabilityTestCase method testRegionDistributionException.

@Test
public void testRegionDistributionException() throws Exception {
    if (getRegionScope().isDistributedNoAck())
        // skip test under DistributedNoAck
        return;
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {

        public void afterRemoteRegionDeparture(RegionEvent event) {
            synchronized (detectedDeparture_testRegionDistributionException) {
                detectedDeparture_testRegionDistributionException[0] = Boolean.TRUE;
                detectedDeparture_testRegionDistributionException.notify();
            }
        }
    };
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    // fac.addCacheListener(listener);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    // use vm1 to create role
    CacheSerializableRunnable createRegion = new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    };
    Host.getHost(0).getVM(1).invoke(createRegion);
    region.put("DESTROY_ME", "VAL");
    region.put("INVALIDATE_ME", "VAL");
    // define the afterReleaseLocalLocks callback
    SerializableRunnable removeRequiredRole = new SerializableRunnable() {

        public void run() {
            Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {

                public void run() {
                    getRootRegion(name).close();
                }
            });
        // try {
        // synchronized (detectedDeparture_testRegionDistributionException) {
        // while (detectedDeparture_testRegionDistributionException[0] == Boolean.FALSE) {
        // detectedDeparture_testRegionDistributionException.wait();
        // }
        // }
        // }
        // catch (InterruptedException e) {}
        }
    };
    DistributedCacheOperation.setBeforePutOutgoing(() -> {
        try {
            removeRequiredRole.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    Runnable reset = new Runnable() {

        public void run() {
        // synchronized (detectedDeparture_testRegionDistributionException) {
        // detectedDeparture_testRegionDistributionException[0] = Boolean.FALSE;
        // }
        }
    };
    // PUT
    try {
        region.put("KEY", "VAL");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // INVALIDATE
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.invalidate("INVALIDATE_ME");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // DESTROY
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.destroy("DESTROY_ME");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // CLEAR
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.clear();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // PUTALL
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        Map putAll = new HashMap();
        putAll.put("PUTALL_ME", "VAL");
        region.putAll(putAll);
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // INVALIDATE REGION
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.invalidateRegion();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // DESTROY REGION
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.destroyRegion();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) HashMap(java.util.HashMap) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) RegionAccessException(org.apache.geode.cache.RegionAccessException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) AttributesFactory(org.apache.geode.cache.AttributesFactory) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionMembershipListenerAdapter(org.apache.geode.cache.util.RegionMembershipListenerAdapter) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 3 with AbstractRegion

use of org.apache.geode.internal.cache.AbstractRegion in project geode by apache.

the class RegionManagementDUnitTest method createDiskRegion.

private void createDiskRegion(final VM memberVM) {
    memberVM.invoke("createDiskRegion", () -> {
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.LOCAL);
        factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(20, new TestObjectSizerImpl(), EvictionAction.LOCAL_DESTROY));
        Region region = getCache_tmp().createRegion(REGION_NAME, factory.create());
        LRUStatistics lruStats = ((AbstractRegion) region).getEvictionController().getLRUHelper().getStats();
        assertThat(lruStats).isNotNull();
        RegionMXBean regionMXBean = getManagementService_tmp().getLocalRegionMBean(REGION_PATH);
        assertThat(regionMXBean).isNotNull();
        int total;
        for (total = 0; total < 100; total++) {
            // TODO: why so many?
            int[] array = new int[250];
            array[0] = total;
            region.put(new Integer(total), array);
        }
        assertThat(regionMXBean.getEntrySize()).isGreaterThan(0);
    });
}
Also used : TestObjectSizerImpl(org.apache.geode.internal.cache.TestObjectSizerImpl) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) Region(org.apache.geode.cache.Region) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion)

Example 4 with AbstractRegion

use of org.apache.geode.internal.cache.AbstractRegion in project geode by apache.

the class RegionReliabilityTestCase method testReinitialization.

@Test
public void testReinitialization() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.REINITIALIZE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setDataPolicy(DataPolicy.REPLICATE);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    assertFalse(RequiredRoles.checkForRequiredRoles(region).isEmpty());
    final String key = "KEY-testReinitialization";
    final String val = "VALUE-testReinitialization";
    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Data") {

        public void run2() throws CacheException {
            createConnection(new String[] {});
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            fac.setDataPolicy(DataPolicy.REPLICATE);
            RegionAttributes attr = fac.create();
            Region region = createRootRegion(name, attr);
            region.put(key, val);
        }
    });
    final Region finalRegion = region;
    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {
                RequiredRoles.waitForRequiredRoles(finalRegion, -1);
            } catch (InterruptedException e) {
                fail("interrupted");
            } catch (RegionReinitializedException e) {
            }
        }
    });
    thread.start();
    // create role and verify reinitialization took place
    Host.getHost(0).getVM(1).invokeAsync(new CacheSerializableRunnable("Create Role") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    ThreadUtils.join(thread, 30 * 1000);
    assertTrue(region.isDestroyed());
    try {
        region.put("fee", "fi");
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    try {
        RequiredRoles.checkForRequiredRoles(region);
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    try {
        Role role = (Role) requiredRolesSet.iterator().next();
        RequiredRoles.isRoleInRegionMembership(region, role);
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    region = getRootRegion(name);
    assertNotNull(region);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    assertTrue(RequiredRoles.checkForRequiredRoles(region).isEmpty());
    assertNotNull(region.getEntry(key));
    assertEquals(val, region.getEntry(key).getValue());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Role(org.apache.geode.distributed.Role) InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) AttributesFactory(org.apache.geode.cache.AttributesFactory) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 5 with AbstractRegion

use of org.apache.geode.internal.cache.AbstractRegion in project geode by apache.

the class TXJUnitTest method testTXAndQueries.

@Test
public void testTXAndQueries() throws CacheException, QueryException {
    IndexManager.TEST_RANGEINDEX_ONLY = true;
    try {
        final QueryService qs = this.cache.getQueryService();
        final String fromClause = this.region.getFullPath() + " value ";
        final String qstr = "SELECT DISTINCT * FROM " + fromClause;
        // Create a region with async index updates
        AttributesFactory af = new AttributesFactory(this.region.getAttributes());
        af.setIndexMaintenanceSynchronous(false);
        final Region aIregion = this.cache.createRegion(getUniqueName(), af.create());
        final String aIfromClause = aIregion.getFullPath() + " value ";
        final String aIqstr = "SELECT DISTINCT * FROM " + aIfromClause;
        // Confirm base functionality for query results
        this.region.put("qkey0", "qval0");
        this.region.put("qkey1", "qval01");
        Query q = qs.newQuery(qstr);
        SelectResults res = (SelectResults) q.execute();
        assertEquals(2, res.size());
        String val;
        for (Iterator resI = res.iterator(); resI.hasNext(); ) {
            val = (String) resI.next();
            assertTrue("Value: " + val + " does not start with qval", val.startsWith("qval"));
        }
        q = qs.newQuery(qstr + " where value.length > 6");
        res = (SelectResults) q.execute();
        assertEquals(0, res.size());
        aIregion.put("qkey0", "qval0");
        aIregion.put("qkey1", "qval01");
        q = qs.newQuery(aIqstr);
        res = (SelectResults) q.execute();
        assertEquals(2, res.size());
        for (Iterator resI = res.iterator(); resI.hasNext(); ) {
            val = (String) resI.next();
            assertTrue("Value: " + val + " does not start with qval", val.startsWith("qval"));
        }
        q = qs.newQuery(qstr + " where value.length > 6");
        res = (SelectResults) q.execute();
        assertEquals(0, res.size());
        // Test query results in a transaction
        this.txMgr.begin();
        Query q1 = qs.newQuery(qstr);
        this.region.put("noQkey2", "noQval2");
        res = (SelectResults) q1.execute();
        assertEquals(2, res.size());
        for (Iterator resI = res.iterator(); resI.hasNext(); ) {
            val = (String) resI.next();
            assertTrue("Value: " + val + " does not start with qval", val.startsWith("qval"));
        }
        Query aIq1 = qs.newQuery(aIqstr);
        aIregion.put("noQkey2", "noQval2");
        res = (SelectResults) aIq1.execute();
        assertEquals(2, res.size());
        for (Iterator resI = res.iterator(); resI.hasNext(); ) {
            val = (String) resI.next();
            assertTrue("Value: " + val + " does not start with qval", val.startsWith("qval"));
        }
        Query q2 = qs.newQuery(qstr + " where value.length > 6");
        res = (SelectResults) q2.execute();
        assertEquals(0, res.size());
        Query aIq2 = qs.newQuery(aIqstr + " where value.length > 6");
        res = (SelectResults) aIq2.execute();
        assertEquals(0, res.size());
        this.txMgr.commit();
        res = (SelectResults) q1.execute();
        assertEquals(3, res.size());
        res = (SelectResults) q2.execute();
        assertEquals(1, res.size());
        res = (SelectResults) aIq1.execute();
        assertEquals(3, res.size());
        res = (SelectResults) aIq2.execute();
        assertEquals(1, res.size());
        this.region.destroy("noQkey2");
        aIregion.destroy("noQkey2");
        // Confirm base functionality for index creation
        Index index0 = qs.createIndex("TXIndex0", IndexType.FUNCTIONAL, "value.length", fromClause);
        assertEquals(2, index0.getStatistics().getNumberOfKeys());
        assertEquals(2, index0.getStatistics().getNumberOfValues());
        // Shouldn't this be zero?
        assertEquals(2, index0.getStatistics().getNumUpdates());
        Index aIindex0 = qs.createIndex("aITXIndex0", IndexType.FUNCTIONAL, "value.length", aIfromClause);
        assertEquals(2, aIindex0.getStatistics().getNumberOfKeys());
        assertEquals(2, aIindex0.getStatistics().getNumberOfValues());
        // Shouldn't this be zero?
        assertEquals(2, aIindex0.getStatistics().getNumUpdates());
        q = qs.newQuery(qstr);
        res = (SelectResults) q.execute();
        assertEquals(2, res.size());
        assertEquals(0, index0.getStatistics().getTotalUses());
        aIq1 = qs.newQuery(aIqstr);
        res = (SelectResults) aIq1.execute();
        assertEquals(2, res.size());
        assertEquals(0, aIindex0.getStatistics().getTotalUses());
        final String val2 = "qval000002";
        this.region.put("qkey2", val2);
        // Shouldn't this be 1?
        assertEquals(3, index0.getStatistics().getNumUpdates());
        assertEquals(3, index0.getStatistics().getNumberOfKeys());
        assertEquals(3, index0.getStatistics().getNumberOfValues());
        aIregion.put("qkey2", val2);
        final IndexManager.IndexUpdaterThread upThread = ((AbstractRegion) aIregion).getIndexManager().getUpdaterThread();
        while (!upThread.isDone()) {
            pause(20);
        }
        // @todo asif: for some reason the value returned by getNumberOfKeys is unstable.
        // Even when the code waits for it to be the expected value it intermittently
        // will fail because it never gets to be the expected value.
        // This stat (in RangeIndex at least) is only updated when we add a new key
        // to the valueToEntriesMap. I do not see a place that we ever remove from
        // this map (even when we do a removeMapping).
        waitForUpdates(aIindex0, 3);
        waitForKeys(aIindex0, 3);
        // Shouldn't this be 1?
        assertEquals(3, aIindex0.getStatistics().getNumUpdates());
        assertEquals(3, aIindex0.getStatistics().getNumberOfKeys());
        assertEquals(3, aIindex0.getStatistics().getNumberOfValues());
        q = qs.newQuery("ELEMENT(" + qstr + " where value.length > 6)");
        assertEquals(val2, (String) q.execute());
        assertEquals(1, index0.getStatistics().getTotalUses());
        aIq1 = qs.newQuery("ELEMENT(" + aIqstr + " where value.length > 6)");
        assertEquals(val2, (String) aIq1.execute());
        this.region.destroy("qkey2");
        waitForKeys(index0, 2);
        // Shouldn't this be 1, again?
        assertEquals(2, index0.getStatistics().getNumberOfKeys());
        assertEquals(2, index0.getStatistics().getNumberOfValues());
        assertEquals(4, index0.getStatistics().getNumUpdates());
        aIregion.destroy("qkey2");
        while (!upThread.isDone()) {
            pause(20);
        }
        waitForUpdates(aIindex0, 4);
        // waitForKeys(aIindex0, 3);
        // assertIndexDetailsEquals(3, aIindex0.getStatistics().getNumberOfKeys()); // Shouldn't this
        // be 1, again?
        assertEquals(2, aIindex0.getStatistics().getNumberOfValues());
        assertEquals(4, aIindex0.getStatistics().getNumUpdates());
        // Test index creation
        this.txMgr.begin();
        this.region.destroy("qkey1");
        this.region.put("noQkey3", "noQval3");
        Index index1 = qs.createIndex("TXIndex1", IndexType.FUNCTIONAL, "value", fromClause);
        assertEquals(2, index1.getStatistics().getNumberOfKeys());
        assertEquals(2, index1.getStatistics().getNumberOfValues());
        assertEquals(2, index1.getStatistics().getNumUpdates());
        assertEquals(2, index0.getStatistics().getNumberOfKeys());
        assertEquals(2, index0.getStatistics().getNumberOfValues());
        assertEquals(4, index0.getStatistics().getNumUpdates());
        aIregion.destroy("qkey1");
        aIregion.put("noQkey3", "noQval3");
        Index aIindex1 = qs.createIndex("aITXIndex1", IndexType.FUNCTIONAL, "value", aIfromClause);
        while (!upThread.isDone()) {
            pause(20);
        }
        waitForUpdates(aIindex0, 4);
        waitForUpdates(aIindex1, 2);
        // waitForKeys(aIindex0, 3);
        // waitForKeys(aIindex1, 2);
        assertEquals(2, aIindex1.getStatistics().getNumberOfKeys());
        assertEquals(2, aIindex1.getStatistics().getNumberOfValues());
        assertEquals(2, aIindex1.getStatistics().getNumUpdates());
        // assertIndexDetailsEquals(3, aIindex0.getStatistics().getNumberOfKeys());
        assertEquals(2, aIindex0.getStatistics().getNumberOfValues());
        assertEquals(4, aIindex0.getStatistics().getNumUpdates());
        q = qs.newQuery(qstr);
        res = (SelectResults) q.execute();
        assertEquals(2, res.size());
        assertEquals(0, index1.getStatistics().getTotalUses());
        assertEquals(1, index0.getStatistics().getTotalUses());
        aIq1 = qs.newQuery(aIqstr);
        res = (SelectResults) aIq1.execute();
        assertEquals(2, res.size());
        assertEquals(0, aIindex1.getStatistics().getTotalUses());
        assertEquals(1, aIindex0.getStatistics().getTotalUses());
        q = qs.newQuery(qstr + " where value < 'q'");
        res = (SelectResults) q.execute();
        assertEquals(1, index1.getStatistics().getTotalUses());
        assertEquals(0, res.size());
        aIq1 = qs.newQuery(aIqstr + " where value < 'q'");
        res = (SelectResults) aIq1.execute();
        assertEquals(1, aIindex1.getStatistics().getTotalUses());
        assertEquals(0, res.size());
        this.region.put("noQkey4", "noQval4");
        assertEquals(2, index1.getStatistics().getNumberOfKeys());
        assertEquals(2, index1.getStatistics().getNumberOfValues());
        assertEquals(2, index1.getStatistics().getNumUpdates());
        assertEquals(2, index0.getStatistics().getNumberOfKeys());
        assertEquals(2, index0.getStatistics().getNumberOfValues());
        assertEquals(4, index0.getStatistics().getNumUpdates());
        aIregion.put("noQkey4", "noQval4");
        while (!upThread.isDone()) {
            pause(20);
        }
        waitForUpdates(aIindex0, 4);
        waitForUpdates(aIindex1, 2);
        waitForKeys(aIindex0, 2);
        // waitForKeys(aIindex1, 2);
        // assertIndexDetailsEquals(2, aIindex1.getStatistics().getNumberOfKeys());
        assertEquals(2, aIindex1.getStatistics().getNumberOfValues());
        assertEquals(2, aIindex1.getStatistics().getNumUpdates());
        assertEquals(2, aIindex0.getStatistics().getNumberOfKeys());
        assertEquals(2, aIindex0.getStatistics().getNumberOfValues());
        assertEquals(4, aIindex0.getStatistics().getNumUpdates());
        q = qs.newQuery(qstr + " where value < 'q'");
        res = (SelectResults) q.execute();
        assertEquals(2, index1.getStatistics().getTotalUses());
        assertEquals(0, res.size());
        aIq1 = qs.newQuery(aIqstr + " where value <'q'");
        res = (SelectResults) aIq1.execute();
        assertEquals(2, aIindex1.getStatistics().getTotalUses());
        assertEquals(0, res.size());
        q = qs.newQuery(qstr + " where value.length > 6");
        res = (SelectResults) q.execute();
        assertEquals(2, index0.getStatistics().getTotalUses());
        assertEquals(0, res.size());
        aIq1 = qs.newQuery(aIqstr + " where value.length > 6");
        res = (SelectResults) aIq1.execute();
        assertEquals(2, aIindex0.getStatistics().getTotalUses());
        assertEquals(0, res.size());
        this.txMgr.commit();
        assertEquals(3, index1.getStatistics().getNumberOfKeys());
        // Shouldn't this be 4?
        assertEquals(3, index1.getStatistics().getNumberOfValues());
        assertEquals(5, index1.getStatistics().getNumUpdates());
        assertEquals(2, index0.getStatistics().getNumberOfKeys());
        // Shouldn't this be 4?
        assertEquals(3, index0.getStatistics().getNumberOfValues());
        assertEquals(7, index0.getStatistics().getNumUpdates());
        while (!upThread.isDone()) {
            pause(20);
        }
        waitForUpdates(aIindex0, 7);
        waitForUpdates(aIindex1, 5);
        // waitForKeys(aIindex0, 4); // sometimes 3 sometimes 4
        // waitForKeys(aIindex1, 3);
        assertEquals(3, aIindex1.getStatistics().getNumberOfKeys());
        // Shouldn't this be 4?
        assertEquals(3, aIindex1.getStatistics().getNumberOfValues());
        assertEquals(5, aIindex1.getStatistics().getNumUpdates());
        // assertIndexDetailsEquals(4, aIindex0.getStatistics().getNumberOfKeys());
        // Shouldn't this be 4?
        assertEquals(3, aIindex0.getStatistics().getNumberOfValues());
        assertEquals(7, aIindex0.getStatistics().getNumUpdates());
        q = qs.newQuery(qstr + " where value <'q'");
        res = (SelectResults) q.execute();
        assertEquals(3, index1.getStatistics().getTotalUses());
        assertEquals(2, res.size());
        aIq1 = qs.newQuery(aIqstr + " where value < 'q'");
        res = (SelectResults) aIq1.execute();
        assertEquals(3, aIindex1.getStatistics().getTotalUses());
        assertEquals(2, res.size());
        q = qs.newQuery(qstr + " where value.length > 6");
        res = (SelectResults) q.execute();
        assertEquals(3, index0.getStatistics().getTotalUses());
        assertEquals(2, res.size());
        aIq1 = qs.newQuery(aIqstr + " where value.length > 6");
        res = (SelectResults) aIq1.execute();
        assertEquals(3, aIindex0.getStatistics().getTotalUses());
        assertEquals(2, res.size());
    } finally {
        IndexManager.TEST_RANGEINDEX_ONLY = false;
    }
}
Also used : IndexManager(org.apache.geode.cache.query.internal.index.IndexManager) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Iterator(java.util.Iterator) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

AbstractRegion (org.apache.geode.internal.cache.AbstractRegion)5 AttributesFactory (org.apache.geode.cache.AttributesFactory)4 Region (org.apache.geode.cache.Region)4 LocalRegion (org.apache.geode.internal.cache.LocalRegion)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2 Set (java.util.Set)2 CacheException (org.apache.geode.cache.CacheException)2 MembershipAttributes (org.apache.geode.cache.MembershipAttributes)2 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 RegionReinitializedException (org.apache.geode.cache.RegionReinitializedException)2 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 AttributesMutator (org.apache.geode.cache.AttributesMutator)1