Search in sources :

Example 1 with XmlEntity

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

the class LuceneIndexCommands method destroyIndex.

@CliCommand(value = LuceneCliStrings.LUCENE_DESTROY_INDEX, help = LuceneCliStrings.LUCENE_DESTROY_INDEX__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public Result destroyIndex(@CliOption(key = LuceneCliStrings.LUCENE__INDEX_NAME, help = LuceneCliStrings.LUCENE_DESTROY_INDEX__NAME__HELP) final String indexName, @CliOption(key = LuceneCliStrings.LUCENE__REGION_PATH, mandatory = true, optionContext = ConverterHint.REGION_PATH, help = LuceneCliStrings.LUCENE_DESTROY_INDEX__REGION_HELP) final String regionPath) {
    if (StringUtils.isBlank(regionPath) || regionPath.equals(Region.SEPARATOR)) {
        return ResultBuilder.createInfoResult(CliStrings.format(LuceneCliStrings.LUCENE_DESTROY_INDEX__MSG__REGION_CANNOT_BE_EMPTY));
    }
    if (indexName != null && StringUtils.isEmpty(indexName)) {
        return ResultBuilder.createInfoResult(CliStrings.format(LuceneCliStrings.LUCENE_DESTROY_INDEX__MSG__INDEX_CANNOT_BE_EMPTY));
    }
    this.securityService.authorizeRegionManage(regionPath);
    Result result;
    try {
        List<CliFunctionResult> accumulatedResults = new ArrayList<>();
        final XmlEntity xmlEntity = executeDestroyIndexFunction(accumulatedResults, indexName, regionPath);
        result = getDestroyIndexResult(accumulatedResults, indexName, regionPath);
        if (xmlEntity != null) {
            persistClusterConfiguration(result, () -> {
                // Delete the xml entity to remove the index(es) in all groups
                getSharedConfiguration().deleteXmlEntity(xmlEntity, null);
            });
        }
    } catch (FunctionInvocationTargetException ignore) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, LuceneCliStrings.LUCENE_DESTROY_INDEX));
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (IllegalArgumentException e) {
        result = ResultBuilder.createInfoResult(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
        SystemFailure.checkFailure();
        getCache().getLogger().warning(LuceneCliStrings.LUCENE_DESTROY_INDEX__EXCEPTION_MESSAGE, t);
        result = ResultBuilder.createGemFireErrorResult(t.getMessage());
    }
    return result;
}
Also used : CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) ArrayList(java.util.ArrayList) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 2 with XmlEntity

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

the class LuceneIndexCommands method createIndex.

