Search in sources :

Example 1 with RegionMembershipListener

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

the class RegionReliabilityTestCase method testCommitDistributionException.

@Test
public void testCommitDistributionException() throws Exception {
    if (getRegionScope().isGlobal())
        // skip test under Global
        return;
    if (getRegionScope().isDistributedNoAck())
        // skip test under DistributedNoAck
        return;
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {

        public void afterRemoteRegionDeparture(RegionEvent event) {
            synchronized (detectedDeparture_testCommitDistributionException) {
                detectedDeparture_testCommitDistributionException[0] = Boolean.TRUE;
                detectedDeparture_testCommitDistributionException.notify();
            }
        }
    };
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.addCacheListener(listener);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    // use vm1 to create role
    Host.getHost(0).getVM(1).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    // define the afterReleaseLocalLocks callback
    SerializableRunnableIF removeRequiredRole = new SerializableRunnableIF() {

        public void run() {
            Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {

                public void run() {
                    getRootRegion(name).close();
                }
            });
            try {
                synchronized (detectedDeparture_testCommitDistributionException) {
                    while (detectedDeparture_testCommitDistributionException[0] == Boolean.FALSE) {
                        detectedDeparture_testCommitDistributionException.wait();
                    }
                }
            } catch (InterruptedException e) {
                fail("interrupted");
            }
        }
    };
    // define the add and remove expected exceptions
    final String expectedExceptions = "org.apache.geode.internal.cache.CommitReplyException";
    SerializableRunnable addExpectedExceptions = new CacheSerializableRunnable("addExpectedExceptions") {

        public void run2() throws CacheException {
            getCache().getLogger().info("<ExpectedException action=add>" + expectedExceptions + "</ExpectedException>");
        }
    };
    SerializableRunnable removeExpectedExceptions = new CacheSerializableRunnable("removeExpectedExceptions") {

        public void run2() throws CacheException {
            getCache().getLogger().info("<ExpectedException action=remove>" + expectedExceptions + "</ExpectedException>");
        }
    };
    // perform the actual test...
    CacheTransactionManager ctm = cache.getCacheTransactionManager();
    ctm.begin();
    TXStateInterface txStateProxy = ((TXManagerImpl) ctm).getTXState();
    ((TXStateProxyImpl) txStateProxy).forceLocalBootstrap();
    TXState txState = (TXState) ((TXStateProxyImpl) txStateProxy).getRealDeal(null, null);
    txState.setBeforeSend(() -> {
        try {
            removeRequiredRole.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    // now start a transaction and commit it
    region.put("KEY", "VAL");
    addExpectedExceptions.run();
    Host.getHost(0).getVM(1).invoke(addExpectedExceptions);
    try {
        ctm.commit();
        fail("Should have thrown CommitDistributionException");
    } catch (CommitDistributionException e) {
    // pass
    } finally {
        removeExpectedExceptions.run();
        Host.getHost(0).getVM(1).invoke(removeExpectedExceptions);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) TXStateProxyImpl(org.apache.geode.internal.cache.TXStateProxyImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TXState(org.apache.geode.internal.cache.TXState) AttributesFactory(org.apache.geode.cache.AttributesFactory) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) HashSet(java.util.HashSet) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) TXStateInterface(org.apache.geode.internal.cache.TXStateInterface) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) RegionAccessException(org.apache.geode.cache.RegionAccessException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionMembershipListenerAdapter(org.apache.geode.cache.util.RegionMembershipListenerAdapter) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 2 with RegionMembershipListener

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

the class RegionReliabilityTestCase method testRegionDistributionException.

@Test
public void testRegionDistributionException() throws Exception {
    if (getRegionScope().isDistributedNoAck())
        // skip test under DistributedNoAck
        return;
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    RegionMembershipListener listener = new RegionMembershipListenerAdapter() {

        public void afterRemoteRegionDeparture(RegionEvent event) {
            synchronized (detectedDeparture_testRegionDistributionException) {
                detectedDeparture_testRegionDistributionException[0] = Boolean.TRUE;
                detectedDeparture_testRegionDistributionException.notify();
            }
        }
    };
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    // fac.addCacheListener(listener);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    // use vm1 to create role
    CacheSerializableRunnable createRegion = new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    };
    Host.getHost(0).getVM(1).invoke(createRegion);
    region.put("DESTROY_ME", "VAL");
    region.put("INVALIDATE_ME", "VAL");
    // define the afterReleaseLocalLocks callback
    SerializableRunnable removeRequiredRole = new SerializableRunnable() {

        public void run() {
            Host.getHost(0).getVM(1).invoke(new SerializableRunnable("Close Region") {

                public void run() {
                    getRootRegion(name).close();
                }
            });
        // try {
        // synchronized (detectedDeparture_testRegionDistributionException) {
        // while (detectedDeparture_testRegionDistributionException[0] == Boolean.FALSE) {
        // detectedDeparture_testRegionDistributionException.wait();
        // }
        // }
        // }
        // catch (InterruptedException e) {}
        }
    };
    DistributedCacheOperation.setBeforePutOutgoing(() -> {
        try {
            removeRequiredRole.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
    Runnable reset = new Runnable() {

        public void run() {
        // synchronized (detectedDeparture_testRegionDistributionException) {
        // detectedDeparture_testRegionDistributionException[0] = Boolean.FALSE;
        // }
        }
    };
    // PUT
    try {
        region.put("KEY", "VAL");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // INVALIDATE
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.invalidate("INVALIDATE_ME");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // DESTROY
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.destroy("DESTROY_ME");
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // CLEAR
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.clear();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // PUTALL
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        Map putAll = new HashMap();
        putAll.put("PUTALL_ME", "VAL");
        region.putAll(putAll);
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // INVALIDATE REGION
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.invalidateRegion();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
    // DESTROY REGION
    reset.run();
    Host.getHost(0).getVM(1).invoke(createRegion);
    try {
        region.destroyRegion();
        fail("Should have thrown RegionDistributionException");
    } catch (RegionDistributionException e) {
    // pass
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) HashMap(java.util.HashMap) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) CommitDistributionException(org.apache.geode.cache.CommitDistributionException) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) RegionAccessException(org.apache.geode.cache.RegionAccessException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) AttributesFactory(org.apache.geode.cache.AttributesFactory) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) RegionMembershipListenerAdapter(org.apache.geode.cache.util.RegionMembershipListenerAdapter) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 3 with RegionMembershipListener

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

the class PartitionedRegion method postCreateRegion.

@Override
protected void postCreateRegion() {
    super.postCreateRegion();
    CacheListener[] listeners = fetchCacheListenersField();
    if (listeners != null && listeners.length > 0) {
        Set others = getRegionAdvisor().adviseGeneric();
        for (int i = 0; i < listeners.length; i++) {
            if (listeners[i] instanceof RegionMembershipListener) {
                RegionMembershipListener rml = (RegionMembershipListener) listeners[i];
                try {
                    DistributedMember[] otherDms = new DistributedMember[others.size()];
                    others.toArray(otherDms);
                    rml.initialMembers(this, otherDms);
                } catch (VirtualMachineError err) {
                    SystemFailure.initiateFailure(err);
                    // now, so don't let this thread continue.
                    throw err;
                } catch (Throwable t) {
                    // Whenever you catch Error or Throwable, you must also
                    // catch VirtualMachineError (see above). However, there is
                    // _still_ a possibility that you are dealing with a cascading
                    // error condition, so you also need to check to see if the JVM
                    // is still usable:
                    SystemFailure.checkFailure();
                    logger.error(LocalizedMessage.create(LocalizedStrings.DistributedRegion_EXCEPTION_OCCURRED_IN_REGIONMEMBERSHIPLISTENER), t);
                }
            }
        }
    }
    PartitionListener[] partitionListeners = this.getPartitionListeners();
    if (partitionListeners != null && partitionListeners.length != 0) {
        for (int i = 0; i < partitionListeners.length; i++) {
            PartitionListener listener = partitionListeners[i];
            if (listener != null) {
                listener.afterRegionCreate(this);
            }
        }
    }
    Set<String> allGatewaySenderIds = getAllGatewaySenderIds();
    if (!allGatewaySenderIds.isEmpty()) {
        for (GatewaySender sender : cache.getAllGatewaySenders()) {
            if (sender.isParallel() && allGatewaySenderIds.contains(sender.getId())) {
                /*
           * get the ParallelGatewaySender to create the colocated partitioned region for this
           * region.
           */
                if (sender.isRunning()) {
                    AbstractGatewaySender senderImpl = (AbstractGatewaySender) sender;
                    ((ConcurrentParallelGatewaySenderQueue) senderImpl.getQueues().toArray(new RegionQueue[1])[0]).addShadowPartitionedRegionForUserPR(this);
                }
            }
        }
    }
}
Also used : AbstractGatewaySender(org.apache.geode.internal.cache.wan.AbstractGatewaySender) GatewaySender(org.apache.geode.cache.wan.GatewaySender) HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) CacheListener(org.apache.geode.cache.CacheListener) PartitionListener(org.apache.geode.cache.partition.PartitionListener) ConcurrentParallelGatewaySenderQueue(org.apache.geode.internal.cache.wan.parallel.ConcurrentParallelGatewaySenderQueue) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) AbstractGatewaySender(org.apache.geode.internal.cache.wan.AbstractGatewaySender)

Example 4 with RegionMembershipListener

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

the class DistributedRegion method postCreateRegion.

/**
   * In addition to inherited code this method also invokes RegionMembershipListeners
   */
@Override
protected void postCreateRegion() {
    super.postCreateRegion();
    // should we sync on this.distAdvisor first to prevent bug 44369?
    synchronized (this.advisorListener) {
        Set others = this.advisorListener.getInitialMembers();
        CacheListener[] listeners = fetchCacheListenersField();
        if (listeners != null) {
            for (CacheListener listener : listeners) {
                if (listener instanceof RegionMembershipListener) {
                    RegionMembershipListener regionMembershipListener = (RegionMembershipListener) listener;
                    try {
                        DistributedMember[] otherDms = new DistributedMember[others.size()];
                        others.toArray(otherDms);
                        regionMembershipListener.initialMembers(this, otherDms);
                    } catch (VirtualMachineError err) {
                        SystemFailure.initiateFailure(err);
                        // now, so don't let this thread continue.
                        throw err;
                    } catch (Throwable t) {
                        // Whenever you catch Error or Throwable, you must also
                        // catch VirtualMachineError (see above). However, there is
                        // _still_ a possibility that you are dealing with a cascading
                        // error condition, so you also need to check to see if the JVM
                        // is still usable:
                        SystemFailure.checkFailure();
                        logger.error(LocalizedMessage.create(LocalizedStrings.DistributedRegion_EXCEPTION_OCCURRED_IN_REGIONMEMBERSHIPLISTENER), t);
                    }
                }
            }
        }
        Set<String> allGatewaySenderIds = getAllGatewaySenderIds();
        if (!allGatewaySenderIds.isEmpty()) {
            for (GatewaySender sender : this.cache.getAllGatewaySenders()) {
                if (sender.isParallel() && allGatewaySenderIds.contains(sender.getId())) {
                    // addShadowPartitionedRegionForUserRR
                    if (sender.getId().contains(AsyncEventQueueImpl.ASYNC_EVENT_QUEUE_PREFIX)) {
                        throw new AsyncEventQueueConfigurationException(LocalizedStrings.ParallelAsyncEventQueue_0_CAN_NOT_BE_USED_WITH_REPLICATED_REGION_1.toLocalizedString(new Object[] { AsyncEventQueueImpl.getAsyncEventQueueIdFromSenderId(sender.getId()), this.getFullPath() }));
                    }
                    throw new GatewaySenderConfigurationException(LocalizedStrings.ParallelGatewaySender_0_CAN_NOT_BE_USED_WITH_REPLICATED_REGION_1.toLocalizedString(new Object[] { sender.getId(), this.getFullPath() }));
                }
            }
        }
    }
}
Also used : GatewaySender(org.apache.geode.cache.wan.GatewaySender) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) GatewaySenderConfigurationException(org.apache.geode.internal.cache.wan.GatewaySenderConfigurationException) RegionMembershipListener(org.apache.geode.cache.RegionMembershipListener) CacheListener(org.apache.geode.cache.CacheListener) AsyncEventQueueConfigurationException(org.apache.geode.internal.cache.wan.AsyncEventQueueConfigurationException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember)

Aggregations

HashSet (java.util.HashSet)4 Set (java.util.Set)4 RegionMembershipListener (org.apache.geode.cache.RegionMembershipListener)4 Properties (java.util.Properties)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 CacheException (org.apache.geode.cache.CacheException)2 CacheListener (org.apache.geode.cache.CacheListener)2 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)2 CommitDistributionException (org.apache.geode.cache.CommitDistributionException)2 MembershipAttributes (org.apache.geode.cache.MembershipAttributes)2 Region (org.apache.geode.cache.Region)2 RegionAccessException (org.apache.geode.cache.RegionAccessException)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 RegionDistributionException (org.apache.geode.cache.RegionDistributionException)2 RegionEvent (org.apache.geode.cache.RegionEvent)2 RegionReinitializedException (org.apache.geode.cache.RegionReinitializedException)2 RegionMembershipListenerAdapter (org.apache.geode.cache.util.RegionMembershipListenerAdapter)2 GatewaySender (org.apache.geode.cache.wan.GatewaySender)2 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)2 DistributedMember (org.apache.geode.distributed.DistributedMember)2