Search in sources :

Example 1 with TransactionListenerAdapter

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

the class TXJUnitTest method testNoopInvalidates.

@Test
public void testNoopInvalidates() throws CacheException {
    final CachePerfStats stats = this.cache.getCachePerfStats();
    TransactionListener tl = new TransactionListenerAdapter() {

        @Override
        public void afterRollback(TransactionEvent event) {
            te = event;
        }
    };
    this.txMgr.addListener(tl);
    // Make sure invalidates done on invalid entries are noops
    {
        // distributed invalidate
        // first make sure invalidate is counted as a change
        int txRollbackChanges = stats.getTxRollbackChanges();
        this.region.create("key1", "value1");
        this.txMgr.begin();
        this.region.invalidate("key1");
        this.txMgr.rollback();
        assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
        assertEquals(1, te.getEvents().size());
        this.region.destroy("key1");
        this.region.create("key1", "value1");
        this.txMgr.begin();
        this.region.invalidate("key1");
        this.txMgr.commit();
        assertEquals(1, te.getEvents().size());
        this.region.destroy("key1");
        // now make sure a committed entry that is invalid is not counted as a change
        txRollbackChanges = stats.getTxRollbackChanges();
        this.region.create("key1", "value1");
        this.region.invalidate("key1");
        this.txMgr.begin();
        this.region.invalidate("key1");
        this.txMgr.rollback();
        assertEquals(txRollbackChanges, stats.getTxRollbackChanges());
        assertEquals(0, te.getEvents().size());
        this.region.destroy("key1");
        this.region.create("key1", "value1");
        this.region.invalidate("key1");
        this.txMgr.begin();
        this.region.invalidate("key1");
        this.txMgr.commit();
        assertEquals(0, te.getEvents().size());
        this.region.destroy("key1");
        // now make sure that multiple invalidates of same entry are a single change
        txRollbackChanges = stats.getTxRollbackChanges();
        this.region.create("key1", "value1");
        this.txMgr.begin();
        this.region.invalidate("key1");
        this.region.invalidate("key1");
        this.region.invalidate("key1");
        this.txMgr.rollback();
        assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
        assertEquals(1, te.getEvents().size());
        this.region.destroy("key1");
        this.region.create("key1", "value1");
        this.txMgr.begin();
        this.region.invalidate("key1");
        this.region.invalidate("key1");
        this.region.invalidate("key1");
        this.txMgr.commit();
        assertEquals(1, te.getEvents().size());
        this.region.destroy("key1");
    }
    {
        // local invalidate
        // first make sure invalidate is counted as a change
        int txRollbackChanges = stats.getTxRollbackChanges();
        this.region.create("key1", "value1");
        this.txMgr.begin();
        this.region.localInvalidate("key1");
        this.txMgr.rollback();
        assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
        this.region.destroy("key1");
        // now make sure a committed entry that is invalid is not counted as a change
        txRollbackChanges = stats.getTxRollbackChanges();
        this.region.create("key1", "value1");
        this.region.localInvalidate("key1");
        this.txMgr.begin();
        this.region.localInvalidate("key1");
        this.txMgr.rollback();
        assertEquals(txRollbackChanges, stats.getTxRollbackChanges());
        this.region.destroy("key1");
        // now make sure that multiple localInvalidates of same entry are a single change
        txRollbackChanges = stats.getTxRollbackChanges();
        this.region.create("key1", "value1");
        this.txMgr.begin();
        this.region.localInvalidate("key1");
        this.region.localInvalidate("key1");
        this.region.localInvalidate("key1");
        this.txMgr.rollback();
        assertEquals(txRollbackChanges + 1, stats.getTxRollbackChanges());
        this.region.destroy("key1");
    }
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) TransactionEvent(org.apache.geode.cache.TransactionEvent) CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with TransactionListenerAdapter

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

the class CallbackArgDUnitTest method doCommitOtherVm.

private void doCommitOtherVm() {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            AttributesFactory af = new AttributesFactory();
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    assertEquals(callbackArg, e.getCallbackArgument());
                }
            };
            af.addCacheListener(cl1);
            af.setScope(Scope.DISTRIBUTED_ACK);
            Region r1 = createRootRegion("r1", af.create());
            Region r2 = r1.createSubregion("r2", af.create());
            Region r3 = r2.createSubregion("r3", af.create());
            CacheTransactionManager ctm = getCache().getCacheTransactionManager();
            TransactionListener tl1 = new TransactionListenerAdapter() {

                public void afterCommit(TransactionEvent e) {
                    assertEquals(6, e.getEvents().size());
                    Iterator it = e.getEvents().iterator();
                    while (it.hasNext()) {
                        EntryEvent ee = (EntryEvent) it.next();
                        assertEquals(callbackArg, ee.getCallbackArgument());
                        assertEquals(true, ee.isCallbackArgumentAvailable());
                    }
                }
            };
            ctm.addListener(tl1);
            ctm.begin();
            r2.put("b", "value1", callbackArg);
            r3.put("c", "value2", callbackArg);
            r1.put("a", "value3", callbackArg);
            r1.put("a2", "value4", callbackArg);
            r3.put("c2", "value5", callbackArg);
            r2.put("b2", "value6", callbackArg);
            ctm.commit();
        }
    });
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) CacheException(org.apache.geode.cache.CacheException) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region)

