Search in sources :

Example 51 with InternalDistributedSystem

use of org.apache.geode.distributed.internal.InternalDistributedSystem in project geode by apache.

the class DiskStoreCommands method compactDiskStore.

@CliCommand(value = CliStrings.COMPACT_DISK_STORE, help = CliStrings.COMPACT_DISK_STORE__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_DISKSTORE })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result compactDiskStore(@CliOption(key = CliStrings.COMPACT_DISK_STORE__NAME, mandatory = true, optionContext = ConverterHint.DISKSTORE, help = CliStrings.COMPACT_DISK_STORE__NAME__HELP) String diskStoreName, @CliOption(key = CliStrings.COMPACT_DISK_STORE__GROUP, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.COMPACT_DISK_STORE__GROUP__HELP) String[] groups) {
    Result result = null;
    try {
        // disk store exists validation
        if (!diskStoreExists(diskStoreName)) {
            result = ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.COMPACT_DISK_STORE__DISKSTORE_0_DOESNOT_EXIST, new Object[] { diskStoreName }));
        } else {
            InternalDistributedSystem ds = getCache().getInternalDistributedSystem();
            Map<DistributedMember, PersistentID> overallCompactInfo = new HashMap<DistributedMember, PersistentID>();
            Set<?> otherMembers = ds.getDistributionManager().getOtherNormalDistributionManagerIds();
            Set<InternalDistributedMember> allMembers = new HashSet<InternalDistributedMember>();
            for (Object member : otherMembers) {
                allMembers.add((InternalDistributedMember) member);
            }
            allMembers.add(ds.getDistributedMember());
            otherMembers = null;
            String groupInfo = "";
            // if groups are specified, find members in the specified group
            if (groups != null && groups.length > 0) {
                groupInfo = CliStrings.format(CliStrings.COMPACT_DISK_STORE__MSG__FOR_GROUP, new Object[] { Arrays.toString(groups) + "." });
                final Set<InternalDistributedMember> selectedMembers = new HashSet<InternalDistributedMember>();
                List<String> targetedGroups = Arrays.asList(groups);
                for (Iterator<InternalDistributedMember> iterator = allMembers.iterator(); iterator.hasNext(); ) {
                    InternalDistributedMember member = iterator.next();
                    List<String> memberGroups = member.getGroups();
                    if (!Collections.disjoint(targetedGroups, memberGroups)) {
                        selectedMembers.add(member);
                    }
                }
                allMembers = selectedMembers;
            }
            // have at least one member
            if (allMembers.isEmpty()) {
                result = ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.COMPACT_DISK_STORE__NO_MEMBERS_FOUND_IN_SPECIFED_GROUP, new Object[] { Arrays.toString(groups) }));
            } else {
                // first invoke on local member if it exists in the targeted set
                if (allMembers.remove(ds.getDistributedMember())) {
                    PersistentID compactedDiskStoreId = CompactRequest.compactDiskStore(diskStoreName);
                    if (compactedDiskStoreId != null) {
                        overallCompactInfo.put(ds.getDistributedMember(), compactedDiskStoreId);
                    }
                }
                // CompactRequest. Otherwise, send the request to others
                if (!allMembers.isEmpty()) {
                    // Invoke compact on all 'other' members
                    Map<DistributedMember, PersistentID> memberCompactInfo = CompactRequest.send(ds.getDistributionManager(), diskStoreName, allMembers);
                    if (memberCompactInfo != null && !memberCompactInfo.isEmpty()) {
                        overallCompactInfo.putAll(memberCompactInfo);
                        memberCompactInfo.clear();
                    }
                    String notExecutedMembers = CompactRequest.getNotExecutedMembers();
                    LogWrapper.getInstance().info("compact disk-store \"" + diskStoreName + "\" message was scheduled to be sent to but was not send to " + notExecutedMembers);
                }
                // If compaction happened at all, then prepare the summary
                if (overallCompactInfo != null && !overallCompactInfo.isEmpty()) {
                    CompositeResultData compositeResultData = ResultBuilder.createCompositeResultData();
                    SectionResultData section = null;
                    Set<Entry<DistributedMember, PersistentID>> entries = overallCompactInfo.entrySet();
                    for (Entry<DistributedMember, PersistentID> entry : entries) {
                        String memberId = entry.getKey().getId();
                        section = compositeResultData.addSection(memberId);
                        section.addData("On Member", memberId);
                        PersistentID persistentID = entry.getValue();
                        if (persistentID != null) {
                            SectionResultData subSection = section.addSection("DiskStore" + memberId);
                            subSection.addData("UUID", persistentID.getUUID());
                            subSection.addData("Host", persistentID.getHost().getHostName());
                            subSection.addData("Directory", persistentID.getDirectory());
                        }
                    }
                    compositeResultData.setHeader("Compacted " + diskStoreName + groupInfo);
                    result = ResultBuilder.buildResult(compositeResultData);
                } else {
                    result = ResultBuilder.createInfoResult(CliStrings.COMPACT_DISK_STORE__COMPACTION_ATTEMPTED_BUT_NOTHING_TO_COMPACT);
                }
            }
        // all members' if
        }
    // disk store exists' if
    } catch (RuntimeException e) {
        LogWrapper.getInstance().info(e.getMessage(), e);
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMPACT_DISK_STORE__ERROR_WHILE_COMPACTING_REASON_0, new Object[] { e.getMessage() }));
    }
    return result;
}
Also used : CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) HashMap(java.util.HashMap) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Entry(java.util.Map.Entry) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) SectionResultData(org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) PersistentID(org.apache.geode.cache.persistence.PersistentID) HashSet(java.util.HashSet) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 52 with InternalDistributedSystem

