Search in sources :

Example 41 with XmlEntity

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

the class DestroyIndexFunction method execute.

@Override
public void execute(FunctionContext context) {
    IndexInfo indexInfo = (IndexInfo) context.getArguments();
    String memberId = null;
    try {
        Cache cache = CacheFactory.getAnyInstance();
        memberId = cache.getDistributedSystem().getDistributedMember().getId();
        QueryService queryService = cache.getQueryService();
        String indexName = indexInfo.getIndexName();
        String regionPath = indexInfo.getRegionPath();
        XmlEntity xmlEntity = new XmlEntity(CacheXml.REGION, "name", regionPath, CacheXml.INDEX, "name", indexName);
        if (regionPath != null && !regionPath.isEmpty()) {
            Region<?, ?> region = cache.getRegion(regionPath);
            if (region != null) {
                if (indexName == null || indexName.isEmpty()) {
                    queryService.removeIndexes(region);
                    context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
                } else {
                    Index index = queryService.getIndex(region, indexName);
                    if (index != null) {
                        queryService.removeIndex(index);
                        context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
                    } else {
                        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, CliStrings.format(CliStrings.DESTROY_INDEX__INDEX__NOT__FOUND, indexName)));
                    }
                }
            } else {
                context.getResultSender().lastResult(new CliFunctionResult(memberId, false, CliStrings.format(CliStrings.DESTROY_INDEX__REGION__NOT__FOUND, regionPath)));
            }
        } else {
            if (indexName == null || indexName.isEmpty()) {
                queryService.removeIndexes();
                context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
            } else {
                if (removeIndexByName(indexName, queryService)) {
                    context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
                } else {
                    context.getResultSender().lastResult(new CliFunctionResult(memberId, false, CliStrings.format(CliStrings.DESTROY_INDEX__INDEX__NOT__FOUND, indexName)));
                }
            }
        }
    } catch (CacheClosedException e) {
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    } catch (Exception e) {
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    }
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) QueryService(org.apache.geode.cache.query.QueryService) Index(org.apache.geode.cache.query.Index) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) CacheClosedException(org.apache.geode.cache.CacheClosedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) Cache(org.apache.geode.cache.Cache)

Example 42 with XmlEntity

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

the class IndexCommands method createIndex.

