Search in sources :

Example 46 with Customer

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

the class RemoteTransactionDUnitTest method testBug43081.

@Test
public void testBug43081() throws Exception {
    createRegion(false, 0, null);
    Context ctx = getCache().getJNDIContext();
    UserTransaction tx = (UserTransaction) ctx.lookup("java:/UserTransaction");
    assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
    Region pr = getCache().getRegion(CUSTOMER);
    Region rr = getCache().getRegion(D_REFERENCE);
    // test all ops
    for (int i = 0; i < 6; i++) {
        pr.put(new CustId(1), new Customer("name1", "address1"));
        rr.put("key1", "value1");
        tx.begin();
        switch(i) {
            case 0:
                pr.get(new CustId(1));
                rr.get("key1");
                break;
            case 1:
                pr.put(new CustId(1), new Customer("nameNew", "addressNew"));
                rr.put("key1", "valueNew");
                break;
            case 2:
                pr.invalidate(new CustId(1));
                rr.invalidate("key1");
                break;
            case 3:
                pr.destroy(new CustId(1));
                rr.destroy("key1");
                break;
            case 4:
                Map m = new HashMap();
                m.put(new CustId(1), new Customer("nameNew", "addressNew"));
                pr.putAll(m);
                m = new HashMap();
                m.put("key1", "valueNew");
                rr.putAll(m);
                break;
            case 5:
                Set s = new HashSet();
                s.add(new CustId(1));
                pr.getAll(s);
                s = new HashSet();
                s.add("key1");
                pr.getAll(s);
                break;
            case 6:
                pr.getEntry(new CustId(1));
                rr.getEntry("key1");
                break;
            default:
                break;
        }
        // Putting a string key causes this, the partition resolver
        // doesn't handle it.
        IgnoredException.addIgnoredException("IllegalStateException");
        assertEquals(Status.STATUS_ACTIVE, tx.getStatus());
        final CountDownLatch latch = new CountDownLatch(1);
        Thread t = new Thread(new Runnable() {

            public void run() {
                Context ctx = getCache().getJNDIContext();
                try {
                    UserTransaction tx = (UserTransaction) ctx.lookup("java:/UserTransaction");
                } catch (NamingException e) {
                    e.printStackTrace();
                }
                Region pr = getCache().getRegion(CUSTOMER);
                Region rr = getCache().getRegion(D_REFERENCE);
                pr.put(new CustId(1), new Customer("name11", "address11"));
                rr.put("key1", "value1");
                latch.countDown();
            }
        });
        t.start();
        latch.await();
        try {
            pr.put(new CustId(1), new Customer("name11", "address11"));
            tx.commit();
            fail("expected exception not thrown");
        } catch (RollbackException e) {
        }
    }
}
Also used : FunctionContext(org.apache.geode.cache.execute.FunctionContext) Context(javax.naming.Context) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) UserTransaction(javax.transaction.UserTransaction) Set(java.util.Set) HashSet(java.util.HashSet) Customer(org.apache.geode.internal.cache.execute.data.Customer) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) RollbackException(javax.transaction.RollbackException) CustId(org.apache.geode.internal.cache.execute.data.CustId) Region(org.apache.geode.cache.Region) NamingException(javax.naming.NamingException) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 47 with Customer

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

the class RemoteTransactionDUnitTest method verifyAfterRollback.