Example 3 with TransactionListenerAdapter

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

the class ClientServerTransactionDUnitTest method testCallbacks.

// Disabled due to bug 47083
@Ignore
@Test
public void testCallbacks() {
    Host host = Host.getHost(0);
    VM datastore = host.getVM(1);
    VM client = host.getVM(2);
    int port = createRegionsAndStartServer(datastore, false);
    createClientRegion(client, port, true);
    class ClientTxListener extends TransactionListenerAdapter {

        private boolean afterRollbackInvoked = false;

        boolean afterCommitInvoked = false;

        @Override
        public void afterCommit(TransactionEvent event) {
            afterCommitInvoked = true;
            verifyEvent(event);
        }

        @Override
        public void afterRollback(TransactionEvent event) {
            afterRollbackInvoked = true;
            verifyEvent(event);
        }

        protected void verifyEvent(TransactionEvent event) {
            Iterator it = event.getEvents().iterator();
            int i = 0;
            while (it.hasNext()) {
                EntryEvent ev = (EntryEvent) it.next();
                if (i == 0)
                    assertNull(ev.getNewValue());
                if (i > 1) {
                    assertEquals(new CustId(i), ev.getKey());
                    assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
                }
                assertTrue(ev.isOriginRemote());
                i++;
            }
            assertEquals(5, event.getEvents().size());
        }
    }
    class ClientTxWriter implements TransactionWriter {

        boolean txWriterCalled = false;

        public void close() {
        }

        public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
            txWriterCalled = true;
            Iterator it = event.getEvents().iterator();
            int i = 0;
            while (it.hasNext()) {
                EntryEvent ev = (EntryEvent) it.next();
                if (i == 0)
                    assertNull(ev.getNewValue());
                if (i > 1) {
                    assertEquals(new CustId(i), ev.getKey());
                    assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
                }
                assertTrue(ev.isOriginRemote());
                i++;
            }
            assertEquals(5, event.getEvents().size());
        }
    }
    class ClientListener extends CacheListenerAdapter {

        boolean invoked = false;

        @Override
        public void afterCreate(EntryEvent event) {
            CustId c = (CustId) event.getKey();
            if (c.getCustId() > 1) {
                invoked = true;
            }
            // we document that client transaction are proxied to the server
            // and that the callback should be handled appropriately (hence true)
            assertTrue(event.isOriginRemote());
            assertTrue(event.isOriginRemote());
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            assertFalse(event.isOriginRemote());
        }
    }
    class ClientWriter extends CacheWriterAdapter {

        @Override
        public void beforeCreate(EntryEvent event) throws CacheWriterException {
            CustId c = (CustId) event.getKey();
            if (c.getCustId() < 2) {
                return;
            }
            fail("cache writer should not be invoked");
        }

        @Override
        public void beforeUpdate(EntryEvent event) throws CacheWriterException {
            fail("cache writer should not be invoked");
        }
    }
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.addListener(new ClientTxListener());
            mgr.setWriter(new ClientTxWriter());
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            pr.getAttributesMutator().addCacheListener(new ServerListener());
            pr.getAttributesMutator().setCacheWriter(new ServerWriter());
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.addListener(new ClientTxListener());
            try {
                mgr.setWriter(new ClientTxWriter());
                fail("expected exception not thrown");
            } catch (IllegalStateException e) {
            }
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            pr.getAttributesMutator().addCacheListener(new ClientListener());
            pr.getAttributesMutator().setCacheWriter(new ClientWriter());
            return null;
        }
    });
    class doOps extends SerializableCallable {

        public doOps(boolean commit) {
            this.commit = commit;
        }

        boolean commit = false;

        public Object call() throws Exception {
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            pr.put(new CustId(0), new Customer("name0", "address0"));
            pr.put(new CustId(1), new Customer("name1", "address1"));
            mgr.begin();
            pr.invalidate(new CustId(0));
            pr.destroy(new CustId(1));
            for (int i = 2; i < 5; i++) {
                pr.put(new CustId(i), new Customer("name" + i, "address" + i));
            }
            if (commit) {
                mgr.commit();
            } else {
                mgr.rollback();
            }
            return null;
        }
    }
    client.invoke(new doOps(false));
    datastore.invoke(new SerializableCallable() {

        @SuppressWarnings("synthetic-access")
        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertTrue(l.afterRollbackInvoked);
            return null;
        }
    });
    client.invoke(new doOps(true));
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertTrue(l.afterCommitInvoked);
            ClientTxWriter w = (ClientTxWriter) mgr.getWriter();
            assertTrue(w.txWriterCalled);
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertFalse(l.afterCommitInvoked);
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            ClientListener cl = (ClientListener) pr.getAttributes().getCacheListeners()[0];
            assertTrue(cl.invoked);
            return null;
        }
    });
}
Also used : TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) Customer(org.apache.geode.internal.cache.execute.data.Customer) CustId(org.apache.geode.internal.cache.execute.data.CustId) Iterator(java.util.Iterator) CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) RollbackException(javax.transaction.RollbackException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) Ignore(org.junit.Ignore) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 4 with TransactionListenerAdapter

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

