Search in sources :

Example 6 with Order

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

the class MyTransactionFunction method verifyTransactionRollback.

private void verifyTransactionRollback(RegionFunctionContext ctx) {
    Region custPR = ctx.getDataSet();
    Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.OrderPartitionedRegionName);
    CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
    ArrayList args = (ArrayList) ctx.getArguments();
    CustId custId = (CustId) args.get(1);
    Customer newCus = (Customer) args.get(2);
    OrderId orderId = (OrderId) args.get(3);
    Order order = (Order) args.get(4);
    Customer oldCustomer = (Customer) custPR.get(custId);
    Order oldOrder = (Order) orderPR.get(orderId);
    mgr.begin();
    custPR.put(custId, newCus);
    Customer txCust = (Customer) custPR.get(custId);
    orderPR.put(orderId, order);
    Order txOrder = (Order) orderPR.get(orderId);
    Assert.assertTrue(newCus.equals(txCust), "Expected Customer to be:" + newCus + " but was:" + txCust);
    Assert.assertTrue(txOrder.equals(order), "Expected Order to be:" + order + " but was:" + txOrder);
    mgr.rollback();
    Customer commitedCust = (Customer) custPR.get(custId);
    Assert.assertTrue(oldCustomer.equals(commitedCust), "Expected Customer to be:" + oldCustomer + " but was:" + commitedCust);
    Order commitedOrder = (Order) orderPR.get(orderId);
    Assert.assertTrue(oldOrder.equals(commitedOrder), "Expected Order to be:" + oldOrder + " but was:" + commitedOrder);
    mgr.begin();
    Assert.assertTrue(custPR.remove(custId, oldCustomer));
    orderPR.replace(orderId, order);
    mgr.rollback();
    Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
    Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
    mgr.begin();
    Assert.assertTrue(custPR.replace(custId, oldCustomer, newCus));
    orderPR.remove(orderId, oldOrder);
    Assert.assertTrue(null == orderPR.putIfAbsent(orderId, order));
    mgr.rollback();
    Assert.assertTrue(oldCustomer.equals(custPR.get(custId)));
    Assert.assertTrue(oldOrder.equals(orderPR.get(orderId)));
}
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) ArrayList(java.util.ArrayList) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) OrderId(org.apache.geode.internal.cache.execute.data.OrderId) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Example 7 with Order

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

the class MyTransactionFunction method verifyInvalidateOperation.

private void verifyInvalidateOperation(RegionFunctionContext ctx) {
    Region custPR = ctx.getDataSet();
    Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.OrderPartitionedRegionName);
    CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
    ArrayList args = (ArrayList) ctx.getArguments();
    CustId custId = (CustId) args.get(1);
    Customer newCus = (Customer) args.get(2);
    OrderId orderId = (OrderId) args.get(3);
    Order order = (Order) args.get(4);
    Customer oldCustomer = (Customer) custPR.get(custId);
    Customer commitedCust = null;
    // test destroy rollback
    mgr.begin();
    custPR.put(custId, newCus);
    custPR.invalidate(custId);
    orderPR.put(orderId, order);
    mgr.rollback();
    commitedCust = (Customer) custPR.get(custId);
    Assert.assertTrue(oldCustomer.equals(commitedCust), "Expected customer to rollback to:" + oldCustomer + " but was:" + commitedCust);
    // test destroy rollback on unmodified entry
    mgr.begin();
    custPR.invalidate(custId);
    orderPR.put(orderId, order);
    mgr.rollback();
    commitedCust = (Customer) custPR.get(custId);
    Assert.assertTrue(oldCustomer.equals(commitedCust), "Expected customer to rollback to:" + oldCustomer + " but was:" + commitedCust);
    // test remote destroy
    boolean caughtEx = false;
    try {
        mgr.begin();
        custPR.put(custId, new Customer("foo", "bar"));
        custPR.invalidate(custId);
        custPR.invalidate(new CustId(1));
        custPR.invalidate(new CustId(3));
        custPR.invalidate(new CustId(7));
        mgr.commit();
    } catch (Exception e) {
        mgr.rollback();
        if ((e instanceof TransactionDataNotColocatedException) || (e instanceof TransactionDataRebalancedException)) {
            caughtEx = true;
        } else if (e instanceof EntryNotFoundException && e.getMessage().matches("Entry not found for key.*1")) {
            caughtEx = true;
        } else {
            throw new TestException("Expected to catch PR remote destroy exception, but caught:" + e.getMessage(), e);
        }
    }
    if (!caughtEx) {
        throw new TestException("An Expected exception was not thrown");
    }
    // test destroy on unmodified entry
    mgr.begin();
    custPR.invalidate(custId);
    orderPR.put(orderId, order);
    mgr.commit();
    commitedCust = (Customer) custPR.get(custId);
    Assert.assertTrue(commitedCust == null, "Expected Customer to be null but was:" + commitedCust);
    Order commitedOrder = (Order) orderPR.get(orderId);
    Assert.assertTrue(order.equals(commitedOrder), "Expected Order to be:" + order + " but was:" + commitedOrder);
