Search in sources :

Example 11 with XmlEntity

use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.

the class DestroyMockCacheExtensionFunction method execute.

@Override
public void execute(FunctionContext context) {
    final Cache cache = CacheFactory.getAnyInstance();
    if (!(cache instanceof Extensible)) {
        throw new FunctionException("Not extensible cache.");
    }
    @SuppressWarnings("unchecked") final Extensible<Cache> extensible = (Extensible<Cache>) cache;
    for (Extension<Cache> extension : extensible.getExtensionPoint().getExtensions()) {
        if (extension instanceof MockCacheExtension) {
            extensible.getExtensionPoint().removeExtension(extension);
            break;
        }
    }
    final XmlEntity xmlEntity = XmlEntity.builder().withType(ELEMENT_CACHE).withNamespace(PREFIX, NAMESPACE).build();
    final ResultSender<Object> resultSender = context.getResultSender();
    final String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
    resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity, CliStrings.format("Mock cache extension destroyed on \"{0}\"", new Object[] { memberNameOrId })));
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Extensible(org.apache.geode.internal.cache.extension.Extensible) FunctionException(org.apache.geode.cache.execute.FunctionException) Cache(org.apache.geode.cache.Cache)

Example 12 with XmlEntity

use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.

the class DestroyMockRegionExtensionFunction method execute.

@Override
public void execute(FunctionContext context) {
    final Cache cache = CacheFactory.getAnyInstance();
    final Region<?, ?> region = cache.getRegion((String) ((Object[]) context.getArguments())[0]);
    if (!(region instanceof Extensible)) {
        throw new FunctionException("Not extensible region.");
    }
    @SuppressWarnings("unchecked") final Extensible<Region<?, ?>> extensible = (Extensible<Region<?, ?>>) region;
    for (Extension<Region<?, ?>> extension : extensible.getExtensionPoint().getExtensions()) {
        if (extension instanceof MockRegionExtension) {
            extensible.getExtensionPoint().removeExtension(extension);
            break;
        }
    }
    XmlEntity xmlEntity = new XmlEntity(CacheXml.REGION, "name", region.getName());
    final ResultSender<Object> resultSender = context.getResultSender();
    final String memberNameOrId = CliUtil.getMemberNameOrId(cache.getDistributedSystem().getDistributedMember());
    resultSender.lastResult(new CliFunctionResult(memberNameOrId, xmlEntity, CliStrings.format("Mock region extension \"{0}\" destroyed on \"{1}\"", new Object[] { region.getFullPath(), memberNameOrId })));
}
Also used : Extensible(org.apache.geode.internal.cache.extension.Extensible) FunctionException(org.apache.geode.cache.execute.FunctionException) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Example 13 with XmlEntity

use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.

the class IndexCommands method destroyIndex.