void verifyAfterRollback(OP op) {
    Region<CustId, Customer> custRegion = getCache().getRegion(CUSTOMER);
    Region<OrderId, Order> orderRegion = getCache().getRegion(ORDER);
    Region<CustId, Customer> refRegion = getCache().getRegion(D_REFERENCE);
    assertNotNull(custRegion);
    assertNotNull(orderRegion);
    assertNotNull(refRegion);
    CustId custId = new CustId(1);
    OrderId orderId = new OrderId(1, custId);
    OrderId orderId2 = new OrderId(2, custId);
    OrderId orderId3 = new OrderId(3, custId);
    Customer expectedCust;
    Order expectedOrder;
    Customer expectedRef;
    switch(op) {
        case PUT:
            expectedCust = new Customer("customer1", "address1");
            expectedOrder = new Order("order1");
            expectedRef = new Customer("customer1", "address1");
            assertEquals(expectedCust, custRegion.getEntry(custId).getValue());
            assertEquals(expectedOrder, orderRegion.getEntry(orderId).getValue());
            getCache().getLogger().info("SWAP:verifyRollback:" + orderRegion);
            getCache().getLogger().info("SWAP:verifyRollback:" + orderRegion.getEntry(orderId2));
            assertNull(getGemfireCache().getTXMgr().getTXState());
            assertNull("" + orderRegion.getEntry(orderId2), orderRegion.getEntry(orderId2));
            assertNull(orderRegion.getEntry(orderId3));
            assertNull(orderRegion.get(orderId2));
            assertNull(orderRegion.get(orderId3));
            assertEquals(expectedRef, refRegion.getEntry(custId).getValue());
            validateContains(custId, Collections.singleton(orderId), true);
            break;
        case GET:
            expectedCust = custRegion.getEntry(custId).getValue();
            expectedOrder = orderRegion.getEntry(orderId).getValue();
            expectedRef = refRegion.getEntry(custId).getValue();
            validateContains(custId, Collections.singleton(orderId), true);
            break;
        case DESTROY:
            assertTrue(!custRegion.containsKey(custId));
            assertTrue(!orderRegion.containsKey(orderId));
            assertTrue(!refRegion.containsKey(custId));
            validateContains(custId, Collections.singleton(orderId), true);
            break;
        case INVALIDATE:
            assertTrue(custRegion.containsKey(custId));
            assertTrue(orderRegion.containsKey(orderId));
            assertTrue(refRegion.containsKey(custId));
            assertNull(custRegion.get(custId));
            assertNull(orderRegion.get(orderId));
            assertNull(refRegion.get(custId));
            validateContains(custId, Collections.singleton(orderId), true, true);
            break;
        default:
            throw new IllegalStateException();
    }
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) OrderId(org.apache.geode.internal.cache.execute.data.OrderId)

Example 48 with Customer

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

the class RemoteTransactionDUnitTest method testTxReplace.

@Test
public void testTxReplace() {
    Host host = Host.getHost(0);
    VM acc = host.getVM(0);
    VM datastore = host.getVM(1);
    initAccessorAndDataStore(acc, datastore, 0);
    VM accessor = getVMForTransactions(acc, datastore);
    final CustId custId = new CustId(1);
    final Customer updatedCust = new Customer("updated", "updated");
    final TXId txId = (TXId) accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region<CustId, Customer> cust = getGemfireCache().getRegion(CUSTOMER);
            Region<CustId, Customer> ref = getGemfireCache().getRegion(D_REFERENCE);
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            mgr.begin();
            Customer customer = new Customer("customer1", "address1");
            Customer fakeCust = new Customer("foo", "bar");
            assertFalse(cust.replace(custId, fakeCust, updatedCust));
            assertTrue(cust.replace(custId, customer, updatedCust));
            assertFalse(ref.replace(custId, fakeCust, updatedCust));
            assertTrue(ref.replace(custId, customer, updatedCust));
            TXStateProxy tx = mgr.internalSuspend();
            assertEquals(cust.get(custId), customer);
            assertEquals(ref.get(custId), customer);
            mgr.internalResume(tx);
            return mgr.getTransactionId();
        }
    });
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            assertTrue(mgr.isHostedTxInProgress(txId));
            TXStateProxy tx = mgr.getHostedTXState(txId);
            // 2 buckets for the two puts we
            assertEquals(2, tx.getRegions().size());
            // different buckets
            for (LocalRegion r : tx.getRegions()) {
                assertTrue(r instanceof BucketRegion || r instanceof DistributedRegion);
                TXRegionState rs = tx.readRegion(r);
                for (Object key : rs.getEntryKeys()) {
                    TXEntryState es = rs.readEntry(key);
                    assertNotNull(es.getValue(key, r, false));
                    assertTrue("key:" + key + " r:" + r.getFullPath(), es.isDirty());
                }
            }
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            mgr.commit();
            Region<CustId, Customer> cust = getGemfireCache().getRegion(CUSTOMER);
            Region<CustId, Customer> rr = getGemfireCache().getRegion(D_REFERENCE);
            assertEquals(updatedCust, cust.get(custId));
            assertEquals(updatedCust, rr.get(custId));
            // check conflict
            mgr.begin();
            CustId conflictCust = new CustId(2);
            Customer customer = new Customer("customer2", "address2");
            getGemfireCache().getLoggerI18n().fine("SWAP:removeConflict");
            assertTrue(cust.replace(conflictCust, customer, new Customer("conflict", "conflict")));
            TXStateProxy tx = mgr.internalSuspend();
            cust.put(conflictCust, new Customer("foo", "bar"));
            mgr.internalResume(tx);
            try {
                mgr.commit();
                fail("expected exception not thrown");
            } catch (CommitConflictException e) {
            }
            return null;
        }
    });
}
Also used : Customer(org.apache.geode.internal.cache.execute.data.Customer) 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) CommitConflictException(org.apache.geode.cache.CommitConflictException) CustId(org.apache.geode.internal.cache.execute.data.CustId) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 49 with Customer

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