// test destroy on new entry
// TODO: This throws EntryNotFound
/*
     * OrderId newOrderId = new OrderId(5000,custId); mgr.begin(); orderPR.put(newOrderId, new
     * Order("New Order to be destroyed")); orderPR.invalidate(newOrderId); mgr.commit();
     * Assert.assertTrue(orderPR.get(newOrderId)==null,"Did not expect orderId to be present");
     */
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) TestException(util.TestException) Customer(org.apache.geode.internal.cache.execute.data.Customer) ArrayList(java.util.ArrayList) OrderId(org.apache.geode.internal.cache.execute.data.OrderId) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) TestException(util.TestException) CommitConflictException(org.apache.geode.cache.CommitConflictException) PartitionedRegionException(org.apache.geode.internal.cache.PartitionedRegionException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CustId(org.apache.geode.internal.cache.execute.data.CustId) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 8 with Order

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

the class PRTransactionDUnitTest method _resumeTx.

private void _resumeTx(Op op, TransactionId txId, PartitionedRegion pr, CacheTransactionManager mgr) {
    CustId cust1 = new CustId(1);
    OrderId order1 = new OrderId(11, cust1);
    OrderId neworder = new OrderId(111, cust1);
    mgr.resume(txId);
    try {
        switch(op) {
            case GET:
                pr.get(order1);
                break;
            case CONTAINSVALUEFORKEY:
                pr.containsValueForKey(order1);
                break;
            case CONTAINSKEY:
                pr.containsKey(order1);
                break;
            case CREATE:
                pr.create(neworder, new Order("test"));
                break;
            case PUT:
                pr.put(order1, new Order("test"));
                break;
            case INVALIDATE:
                pr.invalidate(order1);
                break;
            case DESTROY:
                pr.destroy(order1);
                break;
            case GETENTRY:
                pr.getEntry(order1);
                break;
            default:
                throw new AssertionError("Unknown operations " + op);
        }
    } finally {
        mgr.rollback();
    }
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) CustId(org.apache.geode.internal.cache.execute.data.CustId) OrderId(org.apache.geode.internal.cache.execute.data.OrderId)

Example 9 with Order

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

the class PRTransactionDUnitTest method validatePRTXInCacheListener.

/**
   * verify that 10 orders are created for each customer
   * 
   * @throws ClassNotFoundException
   */
public static void validatePRTXInCacheListener() throws ClassNotFoundException {
    PartitionedRegion customerPartitionedregion = null;
    PartitionedRegion orderPartitionedregion = null;
    try {
        customerPartitionedregion = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
        orderPartitionedregion = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + OrderPartitionedRegionName);
    } catch (Exception e) {
        Assert.fail("validateAfterPutPartitionedRegion : failed while getting the region", e);
    }
    assertNotNull(customerPartitionedregion);
    assertNotNull(orderPartitionedregion);
    customerPartitionedregion.getDataStore().dumpEntries(false);
    orderPartitionedregion.getDataStore().dumpEntries(false);
    Iterator custIterator = customerPartitionedregion.getDataStore().getEntries().iterator();
    LogWriterUtils.getLogWriter().info("Found " + customerPartitionedregion.getDataStore().getEntries().size() + " Customer entries in the partition");
    Region.Entry custEntry = null;
    while (custIterator.hasNext()) {
        custEntry = (Entry) custIterator.next();
        CustId custid = (CustId) custEntry.getKey();
        Customer cust = (Customer) custEntry.getValue();
        Iterator orderIterator = orderPartitionedregion.getDataStore().getEntries().iterator();
        LogWriterUtils.getLogWriter().info("Found " + orderPartitionedregion.getDataStore().getEntries().size() + " Order entries in the partition");
        int orderPerCustomer = 0;
        Region.Entry orderEntry = null;
        while (orderIterator.hasNext()) {
            orderEntry = (Entry) orderIterator.next();
            OrderId orderId = (OrderId) orderEntry.getKey();
            Order order = (Order) orderEntry.getValue();
            if (custid.equals(orderId.getCustId())) {
                orderPerCustomer++;
            }
        }
        assertEquals(10, orderPerCustomer);
    }
}
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) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Entry(org.apache.geode.cache.Region.Entry) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) OrderId(org.apache.geode.internal.cache.execute.data.OrderId) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) FunctionException(org.apache.geode.cache.execute.FunctionException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) TestException(util.TestException) PRLocallyDestroyedException(org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException)

