Search in sources :

Example 21 with MembershipAttributes

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

the class RegionReliabilityTestCase method testReinitialization.

@Test
public void testReinitialization() throws Exception {
    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();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.REINITIALIZE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setDataPolicy(DataPolicy.REPLICATE);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    assertFalse(RequiredRoles.checkForRequiredRoles(region).isEmpty());
    final String key = "KEY-testReinitialization";
    final String val = "VALUE-testReinitialization";
    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Data") {

        public void run2() throws CacheException {
            createConnection(new String[] {});
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            fac.setDataPolicy(DataPolicy.REPLICATE);
            RegionAttributes attr = fac.create();
            Region region = createRootRegion(name, attr);
            region.put(key, val);
        }
    });
    final Region finalRegion = region;
    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {
                RequiredRoles.waitForRequiredRoles(finalRegion, -1);
            } catch (InterruptedException e) {
                fail("interrupted");
            } catch (RegionReinitializedException e) {
            }
        }
    });
    thread.start();
    // create role and verify reinitialization took place
    Host.getHost(0).getVM(1).invokeAsync(new CacheSerializableRunnable("Create Role") {

        public void run2() throws CacheException {
            createConnection(new String[] { roleA });
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            RegionAttributes attr = fac.create();
            createRootRegion(name, attr);
        }
    });
    ThreadUtils.join(thread, 30 * 1000);
    assertTrue(region.isDestroyed());
    try {
        region.put("fee", "fi");
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    try {
        RequiredRoles.checkForRequiredRoles(region);
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    try {
        Role role = (Role) requiredRolesSet.iterator().next();
        RequiredRoles.isRoleInRegionMembership(region, role);
        fail("Should have thrown RegionReinitializedException");
    } catch (RegionReinitializedException e) {
    // pass
    }
    region = getRootRegion(name);
    assertNotNull(region);
    assertTrue(((AbstractRegion) region).requiresReliabilityCheck());
    assertTrue(RequiredRoles.checkForRequiredRoles(region).isEmpty());
    assertNotNull(region.getEntry(key));
    assertEquals(val, region.getEntry(key).getValue());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Role(org.apache.geode.distributed.Role) InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) 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) RegionReinitializedException(org.apache.geode.cache.RegionReinitializedException) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 22 with MembershipAttributes

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

the class DistributedRegion method isNoDistributionOk.

/**
   * Called when we do a distributed operation and don't have anyone to distributed it too. Since
   * this is only called when no distribution was done (i.e. no recipients) we do not check
   * isMissingRequiredRoles because it might not longer be true due to race conditions
   * 
   * @return false if this region has at least one required role and queuing is configured. Returns
   *         true if sending to no one is ok.
   * @throws RoleException if a required role is missing and the LossAction is either NO_ACCESS or
   *         LIMITED_ACCESS.
   * @since GemFire 5.0
   */
boolean isNoDistributionOk() {
    if (this.requiresReliabilityCheck) {
        MembershipAttributes ra = getMembershipAttributes();
        Set<Role> failedRoles = ra.getRequiredRoles();
        throw new RegionDistributionException(LocalizedStrings.DistributedRegion_OPERATION_DISTRIBUTION_WAS_NOT_DONE_TO_THESE_REQUIRED_ROLES_0.toLocalizedString(failedRoles), getFullPath(), failedRoles);
    }
    return true;
}
Also used : Role(org.apache.geode.distributed.Role) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) MembershipAttributes(org.apache.geode.cache.MembershipAttributes)

Example 23 with MembershipAttributes

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

the class DistributedRegion method handleReliableDistribution.

private void handleReliableDistribution(Set successfulRecipients, Set otherRecipients1, Set otherRecipients2) {
    if (this.requiresReliabilityCheck) {
        MembershipAttributes ra = getMembershipAttributes();
        // determine the successful roles
        Set roles = new HashSet();
        for (Object successfulRecipient : successfulRecipients) {
            InternalDistributedMember mbr = (InternalDistributedMember) successfulRecipient;
            if (mbr != null) {
                roles.addAll(mbr.getRoles());
            }
        }
        for (Object anOtherRecipients1 : otherRecipients1) {
            InternalDistributedMember mbr = (InternalDistributedMember) anOtherRecipients1;
            if (mbr != null) {
                roles.addAll(mbr.getRoles());
            }
        }
        for (Object anOtherRecipients2 : otherRecipients2) {
            InternalDistributedMember mbr = (InternalDistributedMember) anOtherRecipients2;
            if (mbr != null) {
                roles.addAll(mbr.getRoles());
            }
        }
        // determine the missing roles
        Set failedRoles = new HashSet(ra.getRequiredRoles());
        failedRoles.removeAll(roles);
        if (failedRoles.isEmpty()) {
            return;
        }
        throw new RegionDistributionException(LocalizedStrings.DistributedRegion_OPERATION_DISTRIBUTION_MAY_HAVE_FAILED_TO_NOTIFY_THESE_REQUIRED_ROLES_0.toLocalizedString(failedRoles), getFullPath(), failedRoles);
    }
}
Also used : Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HashSet(java.util.HashSet) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) RegionDistributionException(org.apache.geode.cache.RegionDistributionException) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) HashSet(java.util.HashSet)

Example 24 with MembershipAttributes

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

the class RequiredRolesDUnitTest method testRequiredRolesInLoss.

/**
   * Tests that RequiredRoles detects missing roles.
   */
@Test
public void testRequiredRolesInLoss() throws Exception {
    String name = this.getUniqueName();
    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 };
    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);
    // 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);
    RegionAttributes rattr = region.getAttributes();
    assertEquals(true, rattr.getMembershipAttributes().hasRequiredRoles());
    Set roles = rattr.getMembershipAttributes().getRequiredRoles();
    assertNotNull(roles);
    assertEquals(false, roles.isEmpty());
    assertEquals(requiredRolesSet.size(), roles.size());
    assertEquals(true, roles.containsAll(requiredRolesSet));
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    // assert all are missing according to RequiredRoles...
    Set missingRoles = RequiredRoles.checkForRequiredRoles(region);
    assertNotNull(missingRoles);
    assertEquals(requiredRolesSet.size(), missingRoles.size());
    assertEquals(true, missingRoles.containsAll(requiredRolesSet));
    // assert isPresent is false on each missing role...
    for (Iterator iter = missingRoles.iterator(); iter.hasNext(); ) {
        Role role = (Role) iter.next();
        assertEquals(false, role.isPresent());
    }
}
Also used : InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) Role(org.apache.geode.distributed.Role) HashSet(java.util.HashSet) Set(java.util.Set) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

MembershipAttributes (org.apache.geode.cache.MembershipAttributes)24 HashSet (java.util.HashSet)18 Properties (java.util.Properties)18 RegionAttributes (org.apache.geode.cache.RegionAttributes)18 Test (org.junit.Test)18 AttributesFactory (org.apache.geode.cache.AttributesFactory)17 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)17 Set (java.util.Set)16 Region (org.apache.geode.cache.Region)16 AbstractRegion (org.apache.geode.internal.cache.AbstractRegion)12 LocalRegion (org.apache.geode.internal.cache.LocalRegion)12 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)12 CacheException (org.apache.geode.cache.CacheException)9 Role (org.apache.geode.distributed.Role)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 AttributesMutator (org.apache.geode.cache.AttributesMutator)6 ExpirationAttributes (org.apache.geode.cache.ExpirationAttributes)6 InternalRole (org.apache.geode.distributed.internal.membership.InternalRole)5 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)5 RegionDistributionException (org.apache.geode.cache.RegionDistributionException)4