@CliCommand(value = LuceneCliStrings.LUCENE_CREATE_INDEX, help = LuceneCliStrings.LUCENE_CREATE_INDEX__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public // TODO : Add optionContext for indexName
Result createIndex(@CliOption(key = LuceneCliStrings.LUCENE__INDEX_NAME, mandatory = true, help = LuceneCliStrings.LUCENE_CREATE_INDEX__NAME__HELP) final String indexName, @CliOption(key = LuceneCliStrings.LUCENE__REGION_PATH, mandatory = true, optionContext = ConverterHint.REGION_PATH, help = LuceneCliStrings.LUCENE_CREATE_INDEX__REGION_HELP) final String regionPath, @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, mandatory = true, help = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD_HELP) final String[] fields, @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, help = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER_HELP) final String[] analyzers) {
    Result result;
    XmlEntity xmlEntity = null;
    this.securityService.authorizeRegionManage(regionPath);
    try {
        final InternalCache cache = getCache();
        // trim fields for any leading trailing spaces.
        String[] trimmedFields = Arrays.stream(fields).map(field -> field.trim()).toArray(size -> new String[size]);
        LuceneIndexInfo indexInfo = new LuceneIndexInfo(indexName, regionPath, trimmedFields, analyzers);
        final ResultCollector<?, ?> rc = this.executeFunctionOnAllMembers(createIndexFunction, indexInfo);
        final List<CliFunctionResult> funcResults = (List<CliFunctionResult>) rc.getResult();
        final TabularResultData tabularResult = ResultBuilder.createTabularResultData();
        for (final CliFunctionResult cliFunctionResult : funcResults) {
            tabularResult.accumulate("Member", cliFunctionResult.getMemberIdOrName());
            if (cliFunctionResult.isSuccessful()) {
                tabularResult.accumulate("Status", "Successfully created lucene index");
            // if (xmlEntity == null) {
            // xmlEntity = cliFunctionResult.getXmlEntity();
            // }
            } else {
                tabularResult.accumulate("Status", "Failed: " + cliFunctionResult.getMessage());
            }
        }
        result = ResultBuilder.buildResult(tabularResult);
    } catch (IllegalArgumentException iae) {
        LogWrapper.getInstance().info(iae.getMessage());
        result = ResultBuilder.createUserErrorResult(iae.getMessage());
    } catch (CommandResultException crex) {
        result = crex.getResult();
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    }
    return result;
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) StringUtils(org.apache.commons.lang.StringUtils) CliAvailabilityIndicator(org.springframework.shell.core.annotation.CliAvailabilityIndicator) Arrays(java.util.Arrays) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) Execution(org.apache.geode.cache.execute.Execution) CliMetaData(org.apache.geode.management.cli.CliMetaData) Function(org.apache.geode.cache.execute.Function) CliOption(org.springframework.shell.core.annotation.CliOption) ArrayList(java.util.ArrayList) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) HashSet(java.util.HashSet) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) LuceneDescribeIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDescribeIndexFunction) CliStrings(org.apache.geode.management.internal.cli.i18n.CliStrings) Region(org.apache.geode.cache.Region) LuceneSearchIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneSearchIndexFunction) SystemFailure(org.apache.geode.SystemFailure) IntegratedSecurityService(org.apache.geode.internal.security.IntegratedSecurityService) Result(org.apache.geode.management.cli.Result) DistributedMember(org.apache.geode.distributed.DistributedMember) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LinkedHashSet(java.util.LinkedHashSet) CliUtil(org.apache.geode.management.internal.cli.CliUtil) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) CliCommand(org.springframework.shell.core.annotation.CliCommand) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) GfshCommand(org.apache.geode.management.internal.cli.commands.GfshCommand) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) SecurityService(org.apache.geode.internal.security.SecurityService) Set(java.util.Set) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Collectors(java.util.stream.Collectors) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) List(java.util.List) LogWrapper(org.apache.geode.management.internal.cli.LogWrapper) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation) LuceneCreateIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneCreateIndexFunction) LuceneDestroyIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDestroyIndexFunction) Resource(org.apache.geode.security.ResourcePermission.Resource) ConverterHint(org.apache.geode.management.cli.ConverterHint) Operation(org.apache.geode.security.ResourcePermission.Operation) ResultBuilder(org.apache.geode.management.internal.cli.result.ResultBuilder) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) InternalCache(org.apache.geode.internal.cache.InternalCache) 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) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ArrayList(java.util.ArrayList) List(java.util.List) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 3 with XmlEntity

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

the class LuceneIndexCommands method executeDestroyIndexFunction.