@CliCommand(value = CliStrings.DESTROY_INDEX, help = CliStrings.DESTROY_INDEX__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public Result destroyIndex(@CliOption(key = CliStrings.DESTROY_INDEX__NAME, mandatory = false, unspecifiedDefaultValue = "", help = CliStrings.DESTROY_INDEX__NAME__HELP) final String indexName, @CliOption(key = CliStrings.DESTROY_INDEX__REGION, mandatory = false, optionContext = ConverterHint.REGION_PATH, help = CliStrings.DESTROY_INDEX__REGION__HELP) final String regionPath, @CliOption(key = CliStrings.DESTROY_INDEX__MEMBER, mandatory = false, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.DESTROY_INDEX__MEMBER__HELP) final String[] memberNameOrID, @CliOption(key = CliStrings.DESTROY_INDEX__GROUP, mandatory = false, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.DESTROY_INDEX__GROUP__HELP) final String[] group) {
    Result result = null;
    if (StringUtils.isBlank(indexName) && StringUtils.isBlank(regionPath) && ArrayUtils.isEmpty(group) && ArrayUtils.isEmpty(memberNameOrID)) {
        return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.PROVIDE_ATLEAST_ONE_OPTION, CliStrings.DESTROY_INDEX));
    }
    String regionName = null;
    final Cache cache = CacheFactory.getAnyInstance();
    // requires data manage permission on all regions
    if (StringUtils.isNotBlank(regionPath)) {
        regionName = regionPath.startsWith("/") ? regionPath.substring(1) : regionPath;
        this.securityService.authorizeRegionManage(regionName);
    } else {
        this.securityService.authorizeDataManage();
    }
    IndexInfo indexInfo = new IndexInfo(indexName, regionName);
    Set<DistributedMember> targetMembers = CliUtil.findMembers(group, memberNameOrID);
    if (targetMembers.isEmpty()) {
        return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
    }
    ResultCollector rc = CliUtil.executeFunction(destroyIndexFunction, indexInfo, targetMembers);
    List<Object> funcResults = (List<Object>) rc.getResult();
    Set<String> successfulMembers = new TreeSet<String>();
    Map<String, Set<String>> indexOpFailMap = new HashMap<String, Set<String>>();
    AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
    for (Object funcResult : funcResults) {
        if (!(funcResult instanceof CliFunctionResult)) {
            continue;
        }
        CliFunctionResult cliFunctionResult = (CliFunctionResult) funcResult;
        if (cliFunctionResult.isSuccessful()) {
            successfulMembers.add(cliFunctionResult.getMemberIdOrName());
            if (xmlEntity.get() == null) {
                xmlEntity.set(cliFunctionResult.getXmlEntity());
            }
        } else {
            String exceptionMessage = cliFunctionResult.getMessage();
            Set<String> failedMembers = indexOpFailMap.get(exceptionMessage);
            if (failedMembers == null) {
                failedMembers = new TreeSet<String>();
            }
            failedMembers.add(cliFunctionResult.getMemberIdOrName());
            indexOpFailMap.put(exceptionMessage, failedMembers);
        }
    }
    if (!successfulMembers.isEmpty()) {
        InfoResultData infoResult = ResultBuilder.createInfoResultData();
        if (StringUtils.isNotBlank(indexName)) {
            if (StringUtils.isNotBlank(regionPath)) {
                infoResult.addLine(CliStrings.format(CliStrings.DESTROY_INDEX__ON__REGION__SUCCESS__MSG, indexName, regionPath));
            } else {
                infoResult.addLine(CliStrings.format(CliStrings.DESTROY_INDEX__SUCCESS__MSG, indexName));
            }
        } else {
            if (StringUtils.isNotBlank(regionPath)) {
                infoResult.addLine(CliStrings.format(CliStrings.DESTROY_INDEX__ON__REGION__ONLY__SUCCESS__MSG, regionPath));
            } else {
                infoResult.addLine(CliStrings.DESTROY_INDEX__ON__MEMBERS__ONLY__SUCCESS__MSG);
            }
        }
        int num = 0;
        for (String memberId : successfulMembers) {
            infoResult.addLine(CliStrings.format(CliStrings.format(CliStrings.DESTROY_INDEX__NUMBER__AND__MEMBER, ++num, memberId)));
            ;
        }
        result = ResultBuilder.buildResult(infoResult);
    } else {
        ErrorResultData erd = ResultBuilder.createErrorResultData();
        if (StringUtils.isNotBlank(indexName)) {
            erd.addLine(CliStrings.format(CliStrings.DESTROY_INDEX__FAILURE__MSG, indexName));
        } else {
            erd.addLine("Indexes could not be destroyed for following reasons");
        }
        Set<String> exceptionMessages = indexOpFailMap.keySet();
        for (String exceptionMessage : exceptionMessages) {
            erd.addLine(CliStrings.format(CliStrings.DESTROY_INDEX__REASON_MESSAGE, exceptionMessage));
            erd.addLine(CliStrings.DESTROY_INDEX__EXCEPTION__OCCURRED__ON);
            Set<String> memberIds = indexOpFailMap.get(exceptionMessage);
            int num = 0;
            for (String memberId : memberIds) {
                erd.addLine(CliStrings.format(CliStrings.format(CliStrings.DESTROY_INDEX__NUMBER__AND__MEMBER, ++num, memberId)));
            }
            erd.addLine("");
        }
        result = ResultBuilder.buildResult(erd);
    }
    if (xmlEntity.get() != null) {
        persistClusterConfiguration(result, () -> getSharedConfiguration().deleteXmlEntity(xmlEntity.get(), group));
    }
    return result;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) ConverterHint(org.apache.geode.management.cli.ConverterHint) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) TreeSet(java.util.TreeSet) DistributedMember(org.apache.geode.distributed.DistributedMember) ArrayList(java.util.ArrayList) List(java.util.List) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) ResultCollector(org.apache.geode.cache.execute.ResultCollector) InternalCache(org.apache.geode.internal.cache.InternalCache) Cache(org.apache.geode.cache.Cache) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 14 with XmlEntity

