Search in sources :

Example 66 with AttributesFactory

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

the class RegionTestCase method testEntryTtlDestroy.

/**
   * Tests that an entry in a region expires with a destroy after a given time to live.
   */
@Test
public void testEntryTtlDestroy() throws CacheException, InterruptedException {
    final String name = this.getUniqueName();
    // ms
    final int timeout = 20;
    final String key = "KEY";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.DESTROY);
    factory.setEntryTimeToLive(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
        ExpiryTask.suspendExpiration();
        Region.Entry entry = null;
        long tilt;
        try {
            region.put(key, value);
            tilt = System.currentTimeMillis();
            entry = region.getEntry(key);
            assertNotNull(entry.getValue());
        } finally {
            ExpiryTask.permitExpiration();
        }
        waitForDestroy(entry, tilt);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 67 with AttributesFactory

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

the class SlowRecDUnitTest method doTestPartialMessage.

private void doTestPartialMessage() throws Exception {
    final AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_NO_ACK);
    factory.setEnableAsyncConflation(true);
    final Region r = createRootRegion("slowrec", factory.create());
    final DM dm = getSystem().getDistributionManager();
    final DMStats stats = dm.getStats();
    // set others before vm0 connects
    long initialQueuedMsgs = stats.getAsyncQueuedMsgs();
    // create receiver in vm0 with queuing enabled
    final Properties p = new Properties();
    // 4 sec
    p.setProperty(ASYNC_DISTRIBUTION_TIMEOUT, String.valueOf(1000 * 4));
    // max value
    p.setProperty(ASYNC_QUEUE_TIMEOUT, "86400000");
    // max value
    p.setProperty(ASYNC_MAX_QUEUE_SIZE, "1024");
    getOtherVm().invoke(new CacheSerializableRunnable("Create other vm") {

        public void run2() throws CacheException {
            getSystem(p);
            AttributesFactory af = new AttributesFactory();
            af.setScope(Scope.DISTRIBUTED_NO_ACK);
            af.setDataPolicy(DataPolicy.REPLICATE);
            doTestPartialMessage_Listener = new ControlListener();
            af.setCacheListener(doTestPartialMessage_Listener);
            createRootRegion("slowrec", af.create());
        }
    });
    // put vm0 cache listener into wait
    LogWriterUtils.getLogWriter().info("[testPartialMessage] about to put vm0 into wait");
    // 5 minutes
    final int millisToWait = 1000 * 60 * 5;
    r.put(KEY_WAIT, new Integer(millisToWait));
    // build up queue size
    LogWriterUtils.getLogWriter().info("[testPartialMessage] building up queue size...");
    final Object key = "key";
    final int socketBufferSize = getSystem().getConfig().getSocketBufferSize();
    final int VALUE_SIZE = socketBufferSize * 3;
    // 1024 * 20; // 20 KB
    final byte[] value = new byte[VALUE_SIZE];
    int count = 0;
    while (stats.getAsyncQueuedMsgs() == initialQueuedMsgs) {
        count++;
        r.put(key, value, new Integer(count));
    }
    final int partialId = count;
    assertEquals(0, stats.getAsyncConflatedMsgs());
    LogWriterUtils.getLogWriter().info("[testPartialMessage] After " + count + " puts of size " + VALUE_SIZE + " slowrec mode kicked in with queue size=" + stats.getAsyncQueueSize());
    Wait.pause(2000);
    // conflate 10 times
    while (stats.getAsyncConflatedMsgs() < 10) {
        count++;
        r.put(key, value, new Integer(count));
        if (count == partialId + 1) {
            assertEquals(initialQueuedMsgs + 2, stats.getAsyncQueuedMsgs());
            assertEquals(0, stats.getAsyncConflatedMsgs());
        } else if (count == partialId + 2) {
            assertEquals(initialQueuedMsgs + 2, stats.getAsyncQueuedMsgs());
            assertEquals(1, stats.getAsyncConflatedMsgs());
        }
    }
    final int conflateId = count;
    final int[] expectedArgs = { partialId, conflateId };
    // send notify to vm0
    LogWriterUtils.getLogWriter().info("[testPartialMessage] wake up vm0");
    getOtherVm().invoke(new SerializableRunnable("Wake up other vm") {

        public void run() {
            synchronized (doTestPartialMessage_Listener.CONTROL_LOCK) {
                doTestPartialMessage_Listener.CONTROL_LOCK.notify();
            }
        }
    });
    // wait for queue to be flushed
    LogWriterUtils.getLogWriter().info("[testPartialMessage] wait for vm0");
    getOtherVm().invoke(new SerializableRunnable("Wait for other vm") {

        public void run() {
            try {
                synchronized (doTestPartialMessage_Listener.CONTROL_LOCK) {
                    boolean done = false;
                    while (!done) {
                        if (doTestPartialMessage_Listener.callbackArguments.size() > 0) {
                            CallbackWrapper last = (CallbackWrapper) doTestPartialMessage_Listener.callbackArguments.getLast();
                            Integer lastId = (Integer) last.callbackArgument;
                            if (lastId.intValue() == conflateId) {
                                done = true;
                            } else {
                                doTestPartialMessage_Listener.CONTROL_LOCK.wait(millisToWait);
                            }
                        } else {
                            doTestPartialMessage_Listener.CONTROL_LOCK.wait(millisToWait);
                        }
                    }
                }
            } catch (InterruptedException ignore) {
                fail("interrupted");
            }
        }
    });
    // assert values on both listeners
    LogWriterUtils.getLogWriter().info("[testPartialMessage] assert callback arguments");
    getOtherVm().invoke(new SerializableRunnable("Assert callback arguments") {

        public void run() {
            synchronized (doTestPartialMessage_Listener.CONTROL_LOCK) {
                LogWriterUtils.getLogWriter().info("[testPartialMessage] " + "doTestPartialMessage_Listener.callbackArguments=" + doTestPartialMessage_Listener.callbackArguments);
                assertEquals(doTestPartialMessage_Listener.callbackArguments.size(), doTestPartialMessage_Listener.callbackTypes.size());
                int i = 0;
                Iterator argIter = doTestPartialMessage_Listener.callbackArguments.iterator();
                Iterator typeIter = doTestPartialMessage_Listener.callbackTypes.iterator();
                while (argIter.hasNext()) {
                    CallbackWrapper wrapper = (CallbackWrapper) argIter.next();
                    Integer arg = (Integer) wrapper.callbackArgument;
                    // Integer type
                    typeIter.next();
                    if (arg.intValue() < partialId) {
                        continue;
                    }
                    assertEquals(new Integer(expectedArgs[i]), arg);
                    // assertIndexDetailsEquals(CALLBACK_UPDATE_INTEGER, type);
                    i++;
                }
            }
        }
    });
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) DM(org.apache.geode.distributed.internal.DM) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AttributesFactory(org.apache.geode.cache.AttributesFactory) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region)

