Search in sources :

Example 6 with DistributionMessageObserver

use of org.apache.geode.distributed.internal.DistributionMessageObserver in project geode by apache.

the class PersistentRVVRecoveryDUnitTest method testSkipConflictChecksForConcurrentOps.

/**
   * Test that we skip conflict checks with entries that are on disk compared to entries that come
   * in as part of a concurrent operation
   */
@Test
public void testSkipConflictChecksForConcurrentOps() throws Throwable {
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    // Create the region in few members to test recovery
    createPersistentRegion(vm0);
    createPersistentRegion(vm1);
    // Create an update some entries in vm0 and vm1.
    createData(vm0, 0, 1, "value1");
    createData(vm0, 0, 1, "value2");
    createData(vm0, 0, 1, "value2");
    closeCache(vm1);
    // Update the keys in vm0 until the entry version rolls over.
    // This means that if we did a conflict check, vm0's key will have
    // a lower entry version than vm1, which would cause us to prefer vm1's
    // value
    SerializableRunnable createData = new SerializableRunnable("rollEntryVersion") {

        public void run() {
            Cache cache = getCache();
            LocalRegion region = (LocalRegion) cache.getRegion(REGION_NAME);
            region.put(0, "value3");
            RegionEntry entry = region.getRegionEntry(0);
            entry = region.getRegionEntry(0);
            // Sneak in and change the version number for an entry to generate
            // a conflict.
            VersionTag tag = entry.getVersionStamp().asVersionTag();
            tag.setEntryVersion(tag.getEntryVersion() - 2);
            entry.getVersionStamp().setVersions(tag);
        }
    };
    vm0.invoke(createData);
    // Add an observer to vm0 which will perform a concurrent operation during
    // the GII. If we do a conflict check, this operation will be rejected
    // because it will have a lower entry version that what vm1 recovered from
    // disk
    vm0.invoke(new SerializableRunnable() {

        @Override
        public void run() {
            DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                @Override
                public void beforeProcessMessage(DistributionManager dm, DistributionMessage msg) {
                    if (msg instanceof InitialImageOperation.RequestImageMessage) {
                        if (((InitialImageOperation.RequestImageMessage) msg).regionPath.contains(REGION_NAME)) {
                            createData(vm0, 0, 1, "value4");
                            DistributionMessageObserver.setInstance(null);
                        }
                    }
                }
            });
        }
    });
    // Create vm1, doing a GII
    createPersistentRegion(vm1);
    // If we did a conflict check, this would be value2
    checkData(vm0, 0, 1, "value4");
    checkData(vm1, 0, 1, "value4");
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) RegionEntry(org.apache.geode.internal.cache.RegionEntry) InitialImageOperation(org.apache.geode.internal.cache.InitialImageOperation) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Cache(org.apache.geode.cache.Cache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 7 with DistributionMessageObserver

use of org.apache.geode.distributed.internal.DistributionMessageObserver in project geode by apache.

the class DistributedLockServiceDUnitTest method testDestroyLockServiceBeforeGrantRequest.

@Test
public void testDestroyLockServiceBeforeGrantRequest() throws Throwable {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    final String serviceName = getUniqueName();
    vm0.invoke(new SerializableRunnable("Create the grantor") {

        public void run() {
            connectDistributedSystem();
            final DistributedLockService service = DistributedLockService.create(serviceName, dlstSystem);
            // lock and unlock to make sure this vm is grantor
            assertTrue(service.lock("obj", -1, -1));
            service.unlock("obj");
        }
    });
    DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

        @Override
        public void beforeSendMessage(DistributionManager dm, DistributionMessage message) {
            if (message instanceof DLockRequestMessage) {
                DistributedLockService.destroy(serviceName);
            }
        }
    });
    connectDistributedSystem();
    final DistributedLockService service = DistributedLockService.create(serviceName, dlstSystem);
    try {
        service.lock("obj", -1, -1);
        fail("The lock service should have been destroyed");
    } catch (LockServiceDestroyedException expected) {
    // Do nothing
    }
    vm0.invoke(new SerializableRunnable("check to make sure the lock is not orphaned") {

        public void run() {
            final DistributedLockService service = DistributedLockService.getServiceNamed(serviceName);
            // lock and unlock to make sure this vm is grantor
            assertTrue(service.lock("obj", -1, -1));
            service.unlock("obj");
        }
    });
}
Also used : DLockRequestMessage(org.apache.geode.distributed.internal.locks.DLockRequestProcessor.DLockRequestMessage) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 8 with DistributionMessageObserver

