Search in sources :

Example 6 with Customer

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

the class ClientServerTransactionDUnitTest method testClientInitiatedInvalidates.

@Test
public void testClientInitiatedInvalidates() 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;
        }
    });
    /*
     * Test a no-op commit: put/invalidate
     */
    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(1777777777);
            getCache().getCacheTransactionManager().begin();
            custRegion.put(custId, new Customer("foo", "bar"));
            custRegion.invalidate(custId);
            // orderRegion.put(orderId, new Order("fooOrder"));
            // refRegion.put(custId, new Customer("foo", "bar"));
            getCache().getCacheTransactionManager().commit();
            return null;
        }
    });
    /*
     * Validate nothing came through
     */
    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);
            assertEquals(1, cl.putCount);
            assertEquals(1, cl.invokeCount);
            assertEquals(0, cl.invalidateCount);
            CustId custId = new CustId(1777777777);
            assertTrue(custRegion.containsKey(custId));
            assertTrue(!custRegion.containsValueForKey(custId));
            cl.reset();
            return null;
        }
    });
    datastore.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);
            assertEquals(1, cl.putCount);
            assertEquals(1, cl.invokeCount);
            CustId custId = new CustId(1777777777);
            assertTrue(custRegion.containsKey(custId));
            assertTrue(!custRegion.containsValueForKey(custId));
            cl.reset();
            return null;
        }
    });
    /*
     * Ok lets do a put in tx, then an invalidate in a another tx and make sure it invalidates on
     * client and server
     */
    client.invoke(doAPutInTx);
    client.invoke(doAnInvalidateInTx);
    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);
            assertEquals("totalEvents should be 2 but its:" + cl.invokeCount, 2, cl.invokeCount);
            assertEquals("it should be 1 but its:" + cl.invalidateCount, 1, cl.invalidateCount);
            assertEquals("it should be 1 but its:" + cl.putCount, 1, cl.putCount);
            CustId custId = new CustId(1);
            assertTrue(custRegion.containsKey(custId));
            assertFalse(custRegion.containsValueForKey(custId));
            return null;
        }
    });
    datastore.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);
            assertEquals("totalEvents should be 2 but its:" + cl.invokeCount, 2, cl.invokeCount);
            assertEquals("it should be 1 but its:" + cl.invalidateCount, 1, cl.invalidateCount);
            assertEquals("it should be 1 but its:" + cl.putCount, 1, cl.putCount);
            return null;
        }
    });
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) 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 7 with Customer

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

the class ClientServerTransactionDUnitTest method doBasicFunctionExecution.