use of org.apache.geode.distributed.internal.InternalDistributedSystem in project geode by apache.

the class MemoryThresholdsDUnitTest method createPR.

private CacheSerializableRunnable createPR(final String rName, final boolean accessor, final int fakeHeapMaxSize, final float criticalHeapThresh) {
    return new CacheSerializableRunnable("create PR accessor") {

        @Override
        public void run2() throws CacheException {
            // Assert some level of connectivity
            InternalDistributedSystem ds = getSystem();
            assertTrue(ds.getDistributionManager().getNormalDistributionManagerIds().size() >= 2);
            // below
            final long fakeHeapUsage = Math.round(fakeHeapMaxSize * (criticalHeapThresh - 0.5f));
            // critical
            // by
            // 50%
            InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
            HeapMemoryMonitor hmm = irm.getHeapMonitor();
            assertTrue(fakeHeapMaxSize > 0);
            hmm.setTestMaxMemoryBytes(fakeHeapMaxSize);
            HeapMemoryMonitor.setTestBytesUsedForThresholdSet(fakeHeapUsage);
            irm.setCriticalHeapPercentage((criticalHeapThresh * 100.0f));
            assertFalse(hmm.getState().isCritical());
            AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
            if (!accessor) {
                af.setCacheLoader(new CacheLoader<Integer, String>() {

                    final AtomicInteger numLoaderInvocations = new AtomicInteger();

                    public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
                        Integer expectedInvocations = (Integer) helper.getArgument();
                        final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
                        if (expectedInvocations.intValue() != actualInvocations) {
                            throw new CacheLoaderException("Expected " + expectedInvocations + " invocations, actual is " + actualInvocations);
                        }
                        return helper.getKey().toString();
                    }

                    public void close() {
                    }
                });
                af.setPartitionAttributes(new PartitionAttributesFactory().create());
            } else {
                af.setPartitionAttributes(new PartitionAttributesFactory().setLocalMaxMemory(0).create());
            }
            getCache().createRegion(rName, af.create());
        }
    };
}
Also used : HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem)

Example 53 with InternalDistributedSystem

use of org.apache.geode.distributed.internal.InternalDistributedSystem in project geode by apache.

the class RoleDUnitTest method testRolesInDistributedVMs.

/**
   * Tests usage of Roles in four distributed vms.
   */
@Test
public void testRolesInDistributedVMs() {
    // connect all four vms...
    final String[] vmRoles = new String[] { "VM_A", "BAR", "Foo,BAR", "Bip,BAM" };
    final Object[][] roleCounts = new Object[][] { { "VM_A", new Integer(1) }, { "BAR", new Integer(2) }, { "Foo", new Integer(1) }, { "Bip", new Integer(1) }, { "BAM", new Integer(1) } };
    for (int i = 0; i < vmRoles.length; i++) {
        final int vm = i;
        Host.getHost(0).getVM(vm).invoke(new SerializableRunnable("create system") {

            public void run() {
                disconnectFromDS();
                Properties config = new Properties();
                config.setProperty(ROLES, vmRoles[vm]);
                config.setProperty(LOG_LEVEL, "fine");
                distributionProperties = config;
                getSystem();
            }
        });
    }
    // validate roles from each vm...
    for (int i = 0; i < vmRoles.length; i++) {
        final int vm = i;
        Host.getHost(0).getVM(vm).invoke(new SerializableRunnable("verify roles") {

            public void run() {
                InternalDistributedSystem sys = getSystem();
                DM dm = sys.getDistributionManager();
                Set allRoles = dm.getAllRoles();
                assertEquals("allRoles is " + allRoles.size() + " but roleCounts should be " + roleCounts.length, roleCounts.length, allRoles.size());
                for (Iterator iter = allRoles.iterator(); iter.hasNext(); ) {
                    // match role with string in roleCounts
                    Role role = (Role) iter.next();
                    for (int j = 0; j < roleCounts.length; j++) {
                        if (role.getName().equals(roleCounts[j][0])) {
                            // parse count
                            int count = ((Integer) roleCounts[j][1]).intValue();
                            // assert count
                            assertEquals("count for role " + role + " is wrong", count, dm.getRoleCount(role));
                            assertEquals("isRolePresent for role " + role + " should be true", true, dm.isRolePresent(role));
                        }
                    }
                }
            }
        });
    }
    System.out.println("testRolesInDistributedVMs completed");
}
Also used : Set(java.util.Set) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) DM(org.apache.geode.distributed.internal.DM) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) Iterator(java.util.Iterator) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 54 with InternalDistributedSystem

