Search in sources :

Example 11 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class OplogJUnitTest method testPutClearPut.

// @todo this test is failing for some reason. Does it need to be fixed?
/**
   * Bug test to reproduce the bug 37261. The scenario which this test depicts is not actually the
   * cause of Bug 37261. This test validates the case where a synch persist only entry1 is created
   * in Oplog1. A put operation on entry2 causes the switch , but before Oplog1 is rolled , the
   * entry1 is modified so that it references Oplog2. Thus in effect roller will skip rolling entry1
   * when rolling Oplog1.Now entry1 is deleted in Oplog2 and then a rolling happens. There should
   * not be any error
   */
// @Test
// public void testBug37261_1()
// {
// CacheObserver old = CacheObserverHolder.getInstance();
// try {
// // Create a persist only region with rolling true
// diskProps.setPersistBackup(true);
// diskProps.setRolling(true);
// diskProps.setCompactionThreshold(100);
// diskProps.setMaxOplogSize(1024);
// diskProps.setSynchronous(true);
// this.proceed = false;
// region = DiskRegionHelperFactory.getSyncPersistOnlyRegion(cache,
// diskProps);
// // create an entry 1 in oplog1,
// region.put("key1", new byte[800]);
// // Asif the second put will cause a switch to oplog 2 & also cause the
// // oplog1
// // to be submitted to the roller
// LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
// CacheObserverHolder.setInstance(new CacheObserverAdapter() {
// public void beforeGoingToCompact()
// {
// // modify entry 1 so that it points to the new switched oplog
// Thread th = new Thread(new Runnable() {
// public void run()
// {
// region.put("key1", new byte[400]);
// }
// });
// th.start();
// try {
// DistributedTestCase.join(th, 30 * 1000, null);
// }
// catch (Exception e) {
// e.printStackTrace();
// failureCause = e.toString();
// failure = true;
// }
// }
// public void afterHavingCompacted()
// {
// synchronized (OplogJUnitTest.this) {
// rollerThread = Thread.currentThread();
// OplogJUnitTest.this.notify();
// OplogJUnitTest.this.proceed = true;
// }
// }
// });
// region.put("key2", new byte[300]);
// synchronized (this) {
// if (!this.proceed) {
// this.wait(15000);
// assertTrue(this.proceed);
// }
// }
// this.proceed = false;
// // Asif Delete the 1st entry
// region.destroy("key1");
// CacheObserverHolder.setInstance(new CacheObserverAdapter() {
// public void afterHavingCompacted()
// {
// synchronized (OplogJUnitTest.this) {
// OplogJUnitTest.this.notify();
// OplogJUnitTest.this.proceed = true;
// }
// }
// });
// // Coz another switch and wait till rolling done
// region.put("key2", new byte[900]);
// synchronized (this) {
// if (!this.proceed) {
// this.wait(15000);
// assertFalse(this.proceed);
// }
// }
// // Check if the roller is stil alive
// assertTrue(rollerThread.isAlive());
// }
// catch (Exception e) {
// e.printStackTrace();
// fail("Test failed du toe xception" + e);
// }
// finally {
// CacheObserverHolder.setInstance(old);
// LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
// }
// }
/**
   * Tests the condition when a 'put' is in progress and concurrent 'clear' and 'put'(on the same
   * key) occur. Thus if after Htree ref was set (in 'put'), the region got cleared (and same key
   * re-'put'), the entry will get recorded in the new Oplog without a corresponding create (
   * because the Oplogs containing create have already been deleted due to the clear operation).
   * This put should not proceed. Also, Region creation after closing should not give an exception.
   */