@CliCommand(value = CliStrings.CREATE_INDEX, help = CliStrings.CREATE_INDEX__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public // TODO : Add optionContext for indexName
Result createIndex(@CliOption(key = CliStrings.CREATE_INDEX__NAME, mandatory = true, help = CliStrings.CREATE_INDEX__NAME__HELP) final String indexName, @CliOption(key = CliStrings.CREATE_INDEX__EXPRESSION, mandatory = true, help = CliStrings.CREATE_INDEX__EXPRESSION__HELP) final String indexedExpression, @CliOption(key = CliStrings.CREATE_INDEX__REGION, mandatory = true, optionContext = ConverterHint.REGION_PATH, help = CliStrings.CREATE_INDEX__REGION__HELP) String regionPath, @CliOption(key = CliStrings.CREATE_INDEX__MEMBER, mandatory = false, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.CREATE_INDEX__MEMBER__HELP) final String[] memberNameOrID, @CliOption(key = CliStrings.CREATE_INDEX__TYPE, mandatory = false, unspecifiedDefaultValue = "range", optionContext = ConverterHint.INDEX_TYPE, help = CliStrings.CREATE_INDEX__TYPE__HELP) final String indexType, @CliOption(key = CliStrings.CREATE_INDEX__GROUP, mandatory = false, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.CREATE_INDEX__GROUP__HELP) final String[] group) {
    Result result = null;
    AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
    this.securityService.authorizeRegionManage(regionPath);
    try {
        final Cache cache = CacheFactory.getAnyInstance();
        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.CREATE_INDEX__INVALID__INDEX__TYPE__MESSAGE);
        }
        if (indexName == null || indexName.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.CREATE_INDEX__INVALID__INDEX__NAME);
        }
        if (indexedExpression == null || indexedExpression.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.CREATE_INDEX__INVALID__EXPRESSION);
        }
        if (StringUtils.isBlank(regionPath) || regionPath.equals(Region.SEPARATOR)) {
            return ResultBuilder.createUserErrorResult(CliStrings.CREATE_INDEX__INVALID__REGIONPATH);
        }
        if (!regionPath.startsWith(Region.SEPARATOR)) {
            regionPath = Region.SEPARATOR + regionPath;
        }
        IndexInfo indexInfo = new IndexInfo(indexName, indexedExpression, regionPath, idxType);
        final Set<DistributedMember> targetMembers = CliUtil.findMembers(group, memberNameOrID);
        if (targetMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        final ResultCollector<?, ?> rc = CliUtil.executeFunction(createIndexFunction, indexInfo, targetMembers);
        final List<Object> funcResults = (List<Object>) rc.getResult();
        final Set<String> successfulMembers = new TreeSet<String>();
        final Map<String, Set<String>> indexOpFailMap = new HashMap<String, Set<String>>();
        for (final Object funcResult : funcResults) {
            if (funcResult instanceof CliFunctionResult) {
                final CliFunctionResult cliFunctionResult = (CliFunctionResult) funcResult;
                if (cliFunctionResult.isSuccessful()) {
                    successfulMembers.add(cliFunctionResult.getMemberIdOrName());
                    if (xmlEntity.get() == null) {
                        xmlEntity.set(cliFunctionResult.getXmlEntity());
                    }
                } else {
                    final 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()) {
            final InfoResultData infoResult = ResultBuilder.createInfoResultData();
            infoResult.addLine(CliStrings.CREATE_INDEX__SUCCESS__MSG);
            infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__NAME__MSG, indexName));
            infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__EXPRESSION__MSG, indexedExpression));
            infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__REGIONPATH__MSG, regionPath));
            infoResult.addLine(CliStrings.CREATE_INDEX__MEMBER__MSG);
            int num = 0;
            for (final String memberId : successfulMembers) {
                ++num;
                infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__NUMBER__AND__MEMBER, num, memberId));
            }
            result = ResultBuilder.buildResult(infoResult);
        } else {
            // Group members by the exception thrown.
            final ErrorResultData erd = ResultBuilder.createErrorResultData();
            erd.addLine(CliStrings.format(CliStrings.CREATE_INDEX__FAILURE__MSG, indexName));
            final Set<String> exceptionMessages = indexOpFailMap.keySet();
            for (final String exceptionMessage : exceptionMessages) {
                erd.addLine(exceptionMessage);
                erd.addLine(CliStrings.CREATE_INDEX__EXCEPTION__OCCURRED__ON);
                final Set<String> memberIds = indexOpFailMap.get(exceptionMessage);
                int num = 0;
                for (final String memberId : memberIds) {
                    ++num;
                    erd.addLine(CliStrings.format(CliStrings.CREATE_INDEX__NUMBER__AND__MEMBER, num, memberId));
                }
            }
            result = ResultBuilder.buildResult(erd);
        }
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    }
    if (xmlEntity.get() != null) {
        persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(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) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) 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) 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 43 with XmlEntity

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

the class IndexCommands method createDefinedIndexes.

