Search in sources :

Example 61 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class DistributedSystemDUnitTest method testMembershipPortRangeWithExactThreeValues.

@Test
public void testMembershipPortRangeWithExactThreeValues() throws Exception {
    Properties config = new Properties();
    config.setProperty(LOCATORS, "localhost[" + getDUnitLocatorPort() + "]");
    config.setProperty(MEMBERSHIP_PORT_RANGE, this.lowerBoundOfPortRange + "-" + this.upperBoundOfPortRange);
    InternalDistributedSystem system = getSystem(config);
    Cache cache = CacheFactory.create(system);
    cache.addCacheServer();
    DistributionManager dm = (DistributionManager) system.getDistributionManager();
    InternalDistributedMember member = dm.getDistributionManagerId();
    GMSMembershipManager gms = (GMSMembershipManager) MembershipManagerHelper.getMembershipManager(system);
    JGroupsMessenger messenger = (JGroupsMessenger) gms.getServices().getMessenger();
    String jgConfig = messenger.getJGroupsStackConfig();
    assertThat(jgConfig).as("expected to find port_range=\"2\" in " + jgConfig).contains("port_range=\"2\"");
    verifyMembershipPortsInRange(member, this.lowerBoundOfPortRange, this.upperBoundOfPortRange);
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) GMSMembershipManager(org.apache.geode.distributed.internal.membership.gms.mgr.GMSMembershipManager) JGroupsMessenger(org.apache.geode.distributed.internal.membership.gms.messenger.JGroupsMessenger) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 62 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class ClientServerInvalidAndDestroyedEntryDUnitTest method getCreateServerCallable.

/* this method creates a server cache and is used by all of the tests in this class */
private SerializableCallableIF getCreateServerCallable(final String regionName, final boolean usePR) {
    return new SerializableCallable("create server and entries") {

        public Object call() {
            Cache cache = getCache();
            List<CacheServer> servers = cache.getCacheServers();
            CacheServer server;
            if (servers.size() > 0) {
                server = servers.get(0);
            } else {
                server = cache.addCacheServer();
                int port = AvailablePortHelper.getRandomAvailableTCPPort();
                server.setPort(port);
                server.setHostnameForClients("localhost");
                try {
                    server.start();
                } catch (IOException e) {
                    Assert.fail("Failed to start server ", e);
                }
            }
            if (usePR) {
                RegionFactory factory = cache.createRegionFactory(RegionShortcut.PARTITION);
                PartitionAttributesFactory pf = new PartitionAttributesFactory();
                pf.setTotalNumBuckets(2);
                factory.setPartitionAttributes(pf.create());
                factory.create(regionName);
            } else {
                cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
            }
            return server.getPort();
        }
    };
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) RegionFactory(org.apache.geode.cache.RegionFactory) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheServer(org.apache.geode.cache.server.CacheServer) IOException(java.io.IOException) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache)

Example 63 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class CommitFunction method execute.

public void execute(FunctionContext context) {
    Cache cache = CacheFactory.getAnyInstance();
    TXId txId = null;
    try {
        txId = (TXId) context.getArguments();
    } catch (ClassCastException e) {
        logger.info("CommitFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
        throw e;
    }
    DistributedMember member = txId.getMemberId();
    Boolean result = false;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (cache.getDistributedSystem().getDistributedMember().equals(member)) {
        if (isDebugEnabled) {
            logger.debug("CommitFunction: for transaction: {} committing locally", txId);
        }
        CacheTransactionManager txMgr = cache.getCacheTransactionManager();
        if (txMgr.tryResume(txId)) {
            if (isDebugEnabled) {
                logger.debug("CommitFunction: resumed transaction: {}", txId);
            }
            txMgr.commit();
            result = true;
        }
    } else {
        ArrayList args = new ArrayList();
        args.add(txId);
        args.add(NestedTransactionFunction.COMMIT);
        Execution ex = FunctionService.onMember(member).setArguments(args);
        if (isDebugEnabled) {
            logger.debug("CommitFunction: for transaction: {} executing NestedTransactionFunction on member: {}", txId, member);
        }
        try {
            List list = (List) ex.execute(new NestedTransactionFunction()).getResult();
            result = (Boolean) list.get(0);
        } catch (FunctionException fe) {
            if (fe.getCause() instanceof FunctionInvocationTargetException) {
                throw new TransactionDataNodeHasDepartedException("Could not commit on member:" + member);
            } else {
                throw fe;
            }
        }
    }
    if (isDebugEnabled) {
        logger.debug("CommitFunction: for transaction: {} returning result: {}", txId, result);
    }
    context.getResultSender().lastResult(result);
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException) Execution(org.apache.geode.cache.execute.Execution) TXId(org.apache.geode.internal.cache.TXId) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.apache.geode.cache.Cache)

