Search in sources :

Example 16 with DistributionMessageObserver

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

the class PersistentRecoveryOrderDUnitTest method doTestPersistConflictOperations.

/**
   * vm0 and vm1 are peers, each holds a DR. They do put to the same key for different value at the
   * same time. Use DistributionMessageObserver.beforeSendMessage to hold on the distribution
   * message. One of the member will persist the conflict version tag, while another member will
   * persist both of the 2 operations. Overall, their RVV should match after the operations.
   */
public void doTestPersistConflictOperations(boolean diskSync) throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    // Add a hook which will disconnect the DS before sending a prepare message
    SerializableRunnable addObserver = new SerializableRunnable() {

        public void run() {
            // System.setProperty("disk.TRACE_WRITES", "true");
            // System.setProperty("disk.TRACE_RECOVERY", "true");
            DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                @Override
                public void beforeSendMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof AbstractUpdateMessage) {
                        try {
                            Thread.sleep(2000);
                            getCache().getLogger().info("testPersistConflictOperations, beforeSendMessage");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void afterProcessMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof AbstractUpdateMessage) {
                        getCache().getLogger().info("testPersistConflictOperations, beforeSendMessage");
                        DistributionMessageObserver.setInstance(null);
                    }
                }
            });
        }
    };
    vm0.invoke(addObserver);
    vm1.invoke(addObserver);
    AsyncInvocation future0 = createPersistentRegionAsync(vm0, diskSync);
    AsyncInvocation future1 = createPersistentRegionAsync(vm1, diskSync);
    future0.join(MAX_WAIT);
    future1.join(MAX_WAIT);
    // createPersistentRegion(vm0);
    // createPersistentRegion(vm1);
    AsyncInvocation ins0 = vm0.invokeAsync(new SerializableRunnable("change the entry") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion(REGION_NAME);
            region.put("A", "vm0");
        }
    });
    AsyncInvocation ins1 = vm1.invokeAsync(new SerializableRunnable("change the entry") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion(REGION_NAME);
            region.put("A", "vm1");
        }
    });
    ins0.join(MAX_WAIT);
    ins1.join(MAX_WAIT);
    RegionVersionVector rvv0 = getRVV(vm0);
    RegionVersionVector rvv1 = getRVV(vm1);
    assertSameRVV(rvv1, rvv0);
    Object value0 = getEntry(vm0, "A");
    Object value1 = getEntry(vm1, "A");
    assertEquals(value0, value1);
    closeRegion(vm0);
    closeRegion(vm1);
    // recover
    future1 = createPersistentRegionAsync(vm1, diskSync);
    future0 = createPersistentRegionAsync(vm0, diskSync);
    future1.join(MAX_WAIT);
    future0.join(MAX_WAIT);
    value0 = getEntry(vm0, "A");
    value1 = getEntry(vm1, "A");
    assertEquals(value0, value1);
    rvv0 = getRVV(vm0);
    rvv1 = getRVV(vm1);
    assertSameRVV(rvv1, rvv0);
    // round 2: async disk write
    vm0.invoke(addObserver);
    vm1.invoke(addObserver);
    ins0 = vm0.invokeAsync(new SerializableRunnable("change the entry at vm0") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion(REGION_NAME);
            for (int i = 0; i < 1000; i++) {
                region.put("A", "vm0-" + i);
            }
        }
    });
    ins1 = vm1.invokeAsync(new SerializableRunnable("change the entry at vm1") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion(REGION_NAME);
            for (int i = 0; i < 1000; i++) {
                region.put("A", "vm1-" + i);
            }
        }
    });
    ins0.join(MAX_WAIT);
    ins1.join(MAX_WAIT);
    rvv0 = getRVV(vm0);
    rvv1 = getRVV(vm1);
    assertSameRVV(rvv1, rvv0);
    value0 = getEntry(vm0, "A");
    value1 = getEntry(vm1, "A");
    assertEquals(value0, value1);
    closeCache(vm0);
    closeCache(vm1);
    // recover again
    future1 = createPersistentRegionAsync(vm1, diskSync);
    future0 = createPersistentRegionAsync(vm0, diskSync);
    future1.join(MAX_WAIT);
    future0.join(MAX_WAIT);
    value0 = getEntry(vm0, "A");
    value1 = getEntry(vm1, "A");
    assertEquals(value0, value1);
    rvv0 = getRVV(vm0);
    rvv1 = getRVV(vm1);
    assertSameRVV(rvv1, rvv0);
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) RegionVersionVector(org.apache.geode.internal.cache.versions.RegionVersionVector) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) AbstractUpdateMessage(org.apache.geode.internal.cache.AbstractUpdateOperation.AbstractUpdateMessage) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) DiskRegion(org.apache.geode.internal.cache.DiskRegion) 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)

Example 17 with DistributionMessageObserver

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

the class PersistentRecoveryOrderDUnitTest method testMissingEntryOnDisk.

