Search in sources :

Example 31 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class ClientServerMiscDUnitTest method testLargeMessageIsRejected.

/**
   * GEODE-478 - large payloads are rejected by client->server
   */
@Test
public void testLargeMessageIsRejected() throws Exception {
    PORT1 = initServerCache(false);
    createClientCache(NetworkUtils.getServerHostName(Host.getHost(0)), PORT1);
    Region region = static_cache.getRegion(REGION_NAME1);
    Op operation = new Op() {

        @Override
        public Object attempt(Connection cnx) throws Exception {
            throw new MessageTooLargeException("message is too big");
        }

        @Override
        public boolean useThreadLocalConnection() {
            return false;
        }
    };
    try {
        ((LocalRegion) region).getServerProxy().getPool().execute(operation);
    } catch (GemFireIOException e) {
        assertTrue(e.getCause() instanceof MessageTooLargeException);
        return;
    }
    fail("expected an exception to be thrown");
}
Also used : Op(org.apache.geode.cache.client.internal.Op) Connection(org.apache.geode.cache.client.internal.Connection) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) GemFireIOException(org.apache.geode.GemFireIOException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 32 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class ClientServerMiscDUnitTest method verifyUpdates.

public static void verifyUpdates() {
    Cache cache = new ClientServerMiscDUnitTest().getCache();
    final Region r1 = cache.getRegion(Region.SEPARATOR + REGION_NAME1);
    final Region r2 = cache.getRegion(Region.SEPARATOR + REGION_NAME2);
    assertNotNull(r1);
    assertNotNull(r2);
    // no interest registered in region1 - it should hold client values, which are
    // the same as the keys
    Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> {
        Object val = r1.getEntry(k1).getValue();
        return k1.equals(val);
    });
    Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> {
        Object val = r1.getEntry(k2).getValue();
        return k2.equals(val);
    });
    // interest was registered in region2 - it should contain server values
    Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> {
        Object val = r2.getEntry(k1).getValue();
        return server_k1.equals(val);
    });
    Awaitility.await().atMost(60, TimeUnit.SECONDS).until(() -> {
        Object val = r2.getEntry(k2).getValue();
        return server_k2.equals(val);
    });
    // events should have contained a memberID
    MemberIDVerifier verifier = (MemberIDVerifier) ((LocalRegion) r2).getCacheListener();
    assertTrue("client should have received a listener event", verifier.eventReceived);
    assertFalse("client received an update but the event had no member id", verifier.memberIDNotReceived);
    verifier.reset();
}
Also used : LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Example 33 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class DistributedAckRegionCCEDUnitTest method testConcurrentOpWithGII.

/**
   * test for bug #45564. a create() is received by region creator and then a later destroy() is
   * received in initial image and while the version info from the destroy is recorded we keep the
   * value from the create event
   */