use of org.apache.geode.distributed.internal.InternalDistributedSystem in project geode by apache.

the class RoleDUnitTest method testDuplicateRoleNames.

/**
   * Tests that specifying duplicate role names results in just one Role.
   */
@Test
public void testDuplicateRoleNames() {
    final String rolesProp = "A,A";
    Properties config = new Properties();
    config.setProperty(MCAST_PORT, "0");
    config.setProperty(LOCATORS, "");
    config.setProperty(ROLES, rolesProp);
    distributionProperties = config;
    InternalDistributedSystem system = getSystem();
    try {
        DM dm = system.getDistributionManager();
        InternalDistributedMember member = dm.getDistributionManagerId();
        Set roles = member.getRoles();
        assertEquals(1, roles.size());
        Role role = (Role) roles.iterator().next();
        assertEquals(true, role.isPresent());
        assertEquals(1, role.getCount());
    } finally {
        system.disconnect();
    }
}
Also used : InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) Set(java.util.Set) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DM(org.apache.geode.distributed.internal.DM) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 55 with InternalDistributedSystem

use of org.apache.geode.distributed.internal.InternalDistributedSystem in project geode by apache.

the class RoleDUnitTest method testRolesInLonerVM.

/**
   * Tests usage of Roles in a Loner vm.
   */
@Test
public void testRolesInLonerVM() {
    final String rolesProp = "A,B,C,D,E,F,G";
    final String[] rolesArray = new String[] { "A", "B", "C", "D", "E", "F", "G" };
    distributionProperties = new Properties();
    distributionProperties.setProperty(MCAST_PORT, "0");
    distributionProperties.setProperty(LOCATORS, "");
    distributionProperties.setProperty(ROLES, rolesProp);
    InternalDistributedSystem system = getSystem(distributionProperties);
    try {
        DM dm = system.getDistributionManager();
        Set allRoles = dm.getAllRoles();
        assertEquals(rolesArray.length, allRoles.size());
        InternalDistributedMember member = dm.getDistributionManagerId();
        Set roles = member.getRoles();
        assertEquals(rolesArray.length, roles.size());
        Role roleA = InternalRole.getRole("roleA");
        assertEquals(false, roleA.isPresent());
        assertEquals(0, roleA.getCount());
        for (Iterator iter = roles.iterator(); iter.hasNext(); ) {
            Role role = (Role) iter.next();
            assertEquals(true, role.isPresent());
            assertEquals(1, role.getCount());
        }
    } finally {
        system.disconnect();
    }
}
Also used : InternalRole(org.apache.geode.distributed.internal.membership.InternalRole) Set(java.util.Set) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) Iterator(java.util.Iterator) DM(org.apache.geode.distributed.internal.DM) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Test(org.junit.Test) MembershipTest(org.apache.geode.test.junit.categories.MembershipTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)209 Properties (java.util.Properties)70 Test (org.junit.Test)60 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)58 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)41 IOException (java.io.IOException)35 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)32 DM (org.apache.geode.distributed.internal.DM)30 File (java.io.File)22 HashSet (java.util.HashSet)21 Set (java.util.Set)20 AttributesFactory (org.apache.geode.cache.AttributesFactory)19 DistributionConfig (org.apache.geode.distributed.internal.DistributionConfig)19 Region (org.apache.geode.cache.Region)17 ArrayList (java.util.ArrayList)16 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)16 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)14 VM (org.apache.geode.test.dunit.VM)14 Cache (org.apache.geode.cache.Cache)13 IgnoredException (org.apache.geode.test.dunit.IgnoredException)13