@Test
public void testMissingEntryOnDisk() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    // Add a hook which will perform some updates while the region is initializing
    vm0.invoke(new SerializableRunnable() {

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

                @Override
                public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof RequestImageMessage) {
                        Cache cache = getCache();
                        Region region = cache.getRegion(REGION_NAME);
                        if (region == null) {
                            LogWriterUtils.getLogWriter().severe("removing listener for PersistentRecoveryOrderDUnitTest because region was not found: " + REGION_NAME);
                            Object old = DistributionMessageObserver.setInstance(null);
                            if (old != this) {
                                LogWriterUtils.getLogWriter().severe("removed listener was not the invoked listener", new Exception("stack trace"));
                            }
                            return;
                        }
                        region.put("A", "B");
                        region.destroy("A");
                        region.put("A", "C");
                    }
                }

                @Override
                public void afterProcessMessage(DistributionManager dm, DistributionMessage message) {
                }
            });
        }
    });
    createPersistentRegion(vm0);
    createPersistentRegion(vm1);
    checkForEntry(vm1);
    closeRegion(vm0);
    closeRegion(vm1);
    // This should work
    createPersistentRegion(vm1);
    checkForEntry(vm1);
}
Also used : DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) DiskRegion(org.apache.geode.internal.cache.DiskRegion) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) RequestImageMessage(org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) RevokedPersistentDataException(org.apache.geode.cache.persistence.RevokedPersistentDataException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) AdminException(org.apache.geode.admin.AdminException) ConflictingPersistentDataException(org.apache.geode.cache.persistence.ConflictingPersistentDataException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) LockServiceDestroyedException(org.apache.geode.distributed.LockServiceDestroyedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) PersistentReplicatesOfflineException(org.apache.geode.cache.persistence.PersistentReplicatesOfflineException) IOException(java.io.IOException) Cache(org.apache.geode.cache.Cache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 18 with DistributionMessageObserver

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

the class PersistentRecoveryOrderDUnitTest method testGIIDuringDestroy.

/**
   * Test to make sure we don't leak any persistent ids if a member does GII while a distributed
   * destroy is in progress
   */
@Test
public void testGIIDuringDestroy() throws Exception {
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    LogWriterUtils.getLogWriter().info("Creating region in VM0");
    createPersistentRegion(vm0);
    // Add a hook which will disconnect from the distributed
    // system when the initial image message shows up.
    vm1.invoke(new SerializableRunnable() {

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

                @Override
                public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof DestroyRegionMessage) {
                        createPersistentRegionAsync(vm2);
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } finally {
                            DistributionMessageObserver.setInstance(null);
                        }
                    }
                }

                @Override
                public void afterProcessMessage(DistributionManager dm, DistributionMessage message) {
                }

                @Override
                public void beforeSendMessage(DistributionManager dm, DistributionMessage msg) {
                }
            });
        }
    });
    createPersistentRegion(vm1);
    vm0.invoke(new SerializableRunnable("Destroy region") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion(REGION_NAME);
            region.destroyRegion();
        }
    });
    vm1.invoke(new SerializableRunnable("check destroyed") {

        public void run() {
            Cache cache = getCache();
            assertNull(cache.getRegion(REGION_NAME));
        }
    });
    vm2.invoke(new SerializableRunnable("Wait for region creation") {

        public void run() {
            final Cache cache = getCache();
            Wait.waitForCriterion(new WaitCriterion() {

                public String description() {
                    return "Waiting for creation of region " + REGION_NAME;
                }

                public boolean done() {
                    Region region = cache.getRegion(REGION_NAME);
                    return region != null;
                }
            }, MAX_WAIT, 100, true);
        }
    });
    vm2.invoke(new SerializableRunnable("Check offline members") {

        public void run() {
            final Cache cache = getCache();
            DistributedRegion region = (DistributedRegion) cache.getRegion(REGION_NAME);
            PersistenceAdvisor persistAdvisor = region.getPersistenceAdvisor();
            assertEquals(Collections.emptySet(), persistAdvisor.getMembershipView().getOfflineMembers());
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) DiskRegion(org.apache.geode.internal.cache.DiskRegion) Region(org.apache.geode.cache.Region) DestroyRegionMessage(org.apache.geode.internal.cache.DestroyRegionOperation.DestroyRegionMessage) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) 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) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 19 with DistributionMessageObserver

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

the class PersistentRecoveryOrderDUnitTest method testCrashDuringGII.

/**
   * Test to make sure that if if a member crashes while a GII is in progress, we wait for the
   * member to come back for starting.
   */