@Test
public void testConcurrentOpWithGII() {
    if (this.getClass() != DistributedAckRegionCCEDUnitTest.class) {
        // not really a scope-related thing
        return;
    }
    final String name = this.getUniqueName() + "-CC";
    final String key = "mykey";
    VM vm1 = Host.getHost(0).getVM(1);
    VM vm2 = Host.getHost(0).getVM(2);
    // create some destroyed entries so the GC service is populated
    SerializableCallable create = new SerializableCallable("create region") {

        public Object call() {
            RegionFactory f = getCache().createRegionFactory(getRegionAttributes());
            CCRegion = (LocalRegion) f.create(name);
            return CCRegion.getDistributionManager().getDistributionManagerId();
        }
    };
    // do conflicting update() and destroy() on the region. We want the update() to
    // be sent with a message and the destroy() to be transferred in the initial image
    // and be the value that we want to keep
    InternalDistributedMember vm1ID = (InternalDistributedMember) vm1.invoke(create);
    AsyncInvocation partialCreate = vm2.invokeAsync(new SerializableCallable("create region with stall") {

        public Object call() throws Exception {
            final GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
            RegionFactory f = cache.createRegionFactory(getRegionAttributes());
            InitialImageOperation.VMOTION_DURING_GII = true;
            // this will stall region creation at the point of asking for an initial image
            VMotionObserverHolder.setInstance(new VMotionObserver() {

                @Override
                public void vMotionBeforeCQRegistration() {
                }

                @Override
                public void vMotionBeforeRegisterInterest() {
                }

                @Override
                public void vMotionDuringGII(Set recipientSet, LocalRegion region) {
                    InitialImageOperation.VMOTION_DURING_GII = false;
                    int oldLevel = LocalRegion.setThreadInitLevelRequirement(LocalRegion.BEFORE_INITIAL_IMAGE);
                    LocalRegion ccregion = cache.getRegionByPath("/" + name);
                    try {
                        // happen
                        while (!ccregion.isDestroyed() && ccregion.getRegionEntry(key) == null) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                return;
                            }
                        }
                    } finally {
                        LocalRegion.setThreadInitLevelRequirement(oldLevel);
                    }
                }
            });
            try {
                CCRegion = (LocalRegion) f.create(name);
                // at this point we should have received the update op and then the GII, which should
                // overwrite
                // the conflicting update op
                assertFalse("expected initial image transfer to destroy entry", CCRegion.containsKey(key));
            } finally {
                InitialImageOperation.VMOTION_DURING_GII = false;
            }
            return null;
        }
    });
    vm1.invoke(new SerializableRunnable("create conflicting events") {

        public void run() {
            // wait for the other to come on line
            long waitEnd = System.currentTimeMillis() + 45000;
            DistributionAdvisor adv = ((DistributedRegion) CCRegion).getCacheDistributionAdvisor();
            while (System.currentTimeMillis() < waitEnd && adv.adviseGeneric().isEmpty()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    return;
                }
            }
            if (adv.adviseGeneric().isEmpty()) {
                fail("other member never came on line");
            }
            // inhibit all messaging
            DistributedCacheOperation.LOSS_SIMULATION_RATIO = 200.0;
            try {
                CCRegion.put("mykey", "initialValue");
                CCRegion.destroy("mykey");
            } finally {
                DistributedCacheOperation.LOSS_SIMULATION_RATIO = 0.0;
            }
            // generate a fake version tag for the message
            VersionTag tag = CCRegion.getRegionEntry(key).getVersionStamp().asVersionTag();
            // create a fake member ID that will be < mine and lose a concurrency check
            NetMember nm = CCRegion.getDistributionManager().getDistributionManagerId().getNetMember();
            InternalDistributedMember mbr = null;
            try {
                mbr = new InternalDistributedMember(nm.getInetAddress().getCanonicalHostName(), nm.getPort() - 1, "fake_id", "fake_id_ustring", DistributionManager.NORMAL_DM_TYPE, null, null);
                tag.setMemberID(mbr);
            } catch (UnknownHostException e) {
                org.apache.geode.test.dunit.Assert.fail("could not create member id", e);
            }
            // generate an event to distribute that contains the fake version tag
            EntryEventImpl event = EntryEventImpl.create(CCRegion, Operation.UPDATE, key, false, mbr, true, false);
            event.setNewValue("newValue");
            event.setVersionTag(tag);
            // this should update the controller's cache with the updated value but leave this cache
            // alone
            DistributedCacheOperation op = new UpdateOperation(event, tag.getVersionTimeStamp());
            op.distribute();
            event.release();
        }
    });
    try {
        partialCreate.getResult();
    } catch (Throwable e) {
        org.apache.geode.test.dunit.Assert.fail("async invocation in vm2 failed", e);
    }
}
Also used : DistributionAdvisor(org.apache.geode.distributed.internal.DistributionAdvisor) DistributedCacheOperation(org.apache.geode.internal.cache.DistributedCacheOperation) Set(java.util.Set) UnknownHostException(java.net.UnknownHostException) EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) VMotionObserver(org.apache.geode.internal.cache.vmotion.VMotionObserver) LocalRegion(org.apache.geode.internal.cache.LocalRegion) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) RegionClearedException(org.apache.geode.internal.cache.RegionClearedException) UnknownHostException(java.net.UnknownHostException) CacheException(org.apache.geode.cache.CacheException) RegionFactory(org.apache.geode.cache.RegionFactory) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) UpdateOperation(org.apache.geode.internal.cache.UpdateOperation) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) VMVersionTag(org.apache.geode.internal.cache.versions.VMVersionTag) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) NetMember(org.apache.geode.distributed.internal.membership.NetMember) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 34 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class ClientServerCCEDUnitTest method testClientDoesNotExpireEntryPrematurely.