use of org.apache.geode.distributed.internal.DistributionMessageObserver in project geode by apache.

the class Bug41091DUnitTest method test.

@Test
public void test() {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    // We need to use our own locator because we need enable network partition detection.
    startLocatorInVM(vm3, locatorPort);
    try {
        final SerializableRunnable createRegion = new SerializableRunnable("create the region") {

            public void run() {
                DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                    @Override
                    public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
                        if (message instanceof RequestImageMessage) {
                            RequestImageMessage rim = (RequestImageMessage) message;
                            Region region = getCache().getRegion(rim.regionPath);
                            if (region instanceof BucketRegion) {
                                // We can no longer do any puts until the bucket is completely created,
                                // so this will hang
                                // getCache().getRegion("region").put(113, "b");
                                getCache().close();
                            }
                        }
                    }
                });
                Properties props = new Properties();
                props.setProperty(ENABLE_NETWORK_PARTITION_DETECTION, "true");
                props.setProperty(LOCATORS, NetworkUtils.getServerHostName(host) + "[" + locatorPort + "]");
                getSystem(props);
                Cache cache = getCache();
                AttributesFactory af = new AttributesFactory();
                PartitionAttributesFactory paf = new PartitionAttributesFactory();
                paf.setRedundantCopies(1);
                af.setPartitionAttributes(paf.create());
                cache.createRegion("region", af.create());
            }
        };
        vm0.invoke(createRegion);
        vm1.invoke(createRegion);
        vm2.invoke(new SerializableRunnable("create an entry") {

            public void run() {
                Properties props = new Properties();
                props.setProperty(ENABLE_NETWORK_PARTITION_DETECTION, "true");
                props.setProperty(LOCATORS, NetworkUtils.getServerHostName(host) + "[" + locatorPort + "]");
                getSystem(props);
                Cache cache = getCache();
                AttributesFactory af = new AttributesFactory();
                PartitionAttributesFactory paf = new PartitionAttributesFactory();
                paf.setRedundantCopies(1);
                paf.setLocalMaxMemory(0);
                af.setPartitionAttributes(paf.create());
                Region region = cache.createRegion("region", af.create());
                region.put(Integer.valueOf(0), "a");
            }
        });
    } finally {
        SerializableRunnable stopLocator = new SerializableRunnable("Stop locator") {

            public void run() {
                assertTrue(Locator.hasLocator());
                Locator.getLocator().stop();
                assertFalse(Locator.hasLocator());
            }
        };
        vm3.invoke(stopLocator);
    }
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) RequestImageMessage(org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 9 with DistributionMessageObserver

use of org.apache.geode.distributed.internal.DistributionMessageObserver in project geode by apache.

the class Bug41733DUnitTest method testCrashDuringBucketCreation.

/**
   * Test the we can handle a member departing while we are in the process of creating the bucket on
   * the remote node.
   */
@Test
public void testCrashDuringBucketCreation() throws Throwable {
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    vm1.invoke(new SerializableRunnable("Install observer") {

        public void run() {
            DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                @Override
                public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof ManageBucketMessage) {
                        vm0.invoke(() -> disconnectFromDS());
                    }
                }
            });
        }
    });
    createPR(vm0, 0);
    // Create a couple of buckets in VM0. This will make sure
    // the next bucket we create will be created in VM 1.
    putData(vm0, 0, 2, "a");
    createPR(vm1, 0);
    // Trigger a bucket creation in VM1, which should cause vm0 to close it's cache.
    try {
        putData(vm0, 3, 4, "a");
        fail("should have received a cache closed exception");
    } catch (RMIException e) {
        if (!(e.getCause() instanceof DistributedSystemDisconnectedException)) {
            throw e;
        }
    }
    assertEquals(Collections.singleton(3), getBucketList(vm1));
    // This shouldn't hang, because the bucket creation should finish,.
    putData(vm1, 3, 4, "a");
}
Also used : ManageBucketMessage(org.apache.geode.internal.cache.partitioned.ManageBucketMessage) RMIException(org.apache.geode.test.dunit.RMIException) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 10 with DistributionMessageObserver

