Search in sources :

Example 1 with Customer

use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.

the class PRFunctionExecutionDUnitTest method testExecutionOnAllNodes_LocalReadPR.

/**
   * Ensure that the execution is happening on all the PR as a whole with LocalReadPR as
   * LocalDataSet
   */
@Test
public void testExecutionOnAllNodes_LocalReadPR() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM datastore0 = host.getVM(0);
    final VM datastore1 = host.getVM(1);
    final VM datastore2 = host.getVM(2);
    final VM datastore3 = host.getVM(3);
    getCache();
    SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {

        public Object call() throws Exception {
            RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10);
            AttributesFactory raf = new AttributesFactory(ra);
            PartitionAttributesImpl pa = new PartitionAttributesImpl();
            pa.setAll(ra.getPartitionAttributes());
            pa.setTotalNumBuckets(17);
            pa.setPartitionResolver(new CustomerIDPartitionResolver("CustomerIDPartitionResolver"));
            raf.setPartitionAttributes(pa);
            getCache().createRegion(rName, raf.create());
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION3);
            FunctionService.registerFunction(function);
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(dataStoreCreate);
    datastore1.invoke(dataStoreCreate);
    datastore2.invoke(dataStoreCreate);
    datastore3.invoke(dataStoreCreate);
    Object o = datastore3.invoke(new SerializableCallable("Create data, invoke exectuable") {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            DistributedSystem.setThreadsSocketPolicy(false);
            final HashSet testKeys = new HashSet();
            // later check for them
            for (int i = 1; i <= 10; i++) {
                CustId custid = new CustId(i);
                Customer customer = new Customer("name" + i, "Address" + i);
                try {
                    pr.put(custid, customer);
                    assertNotNull(pr.get(custid));
                    assertEquals(customer, pr.get(custid));
                    testKeys.add(custid);
                } catch (Exception e) {
                    Assert.fail("putCustomerPartitionedRegion : failed while doing put operation in CustomerPartitionedRegion ", e);
                }
                LogWriterUtils.getLogWriter().fine("Customer :- { " + custid + " : " + customer + " }");
            }
            Function function = new TestFunction(true, TestFunction.TEST_FUNCTION3);
            FunctionService.registerFunction(function);
            Execution dataSet = FunctionService.onRegion(pr);
            ResultCollector rc1 = dataSet.setArguments(testKeys).execute(function.getId());
            List l = ((List) rc1.getResult());
            assertEquals(4, l.size());
            ArrayList vals = new ArrayList();
            Iterator itr = l.iterator();
            for (int i = 0; i < 4; i++) {
                vals.addAll((ArrayList) itr.next());
            }
            assertEquals(vals.size(), testKeys.size());
            return Boolean.TRUE;
        }
    });
    assertEquals(Boolean.TRUE, o);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) Customer(org.apache.geode.internal.cache.execute.data.Customer) ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) Execution(org.apache.geode.cache.execute.Execution) CustId(org.apache.geode.internal.cache.execute.data.CustId) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 2 with Customer

use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.

the class ClientServerTransactionDUnitTest method testClientRollsbackWithPutAllAndRI.