use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.

the class DiskStoreCommands method createDiskStore.

@CliCommand(value = CliStrings.CREATE_DISK_STORE, help = CliStrings.CREATE_DISK_STORE__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_DISKSTORE })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result createDiskStore(@CliOption(key = CliStrings.CREATE_DISK_STORE__NAME, mandatory = true, optionContext = ConverterHint.DISKSTORE, help = CliStrings.CREATE_DISK_STORE__NAME__HELP) String name, @CliOption(key = CliStrings.CREATE_DISK_STORE__ALLOW_FORCE_COMPACTION, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.CREATE_DISK_STORE__ALLOW_FORCE_COMPACTION__HELP) boolean allowForceCompaction, @CliOption(key = CliStrings.CREATE_DISK_STORE__AUTO_COMPACT, specifiedDefaultValue = "true", unspecifiedDefaultValue = "true", help = CliStrings.CREATE_DISK_STORE__AUTO_COMPACT__HELP) boolean autoCompact, @CliOption(key = CliStrings.CREATE_DISK_STORE__COMPACTION_THRESHOLD, unspecifiedDefaultValue = "50", help = CliStrings.CREATE_DISK_STORE__COMPACTION_THRESHOLD__HELP) int compactionThreshold, @CliOption(key = CliStrings.CREATE_DISK_STORE__MAX_OPLOG_SIZE, unspecifiedDefaultValue = "1024", help = CliStrings.CREATE_DISK_STORE__MAX_OPLOG_SIZE__HELP) int maxOplogSize, @CliOption(key = CliStrings.CREATE_DISK_STORE__QUEUE_SIZE, unspecifiedDefaultValue = "0", help = CliStrings.CREATE_DISK_STORE__QUEUE_SIZE__HELP) int queueSize, @CliOption(key = CliStrings.CREATE_DISK_STORE__TIME_INTERVAL, unspecifiedDefaultValue = "1000", help = CliStrings.CREATE_DISK_STORE__TIME_INTERVAL__HELP) long timeInterval, @CliOption(key = CliStrings.CREATE_DISK_STORE__WRITE_BUFFER_SIZE, unspecifiedDefaultValue = "32768", help = CliStrings.CREATE_DISK_STORE__WRITE_BUFFER_SIZE__HELP) int writeBufferSize, @CliOption(key = CliStrings.CREATE_DISK_STORE__DIRECTORY_AND_SIZE, mandatory = true, help = CliStrings.CREATE_DISK_STORE__DIRECTORY_AND_SIZE__HELP) String[] directoriesAndSizes, @CliOption(key = CliStrings.CREATE_DISK_STORE__GROUP, help = CliStrings.CREATE_DISK_STORE__GROUP__HELP, optionContext = ConverterHint.MEMBERGROUP) String[] groups, @CliOption(key = CliStrings.CREATE_DISK_STORE__DISK_USAGE_WARNING_PCT, unspecifiedDefaultValue = "90", help = CliStrings.CREATE_DISK_STORE__DISK_USAGE_WARNING_PCT__HELP) float diskUsageWarningPercentage, @CliOption(key = CliStrings.CREATE_DISK_STORE__DISK_USAGE_CRITICAL_PCT, unspecifiedDefaultValue = "99", help = CliStrings.CREATE_DISK_STORE__DISK_USAGE_CRITICAL_PCT__HELP) float diskUsageCriticalPercentage) {
    try {
        DiskStoreAttributes diskStoreAttributes = new DiskStoreAttributes();
        diskStoreAttributes.allowForceCompaction = allowForceCompaction;
        diskStoreAttributes.autoCompact = autoCompact;
        diskStoreAttributes.compactionThreshold = compactionThreshold;
        diskStoreAttributes.maxOplogSizeInBytes = maxOplogSize * (1024 * 1024);
        diskStoreAttributes.queueSize = queueSize;
        diskStoreAttributes.timeInterval = timeInterval;
        diskStoreAttributes.writeBufferSize = writeBufferSize;
        File[] directories = new File[directoriesAndSizes.length];
        int[] sizes = new int[directoriesAndSizes.length];
        for (int i = 0; i < directoriesAndSizes.length; i++) {
            final int hashPosition = directoriesAndSizes[i].indexOf('#');
            if (hashPosition == -1) {
                directories[i] = new File(directoriesAndSizes[i]);
                sizes[i] = Integer.MAX_VALUE;
            } else {
                directories[i] = new File(directoriesAndSizes[i].substring(0, hashPosition));
                sizes[i] = Integer.parseInt(directoriesAndSizes[i].substring(hashPosition + 1));
            }
        }
        diskStoreAttributes.diskDirs = directories;
        diskStoreAttributes.diskDirSizes = sizes;
        diskStoreAttributes.setDiskUsageWarningPercentage(diskUsageWarningPercentage);
        diskStoreAttributes.setDiskUsageCriticalPercentage(diskUsageCriticalPercentage);
        TabularResultData tabularData = ResultBuilder.createTabularResultData();
        boolean accumulatedData = false;
        Set<DistributedMember> targetMembers = CliUtil.findMembers(groups, null);
        if (targetMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        ResultCollector<?, ?> rc = CliUtil.executeFunction(new CreateDiskStoreFunction(), new Object[] { name, diskStoreAttributes }, targetMembers);
        List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) rc.getResult());
        AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
        for (CliFunctionResult result : results) {
            if (result.getThrowable() != null) {
                tabularData.accumulate("Member", result.getMemberIdOrName());
                tabularData.accumulate("Result", "ERROR: " + result.getThrowable().getClass().getName() + ": " + result.getThrowable().getMessage());
                accumulatedData = true;
                tabularData.setStatus(Status.ERROR);
            } else if (result.isSuccessful()) {
                tabularData.accumulate("Member", result.getMemberIdOrName());
                tabularData.accumulate("Result", result.getMessage());
                accumulatedData = true;
                if (xmlEntity.get() == null) {
                    xmlEntity.set(result.getXmlEntity());
                }
            }
        }
        if (!accumulatedData) {
            return ResultBuilder.createInfoResult("Unable to create disk store(s).");
        }
        Result result = ResultBuilder.buildResult(tabularData);
        if (xmlEntity.get() != null) {
            persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(xmlEntity.get(), groups));
        }
        return ResultBuilder.buildResult(tabularData);
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable th) {
        SystemFailure.checkFailure();
        return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.CREATE_DISK_STORE__ERROR_WHILE_CREATING_REASON_0, new Object[] { th.getMessage() }));
    }
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConverterHint(org.apache.geode.management.cli.ConverterHint) CreateDiskStoreFunction(org.apache.geode.management.internal.cli.functions.CreateDiskStoreFunction) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) DiskStoreAttributes(org.apache.geode.internal.cache.DiskStoreAttributes) File(java.io.File) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 15 with XmlEntity