use of org.apache.geode.distributed.internal.DistributionMessageObserver in project geode by apache.

the class PersistentPartitionedRegionDUnitTest method testCrashDuringBucketCreation.

/**
   * Test for bug 41336
   */
// GEODE-1738
@Category(FlakyTest.class)
@Test
public void testCrashDuringBucketCreation() throws Throwable {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    vm0.invoke(new SerializableRunnable("Install observer") {

        public void run() {
            DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                @Override
                public void beforeSendMessage(DistributionManager dm, DistributionMessage msg) {
                    if (msg instanceof ManageBucketReplyMessage) {
                        Cache cache = getCache();
                        disconnectFromDS();
                        await().atMost(30, SECONDS).until(() -> {
                            return (cache == null || cache.isClosed());
                        });
                        LogWriterUtils.getLogWriter().info("Cache is confirmed closed");
                    }
                }
            });
        }
    });
    createPR(vm0, 0);
    createPR(vm1, 0);
    createData(vm1, 0, 4, "a");
    Set<Integer> vm1Buckets = getBucketList(vm1);
    // Make sure the test hook ran
    vm0.invoke(new SerializableRunnable("Check for no distributed system") {

        public void run() {
            assertEquals(null, GemFireCacheImpl.getInstance());
        }
    });
    checkData(vm1, 0, 4, "a");
    assertEquals(4, vm1Buckets.size());
    createPR(vm0, 0);
    checkData(vm0, 0, 4, "a");
    assertEquals(vm1Buckets, getBucketList(vm1));
    assertEquals(Collections.emptySet(), getBucketList(vm0));
    closeCache(vm0);
    closeCache(vm1);
    AsyncInvocation async0 = createPRAsync(vm0, 0);
    AsyncInvocation async1 = createPRAsync(vm1, 0);
    async0.getResult();
    async1.getResult();
    checkData(vm0, 0, 4, "a");
    assertEquals(vm1Buckets, getBucketList(vm1));
    assertEquals(Collections.emptySet(), getBucketList(vm0));
}
Also used : DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ManageBucketReplyMessage(org.apache.geode.internal.cache.partitioned.ManageBucketMessage.ManageBucketReplyMessage) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Cache(org.apache.geode.cache.Cache) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

DistributionManager (org.apache.geode.distributed.internal.DistributionManager)29 DistributionMessage (org.apache.geode.distributed.internal.DistributionMessage)29 DistributionMessageObserver (org.apache.geode.distributed.internal.DistributionMessageObserver)29 VM (org.apache.geode.test.dunit.VM)28 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)28 Test (org.junit.Test)28 Host (org.apache.geode.test.dunit.Host)27 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)22 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)11 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)11 Cache (org.apache.geode.cache.Cache)9 IgnoredException (org.apache.geode.test.dunit.IgnoredException)9 Region (org.apache.geode.cache.Region)7 RequestImageMessage (org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage)7 LocalRegion (org.apache.geode.internal.cache.LocalRegion)6 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)6 IOException (java.io.IOException)5 DistributedRegion (org.apache.geode.internal.cache.DistributedRegion)5 CacheClosedException (org.apache.geode.cache.CacheClosedException)4 DiskRegion (org.apache.geode.internal.cache.DiskRegion)4