@CliCommand(value = CliStrings.CREATE_DEFINED_INDEXES, help = CliStrings.CREATE_DEFINED__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public // TODO : Add optionContext for indexName
Result createDefinedIndexes(@CliOption(key = CliStrings.CREATE_DEFINED_INDEXES__MEMBER, mandatory = false, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.CREATE_DEFINED_INDEXES__MEMBER__HELP) final String[] memberNameOrID, @CliOption(key = CliStrings.CREATE_DEFINED_INDEXES__GROUP, mandatory = false, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.CREATE_DEFINED_INDEXES__GROUP__HELP) final String[] group) {
    Result result = null;
    AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
    if (indexDefinitions.isEmpty()) {
        final InfoResultData infoResult = ResultBuilder.createInfoResultData();
        infoResult.addLine(CliStrings.DEFINE_INDEX__FAILURE__MSG);
        return ResultBuilder.buildResult(infoResult);
    }
    try {
        final Set<DistributedMember> targetMembers = CliUtil.findMembers(group, memberNameOrID);
        if (targetMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        final Cache cache = CacheFactory.getAnyInstance();
        final ResultCollector<?, ?> rc = CliUtil.executeFunction(createDefinedIndexesFunction, indexDefinitions, targetMembers);
        final List<Object> funcResults = (List<Object>) rc.getResult();
        final Set<String> successfulMembers = new TreeSet<String>();
        final Map<String, Set<String>> indexOpFailMap = new HashMap<String, Set<String>>();
        for (final Object funcResult : funcResults) {
            if (funcResult instanceof CliFunctionResult) {
                final CliFunctionResult cliFunctionResult = (CliFunctionResult) funcResult;
                if (cliFunctionResult.isSuccessful()) {
                    successfulMembers.add(cliFunctionResult.getMemberIdOrName());
                    if (xmlEntity.get() == null) {
                        xmlEntity.set(cliFunctionResult.getXmlEntity());
                    }
                } else {
                    final 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()) {
            final InfoResultData infoResult = ResultBuilder.createInfoResultData();
            infoResult.addLine(CliStrings.CREATE_DEFINED_INDEXES__SUCCESS__MSG);
            int num = 0;
            for (final String memberId : successfulMembers) {
                ++num;
                infoResult.addLine(CliStrings.format(CliStrings.CREATE_DEFINED_INDEXES__NUMBER__AND__MEMBER, num, memberId));
            }
            result = ResultBuilder.buildResult(infoResult);
        } else {
            // Group members by the exception thrown.
            final ErrorResultData erd = ResultBuilder.createErrorResultData();
            final Set<String> exceptionMessages = indexOpFailMap.keySet();
            for (final String exceptionMessage : exceptionMessages) {
                erd.addLine(exceptionMessage);
                erd.addLine(CliStrings.CREATE_INDEX__EXCEPTION__OCCURRED__ON);
                final Set<String> memberIds = indexOpFailMap.get(exceptionMessage);
                int num = 0;
                for (final String memberId : memberIds) {
                    ++num;
                    erd.addLine(CliStrings.format(CliStrings.CREATE_DEFINED_INDEXES__NUMBER__AND__MEMBER, num, memberId));
                }
            }
            result = ResultBuilder.buildResult(erd);
        }
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    }
    if (xmlEntity.get() != null) {
        persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(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) ConverterHint(org.apache.geode.management.cli.ConverterHint) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) 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) 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) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 44 with XmlEntity

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

the class MockExtensionCommands method executeFunctionOnAllMembersTabulateResultPersist.

/**
   * Call <code>function</code> with <code>args</code> on all members, tabulate results and persist
   * shared config if changed.
   * 
   * @param function {@link Function} to execute.
   * @param addXmlElement If <code>true</code> then add result {@link XmlEntity} to the config,
   *        otherwise delete it.
   * @param args Arguments to pass to function.
   * @return {@link TabularResultData}
   * @since GemFire 8.1
   */
protected Result executeFunctionOnAllMembersTabulateResultPersist(final Function function, final boolean addXmlElement, final Object... args) {
    final InternalCache cache = getCache();
    final Set<DistributedMember> members = CliUtil.getAllNormalMembers(cache);
    @SuppressWarnings("unchecked") final ResultCollector<CliFunctionResult, List<CliFunctionResult>> resultCollector = (ResultCollector<CliFunctionResult, List<CliFunctionResult>>) CliUtil.executeFunction(function, args, members);
    final List<CliFunctionResult> functionResults = (List<CliFunctionResult>) resultCollector.getResult();
    AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
    final TabularResultData tabularResultData = ResultBuilder.createTabularResultData();
    final String errorPrefix = "ERROR: ";
    for (CliFunctionResult functionResult : functionResults) {
        boolean success = functionResult.isSuccessful();
        tabularResultData.accumulate("Member", functionResult.getMemberIdOrName());
        if (success) {
            tabularResultData.accumulate("Status", functionResult.getMessage());
            xmlEntity.set(functionResult.getXmlEntity());
        } else {
            tabularResultData.accumulate("Status", errorPrefix + functionResult.getMessage());
            tabularResultData.setStatus(Status.ERROR);
        }
    }
    final Result result = ResultBuilder.buildResult(tabularResultData);
    System.out.println("MockExtensionCommands: persisting xmlEntity=" + xmlEntity);
    if (null != xmlEntity.get()) {
        if (addXmlElement) {
            persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(xmlEntity.get(), null));
        } else {
            persistClusterConfiguration(result, () -> getSharedConfiguration().deleteXmlEntity(xmlEntity.get(), null));
        }
    }
    return result;
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) InternalCache(org.apache.geode.internal.cache.InternalCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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) DistributedMember(org.apache.geode.distributed.DistributedMember) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector)

Example 45 with XmlEntity

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

the class AlterMockCacheExtensionFunction method execute.

@Override
public void execute(FunctionContext context) {
    final Cache cache = CacheFactory.getAnyInstance();
    if (!(cache instanceof Extensible)) {
        throw new FunctionException("Not extensible cache.");
    }
    final String value = (String) ((Object[]) context.getArguments())[0];
    @SuppressWarnings("unchecked") final Extensible<Cache> extensible = (Extensible<Cache>) cache;
    for (Extension<Cache> extension : extensible.getExtensionPoint().getExtensions()) {
        if (extension instanceof MockCacheExtension) {
            ((MockCacheExtension) extension).setValue(value);
        }
    }
    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 altered 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)

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