Example 10 with Order

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

the class PRTransactionDUnitTest method doOp.

private void doOp(Op op, CacheTransactionManager mgr) {
    PartitionedRegion cust = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + CustomerPartitionedRegionName);
    PartitionedRegion order = (PartitionedRegion) basicGetCache().getRegion(Region.SEPARATOR + OrderPartitionedRegionName);
    CustId cust1 = new CustId(1);
    CustId cust2 = new CustId(2);
    OrderId order2 = new OrderId(21, cust2);
    OrderId neworder2 = new OrderId(221, cust2);
    mgr.begin();
    // touch 1 bucket
    cust.get(cust1);
    try {
        switch(op) {
            case GET:
                order.get(order2);
                break;
            case CONTAINSVALUEFORKEY:
                order.containsValueForKey(order2);
                break;
            case CONTAINSKEY:
                order.containsKey(order2);
                break;
            case CREATE:
                order.create(neworder2, new Order("test"));
                break;
            case PUT:
                order.put(order2, new Order("test"));
                break;
            case INVALIDATE:
                order.invalidate(order2);
                break;
            case DESTROY:
                order.destroy(order2);
                break;
            case GETENTRY:
                order.getEntry(order2);
                break;
            default:
                throw new AssertionError("Unknown operations " + op);
        }
    } finally {
        mgr.rollback();
    }
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) CustId(org.apache.geode.internal.cache.execute.data.CustId) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) OrderId(org.apache.geode.internal.cache.execute.data.OrderId)

Aggregations

Order (org.apache.geode.internal.cache.execute.data.Order)55 OrderId (org.apache.geode.internal.cache.execute.data.OrderId)53 CustId (org.apache.geode.internal.cache.execute.data.CustId)48 Region (org.apache.geode.cache.Region)32 Customer (org.apache.geode.internal.cache.execute.data.Customer)30 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)23 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)15 Test (org.junit.Test)15 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)14 ArrayList (java.util.ArrayList)13 IgnoredException (org.apache.geode.test.dunit.IgnoredException)13 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)12 TransactionDataNotColocatedException (org.apache.geode.cache.TransactionDataNotColocatedException)12 TransactionDataRebalancedException (org.apache.geode.cache.TransactionDataRebalancedException)12 CommitConflictException (org.apache.geode.cache.CommitConflictException)11 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)11 IOException (java.io.IOException)10 RollbackException (javax.transaction.RollbackException)10 BucketRegion (org.apache.geode.internal.cache.BucketRegion)10 HashMap (java.util.HashMap)8