Search in sources :

Example 6 with MembershipAttributes

use of org.apache.geode.cache.MembershipAttributes 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));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) Role(org.apache.geode.distributed.Role) AttributesFactory(org.apache.geode.cache.AttributesFactory) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 7 with MembershipAttributes

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

the class RequiredRolesDUnitTest method testWaitForRequiredRoles.

/**
   * Tests RequiredRoles.waitForRequiredRoles().
   */
@Test
public void testWaitForRequiredRoles() 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();
    final Region region = 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);
    // create thread to call waitForRequiredRoles
    Runnable runWaitForRequiredRoles = new Runnable() {

        public void run() {
            startTestWaitForRequiredRoles = true;
            try {
                rolesTestWaitForRequiredRoles = RequiredRoles.waitForRequiredRoles(region, -1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                failTestWaitForRequiredRoles = true;
            }
            finishTestWaitForRequiredRoles = true;
        }
    };
    // assert thread is waiting
    Thread threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            return RequiredRolesDUnitTest.this.startTestWaitForRequiredRoles;
        }

        public String description() {
            return "waiting for test start";
        }
    };
    Wait.waitForCriterion(ev, 60 * 1000, 200, true);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vms and assert impact on threadA
    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);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vm1... gain for 1st instance of redundant role
    Host.getHost(0).getVM(vm1).invoke(create);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vm2... no gain for 2nd instance of redundant role
    Host.getHost(0).getVM(vm2).invoke(create);
    assertFalse(this.finishTestWaitForRequiredRoles);
    // create region in vm3... gain for 2 roles
    Host.getHost(0).getVM(vm3).invoke(create);
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    // 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);
    // assert new call to RequiredRoles doesn't wait (no role in vm0)
    this.startTestWaitForRequiredRoles = false;
    this.finishTestWaitForRequiredRoles = false;
    threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    // destroy region in vm1... nothing happens in 1st removal of redundant role
    Host.getHost(0).getVM(vm1).invoke(destroy);
    // assert new call to RequiredRoles doesn't wait (redundant role in vm1)
    this.startTestWaitForRequiredRoles = false;
    this.finishTestWaitForRequiredRoles = false;
    threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    // destroy region in vm2... 2nd removal of redundant role is loss
    Host.getHost(0).getVM(vm2).invoke(destroy);
    // assert new call to RequiredRoles does wait (lost role in vm2)
    this.startTestWaitForRequiredRoles = false;
    this.finishTestWaitForRequiredRoles = false;
    threadA = new Thread(group, runWaitForRequiredRoles);
    threadA.start();
    // assert thread is waiting
    ev = new WaitCriterion() {

        public boolean done() {
            return RequiredRolesDUnitTest.this.startTestWaitForRequiredRoles;
        }

        public String description() {
            return "waiting for test start";
        }
    };
    Wait.waitForCriterion(ev, 60 * 1000, 200, true);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertFalse(this.finishTestWaitForRequiredRoles);
    assertMissingRoles(name, vmRoles[vm2]);
    // end the wait and make sure no roles are missing
    Host.getHost(0).getVM(vm2).invoke(create);
    ThreadUtils.join(threadA, 30 * 1000);
    assertTrue(this.startTestWaitForRequiredRoles);
    assertTrue(this.finishTestWaitForRequiredRoles);
    assertTrue(this.rolesTestWaitForRequiredRoles.isEmpty());
    assertMissingRoles(name, new String[] {});
    assertFalse(failTestWaitForRequiredRoles);
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 8 with MembershipAttributes

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

the class RegionReliabilityTestCase method testFullAccessWithLocalRegionExpiration.

/**
   * Tests affect of FULL_ACCESS on local region expiration actions.
   */
@Test
public void testFullAccessWithLocalRegionExpiration() 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.FULL_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setRegionTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    waitForRegionDestroy(region);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 9 with MembershipAttributes

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

the class RegionReliabilityTestCase method testNoAccessWithLocalRegionExpiration.

/**
   * Tests affect of NO_ACCESS on local region expiration actions.
   */
@Test
public void testNoAccessWithLocalRegionExpiration() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    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.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    final Region region = createExpiryRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    AttributesMutator mutator = region.getAttributesMutator();
    mutator.setRegionTimeToLive(new ExpirationAttributes(1, ExpirationAction.LOCAL_DESTROY));
    // sleep and make sure region does not expire
    sleep(200);
    assertFalse(region.isDestroyed());
    // create region in vm1
    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);
        }
    });
    waitForRegionDestroy(region);
}
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) AttributesFactory(org.apache.geode.cache.AttributesFactory) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 10 with MembershipAttributes

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

the class RegionReliabilityTestCase method testFullAccess.

/**
   * Tests affect of FULL_ACCESS on region operations.
   */
@Test
public void testFullAccess() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    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.FULL_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    // use vm0 for netsearch and netload
    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(null);
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            fac.setCacheLoader(new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    if ("netload".equals(helper.getKey())) {
                        return "netload";
                    } else {
                        return null;
                    }
                }

                public void close() {
                }
            });
            RegionAttributes attr = fac.create();
            Region region = createRootRegion(name, attr);
            Object netsearch = "netsearch";
            region.put(netsearch, netsearch);
        }
    });
    // test ops on Region that should not throw
    assertNoAccessDoesNotThrow(region);
}
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) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

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