use of org.apache.geode.distributed.Role in project geode by apache.
the class RequiredRolesDUnitTest method testIsRoleInRegionMembership.
/**
* Tests RequiredRoles.isRoleInRegionMembership().
*/
@Test
public void testIsRoleInRegionMembership() throws Exception {
final String name = this.getUniqueName();
final int vm0 = 0;
final int vm1 = 1;
final int vm2 = 2;
final int vm3 = 3;
final String roleA = name + "-A";
final String roleC = name + "-C";
final String roleD = name + "-D";
// assign names to 4 vms...
final String[] requiredRoles = { roleA, roleC, roleD };
final String[] rolesProp = { "", roleA, roleA, roleC + "," + roleD };
final String[][] vmRoles = new String[][] { {}, { roleA }, { roleA }, { roleC, roleD } };
for (int i = 0; i < vmRoles.length; i++) {
final int vm = i;
Host.getHost(0).getVM(vm).invoke(new SerializableRunnable() {
public void run() {
Properties config = new Properties();
config.setProperty(ROLES, rolesProp[vm]);
getSystem(config);
}
});
}
// connect controller to system...
Properties config = new Properties();
config.setProperty(ROLES, "");
getSystem(config);
// create region in controller...
MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
AttributesFactory fac = new AttributesFactory();
fac.setMembershipAttributes(ra);
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
Region region = createRootRegion(name, attr);
// wait for memberTimeout to expire
waitForMemberTimeout();
// assert each role is missing
final Set requiredRolesSet = region.getAttributes().getMembershipAttributes().getRequiredRoles();
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory();
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
};
// create region in vm0... no gain for no role
Host.getHost(0).getVM(vm0).invoke(create);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
// create region in vm1... gain for 1st instance of redundant role
Host.getHost(0).getVM(vm1).invoke(create);
for (int i = 0; i < vmRoles[vm1].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm1][i]);
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// create region in vm2... no gain for 2nd instance of redundant role
Host.getHost(0).getVM(vm2).invoke(create);
for (int i = 0; i < vmRoles[vm2].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm2][i]);
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// create region in vm3... gain for 2 roles
Host.getHost(0).getVM(vm3).invoke(create);
for (int i = 0; i < vmRoles[vm3].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm3][i]);
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
SerializableRunnable destroy = new CacheSerializableRunnable("Destroy Region") {
public void run2() throws CacheException {
Region region = getRootRegion(name);
region.localDestroyRegion();
}
};
// destroy region in vm0... no loss of any role
Host.getHost(0).getVM(vm0).invoke(destroy);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// destroy region in vm1... nothing happens in 1st removal of redundant role
Host.getHost(0).getVM(vm1).invoke(destroy);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertTrue(RequiredRoles.isRoleInRegionMembership(region, role));
}
// destroy region in vm2... 2nd removal of redundant role is loss
Host.getHost(0).getVM(vm2).invoke(destroy);
for (int i = 0; i < vmRoles[vm2].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm2][i]);
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
// destroy region in vm3... two more roles are in loss
Host.getHost(0).getVM(vm3).invoke(destroy);
for (Iterator iter = requiredRolesSet.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
assertFalse(RequiredRoles.isRoleInRegionMembership(region, role));
}
}
use of org.apache.geode.distributed.Role in project geode by apache.
the class ReliabilityTestCase method assertMissingRoles.
/** Asserts that the specified roles are missing */
protected void assertMissingRoles(String regionName, String[] roles) {
Region region = getRootRegion(regionName);
Set missingRoles = RequiredRoles.checkForRequiredRoles(region);
assertNotNull(missingRoles);
assertEquals(roles.length, missingRoles.size());
for (Iterator iter = missingRoles.iterator(); iter.hasNext(); ) {
Role role = (Role) iter.next();
boolean found = false;
for (int i = 0; i < roles.length; i++) {
if (role.getName().equals(roles[i])) {
found = true;
break;
}
}
assertTrue("Unexpected missing role: " + role.getName(), found);
}
}
use of org.apache.geode.distributed.Role in project geode by apache.
the class RegionReliabilityListenerDUnitTest method testRoleGainAndLoss.
/**
* Tests the notification of afterRoleGain and afterRoleLoss
*/
@Test
public void testRoleGainAndLoss() throws Exception {
final String name = this.getUniqueName();
final int vm0 = 0;
final int vm1 = 1;
final int vm2 = 2;
final int vm3 = 3;
final String roleA = name + "-A";
final String roleC = name + "-C";
final String roleD = name + "-D";
// assign names to 4 vms...
final String[] requiredRoles = { roleA, roleC, roleD };
final String[] rolesProp = { "", roleA, roleA, roleC + "," + roleD };
final String[][] vmRoles = new String[][] { {}, { roleA }, { roleA }, { roleC, roleD } };
for (int i = 0; i < vmRoles.length; i++) {
final int vm = i;
Host.getHost(0).getVM(vm).invoke(new SerializableRunnable() {
public void run() {
Properties config = new Properties();
config.setProperty(ROLES, rolesProp[vm]);
getSystem(config);
}
});
}
// define RegionRoleListener
final RegionRoleListener listener = new RegionRoleListenerAdapter() {
public void afterRoleGain(RoleEvent event) {
RegionReliabilityListenerDUnitTest.rolesGain = event.getRequiredRoles();
}
public void afterRoleLoss(RoleEvent event) {
RegionReliabilityListenerDUnitTest.rolesLoss = event.getRequiredRoles();
}
};
// connect controller to system...
Properties config = new Properties();
config.setProperty(ROLES, "");
getSystem(config);
// create region in controller...
MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
AttributesFactory fac = new AttributesFactory();
fac.addCacheListener(listener);
fac.setMembershipAttributes(ra);
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
// wait for memberTimeout to expire
waitForMemberTimeout();
// assert in state of role loss... test all are missing according to RequiredRoles
assertMissingRoles(name, requiredRoles);
// assert gain is fired...
SerializableRunnable create = new CacheSerializableRunnable("Create Region") {
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory();
fac.setScope(Scope.DISTRIBUTED_ACK);
RegionAttributes attr = fac.create();
createRootRegion(name, attr);
}
};
// create region in vm0... no gain for no role
Host.getHost(0).getVM(vm0).invoke(create);
assertNull(rolesGain);
assertNull(rolesLoss);
assertMissingRoles(name, requiredRoles);
// create region in vm1... gain for 1st instance of redundant role
Host.getHost(0).getVM(vm1).invoke(create);
assertNotNull(rolesGain);
assertEquals(vmRoles[vm1].length, rolesGain.size());
for (int i = 0; i < vmRoles[vm1].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm1][i]);
assertEquals(true, rolesGain.contains(role));
}
assertNull(rolesLoss);
rolesGain = null;
// only vm3 has missing roles
assertMissingRoles(name, vmRoles[vm3]);
// create region in vm2... no gain for 2nd instance of redundant role
Host.getHost(0).getVM(vm2).invoke(create);
assertNull(rolesGain);
assertNull(rolesLoss);
// only vm3 has missing roles
assertMissingRoles(name, vmRoles[vm3]);
// create region in vm3... gain for 2 roles
Host.getHost(0).getVM(vm3).invoke(create);
assertNotNull(rolesGain);
assertEquals(vmRoles[vm3].length, rolesGain.size());
for (int i = 0; i < vmRoles[vm3].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm3][i]);
assertEquals(true, rolesGain.contains(role));
}
assertNull(rolesLoss);
rolesGain = null;
// no missing roles
assertMissingRoles(name, new String[0]);
// assert loss is fired...
SerializableRunnable destroy = new CacheSerializableRunnable("Destroy Region") {
public void run2() throws CacheException {
Region region = getRootRegion(name);
region.localDestroyRegion();
}
};
// destroy region in vm0... no loss of any role
Host.getHost(0).getVM(vm0).invoke(destroy);
assertNull(rolesGain);
assertNull(rolesLoss);
// no missing roles
assertMissingRoles(name, new String[0]);
// destroy region in vm1... nothing happens in 1st removal of redundant role
Host.getHost(0).getVM(vm1).invoke(destroy);
assertNull(rolesGain);
assertNull(rolesLoss);
// no missing roles
assertMissingRoles(name, new String[0]);
// destroy region in vm2... 2nd removal of redundant role is loss
Host.getHost(0).getVM(vm2).invoke(destroy);
assertNull(rolesGain);
assertNotNull(rolesLoss);
assertEquals(vmRoles[vm2].length, rolesLoss.size());
for (int i = 0; i < vmRoles[vm2].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm2][i]);
assertEquals(true, rolesLoss.contains(role));
}
rolesLoss = null;
// only vm2 has missing roles
assertMissingRoles(name, vmRoles[vm2]);
// destroy region in vm3... two more roles are in loss
Host.getHost(0).getVM(vm3).invoke(destroy);
assertNull(rolesGain);
assertNotNull(rolesLoss);
assertEquals(vmRoles[vm3].length, rolesLoss.size());
for (int i = 0; i < vmRoles[vm3].length; i++) {
Role role = InternalRole.getRole(vmRoles[vm3][i]);
assertEquals(true, rolesLoss.contains(role));
}
rolesLoss = null;
// all roles are missing
assertMissingRoles(name, requiredRoles);
}
use of org.apache.geode.distributed.Role in project geode by apache.
the class DistributionManager method getRoleCount.
/** Returns count of members filling the specified role */
public int getRoleCount(Role role) {
int count = 0;
Set mbrs = getDistributionManagerIds();
for (Iterator mbrIter = mbrs.iterator(); mbrIter.hasNext(); ) {
Set roles = ((InternalDistributedMember) mbrIter.next()).getRoles();
for (Iterator rolesIter = roles.iterator(); rolesIter.hasNext(); ) {
Role mbrRole = (Role) rolesIter.next();
if (mbrRole.equals(role)) {
count++;
break;
}
}
}
return count;
}
use of org.apache.geode.distributed.Role in project geode by apache.
the class DistributionManager method getAllRoles.
/** Returns a set of all roles currently in the distributed system. */
public Set getAllRoles() {
Set allRoles = new HashSet();
Set mbrs = getDistributionManagerIds();
for (Iterator mbrIter = mbrs.iterator(); mbrIter.hasNext(); ) {
Set roles = ((InternalDistributedMember) mbrIter.next()).getRoles();
for (Iterator rolesIter = roles.iterator(); rolesIter.hasNext(); ) {
Role mbrRole = (Role) rolesIter.next();
allRoles.add(mbrRole);
}
}
return allRoles;
}
Aggregations