@Test
public void testClientRollsbackWithPutAllAndRI() throws Exception {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore = host.getVM(1);
    VM client = host.getVM(2);
    initAccessorAndDataStore(accessor, datastore, 0);
    int port = startServer(accessor);
    createClientRegion(client, port, false, true);
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getCache().getRegion(CUSTOMER);
            custRegion.getAttributesMutator().addCacheListener(new ClientListener());
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
            // Region<OrderId, Order> orderRegion = getCache().getRegion(ORDER);
            // Region<CustId,Customer> refRegion = getCache().getRegion(D_REFERENCE);
            CustId custId = new CustId(1);
            // OrderId orderId = new OrderId(1, custId);
            getCache().getCacheTransactionManager().begin();
            Map map = new HashMap();
            map.put(custId, new Customer("foo", "bar"));
            custRegion.putAll(map);
            // orderRegion.put(orderId, new Order("fooOrder"));
            // refRegion.put(custId, new Customer("foo", "bar"));
            getCache().getCacheTransactionManager().rollback();
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
            // Region<OrderId, Order> orderRegion = getCache().getRegion(ORDER);
            // Region<CustId,Customer> refRegion = getCache().getRegion(D_REFERENCE);
            ClientListener cl = (ClientListener) custRegion.getAttributes().getCacheListeners()[0];
            getCache().getLogger().info("SWAP:CLIENTinvoked:" + cl.invoked);
            assertTrue(!cl.invoked);
            assertTrue(!cl.putAllOp);
            assertEquals("it should be 0 but its:" + cl.invokeCount, 0, cl.invokeCount);
            return null;
        }
    });
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) HashMap(java.util.HashMap) Customer(org.apache.geode.internal.cache.execute.data.Customer) Map(java.util.Map) HashMap(java.util.HashMap) RollbackException(javax.transaction.RollbackException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 3 with Customer

use of org.apache.geode.internal.cache.execute.data.Customer 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 Customer

use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.

the class ClientServerTransactionDUnitTest method doFunctionWork.

private void doFunctionWork(final boolean commit) {
    disconnectAllFromDS();
    Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM client = host.getVM(2);
    final int port2 = createRegionsAndStartServer(server2, false);
    final int port = createRegionsAndStartServer(server1, true);
    IgnoredException.addIgnoredException("ClassCastException");
    SerializableRunnable suspectStrings = new SerializableRunnable("suspect string") {

        public void run() {
            InternalDistributedSystem.getLoggerI18n().convertToLogWriter().info("<ExpectedException action=add>" + "ClassCastException" + "</ExpectedException>" + "<ExpectedException action=add>" + "TransactionDataNodeHasDeparted" + "</ExpectedException>");
        }
    };
    server1.invoke(suspectStrings);
    server2.invoke(suspectStrings);
    try {
        client.invoke(new SerializableCallable() {

            public Object call() throws Exception {
                System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "bridge.disableShufflingOfEndpoints", "true");
                ClientCacheFactory ccf = new ClientCacheFactory();
                ccf.addPoolServer("localhost", /* getServerHostName(Host.getHost(0)) */
                port);
                setCCF(port2, ccf);
                // these settings were used to manually check that tx operation stats were being updated
                // ccf.set(STATISTIC_SAMPLING_ENABLED, "true");
                // ccf.set(STATISTIC_ARCHIVE_FILE, "clientStats.gfs");
                ClientCache cCache = getClientCache(ccf);
                ClientRegionFactory<Integer, String> crf = cCache.createClientRegionFactory(ClientRegionShortcut.PROXY);
                Region<Integer, String> customer = crf.create(CUSTOMER);
                cCache.getLogger().info("<ExpectedException action=add>" + "ClassCastException" + "</ExpectedException>" + "<ExpectedException action=add>" + "TransactionDataNodeHasDeparted" + "</ExpectedException>");
                Region cust = getCache().getRegion(CUSTOMER);
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().fine("SWAP:doing first get from client");
                assertNull(cust.get(new CustId(0)));
                assertNull(cust.get(new CustId(1)));
                ArrayList args = new ArrayList();
                args.add(new CustId(0));
                args.add(new Customer("name0", "address0"));
                args.add(null);
                List result = (List) FunctionService.onRegion(cust).setArguments(args).execute(new TXFunction()).getResult();
                TransactionId txId = (TransactionId) result.get(0);
                assertNotNull(txId);
                args = new ArrayList();
                args.add(new CustId(1));
                args.add(new Customer("name1", "address1"));
                args.add(txId);
                result = (List) FunctionService.onRegion(cust).setArguments(args).execute(new TXFunction()).getResult();
                TransactionId txId2 = (TransactionId) result.get(0);
                assertEquals(txId, txId2);
                // invoke ClientCommitFunction
                try {
                    if (commit) {
                        FunctionService.onServer(getCache()).setArguments(new CustId(0)).execute(new CommitFunction()).getResult();
                    } else {
                        FunctionService.onServer(getCache()).setArguments(new CustId(0)).execute(new RollbackFunction()).getResult();
                    }
                    fail("expected exception not thrown");
                } catch (FunctionException e) {
                    assertTrue(e.getCause() instanceof ClassCastException);
                }
                List list = null;
                if (commit) {
                    list = (List) FunctionService.onServer(getCache()).setArguments(txId).execute(new CommitFunction()).getResult();
                } else {
                    list = (List) FunctionService.onServer(getCache()).setArguments(txId).execute(new RollbackFunction()).getResult();
                }
                assertEquals(Boolean.TRUE, list.get(0));
                if (commit) {
                    assertEquals(new Customer("name0", "address0"), cust.get(new CustId(0)));
                    assertEquals(new Customer("name1", "address1"), cust.get(new CustId(1)));
                } else {
                    assertNull(cust.get(new CustId(0)));
                    assertNull(cust.get(new CustId(1)));
                }
                return null;
            }
        });
    } finally {
        suspectStrings = new SerializableRunnable("suspect string") {

            public void run() {
                InternalDistributedSystem.getLoggerI18n().convertToLogWriter().info("<ExpectedException action=remove>" + "ClassCastException" + "</ExpectedException>" + "<ExpectedException action=remove>" + "TransactionDataNodeHasDeparted" + "</ExpectedException>");
            }
        };
        server1.invoke(suspectStrings);
        server2.invoke(suspectStrings);
        client.invoke(suspectStrings);
    }
}
Also used : Customer(org.apache.geode.internal.cache.execute.data.Customer) ArrayList(java.util.ArrayList) RollbackException(javax.transaction.RollbackException) CustId(org.apache.geode.internal.cache.execute.data.CustId) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with Customer