private XmlEntity executeDestroyIndexFunction(List<CliFunctionResult> accumulatedResults, String indexName, String regionPath) {
    // Destroy has three cases:
    //
    // - no members define the region
    // In this case, send the request to all members to handle the case where the index has been
    // created, but not the region
    //
    // - all members define the region
    // In this case, send the request to one of the region members to destroy the index on all
    // member
    //
    // - some members define the region; some don't
    // In this case, send the request to one of the region members to destroy the index in all the
    // region members. Then send the function to the remaining members to handle the case where
    // the index has been created, but not the region
    XmlEntity xmlEntity = null;
    InternalCache cache = getCache();
    Set<DistributedMember> regionMembers = getRegionMembers(cache, regionPath);
    Set<DistributedMember> normalMembers = getNormalMembers(cache);
    LuceneDestroyIndexInfo indexInfo = new LuceneDestroyIndexInfo(indexName, regionPath);
    ResultCollector<?, ?> rc;
    if (regionMembers.isEmpty()) {
        // Attempt to destroy the proxy index on all members
        indexInfo.setDefinedDestroyOnly(true);
        rc = executeFunction(destroyIndexFunction, indexInfo, normalMembers);
        accumulatedResults.addAll((List<CliFunctionResult>) rc.getResult());
    } else {
        // Attempt to destroy the index on a region member
        indexInfo.setDefinedDestroyOnly(false);
        Set<DistributedMember> singleMember = new HashSet<>();
        singleMember.add(regionMembers.iterator().next());
        rc = executeFunction(destroyIndexFunction, indexInfo, singleMember);
        List<CliFunctionResult> cliFunctionResults = (List<CliFunctionResult>) rc.getResult();
        CliFunctionResult cliFunctionResult = cliFunctionResults.get(0);
        xmlEntity = cliFunctionResult.getXmlEntity();
        for (DistributedMember regionMember : regionMembers) {
            accumulatedResults.add(new CliFunctionResult(regionMember.getId(), cliFunctionResult.isSuccessful(), cliFunctionResult.getMessage()));
        }
        // If that succeeds, destroy the proxy index(es) on all other members if necessary
        if (cliFunctionResult.isSuccessful()) {
            normalMembers.removeAll(regionMembers);
            if (!normalMembers.isEmpty()) {
                indexInfo.setDefinedDestroyOnly(true);
                rc = executeFunction(destroyIndexFunction, indexInfo, normalMembers);
                accumulatedResults.addAll((List<CliFunctionResult>) rc.getResult());
            }
        } else {
        // @todo Should dummy results be added to the accumulatedResults for the non-region
        // members in the failed case
        }
    }
    return xmlEntity;
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with XmlEntity

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

the class XmlUtilsAddNewNodeJUnitTest method testDeleteNodeUnnamed.

/**
   * Tests {@link XmlUtils#addNewNode(Document, XmlEntity)} with {@link CacheXml} element that does
   * not have a name or id attribute, <code>pdx</code>. It should remove the existing
   * <code>pdx</code> element.
   * 
   * @throws Exception
   * @since GemFire 8.1
   */
@Test
public void testDeleteNodeUnnamed() throws Exception {
    final String xPath = "/cache:cache/cache:pdx";
    NodeList nodes = XmlUtils.query(config, xPath, xPathContext);
    assertEquals(1, nodes.getLength());
    Element element = (Element) nodes.item(0);
    assertEquals("foo", XmlUtils.getAttribute(element, "disk-store-name"));
    assertEquals(CacheXml.GEODE_NAMESPACE, element.getNamespaceURI());
    final Document changes = XmlUtils.createDocumentFromReader(new InputStreamReader(this.getClass().getResourceAsStream("XmlUtilsAddNewNodeJUnitTest.testDeleteNodeUnnamed.xml")));
    nodes = XmlUtils.query(changes, xPath, xPathContext);
    assertEquals(0, nodes.getLength());
    final XmlEntity xmlEntity = XmlEntity.builder().withType("pdx").withConfig(changes).build();
    XmlUtils.deleteNode(config, xmlEntity);
    nodes = XmlUtils.query(config, xPath, xPathContext);
    assertEquals(0, nodes.getLength());
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) InputStreamReader(java.io.InputStreamReader) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with XmlEntity

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

the class XmlUtilsAddNewNodeJUnitTest method testDeleteNodeNamed.

/**
   * Tests {@link XmlUtils#deleteNode(Document, XmlEntity)} with {@link CacheXml} element with a
   * <code>name</code> attribute, <code>region</code>. It should remove existing <code>region</code>
   * element with same <code>name</code>.
   * 
   * @throws Exception
   * @since GemFire 8.1
   */
@Test
public void testDeleteNodeNamed() throws Exception {
    final String xPath = "/cache:cache/cache:region[@name='r1']";
    NodeList nodes = XmlUtils.query(config, xPath, xPathContext);
    assertEquals(1, nodes.getLength());
    Element element = (Element) nodes.item(0);
    assertEquals(1, getElementNodes(element.getChildNodes()).size());
    assertEquals(CacheXml.GEODE_NAMESPACE, element.getNamespaceURI());
    final Document changes = XmlUtils.createDocumentFromReader(new InputStreamReader(this.getClass().getResourceAsStream("XmlUtilsAddNewNodeJUnitTest.testDeleteNodeNamed.xml")));
    nodes = XmlUtils.query(changes, xPath, xPathContext);
    assertEquals(0, nodes.getLength());
    final XmlEntity xmlEntity = XmlEntity.builder().withType("region").withAttribute("name", "r1").withConfig(changes).build();
    XmlUtils.deleteNode(config, xmlEntity);
    nodes = XmlUtils.query(config, xPath, xPathContext);
    assertEquals(0, nodes.getLength());
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) InputStreamReader(java.io.InputStreamReader) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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