@Test
public void testPutClearPut() {
    try {
        // Create a persist only region with rolling true
        diskProps.setPersistBackup(true);
        diskProps.setRolling(true);
        diskProps.setMaxOplogSize(1024);
        diskProps.setSynchronous(true);
        this.proceed = false;
        region = DiskRegionHelperFactory.getSyncPersistOnlyRegion(cache, diskProps, Scope.LOCAL);
        final Thread clearOp = new Thread(new Runnable() {

            public void run() {
                try {
                    LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
                    region.clear();
                    region.put("key1", "value3");
                } catch (Exception e) {
                    testFailed = true;
                    failureCause = "Encountered Exception=" + e;
                }
            }
        });
        region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter() {

            @Override
            public void beforeUpdate(EntryEvent event) throws CacheWriterException {
                clearOp.start();
            }
        });
        try {
            ThreadUtils.join(clearOp, 30 * 1000);
        } catch (Exception e) {
            testFailed = true;
            failureCause = "Encountered Exception=" + e;
            e.printStackTrace();
        }
        region.create("key1", "value1");
        LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
        region.put("key1", "value2");
        if (!testFailed) {
            region.close();
            region = DiskRegionHelperFactory.getSyncPersistOnlyRegion(cache, diskProps, Scope.LOCAL);
        } else {
            fail(failureCause);
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("Test failed due to exception" + e);
    } finally {
        testFailed = false;
        proceed = false;
        LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = false;
    }
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) IOException(java.io.IOException) CommitConflictException(org.apache.geode.cache.CommitConflictException) DiskAccessException(org.apache.geode.cache.DiskAccessException) CacheWriterException(org.apache.geode.cache.CacheWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class MapInterfaceJUnitTest method testSetValue.

@Test
public void testSetValue() {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    DistributedSystem ds = DistributedSystem.connect(props);
    Cache cache = null;
    AttributesFactory factory = null;
    try {
        cache = CacheFactory.create(ds);
        factory = new AttributesFactory();
        factory.setScope(Scope.LOCAL);
        factory.setCacheWriter(new CacheWriterAdapter() {

            @Override
            public void beforeUpdate(EntryEvent event) throws CacheWriterException {
                synchronized (this) {
                    this.notify();
                    counter++;
                    MapInterfaceJUnitTest.this.hasBeenNotified = true;
                }
            }
        });
        region2 = cache.createRegion("testingRegion", factory.create());
        region2.put(new Integer(2), new Integer(2));
        this.hasBeenNotified = false;
        DoesPut doesPut = new DoesPut();
        new Thread(doesPut).start();
        synchronized (this) {
            if (!this.hasBeenNotified) {
                this.wait(3000);
            }
        }
        if (!this.hasBeenNotified) {
            fail(" beforeCreate call back did not come");
        }
        assertEquals(counter, 1);
    } catch (Exception e) {
        throw new AssertionError(" failed due to ", e);
    }
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) CacheWriterException(org.apache.geode.cache.CacheWriterException) AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) Cache(org.apache.geode.cache.Cache) CacheWriterException(org.apache.geode.cache.CacheWriterException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with CacheWriterAdapter

use of org.apache.geode.cache.util.CacheWriterAdapter in project geode by apache.

the class RemoteTransactionDUnitTest method testOriginRemoteIsTrueForRemoteReplicatedRegions.

@Test
public void testOriginRemoteIsTrueForRemoteReplicatedRegions() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore = host.getVM(1);
    initAccessorAndDataStore(accessor, datastore, 0);
    class OriginRemoteRRWriter extends CacheWriterAdapter {

        int fireC = 0;

        int fireD = 0;

        int fireU = 0;

        public void beforeCreate(EntryEvent event) throws CacheWriterException {
            if (!event.isOriginRemote()) {
                throw new CacheWriterException("SUP?? This CREATE is supposed to be isOriginRemote");
            }
            fireC++;
        }

        public void beforeDestroy(EntryEvent event) throws CacheWriterException {
            getGemfireCache().getLoggerI18n().fine("SWAP:writer:createEvent:" + event);
            if (!event.isOriginRemote()) {
                throw new CacheWriterException("SUP?? This DESTROY is supposed to be isOriginRemote");
            }
            fireD++;
        }

        public void beforeUpdate(EntryEvent event) throws CacheWriterException {
            if (!event.isOriginRemote()) {
                throw new CacheWriterException("SUP?? This UPDATE is supposed to be isOriginRemote");
            }
            fireU++;
        }
    }
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region refRegion = getCache().getRegion(D_REFERENCE);
            refRegion.getAttributesMutator().setCacheWriter(new OriginRemoteRRWriter());
            return null;
        }
    });
    accessor.invoke(new DoOpsInTX(OP.PUT));
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            TXStateProxy tx = mgr.internalSuspend();
            assertNotNull(tx);
            mgr.internalResume(tx);
            mgr.commit();
            return null;
        }
    });
    accessor.invoke(new DoOpsInTX(OP.DESTROY));
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            TXStateProxy tx = mgr.internalSuspend();
            assertNotNull(tx);
            mgr.internalResume(tx);
            mgr.commit();
            return null;
        }
    });
    accessor.invoke(new DoOpsInTX(OP.PUT));
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            TXStateProxy tx = mgr.internalSuspend();
            assertNotNull(tx);
            mgr.internalResume(tx);
            mgr.commit();
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region refRegion = getCache().getRegion(D_REFERENCE);
            OriginRemoteRRWriter w = (OriginRemoteRRWriter) refRegion.getAttributes().getCacheWriter();
            assertEquals(1, w.fireC);
            assertEquals(1, w.fireD);
            assertEquals(1, w.fireU);
            return null;
        }
    });
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) 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) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) CacheWriterException(org.apache.geode.cache.CacheWriterException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Aggregations

CacheWriterAdapter (org.apache.geode.cache.util.CacheWriterAdapter)13 CacheWriterException (org.apache.geode.cache.CacheWriterException)9 EntryEvent (org.apache.geode.cache.EntryEvent)9 Test (org.junit.Test)8 AttributesFactory (org.apache.geode.cache.AttributesFactory)7 Region (org.apache.geode.cache.Region)6 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 Properties (java.util.Properties)3 CacheException (org.apache.geode.cache.CacheException)3 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)3 CacheWriter (org.apache.geode.cache.CacheWriter)3 RegionAttributes (org.apache.geode.cache.RegionAttributes)3 RegionEvent (org.apache.geode.cache.RegionEvent)3 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)3 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)3 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RollbackException (javax.transaction.RollbackException)2