the class CallbackArgDUnitTest method doTest.

private void doTest() throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    af.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener cl1 = new CacheListenerAdapter() {

        public void afterCreate(EntryEvent e) {
            assertEquals(getCurrentExpectedKey(), e.getKey());
            assertEquals(callbackArg, e.getCallbackArgument());
            assertEquals(true, e.isCallbackArgumentAvailable());
        }
    };
    af.addCacheListener(cl1);
    Region r1 = createRootRegion("r1", af.create());
    Region r2 = r1.createSubregion("r2", af.create());
    r2.createSubregion("r3", af.create());
    TransactionListener tl1 = new TransactionListenerAdapter() {

        public void afterCommit(TransactionEvent e) {
            assertEquals(6, e.getEvents().size());
            ArrayList keys = new ArrayList();
            Iterator it = e.getEvents().iterator();
            while (it.hasNext()) {
                EntryEvent ee = (EntryEvent) it.next();
                keys.add(ee.getKey());
                assertEquals(callbackArg, ee.getCallbackArgument());
                assertEquals(true, ee.isCallbackArgumentAvailable());
            }
            assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
            CallbackArgDUnitTest.this.invokeCount = 1;
        }
    };
    CacheTransactionManager ctm = getCache().getCacheTransactionManager();
    ctm.addListener(tl1);
    this.invokeCount = 0;
    this.clCount = 0;
    this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
    doCommitOtherVm();
    assertEquals(1, this.invokeCount);
    assertEquals(6, this.clCount);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Example 5 with TransactionListenerAdapter

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

the class ProxyJUnitTest method setCallbacks.

private void setCallbacks(AttributesFactory af) {
    CacheListener cl1 = new CacheListener() {

        public void afterUpdate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterCreate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterInvalidate(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterDestroy(EntryEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionInvalidate(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionDestroy(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionClear(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionCreate(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void afterRegionLive(RegionEvent e) {
            clLastEvent = e;
            clInvokeCount++;
        }

        public void close() {
            clClosed = true;
        }
    };
    CacheWriter cw = new CacheWriter() {

        public void beforeUpdate(EntryEvent e) throws CacheWriterException {
            cwLastEvent = e;
            cwInvokeCount++;
        }

        public void beforeCreate(EntryEvent e) throws CacheWriterException {
            cwLastEvent = e;
            cwInvokeCount++;
        }

        public void beforeDestroy(EntryEvent e) throws CacheWriterException {
            cwLastEvent = e;
            cwInvokeCount++;
        }

        public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
            cwLastEvent = e;
            cwInvokeCount++;
        }

        public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
            cwLastEvent = e;
            cwInvokeCount++;
        }

        public void close() {
            cwClosed = true;
        }
    };
    af.addCacheListener(cl1);
    af.setCacheWriter(cw);
    {
        TransactionListener tl = new TransactionListenerAdapter() {

            public void afterCommit(TransactionEvent e) {
                tlLastEvents = e.getEvents();
                tlInvokeCount++;
            }

            public void close() {
                tlClosed = true;
            }

            ;
        };
        CacheTransactionManager ctm = this.c.getCacheTransactionManager();
        ctm.addListener(tl);
    }
}
Also used : TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter)

Aggregations

TransactionListenerAdapter (org.apache.geode.cache.util.TransactionListenerAdapter)6 Iterator (java.util.Iterator)4 TransactionEvent (org.apache.geode.cache.TransactionEvent)4 TransactionListener (org.apache.geode.cache.TransactionListener)4 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)4 AttributesFactory (org.apache.geode.cache.AttributesFactory)3 CacheListener (org.apache.geode.cache.CacheListener)3 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)3 EntryEvent (org.apache.geode.cache.EntryEvent)3 Region (org.apache.geode.cache.Region)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 RollbackException (javax.transaction.RollbackException)1 CacheException (org.apache.geode.cache.CacheException)1 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)1 CacheWriterAdapter (org.apache.geode.cache.util.CacheWriterAdapter)1 CachePerfStats (org.apache.geode.internal.cache.CachePerfStats)1 CustId (org.apache.geode.internal.cache.execute.data.CustId)1 Customer (org.apache.geode.internal.cache.execute.data.Customer)1