@Test
public void testClientDoesNotExpireEntryPrematurely() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName() + "Region";
    final String key = "testKey";
    int port = createServerRegion(vm0, name, true);
    vm0.invoke(new SerializableCallable("create old entry") {

        public Object call() throws Exception {
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            r.put(key, "value");
            AbstractRegionEntry entry = (AbstractRegionEntry) r.basicGetEntry(key);
            // set an old timestamp in the entry - thirty minutes ago
            entry.getVersionStamp().setVersionTimeStamp(System.currentTimeMillis() - 1800000L);
            return null;
        }
    });
    createClientRegion(vm1, name, port, true, ClientRegionShortcut.CACHING_PROXY, false);
    vm1.invoke(new SerializableCallable("fetch entry and validate") {

        public Object call() throws Exception {
            final Long[] expirationTimeMillis = new Long[1];
            int expirationSeconds = 15;
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            AttributesMutator mutator = r.getAttributesMutator();
            mutator.setEntryIdleTimeout(new ExpirationAttributes(expirationSeconds, ExpirationAction.LOCAL_DESTROY));
            mutator.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterDestroy(EntryEvent event) {
                    expirationTimeMillis[0] = System.currentTimeMillis();
                }
            });
            // fetch the entry from the server and make sure it doesn't expire early
            if (!r.containsKey(key)) {
                r.get(key);
            }
            final long expirationTime = System.currentTimeMillis() + (expirationSeconds * 1000);
            Awaitility.await("waiting for object to expire").atMost(expirationSeconds * 2, TimeUnit.SECONDS).until(() -> {
                return expirationTimeMillis[0] != null;
            });
            disconnectFromDS();
            assertTrue("entry expired " + (expirationTime - expirationTimeMillis[0]) + " milliseconds early", expirationTimeMillis[0] >= expirationTime);
            return null;
        }
    });
    vm0.invoke(new SerializableRunnable() {

        public void run() {
            disconnectFromDS();
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) IgnoredException(org.apache.geode.test.dunit.IgnoredException) AbstractRegionEntry(org.apache.geode.internal.cache.AbstractRegionEntry) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 35 with LocalRegion

use of org.apache.geode.internal.cache.LocalRegion in project geode by apache.

the class TestDiskRegion method main4.

/**
   * Byte arrays
   */
public static void main4(String[] args) throws Exception {
    DistributedSystem system = DistributedSystem.connect(new java.util.Properties());
    Cache cache = CacheFactory.create(system);
    AttributesFactory factory = new AttributesFactory();
    factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(2, (ObjectSizer) null, EvictionAction.OVERFLOW_TO_DISK));
    LocalRegion region = (LocalRegion) cache.createRegion("TestDiskRegion", factory.create());
    for (int i = 0; i < 100000; i++) {
        System.out.println(i);
        region.put(String.valueOf(i), String.valueOf(i).getBytes());
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) ObjectSizer(org.apache.geode.cache.util.ObjectSizer) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedSystem(org.apache.geode.distributed.DistributedSystem) Cache(org.apache.geode.cache.Cache)

Aggregations

LocalRegion (org.apache.geode.internal.cache.LocalRegion)243 Test (org.junit.Test)103 Region (org.apache.geode.cache.Region)70 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)54 IOException (java.io.IOException)50 AttributesFactory (org.apache.geode.cache.AttributesFactory)42 VM (org.apache.geode.test.dunit.VM)39 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)38 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)37 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)34 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)34 Cache (org.apache.geode.cache.Cache)31 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)31 Host (org.apache.geode.test.dunit.Host)31 Iterator (java.util.Iterator)29 QueryService (org.apache.geode.cache.query.QueryService)29 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)27 CancelException (org.apache.geode.CancelException)26 CacheException (org.apache.geode.cache.CacheException)26 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)25