Search in sources :

Example 11 with MembershipAttributes

use of org.apache.geode.cache.MembershipAttributes 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 12 with MembershipAttributes

use of org.apache.geode.cache.MembershipAttributes 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);
}
Also used : RegionRoleListenerAdapter(org.apache.geode.cache.util.RegionRoleListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RoleEvent(org.apache.geode.cache.RoleEvent) InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) Role(org.apache.geode.distributed.Role) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionRoleListener(org.apache.geode.cache.RegionRoleListener) 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 13 with MembershipAttributes

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

the class LuceneIndexForPartitionedRegionTest method initializeAttributes.

private PartitionAttributes initializeAttributes(final Cache cache) {
    PartitionAttributes partitionAttributes = mock(PartitionAttributes.class);
    RegionAttributes attributes = mock(RegionAttributes.class);
    when(attributes.getCacheListeners()).thenReturn(new CacheListener[0]);
    when(attributes.getRegionTimeToLive()).thenReturn(ExpirationAttributes.DEFAULT);
    when(attributes.getRegionIdleTimeout()).thenReturn(ExpirationAttributes.DEFAULT);
    when(attributes.getEntryTimeToLive()).thenReturn(ExpirationAttributes.DEFAULT);
    when(attributes.getEntryIdleTimeout()).thenReturn(ExpirationAttributes.DEFAULT);
    when(attributes.getMembershipAttributes()).thenReturn(new MembershipAttributes());
    when(cache.getRegionAttributes(RegionShortcut.PARTITION.toString())).thenReturn(attributes);
    when(partitionAttributes.getTotalNumBuckets()).thenReturn(113);
    return partitionAttributes;
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) MembershipAttributes(org.apache.geode.cache.MembershipAttributes)

Example 14 with MembershipAttributes

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

the class CacheRegionsReliablityStatsCheckDUnitTest method testRegionsReliablityStats.

/**
   * The tests check to see if all the reliablity stats are working fine and asserts their values to
   * constants.
   */
@Test
public void testRegionsReliablityStats() throws Exception {
    final String rr1 = "roleA";
    final String regionNoAccess = "regionNoAccess";
    final String regionLimitedAccess = "regionLimitedAccess";
    final String regionFullAccess = "regionFullAccess";
    // final String regionNameRoleA = "roleA";
    String[] requiredRoles = { rr1 };
    Cache myCache = getCache();
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.NO_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(Scope.DISTRIBUTED_ACK);
    fac.setDataPolicy(DataPolicy.REPLICATE);
    RegionAttributes attr = fac.create();
    myCache.createRegion(regionNoAccess, attr);
    ra = new MembershipAttributes(requiredRoles, LossAction.LIMITED_ACCESS, ResumptionAction.NONE);
    fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(Scope.DISTRIBUTED_ACK);
    fac.setDataPolicy(DataPolicy.REPLICATE);
    attr = fac.create();
    myCache.createRegion(regionLimitedAccess, attr);
    ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
    fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(Scope.DISTRIBUTED_ACK);
    fac.setDataPolicy(DataPolicy.REPLICATE);
    attr = fac.create();
    myCache.createRegion(regionFullAccess, attr);
    CachePerfStats stats = ((GemFireCacheImpl) myCache).getCachePerfStats();
    assertEquals(stats.getReliableRegionsMissingNoAccess(), 1);
    assertEquals(stats.getReliableRegionsMissingLimitedAccess(), 1);
    assertEquals(stats.getReliableRegionsMissingFullAccess(), 1);
    assertEquals(stats.getReliableRegionsMissing(), (stats.getReliableRegionsMissingNoAccess() + stats.getReliableRegionsMissingLimitedAccess() + stats.getReliableRegionsMissingFullAccess()));
    Host host = Host.getHost(0);
    VM vm1 = host.getVM(1);
    SerializableRunnable roleAPlayer = new CacheSerializableRunnable("ROLEAPLAYER") {

        public void run2() throws CacheException {
            Properties props = new Properties();
            props.setProperty(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel());
            props.setProperty(ROLES, rr1);
            getSystem(props);
            Cache cache = getCache();
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(Scope.DISTRIBUTED_ACK);
            fac.setDataPolicy(DataPolicy.REPLICATE);
            RegionAttributes attr = fac.create();
            cache.createRegion(regionNoAccess, attr);
            cache.createRegion(regionLimitedAccess, attr);
            cache.createRegion(regionFullAccess, attr);
        }
    };
    vm1.invoke(roleAPlayer);
    assertEquals(stats.getReliableRegionsMissingNoAccess(), 0);
    assertEquals(stats.getReliableRegionsMissingLimitedAccess(), 0);
    assertEquals(stats.getReliableRegionsMissingFullAccess(), 0);
    assertEquals(stats.getReliableRegionsMissing(), (stats.getReliableRegionsMissingNoAccess() + stats.getReliableRegionsMissingLimitedAccess() + stats.getReliableRegionsMissingFullAccess()));
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Cache(org.apache.geode.cache.Cache) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 15 with MembershipAttributes

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

the class CacheXml66DUnitTest method testMembershipAttributes.

/**
   * Test xml support of MembershipAttributes.
   */
@Test
public void testMembershipAttributes() throws Exception {
    final String MY_ROLES = "Foo, Bip, BAM";
    final String[][] roles = new String[][] { { "Foo" }, { "Bip", "BAM" } };
    final LossAction[] policies = (LossAction[]) LossAction.VALUES.toArray(new LossAction[LossAction.VALUES.size()]);
    final ResumptionAction[] actions = (ResumptionAction[]) ResumptionAction.VALUES.toArray(new ResumptionAction[ResumptionAction.VALUES.size()]);
    CacheCreation cache = new CacheCreation();
    // for each policy, try each action and each role...
    for (int policy = 0; policy < policies.length; policy++) {
        for (int action = 0; action < actions.length; action++) {
            for (int role = 0; role < roles.length; role++) {
                String[] theRoles = roles[role];
                LossAction thePolicy = policies[policy];
                ResumptionAction theAction = actions[action];
                // if (theRoles.length == 0 && (thePolicy != LossAction.NONE || theAction !=
                // ResumptionAction.NONE
                RegionAttributesCreation attrs = new RegionAttributesCreation(cache);
                MembershipAttributes ra = new MembershipAttributes(theRoles, thePolicy, theAction);
                attrs.setMembershipAttributes(ra);
                String region = "rootMEMBERSHIP_ATTRIBUTES_" + policy + "_" + action + "_" + role;
                cache.createRegion(region, attrs);
            }
        }
    }
    {
        // make our system play the roles used by this test so the create regions
        // will not think the a required role is missing
        Properties config = new Properties();
        config.setProperty(ROLES, MY_ROLES);
        this.xmlProps = config;
    }
    DistributedRegion.ignoreReconnect = true;
    try {
        testXml(cache);
    } finally {
        this.xmlProps = null;
        try {
            preTearDown();
        } finally {
            DistributedRegion.ignoreReconnect = false;
        }
    }
}
Also used : LossAction(org.apache.geode.cache.LossAction) RegionAttributesCreation(org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) ClientCacheCreation(org.apache.geode.internal.cache.xmlcache.ClientCacheCreation) Properties(java.util.Properties) ResumptionAction(org.apache.geode.cache.ResumptionAction) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) 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