Search in sources :

Example 16 with AttributesMutator

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

the class SnapshotDUnitTest method testExportAndImportWithInvokeCallbacksEnabled.

@Test
public void testExportAndImportWithInvokeCallbacksEnabled() throws Exception {
    File dir = new File(getDiskDirs()[0], "callbacks");
    dir.mkdir();
    // save all regions
    CacheSnapshotService service = getCache().getSnapshotService();
    service.save(dir, SnapshotFormat.GEMFIRE);
    // update regions with data to be overwritten by import
    updateRegions();
    SerializableCallable callbacks = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            for (final RegionType rt : RegionType.values()) {
                for (final SerializationType st : SerializationType.values()) {
                    String name = "test-" + rt.name() + "-" + st.name();
                    Cache cache = getCache();
                    Region<Integer, MyObject> region = cache.getRegion(name);
                    // add CacheWriter and CacheListener
                    AttributesMutator mutator = region.getAttributesMutator();
                    mutator.setCacheWriter(new CountingCacheWriter());
                    mutator.addCacheListener(new CountingCacheListener());
                    // add AsyncEventQueue
                    addAsyncEventQueue(region, name);
                }
            }
            return null;
        }
    };
    // add callbacks
    forEachVm(callbacks, true);
    // load all regions with invoke callbacks enabled
    SnapshotOptions options = service.createOptions();
    options.invokeCallbacks(true);
    loadRegions(dir, options);
    // verify callbacks were invoked
    verifyCallbacksInvoked();
}
Also used : RegionType(org.apache.geode.cache.snapshot.RegionGenerator.RegionType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) File(java.io.File) SerializationType(org.apache.geode.cache.snapshot.RegionGenerator.SerializationType) Cache(org.apache.geode.cache.Cache) AttributesMutator(org.apache.geode.cache.AttributesMutator) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 17 with AttributesMutator

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

the class ParallelGatewaySenderQueue method addCacheListener.

@Override
public void addCacheListener(CacheListener listener) {
    for (PartitionedRegion prQ : this.userRegionNameToshadowPRMap.values()) {
        AttributesMutator mutator = prQ.getAttributesMutator();
        mutator.addCacheListener(listener);
    }
}
Also used : PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) AttributesMutator(org.apache.geode.cache.AttributesMutator)

Example 18 with AttributesMutator

use of org.apache.geode.cache.AttributesMutator 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 19 with AttributesMutator

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

the class RemoteTransactionDUnitTest method testPRTXGetOnLocalWithLoader.

@Test
public void testPRTXGetOnLocalWithLoader() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore = host.getVM(1);
    initAccessorAndDataStore(accessor, datastore, 0);
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesMutator am = getCache().getRegion(CUSTOMER).getAttributesMutator();
            am.setCacheLoader(new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    return new Customer("sup dawg", "addr");
                }

                public void close() {
                }
            });
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.begin();
            Region cust = getCache().getRegion(CUSTOMER);
            CustId custId = new CustId(6);
            Customer s = (Customer) cust.get(custId);
            mgr.commit();
            Customer s2 = (Customer) cust.get(custId);
            Customer expectedCust = new Customer("sup dawg", "addr");
            assertEquals(s, expectedCust);
            assertEquals(s2, expectedCust);
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            return null;
        }
    });
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) Customer(org.apache.geode.internal.cache.execute.data.Customer) CustId(org.apache.geode.internal.cache.execute.data.CustId) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) CacheLoader(org.apache.geode.cache.CacheLoader) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) AttributesMutator(org.apache.geode.cache.AttributesMutator) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 20 with AttributesMutator

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

the class RemoteTransactionDUnitTest method testPRTXGetOnRemoteWithLoader.

@Test
public void testPRTXGetOnRemoteWithLoader() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore = host.getVM(1);
    initAccessorAndDataStore(accessor, datastore, 0);
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesMutator am = getCache().getRegion(CUSTOMER).getAttributesMutator();
            am.setCacheLoader(new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    return new Customer("sup dawg", "add");
                }

                public void close() {
                }
            });
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTxManager();
            mgr.begin();
            Region cust = getCache().getRegion(CUSTOMER);
            Customer s = (Customer) cust.get(new CustId(8));
            assertEquals(new Customer("sup dawg", "add"), s);
            assertTrue(cust.containsKey(new CustId(8)));
            TXStateProxy tx = ((TXManagerImpl) mgr).internalSuspend();
            assertFalse(cust.containsKey(new CustId(8)));
            ((TXManagerImpl) mgr).internalResume(tx);
            mgr.commit();
            Customer s2 = (Customer) cust.get(new CustId(8));
            Customer ex = new Customer("sup dawg", "add");
            assertEquals(ex, s);
            assertEquals(ex, s2);
            return null;
        }
    });
}
Also used : Customer(org.apache.geode.internal.cache.execute.data.Customer) Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) LoaderHelper(org.apache.geode.cache.LoaderHelper) CustId(org.apache.geode.internal.cache.execute.data.CustId) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) AttributesMutator(org.apache.geode.cache.AttributesMutator) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Aggregations

AttributesMutator (org.apache.geode.cache.AttributesMutator)37 Region (org.apache.geode.cache.Region)24 Test (org.junit.Test)19 LocalRegion (org.apache.geode.internal.cache.LocalRegion)18 AttributesFactory (org.apache.geode.cache.AttributesFactory)16 RegionAttributes (org.apache.geode.cache.RegionAttributes)13 EntryEvent (org.apache.geode.cache.EntryEvent)11 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)11 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)11 Properties (java.util.Properties)8 CacheException (org.apache.geode.cache.CacheException)8 CacheLoader (org.apache.geode.cache.CacheLoader)8 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)8 HashSet (java.util.HashSet)7 Set (java.util.Set)7 LoaderHelper (org.apache.geode.cache.LoaderHelper)7 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)7 AbstractRegion (org.apache.geode.internal.cache.AbstractRegion)7 CacheListener (org.apache.geode.cache.CacheListener)6 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)6