use of org.apache.geode.internal.cache.execute.data.Customer in project geode by apache.

the class ClientServerTransactionDUnitTest method doTestFunctionFromPeer.

private void doTestFunctionFromPeer(final boolean commit) {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM peer1 = host.getVM(1);
    VM peer2 = host.getVM(2);
    createRegionOnServer(peer1);
    createRegionOnServer(peer2);
    createRegionOnServer(accessor, false, true);
    final TransactionId txId = (TransactionId) peer1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            PartitionedRegion r = (PartitionedRegion) getCache().getRegion(CUSTOMER);
            CustId cust = null;
            DistributedMember myId = getCache().getDistributedSystem().getDistributedMember();
            List<CustId> keys = new ArrayList<CustId>();
            for (int i = 0; i < 10; i++) {
                cust = new CustId(i);
                int bucketId = PartitionedRegionHelper.getHashKey(r, cust);
                if (!myId.equals(r.getBucketPrimary(bucketId))) {
                    keys.add(cust);
                }
            }
            assertTrue(keys.size() > 2);
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.begin();
            for (CustId custId : keys) {
                r.put(cust, new Customer("newname", "newaddress"));
            }
            return mgr.suspend();
        }
    });
    assertNotNull(txId);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Execution exe = FunctionService.onMember(((TXId) txId).getMemberId()).setArguments(txId);
            List list = null;
            if (commit) {
                list = (List) exe.execute(new CommitFunction()).getResult();
            } else {
                list = (List) exe.execute(new RollbackFunction()).getResult();
            }
            assertEquals(1, list.size());
            assertTrue((Boolean) list.get(0));
            return null;
        }
    });
}
Also used : Customer(org.apache.geode.internal.cache.execute.data.Customer) ArrayList(java.util.ArrayList) RollbackException(javax.transaction.RollbackException) CustId(org.apache.geode.internal.cache.execute.data.CustId) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Customer (org.apache.geode.internal.cache.execute.data.Customer)115 CustId (org.apache.geode.internal.cache.execute.data.CustId)114 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)73 Test (org.junit.Test)73 Region (org.apache.geode.cache.Region)71 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)58 Host (org.apache.geode.test.dunit.Host)54 VM (org.apache.geode.test.dunit.VM)54 RollbackException (javax.transaction.RollbackException)53 CommitConflictException (org.apache.geode.cache.CommitConflictException)48 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)39 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)37 OrderId (org.apache.geode.internal.cache.execute.data.OrderId)35 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)34 CacheWriterException (org.apache.geode.cache.CacheWriterException)33 TransactionDataNotColocatedException (org.apache.geode.cache.TransactionDataNotColocatedException)33 TransactionDataRebalancedException (org.apache.geode.cache.TransactionDataRebalancedException)33 IgnoredException (org.apache.geode.test.dunit.IgnoredException)32 Order (org.apache.geode.internal.cache.execute.data.Order)30 NamingException (javax.naming.NamingException)28