Example 68 with AttributesFactory

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

the class RegionTestCase method testEntryTtlInvalidate.

/**
   * Tests that an entry in a region expires with an invalidation after a given time to live.
   */
@Test
public void testEntryTtlInvalidate() throws CacheException {
    final String name = this.getUniqueName();
    // ms!
    final int timeout = 20;
    final String key = "KEY";
    final String value = "VALUE";
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
    factory.setEntryTimeToLive(expire);
    factory.setStatisticsEnabled(true);
    RegionAttributes attrs = factory.create();
    Region region = null;
    /**
     * Crank up the expiration so test runs faster. This property only needs to be set while the
     * region is created
     */
    System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
    try {
        region = createRegion(name, attrs);
    } finally {
        System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
    }
    ExpiryTask.suspendExpiration();
    Region.Entry entry = null;
    long tilt;
    try {
        region.put(key, value);
        tilt = System.currentTimeMillis() + timeout;
        entry = region.getEntry(key);
        assertNotNull(entry.getValue());
    } finally {
        ExpiryTask.permitExpiration();
    }
    waitForInvalidate(entry, tilt);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Entry(org.apache.geode.cache.Region.Entry) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 69 with AttributesFactory

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

the class RegionTestCase method testRegionIdleInvalidate.

/**
   * Tests that a region that remains idle for a given amount of time is invalidated. Also tests
   * that accessing an entry of a region or a subregion counts as an access.
   */
@Test
public void testRegionIdleInvalidate() throws InterruptedException, CacheException {
    if (getRegionAttributes().getPartitionAttributes() != null) {
        // PR does not support INVALID ExpirationAction
        return;
    }
    final String name = this.getUniqueName();
    final String subname = this.getUniqueName() + "-SUB";
    // ms
    final int timeout = 22;
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    vm0.invoke(new CacheSerializableRunnable("testRegionIdleInvalidate") {

        public void run2() throws CacheException {
            TestCacheListener list = new TestCacheListener() {

                private int createCount = 0;

                public void afterInvalidate2(EntryEvent e) {
                    e.getRegion().getCache().getLogger().info("invalidate2 key=" + e.getKey());
                }

                public void afterRegionInvalidate2(RegionEvent e) {
                }

                public void afterUpdate2(EntryEvent e) {
                    // Clear the flag
                    this.wasInvoked();
                }

                public void afterCreate2(EntryEvent e) {
                    this.createCount++;
                    // we only expect one create; all the rest should be updates
                    assertEquals(1, this.createCount);
                    // Clear the flag
                    this.wasInvoked();
                }
            };
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            ExpirationAttributes expire = new ExpirationAttributes(timeout, ExpirationAction.INVALIDATE);
            factory.setRegionIdleTimeout(expire);
            factory.setStatisticsEnabled(true);
            RegionAttributes subRegAttrs = factory.create();
            factory.setCacheListener(list);
            RegionAttributes attrs = factory.create();
            Region region = null;
            Region sub = null;
            Region.Entry entry = null;
            long tilt;
            System.setProperty(LocalRegion.EXPIRY_MS_PROPERTY, "true");
            ExpiryTask.suspendExpiration();
            try {
                region = createRegion(name, attrs);
                region.put(key, value);
                tilt = System.currentTimeMillis() + timeout;
                entry = region.getEntry(key);
                assertEquals(value, entry.getValue());
                sub = region.createSubregion(subname, subRegAttrs);
            } finally {
                System.getProperties().remove(LocalRegion.EXPIRY_MS_PROPERTY);
                ExpiryTask.permitExpiration();
            }
            waitForInvalidate(entry, tilt, 10);
            assertTrue(list.waitForInvocation(333));
            // The next phase of the test verifies that a get will cause the
            // expiration time to be extended.
            // For this phase we don't worry about actually expiring but just
            // making sure the expiration time gets extended.
            final int EXPIRATION_MS = 9000;
            region.getAttributesMutator().setRegionIdleTimeout(new ExpirationAttributes(EXPIRATION_MS, ExpirationAction.INVALIDATE));
            LocalRegion lr = (LocalRegion) region;
            {
                ExpiryTask expiryTask = lr.getRegionIdleExpiryTask();
                region.put(key, value);
                long createExpiry = expiryTask.getExpirationTime();
                long changeTime = Wait.waitForExpiryClockToChange(lr, createExpiry - EXPIRATION_MS);
                region.put(key, "VALUE2");
                long putExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected putBaseExpiry=" + (putExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (putExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected putExpiry=" + putExpiry + " to be > than createExpiry=" + createExpiry, (putExpiry - createExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, putExpiry - EXPIRATION_MS);
                region.get(key);
                long getExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected getBaseExpiry=" + (getExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (getExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected getExpiry=" + getExpiry + " to be > than putExpiry=" + putExpiry, (getExpiry - putExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, getExpiry - EXPIRATION_MS);
                sub.put(key, value);
                long subPutExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subPutBaseExpiry=" + (subPutExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subPutExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subPutExpiry=" + subPutExpiry + " to be > than getExpiry=" + getExpiry, (subPutExpiry - getExpiry) > 0);
                changeTime = Wait.waitForExpiryClockToChange(lr, subPutExpiry - EXPIRATION_MS);
                sub.get(key);
                long subGetExpiry = expiryTask.getExpirationTime();
                assertTrue("CLOCK went back in time! Expected subGetBaseExpiry=" + (subGetExpiry - EXPIRATION_MS) + " to be >= than changeTime=" + changeTime, (subGetExpiry - EXPIRATION_MS - changeTime) >= 0);
                assertTrue("expected subGetExpiry=" + subGetExpiry + " to be > than subPutExpiry=" + subPutExpiry, (subGetExpiry - subPutExpiry) > 0);
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) RegionEvent(org.apache.geode.cache.RegionEvent) Entry(org.apache.geode.cache.Region.Entry) ExpiryTask(org.apache.geode.internal.cache.ExpiryTask) EntryExpiryTask(org.apache.geode.internal.cache.EntryExpiryTask) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 70 with AttributesFactory

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

the class RegionReliabilityTestCase method testCommitDistributionException.

@Test
public void testCommitDistributionException() throws Exception {
    if (getRegionScope().isGlobal())
        // skip test under Global
        return;
    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);
    GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {

        public void afterRemoteRegionDeparture(RegionEvent event) {
            synchronized (detectedDeparture_testCommitDistributionException) {
                detectedDeparture_testCommitDistributionException[0] = Boolean.TRUE;
                detectedDeparture_testCommitDistributionException.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);
    // use vm1 to create role
    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);
        }
    });
    // define the afterReleaseLocalLocks callback
    SerializableRunnableIF removeRequiredRole = new SerializableRunnableIF() {

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

                public void run() {
                    getRootRegion(name).close();
                }
            });
            try {
                synchronized (detectedDeparture_testCommitDistributionException) {
                    while (detectedDeparture_testCommitDistributionException[0] == Boolean.FALSE) {
                        detectedDeparture_testCommitDistributionException.wait();
                    }
                }
            } catch (InterruptedException e) {
                fail("interrupted");
            }
        }
    };
    // define the add and remove expected exceptions
    final String expectedExceptions = "org.apache.geode.internal.cache.CommitReplyException";
    SerializableRunnable addExpectedExceptions = new CacheSerializableRunnable("addExpectedExceptions") {

        public void run2() throws CacheException {
            getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
        }
    };
    SerializableRunnable removeExpectedExceptions = new CacheSerializableRunnable("removeExpectedExceptions") {

        public void run2() throws CacheException {
            getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
        }
    };
    // perform the actual test...
    CacheTransactionManager ctm = cache.getCacheTransactionManager();
    ctm.begin();
    TXStateInterface txStateProxy = ((TXManagerImpl) ctm).getTXState();
    ((TXStateProxyImpl) txStateProxy).forceLocalBootstrap();
    TXState txState = (TXState) ((TXStateProxyImpl) txStateProxy).getRealDeal(null, null);
    txState.setBeforeSend(() -> {
        try {
            removeRequiredRole.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    // now start a transaction and commit it
    region.put("KEY", "VAL");
    addExpectedExceptions.run();
    Host.getHost(0).getVM(1).invoke(addExpectedExceptions);
    try {
        ctm.commit();
        fail("Should have thrown CommitDistributionException");
    } catch (CommitDistributionException e) {
    // pass
    } finally {
        removeExpectedExceptions.run();
        Host.getHost(0).getVM(1).invoke(removeExpectedExceptions);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) TXStateProxyImpl(org.apache.geode.internal.cache.TXStateProxyImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TXState(org.apache.geode.internal.cache.TXState) AttributesFactory(org.apache.geode.cache.AttributesFactory) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) HashSet(java.util.HashSet) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) TXStateInterface(org.apache.geode.internal.cache.TXStateInterface) 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) 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) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

AttributesFactory (org.apache.geode.cache.AttributesFactory)1156 Region (org.apache.geode.cache.Region)565 Test (org.junit.Test)550 RegionAttributes (org.apache.geode.cache.RegionAttributes)471 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)468 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)356 VM (org.apache.geode.test.dunit.VM)304 Host (org.apache.geode.test.dunit.Host)288 Properties (java.util.Properties)244 CacheException (org.apache.geode.cache.CacheException)243 Cache (org.apache.geode.cache.Cache)229 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)206 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)201 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)199 LocalRegion (org.apache.geode.internal.cache.LocalRegion)173 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)156 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)139 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)129 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)126 IgnoredException (org.apache.geode.test.dunit.IgnoredException)125