use of org.apache.geode.cache.CommitDistributionException in project geode by apache.
the class TXCommitMessage method checkDistributionReliability.
/**
* Checks reliable regions and throws CommitDistributionException if any required roles may not
* have received the commit message.
*
* @param distMap map of RegionCommitList keys to Sets of receivers
* @param processor the reply processor
* @throws CommitDistributionException if any required roles may not have received the commit
* message
*/
private void checkDistributionReliability(Map distMap, CommitReplyProcessor processor) {
// key=RegionCommit, value=Set of recipients
Map regionToRecipients = new IdentityHashMap();
// build up the keys in regionToRecipients and add all receivers
for (Iterator distIter = distMap.entrySet().iterator(); distIter.hasNext(); ) {
Map.Entry me = (Map.Entry) distIter.next();
RegionCommitList rcl = (RegionCommitList) me.getKey();
Set recipients = (Set) me.getValue();
for (Iterator rclIter = rcl.iterator(); rclIter.hasNext(); ) {
RegionCommit rc = (RegionCommit) rclIter.next();
// skip region if no required roles
if (!rc.r.requiresReliabilityCheck()) {
continue;
}
Set recipientsForRegion = (Set) regionToRecipients.get(rc);
if (recipientsForRegion == null) {
recipientsForRegion = new HashSet();
regionToRecipients.put(rc, recipientsForRegion);
}
// get the receiver Set for rcl and perform addAll
if (recipients != null) {
recipientsForRegion.addAll(recipients);
}
}
}
Set cacheClosedMembers = (processor == null) ? Collections.emptySet() : processor.getCacheClosedMembers();
Set departedMembers = (processor == null) ? Collections.emptySet() : processor.getDepartedMembers();
// check reliability on each region
Set regionDistributionExceptions = Collections.emptySet();
Set failedRegionNames = Collections.emptySet();
for (Iterator iter = regionToRecipients.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry me = (Map.Entry) iter.next();
final RegionCommit rc = (RegionCommit) me.getKey();
final Set successfulRecipients = new HashSet(msgMap.keySet());
successfulRecipients.removeAll(departedMembers);
// remove members who destroyed that region or closed their cache
Set regionDestroyedMembers = (processor == null) ? Collections.emptySet() : processor.getRegionDestroyedMembers(rc.r.getFullPath());
successfulRecipients.removeAll(cacheClosedMembers);
successfulRecipients.removeAll(regionDestroyedMembers);
try {
rc.r.handleReliableDistribution(successfulRecipients);
} catch (RegionDistributionException e) {
if (regionDistributionExceptions == Collections.emptySet()) {
regionDistributionExceptions = new HashSet();
failedRegionNames = new HashSet();
}
regionDistributionExceptions.add(e);
failedRegionNames.add(rc.r.getFullPath());
}
}
if (!regionDistributionExceptions.isEmpty()) {
throw new CommitDistributionException(LocalizedStrings.TXCommitMessage_THESE_REGIONS_EXPERIENCED_RELIABILITY_FAILURE_DURING_DISTRIBUTION_OF_THE_OPERATION_0.toLocalizedString(failedRegionNames), regionDistributionExceptions);
}
}
use of org.apache.geode.cache.CommitDistributionException 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);
}
}
Aggregations