private void doBasicFunctionExecution(VM client, VM accessor, VM datastore) {
    int datastorePort = createRegionsAndStartServer(datastore, false);
    int accessorPort = accessor == null ? 0 : createRegionsAndStartServer(accessor, true);
    final int port = accessorPort == 0 ? datastorePort : accessorPort;
    createClientRegion(client, port, true);
    class BasicTransactionalFunction extends FunctionAdapter {

        static final String ID = "BasicTransactionalFunction";

        @Override
        public void execute(FunctionContext context) {
            getGemfireCache().getLogger().info("SWAP:in function");
            RegionFunctionContext ctx = (RegionFunctionContext) context;
            Region pr = ctx.getDataSet();
            pr.put(new CustId(0), new Customer("name0", "address0"));
            pr.replace(new CustId(1), new Customer("name1", "address1"));
            pr.put(new CustId(10), new Customer("name10", "address10"));
            pr.put(new CustId(11), new Customer("name11", "address11"));
            Region r = ctx.getDataSet().getRegionService().getRegion(D_REFERENCE);
            r.put(new CustId(10), new Customer("name10", "address10"));
            r.put(new CustId(11), new Customer("name11", "address11"));
            ctx.getResultSender().lastResult(Boolean.TRUE);
        }

        @Override
        public String getId() {
            return ID;
        }
    }
    SerializableCallable registerFunction = new SerializableCallable() {

        public Object call() throws Exception {
            FunctionService.registerFunction(new BasicTransactionalFunction());
            return null;
        }
    };
    datastore.invoke(registerFunction);
    if (accessor != null) {
        accessor.invoke(registerFunction);
    }
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            FunctionService.registerFunction(new BasicTransactionalFunction());
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            Region<CustId, Customer> r = getGemfireCache().getRegion(D_REFERENCE);
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            pr.put(new CustId(0), new Customer("oldname0", "oldaddress0"));
            pr.put(new CustId(1), new Customer("oldname1", "oldaddress1"));
            mgr.begin();
            final Set filter = new HashSet();
            filter.add(new CustId(0));
            filter.add(new CustId(1));
            getGemfireCache().getLogger().info("SWAP:calling execute");
            FunctionService.onRegion(pr).withFilter(filter).execute(BasicTransactionalFunction.ID).getResult();
            assertEquals(new Customer("name0", "address0"), pr.get(new CustId(0)));
            assertEquals(new Customer("name10", "address10"), pr.get(new CustId(10)));
            assertEquals(new Customer("name10", "address10"), r.get(new CustId(10)));
            TXStateProxy tx = mgr.internalSuspend();
            assertEquals(new Customer("oldname0", "oldaddress0"), pr.get(new CustId(0)));
            assertEquals(new Customer("oldname1", "oldaddress1"), pr.get(new CustId(1)));
            assertNull(pr.get(new CustId(10)));
            assertNull(r.get(new CustId(10)));
            mgr.internalResume(tx);
            mgr.commit();
            assertEquals(new Customer("name0", "address0"), pr.get(new CustId(0)));
            assertEquals(new Customer("name1", "address1"), pr.get(new CustId(1)));
            assertEquals(new Customer("name10", "address10"), pr.get(new CustId(10)));
            assertEquals(new Customer("name10", "address10"), r.get(new CustId(10)));
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Customer(org.apache.geode.internal.cache.execute.data.Customer) RollbackException(javax.transaction.RollbackException) CustId(org.apache.geode.internal.cache.execute.data.CustId) HashSet(java.util.HashSet)

Example 8 with Customer

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

the class ClientServerTransactionDUnitTest method testSuspendTimeout.

@Test
public void testSuspendTimeout() throws Exception {
    Host host = Host.getHost(0);
    VM server = host.getVM(0);
    VM client = host.getVM(1);
    final int port = createRegionsAndStartServer(server, false);
    createClientRegion(client, port, true);
    final TransactionId txId = (TransactionId) client.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            TXManagerImpl mgr = (TXManagerImpl) getCache().getCacheTransactionManager();
            mgr.setSuspendedTransactionTimeout(1);
            Region r = getCache().getRegion(CUSTOMER);
            assertNull(r.get(new CustId(101)));
            mgr.begin();
            final TXStateProxy txState = mgr.getTXState();
            assertTrue(txState.isInProgress());
            r.put(new CustId(101), new Customer("name101", "address101"));
            TransactionId txId = mgr.suspend(TimeUnit.MILLISECONDS);
            WaitCriterion waitForTxTimeout = new WaitCriterion() {

                public boolean done() {
                    return !txState.isInProgress();
                }

                public String description() {
                    return "txState stayed in progress indicating that the suspend did not timeout";
                }
            };
            // tx should timeout after 1 ms but to deal with loaded machines and thread
            // scheduling latency wait for 10 seconds before reporting an error.
            Wait.waitForCriterion(waitForTxTimeout, 10 * 1000, 10, true);
            try {
                mgr.resume(txId);
                fail("expected exception not thrown");
            } catch (IllegalStateException expected) {
            }
            assertNull(r.get(new CustId(101)));
            return txId;
        }
    });
    server.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            TXManagerImpl mgr = (TXManagerImpl) getCache().getCacheTransactionManager();
            assertNull(mgr.getHostedTXState((TXId) txId));
            assertEquals(0, mgr.hostedTransactionsInProgressForTest());
            return null;
        }
    });
}
Also used : Customer(org.apache.geode.internal.cache.execute.data.Customer) RollbackException(javax.transaction.RollbackException) CustId(org.apache.geode.internal.cache.execute.data.CustId) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 9 with Customer

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

the class ClientServerTransactionDUnitTest method testClientDoesUnsupportedLocalOps.