use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.

the class IndexCommands method defineIndex.

@CliCommand(value = CliStrings.DEFINE_INDEX, help = CliStrings.DEFINE_INDEX__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public // TODO : Add optionContext for indexName
Result defineIndex(@CliOption(key = CliStrings.DEFINE_INDEX_NAME, mandatory = true, help = CliStrings.DEFINE_INDEX__HELP) final String indexName, @CliOption(key = CliStrings.DEFINE_INDEX__EXPRESSION, mandatory = true, help = CliStrings.DEFINE_INDEX__EXPRESSION__HELP) final String indexedExpression, @CliOption(key = CliStrings.DEFINE_INDEX__REGION, mandatory = true, optionContext = ConverterHint.REGION_PATH, help = CliStrings.DEFINE_INDEX__REGION__HELP) String regionPath, @CliOption(key = CliStrings.DEFINE_INDEX__TYPE, mandatory = false, unspecifiedDefaultValue = "range", optionContext = ConverterHint.INDEX_TYPE, help = CliStrings.DEFINE_INDEX__TYPE__HELP) final String indexType) {
    Result result = null;
    XmlEntity xmlEntity = null;
    this.securityService.authorizeRegionManage(regionPath);
    int idxType = IndexInfo.RANGE_INDEX;
    // Index type check
    if ("range".equalsIgnoreCase(indexType)) {
        idxType = IndexInfo.RANGE_INDEX;
    } else if ("hash".equalsIgnoreCase(indexType)) {
        idxType = IndexInfo.HASH_INDEX;
    } else if ("key".equalsIgnoreCase(indexType)) {
        idxType = IndexInfo.KEY_INDEX;
    } else {
        return ResultBuilder.createUserErrorResult(CliStrings.DEFINE_INDEX__INVALID__INDEX__TYPE__MESSAGE);
    }
    if (indexName == null || indexName.isEmpty()) {
        return ResultBuilder.createUserErrorResult(CliStrings.DEFINE_INDEX__INVALID__INDEX__NAME);
    }
    if (indexedExpression == null || indexedExpression.isEmpty()) {
        return ResultBuilder.createUserErrorResult(CliStrings.DEFINE_INDEX__INVALID__EXPRESSION);
    }
    if (StringUtils.isBlank(regionPath) || regionPath.equals(Region.SEPARATOR)) {
        return ResultBuilder.createUserErrorResult(CliStrings.DEFINE_INDEX__INVALID__REGIONPATH);
    }
    if (!regionPath.startsWith(Region.SEPARATOR)) {
        regionPath = Region.SEPARATOR + regionPath;
    }
    IndexInfo indexInfo = new IndexInfo(indexName, indexedExpression, regionPath, idxType);
    indexDefinitions.add(indexInfo);
    final InfoResultData infoResult = ResultBuilder.createInfoResultData();
    infoResult.addLine(CliStrings.DEFINE_INDEX__SUCCESS__MSG);
    infoResult.addLine(CliStrings.format(CliStrings.DEFINE_INDEX__NAME__MSG, indexName));
    infoResult.addLine(CliStrings.format(CliStrings.DEFINE_INDEX__EXPRESSION__MSG, indexedExpression));
    infoResult.addLine(CliStrings.format(CliStrings.DEFINE_INDEX__REGIONPATH__MSG, regionPath));
    result = ResultBuilder.buildResult(infoResult);
    return result;
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) ConverterHint(org.apache.geode.management.cli.ConverterHint) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Aggregations

XmlEntity (org.apache.geode.management.internal.configuration.domain.XmlEntity)46 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)25 DistributedMember (org.apache.geode.distributed.DistributedMember)18 Cache (org.apache.geode.cache.Cache)17 Result (org.apache.geode.management.cli.Result)17 CliCommand (org.springframework.shell.core.annotation.CliCommand)16 CliMetaData (org.apache.geode.management.cli.CliMetaData)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 InternalCache (org.apache.geode.internal.cache.InternalCache)12 ArrayList (java.util.ArrayList)11 List (java.util.List)11 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)11 Test (org.junit.Test)10 InputStreamReader (java.io.InputStreamReader)9 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 Document (org.w3c.dom.Document)9 Element (org.w3c.dom.Element)9 NodeList (org.w3c.dom.NodeList)9 ConverterHint (org.apache.geode.management.cli.ConverterHint)7