the class RemoteTransactionDUnitTest method doTestTxFunction.

private void doTestTxFunction(final Executions e) {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore1 = host.getVM(1);
    VM datastore2 = host.getVM(2);
    initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
    SerializableCallable registerFunction = new SerializableCallable() {

        public Object call() throws Exception {
            FunctionService.registerFunction(new TXFunction());
            return null;
        }
    };
    accessor.invoke(registerFunction);
    datastore1.invoke(registerFunction);
    datastore2.invoke(registerFunction);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            Set regions = new HashSet();
            regions.add(custRegion);
            regions.add(getGemfireCache().getRegion(ORDER));
            mgr.begin();
            try {
                switch(e) {
                    case OnRegion:
                        FunctionService.onRegion(custRegion).execute(TXFunction.id).getResult();
                        break;
                    case OnMember:
                        FunctionService.onMembers().execute(TXFunction.id).getResult();
                        break;
                }
                fail("Expected exception not thrown");
            } catch (TransactionException expected) {
            }
            try {
                InternalFunctionService.onRegions(regions).execute(TXFunction.id).getResult();
                fail("Expected exception not thrown");
            } catch (TransactionException expected) {
            }
            Set filter = new HashSet();
            filter.add(expectedCustId);
            switch(e) {
                case OnRegion:
                    FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
                    break;
                case OnMember:
                    DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
                    FunctionService.onMember(owner).execute(TXFunction.id).getResult();
                    break;
            }
            TXStateProxy tx = mgr.internalSuspend();
            GemFireCacheImpl.getInstance().getLogger().warning("TX SUSPENDO:" + tx);
            assertNull(custRegion.get(expectedCustId));
            mgr.internalResume(tx);
            return null;
        }
    });
    final Integer txOnDatastore1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
    final Integer txOnDatastore2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
    assertEquals(1, txOnDatastore1 + txOnDatastore2);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTXMgr();
            mgr.commit();
            return null;
        }
    });
    datastore1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getGemfireCache().getRegion(CUSTOMER);
            assertEquals(expectedCustomer, custRegion.get(expectedCustId));
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            mgr.begin();
            PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            Set filter = new HashSet();
            filter.add(expectedCustId);
            switch(e) {
                case OnRegion:
                    FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
                    break;
                case OnMember:
                    DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
                    FunctionService.onMember(owner).execute(TXFunction.id).getResult();
                    break;
            }
            TXStateProxy tx = mgr.internalSuspend();
            custRegion.put(expectedCustId, new Customer("Cust6", "updated6"));
            mgr.internalResume(tx);
            try {
                mgr.commit();
                fail("expected commit conflict not thrown");
            } catch (CommitConflictException expected) {
            }
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Customer(org.apache.geode.internal.cache.execute.data.Customer) 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) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionException(org.apache.geode.cache.TransactionException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet)

Example 50 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)

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