Search in sources :

Example 1 with AbstractUpdateMessage

use of org.apache.geode.internal.cache.AbstractUpdateOperation.AbstractUpdateMessage 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)

Aggregations

Cache (org.apache.geode.cache.Cache)1 Region (org.apache.geode.cache.Region)1 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)1 DistributionMessage (org.apache.geode.distributed.internal.DistributionMessage)1 DistributionMessageObserver (org.apache.geode.distributed.internal.DistributionMessageObserver)1 AbstractUpdateMessage (org.apache.geode.internal.cache.AbstractUpdateOperation.AbstractUpdateMessage)1 DiskRegion (org.apache.geode.internal.cache.DiskRegion)1 DistributedRegion (org.apache.geode.internal.cache.DistributedRegion)1 LocalRegion (org.apache.geode.internal.cache.LocalRegion)1 RegionVersionVector (org.apache.geode.internal.cache.versions.RegionVersionVector)1 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)1 Host (org.apache.geode.test.dunit.Host)1 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)1 VM (org.apache.geode.test.dunit.VM)1