Example 64 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class DeltaFaultInDUnitTest method test.

@Test
public void test() throws Exception {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    final boolean copyOnRead = false;
    final boolean clone = true;
    SerializableCallable createDataRegion = new SerializableCallable("createDataRegion") {

        public Object call() throws Exception {
            Cache cache = getCache();
            cache.setCopyOnRead(copyOnRead);
            cache.createDiskStoreFactory().create("DeltaFaultInDUnitTestData");
            AttributesFactory attr = new AttributesFactory();
            attr.setDiskStoreName("DeltaFaultInDUnitTestData");
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(1);
            PartitionAttributes prAttr = paf.create();
            attr.setPartitionAttributes(prAttr);
            attr.setCloningEnabled(clone);
            attr.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(1, EvictionAction.OVERFLOW_TO_DISK));
            Region region = cache.createRegion("region1", attr.create());
            return null;
        }
    };
    vm0.invoke(createDataRegion);
    SerializableRunnable createEmptyRegion = new SerializableRunnable("createEmptyRegion") {

        public void run() {
            Cache cache = getCache();
            cache.setCopyOnRead(copyOnRead);
            AttributesFactory<Integer, TestDelta> attr = new AttributesFactory<Integer, TestDelta>();
            attr.setCloningEnabled(clone);
            PartitionAttributesFactory<Integer, TestDelta> paf = new PartitionAttributesFactory<Integer, TestDelta>();
            paf.setRedundantCopies(1);
            paf.setLocalMaxMemory(0);
            PartitionAttributes<Integer, TestDelta> prAttr = paf.create();
            attr.setPartitionAttributes(prAttr);
            attr.setDataPolicy(DataPolicy.PARTITION);
            attr.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(1, EvictionAction.OVERFLOW_TO_DISK));
            Region<Integer, TestDelta> region = cache.createRegion("region1", attr.create());
            // Put an entry
            region.put(new Integer(0), new TestDelta(false, "initial"));
            // Put a delta object that is larger
            region.put(new Integer(0), new TestDelta(true, "initial_plus_some_more_data"));
        }
    };
    vm1.invoke(createEmptyRegion);
    vm0.invoke(new SerializableRunnable("doPut") {

        public void run() {
            Cache cache = getCache();
            Region<Integer, TestDelta> region = cache.getRegion("region1");
            // Evict the other object
            region.put(new Integer(113), new TestDelta(false, "bogus"));
            // Something was going weird with the LRU list. It was evicting this object.
            // I want to make sure the other object is the one evicted.
            region.get(new Integer(113));
            long entriesEvicted = ((AbstractLRURegionMap) ((PartitionedRegion) region).entries)._getLruList().stats().getEvictions();
            // assertIndexDetailsEquals(1, entriesEvicted);
            TestDelta result = region.get(new Integer(0));
            assertEquals("initial_plus_some_more_data", result.info);
        }
    });
}
Also used : PartitionAttributes(org.apache.geode.cache.PartitionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 65 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class EventTrackerDUnitTest method checkReplicateEventTracker.

private SerializableRunnable checkReplicateEventTracker(VM vm, final int expectedEntryCount) {
    SerializableRunnable checkEventTracker = new SerializableRunnable("checkEventTracker") {

        public void run() {
            Cache cache = getCache();
            DistributedRegion region = (DistributedRegion) cache.getRegion("replicate");
            checkEventTracker(region, expectedEntryCount);
        }
    };
    vm.invoke(checkEventTracker);
    return checkEventTracker;
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Cache(org.apache.geode.cache.Cache)

Aggregations

Cache (org.apache.geode.cache.Cache)1044 Region (org.apache.geode.cache.Region)478 Test (org.junit.Test)476 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)292 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)277 VM (org.apache.geode.test.dunit.VM)264 Host (org.apache.geode.test.dunit.Host)230 AttributesFactory (org.apache.geode.cache.AttributesFactory)229 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)177 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)176 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)164 LocalRegion (org.apache.geode.internal.cache.LocalRegion)153 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)123 ClientCache (org.apache.geode.cache.client.ClientCache)117 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)112 Properties (java.util.Properties)101 CacheException (org.apache.geode.cache.CacheException)101 RegionAttributes (org.apache.geode.cache.RegionAttributes)99 QueryService (org.apache.geode.cache.query.QueryService)95 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)93