Search in sources :

Example 1 with IndexInfo

use of org.apache.geode.management.internal.cli.domain.IndexInfo 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 2 with IndexInfo

use of org.apache.geode.management.internal.cli.domain.IndexInfo 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)

Example 3 with IndexInfo

use of org.apache.geode.management.internal.cli.domain.IndexInfo in project geode by apache.

the class CreateIndexFunction method execute.

@Override
public void execute(FunctionContext context) {
    final 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 indexedExpression = indexInfo.getIndexedExpression();
        String fromClause = indexInfo.getRegionPath();
        // Check to see if the region path contains an alias e.g "/region1 r1"
        // Then the first string will be the regionPath
        String[] regionPathTokens = fromClause.trim().split(" ");
        String regionPath = regionPathTokens[0];
        switch(indexInfo.getIndexType()) {
            case IndexInfo.RANGE_INDEX:
                queryService.createIndex(indexName, indexedExpression, fromClause);
                break;
            case IndexInfo.KEY_INDEX:
                queryService.createKeyIndex(indexName, indexedExpression, fromClause);
                break;
            case IndexInfo.HASH_INDEX:
                queryService.createHashIndex(indexName, indexedExpression, fromClause);
                break;
            default:
                queryService.createIndex(indexName, indexedExpression, fromClause);
        }
        regionPath = getValidRegionName(cache, regionPath);
        setResultInSender(context, indexInfo, memberId, cache, regionPath);
    } catch (IndexExistsException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__INDEX__EXISTS, indexInfo.getIndexName());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (IndexNameConflictException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__NAME__CONFLICT, indexInfo.getIndexName());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (RegionNotFoundException e) {
        String message = CliStrings.format(CliStrings.CREATE_INDEX__INVALID__REGIONPATH, indexInfo.getRegionPath());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
    } catch (IndexInvalidException e) {
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    } catch (Exception e) {
        String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
    }
}
Also used : IndexExistsException(org.apache.geode.cache.query.IndexExistsException) QueryService(org.apache.geode.cache.query.QueryService) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) Cache(org.apache.geode.cache.Cache)

Example 4 with IndexInfo

use of org.apache.geode.management.internal.cli.domain.IndexInfo in project geode by apache.

the class CreateDefinedIndexesFunction method execute.

@Override
public void execute(FunctionContext context) {
    String memberId = null;
    List<Index> indexes = null;
    Cache cache = null;
    try {
        cache = CacheFactory.getAnyInstance();
        memberId = cache.getDistributedSystem().getDistributedMember().getId();
        QueryService queryService = cache.getQueryService();
        Set<IndexInfo> indexDefinitions = (Set<IndexInfo>) context.getArguments();
        for (IndexInfo indexDefinition : indexDefinitions) {
            String indexName = indexDefinition.getIndexName();
            String indexedExpression = indexDefinition.getIndexedExpression();
            String regionPath = indexDefinition.getRegionPath();
            if (indexDefinition.getIndexType() == IndexInfo.KEY_INDEX) {
                queryService.defineKeyIndex(indexName, indexedExpression, regionPath);
            } else if (indexDefinition.getIndexType() == IndexInfo.HASH_INDEX) {
                queryService.defineHashIndex(indexName, indexedExpression, regionPath);
            } else {
                queryService.defineIndex(indexName, indexedExpression, regionPath);
            }
        }
        indexes = queryService.createDefinedIndexes();
        context.getResultSender().lastResult(new CliFunctionResult(memberId));
    } catch (MultiIndexCreationException e) {
        StringBuffer sb = new StringBuffer();
        sb.append("Index creation failed for indexes: ").append("\n");
        for (Map.Entry<String, Exception> failedIndex : e.getExceptionsMap().entrySet()) {
            sb.append(failedIndex.getKey()).append(" : ").append(failedIndex.getValue().getMessage()).append("\n");
        }
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, sb.toString()));
    } catch (Exception e) {
        String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
        context.getResultSender().lastResult(new CliFunctionResult(memberId, e, exceptionMessage));
    }
}
Also used : Set(java.util.Set) Index(org.apache.geode.cache.query.Index) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) QueryService(org.apache.geode.cache.query.QueryService) MultiIndexCreationException(org.apache.geode.cache.query.MultiIndexCreationException) Cache(org.apache.geode.cache.Cache)

Example 5 with IndexInfo

use of org.apache.geode.management.internal.cli.domain.IndexInfo 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)

Aggregations

IndexInfo (org.apache.geode.management.internal.cli.domain.IndexInfo)6 Cache (org.apache.geode.cache.Cache)5 XmlEntity (org.apache.geode.management.internal.configuration.domain.XmlEntity)4 Set (java.util.Set)3 QueryService (org.apache.geode.cache.query.QueryService)3 CliMetaData (org.apache.geode.management.cli.CliMetaData)3 ConverterHint (org.apache.geode.management.cli.ConverterHint)3 Result (org.apache.geode.management.cli.Result)3 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)3 InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)3 CliCommand (org.springframework.shell.core.annotation.CliCommand)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Index (org.apache.geode.cache.query.Index)2 DistributedMember (org.apache.geode.distributed.DistributedMember)2 InternalCache (org.apache.geode.internal.cache.InternalCache)2