@Test
public void testClientDoesUnsupportedLocalOps() 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);
            custRegion.put(custId, new Customer("foo", "bar"));
            getCache().getCacheTransactionManager().begin();
            try {
                custRegion.localDestroy(custId);
                fail("Should have thrown UOE");
            } catch (UnsupportedOperationInTransactionException uoi) {
            // chill
            }
            try {
                custRegion.localInvalidate(custId);
                fail("Should have thrown UOE");
            } catch (UnsupportedOperationInTransactionException uoi) {
            // chill
            }
            // orderRegion.put(orderId, new Order("fooOrder"));
            // refRegion.put(custId, new Customer("foo", "bar"));
            getCache().getCacheTransactionManager().commit();
            return null;
        }
    });
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) 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 10 with Customer

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

the class ClientServerTransactionDUnitTest method doFailoverWork.

private void doFailoverWork(VM accessor1, VM accessor2, VM datastore, VM client, boolean serverOnDatastore, final boolean cachingProxy) {
    final int port1 = createRegionsAndStartServer(accessor1, true);
    final int port2 = accessor2 == null ? 0 : createRegionsAndStartServer(accessor2, true);
    final int port3 = serverOnDatastore ? createRegionsAndStartServer(datastore, false) : createRegionOnServer(datastore, false, false);
    /* final TXId txid = (TXId) */
    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)) */
            port1);
            if (port2 != 0)
                ccf.addPoolServer("localhost", port2);
            if (port3 != 0)
                ccf.addPoolServer("localhost", port3);
            ccf.setPoolSubscriptionEnabled(false);
            ccf.set(LOG_LEVEL, getDUnitLogLevel());
            ClientCache cCache = getClientCache(ccf);
            ClientRegionFactory<CustId, Customer> custrf = cCache.createClientRegionFactory(cachingProxy ? ClientRegionShortcut.CACHING_PROXY : ClientRegionShortcut.PROXY);
            ClientRegionFactory<Integer, String> refrf = cCache.createClientRegionFactory(cachingProxy ? ClientRegionShortcut.CACHING_PROXY : ClientRegionShortcut.PROXY);
            Region<Integer, String> r = refrf.create(D_REFERENCE);
            Region<CustId, Customer> pr = custrf.create(CUSTOMER);
            // Region<Integer, String> order = refrf.create(ORDER);
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            mgr.begin();
            for (int i = 0; i < 5; i++) {
                CustId custId = new CustId(i);
                Customer cust = new Customer("name" + i, "address" + i);
                getGemfireCache().getLogger().info("SWAP:putting:" + custId);
                pr.put(custId, cust);
                r.put(i, "value" + i);
            }
            return mgr.getTransactionId();
        }
    });
    accessor1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            for (CacheServer s : getCache().getCacheServers()) {
                getCache().getLogger().info("SWAP:Stopping " + s);
                s.stop();
            }
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            /* TXManagerImpl mgr = */
            getGemfireCache().getTxManager();
            Region<Integer, String> r = getGemfireCache().getRegion(D_REFERENCE);
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            for (int i = 5; i < 10; i++) {
                CustId custId = new CustId(i);
                Customer cust = new Customer("name" + i, "address" + i);
                getGemfireCache().getLogger().info("SWAP:AfterFailover:putting:" + custId);
                pr.put(custId, cust);
                r.put(i, "value" + i);
            }
            return null;
        }
    });
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            assertEquals(1, mgr.hostedTransactionsInProgressForTest());
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTxManager();
            Region<Integer, String> r = getGemfireCache().getRegion(D_REFERENCE);
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            mgr.commit();
            for (int i = 0; i < 10; i++) {
                if (cachingProxy) {
                    assertTrue(pr.containsKey(new CustId(i)));
                    assertTrue(r.containsKey(i));
                }
                assertEquals(new Customer("name" + i, "address" + i), pr.get(new CustId(i)));
                assertEquals("value" + i, r.get(i));
            }
            return null;
        }
    });
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) CacheServer(org.apache.geode.cache.server.CacheServer) RollbackException(javax.transaction.RollbackException)

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