@Test
public void testCrashDuringGII() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    LogWriterUtils.getLogWriter().info("Creating region in VM0");
    createPersistentRegion(vm0);
    LogWriterUtils.getLogWriter().info("Creating region in VM1");
    createPersistentRegion(vm1);
    putAnEntry(vm0);
    LogWriterUtils.getLogWriter().info("closing region in vm0");
    closeRegion(vm0);
    updateTheEntry(vm1);
    LogWriterUtils.getLogWriter().info("closing region in vm1");
    closeRegion(vm1);
    // This ought to wait for VM1 to come back
    LogWriterUtils.getLogWriter().info("Creating region in VM0");
    AsyncInvocation future = createPersistentRegionAsync(vm0);
    waitForBlockedInitialization(vm0);
    assertTrue(future.isAlive());
    // Add a hook which will disconnect from the distributed
    // system when the initial image message shows up.
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            sawRequestImageMessage.set(false);
            DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                @Override
                public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof RequestImageMessage) {
                        DistributionMessageObserver.setInstance(null);
                        disconnectFromDS();
                        synchronized (sawRequestImageMessage) {
                            sawRequestImageMessage.set(true);
                            sawRequestImageMessage.notifyAll();
                        }
                    }
                }

                @Override
                public void afterProcessMessage(DistributionManager dm, DistributionMessage message) {
                }
            });
        }
    });
    createPersistentRegion(vm1);
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            synchronized (sawRequestImageMessage) {
                try {
                    while (!sawRequestImageMessage.get()) {
                        sawRequestImageMessage.wait();
                    }
                } catch (InterruptedException ex) {
                }
            }
        }
    });
    waitForBlockedInitialization(vm0);
    assertTrue(future.isAlive());
    // Now create the region again. The initialization should
    // work (the observer was cleared when we disconnected from the DS.
    createPersistentRegion(vm1);
    ;
    future.join(MAX_WAIT);
    if (future.isAlive()) {
        fail("Region not created within" + MAX_WAIT);
    }
    if (future.exceptionOccurred()) {
        throw new Exception(future.getException());
    }
    checkForEntry(vm0);
    checkForEntry(vm1);
    checkForRecoveryStat(vm1, true);
    checkForRecoveryStat(vm0, false);
}
Also used : 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) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) RequestImageMessage(org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) RevokedPersistentDataException(org.apache.geode.cache.persistence.RevokedPersistentDataException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) AdminException(org.apache.geode.admin.AdminException) ConflictingPersistentDataException(org.apache.geode.cache.persistence.ConflictingPersistentDataException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) LockServiceDestroyedException(org.apache.geode.distributed.LockServiceDestroyedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) PersistentReplicatesOfflineException(org.apache.geode.cache.persistence.PersistentReplicatesOfflineException) IOException(java.io.IOException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 20 with DistributionMessageObserver

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

the class InterruptClientServerDUnitTest method testClientPutWithInterrupt.

/**
   * A simple test case that we are actually persisting with a PR.
   * 
   * @throws Throwable
   */
@Test
public void testClientPutWithInterrupt() throws Throwable {
    IgnoredException.addIgnoredException("InterruptedException");
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    int port = AvailablePortHelper.getRandomAvailableTCPPort();
    createRegionAndServer(vm0, port);
    // put some data in vm0
    createData(vm0, 0, 10, "a");
    final SerializableCallable interruptTask = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            puttingThread.interrupt();
            return null;
        }
    };
    vm1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            disconnectFromDS();
            DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

                @Override
                public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
                    if (message instanceof UpdateMessage && ((UpdateMessage) message).regionPath.contains("region") && doInterrupt.compareAndSet(true, false)) {
                        vm2.invoke(interruptTask);
                        DistributionMessageObserver.setInstance(null);
                    }
                }
            });
            return null;
        }
    });
    createRegion(vm1);
    createClientRegion(vm2, port);
    SerializableCallable doPuts = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            puttingThread = Thread.currentThread();
            Region<Object, Object> region = getCache().getRegion("region");
            long value = 0;
            long end = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(MAX_WAIT);
            while (!Thread.currentThread().isInterrupted()) {
                region.put(0, value);
                if (System.nanoTime() > end) {
                    fail("Did not get interrupted in 60 seconds");
                }
            }
            return null;
        }
    };
    AsyncInvocation async0 = vm2.invokeAsync(doPuts);
    vm1.invoke(new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            doInterrupt.set(true);
            return null;
        }
    });
    // vm0.invoke(new SerializableCallable() {
    //
    // @Override
    // public Object call() throws Exception {
    // long end = System.nanoTime() + TimeUnit.SECONDS.toNanos(MAX_WAIT);
    // while(puttingThread == null) {
    // Thread.sleep(50);
    // if(System.nanoTime() > end) {
    // fail("Putting thread not set in 60 seconds");
    // }
    // }
    //
    // puttingThread.interrupt();
    // return null;
    // }
    // });
    async0.getResult();
    Object value0 = checkCacheAndGetValue(vm0);
    Object value1 = checkCacheAndGetValue(vm1);
    assertEquals(value0, value1);
}
Also used : UpdateMessage(org.apache.geode.internal.cache.UpdateOperation.UpdateMessage) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) IgnoredException(org.apache.geode.test.dunit.IgnoredException) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) 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)

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