Search in sources :

Example 6 with AttributesMutator

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

the class ClientServerCCEDUnitTest method testClientDoesNotExpireEntryPrematurely.

@Test
public void testClientDoesNotExpireEntryPrematurely() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName() + "Region";
    final String key = "testKey";
    int port = createServerRegion(vm0, name, true);
    vm0.invoke(new SerializableCallable("create old entry") {

        public Object call() throws Exception {
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            r.put(key, "value");
            AbstractRegionEntry entry = (AbstractRegionEntry) r.basicGetEntry(key);
            // set an old timestamp in the entry - thirty minutes ago
            entry.getVersionStamp().setVersionTimeStamp(System.currentTimeMillis() - 1800000L);
            return null;
        }
    });
    createClientRegion(vm1, name, port, true, ClientRegionShortcut.CACHING_PROXY, false);
    vm1.invoke(new SerializableCallable("fetch entry and validate") {

        public Object call() throws Exception {
            final Long[] expirationTimeMillis = new Long[1];
            int expirationSeconds = 15;
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            AttributesMutator mutator = r.getAttributesMutator();
            mutator.setEntryIdleTimeout(new ExpirationAttributes(expirationSeconds, ExpirationAction.LOCAL_DESTROY));
            mutator.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterDestroy(EntryEvent event) {
                    expirationTimeMillis[0] = System.currentTimeMillis();
                }
            });
            // fetch the entry from the server and make sure it doesn't expire early
            if (!r.containsKey(key)) {
                r.get(key);
            }
            final long expirationTime = System.currentTimeMillis() + (expirationSeconds * 1000);
            Awaitility.await("waiting for object to expire").atMost(expirationSeconds * 2, TimeUnit.SECONDS).until(() -> {
                return expirationTimeMillis[0] != null;
            });
            disconnectFromDS();
            assertTrue("entry expired " + (expirationTime - expirationTimeMillis[0]) + " milliseconds early", expirationTimeMillis[0] >= expirationTime);
            return null;
        }
    });
    vm0.invoke(new SerializableRunnable() {

        public void run() {
            disconnectFromDS();
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) IgnoredException(org.apache.geode.test.dunit.IgnoredException) AbstractRegionEntry(org.apache.geode.internal.cache.AbstractRegionEntry) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 7 with AttributesMutator

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

the class RegionReliabilityTestCase method testFullAccessWithLocalRegionExpiration.

/**
   * Tests affect of FULL_ACCESS on local region expiration actions.
   */
@Test
public void testFullAccessWithLocalRegionExpiration() 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.FULL_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setRegionTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    waitForRegionDestroy(region);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 8 with AttributesMutator

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

the class RegionReliabilityTestCase method testNoAccessWithLocalRegionExpiration.

/**
   * Tests affect of NO_ACCESS on local region expiration actions.
   */
@Test
public void testNoAccessWithLocalRegionExpiration() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    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.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setRegionTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    // sleep and make sure region does not expire
    sleep(200);
    assertFalse(region.isDestroyed());
    // create region in vm1
    Host.getHost(0).getVM(1).invoke(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);
        }
    });
    waitForRegionDestroy(region);
}
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) AttributesFactory(org.apache.geode.cache.AttributesFactory) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 9 with AttributesMutator

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

the class EventIdOptimizationDUnitTest method verifyEventIdsOnClient2.

/**
   * Waits for the listener to receive all events and validates that no exception occurred in client
   */
public static void verifyEventIdsOnClient2() {
    await("Waiting for proceedForValidation to be true").atMost(2, MINUTES).until(() -> proceedForValidation);
    LogWriterUtils.getLogWriter().info("Starting validation on client2");
    if (validationFailed) {
        fail("\n The following eventIds recieved by client2 were not present in the eventId array sent by client1 \n" + failureMsg);
    }
    LogWriterUtils.getLogWriter().info("Validation complete on client2, goin to unregister listeners");
    Region region = cache.getRegion(Region.SEPARATOR + REGION_NAME);
    if (region != null && !region.isDestroyed()) {
        try {
            AttributesMutator mutator = region.getAttributesMutator();
            mutator.initCacheListeners(null);
        } catch (RegionDestroyedException ignore) {
        }
    }
    for (int i = 0; i < eventIds.length; i++) {
        region = cache.getRegion(Region.SEPARATOR + REGION_NAME + i);
        if (region != null && !region.isDestroyed()) {
            try {
                AttributesMutator mutator = region.getAttributesMutator();
                mutator.initCacheListeners(null);
            } catch (RegionDestroyedException ignore) {
            }
        }
    }
    LogWriterUtils.getLogWriter().info("Test completed, Unregistered the listeners");
}
Also used : RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) Region(org.apache.geode.cache.Region) AttributesMutator(org.apache.geode.cache.AttributesMutator)

Example 10 with AttributesMutator

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

the class IndexMaintenanceJUnitTest method testIndexMaintenanceOnCacheLoadedData.

/**
   * Tests Index maintenance on data loaded via cache loader
   */
@Test
public void testIndexMaintenanceOnCacheLoadedData() {
    try {
        IndexManager.TEST_RANGEINDEX_ONLY = true;
        Cache cache = CacheUtils.getCache();
        qs = cache.getQueryService();
        region = CacheUtils.createRegion("portfolio1", null);
        AttributesMutator am = region.getAttributesMutator();
        am.setCacheLoader(new CacheLoader() {

            public Object load(LoaderHelper helper) throws CacheLoaderException {
                String key = (String) helper.getKey();
                Portfolio p = new Portfolio(Integer.parseInt(key));
                return p;
            }

            public void close() {
            // TODO Auto-generated method stub
            }
        });
        Index i1 = qs.createIndex("indx1", IndexType.FUNCTIONAL, "pf.getID()", "/portfolio1 pf");
        List keys = new ArrayList();
        keys.add("1");
        keys.add("2");
        keys.add("3");
        keys.add("4");
        region.getAll(keys);
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    }
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) Portfolio(org.apache.geode.cache.query.data.Portfolio) ArrayList(java.util.ArrayList) CacheLoader(org.apache.geode.cache.CacheLoader) Index(org.apache.geode.cache.query.Index) ArrayList(java.util.ArrayList) List(java.util.List) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) Cache(org.apache.geode.cache.Cache) AttributesMutator(org.apache.geode.cache.AttributesMutator) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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