org.apache.geode.management.internal.cli.commands.FunctionCommands Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geode-core Show documentation
Show all versions of geode-core Show documentation
Apache Geode provides a database-like consistency model, reliable transaction processing and a shared-nothing architecture to maintain very low latency performance with high concurrency processing
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.management.internal.cli.commands;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.geode.SystemFailure;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.execute.Execution;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionException;
import org.apache.geode.cache.execute.FunctionService;
import org.apache.geode.cache.execute.ResultCollector;
import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.internal.ClassPathLoader;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.apache.geode.management.DistributedRegionMXBean;
import org.apache.geode.management.ManagementService;
import org.apache.geode.management.cli.CliMetaData;
import org.apache.geode.management.cli.ConverterHint;
import org.apache.geode.management.cli.Result;
import org.apache.geode.management.cli.Result.Status;
import org.apache.geode.management.internal.MBeanJMXAdapter;
import org.apache.geode.management.internal.cli.AbstractCliAroundInterceptor;
import org.apache.geode.management.internal.cli.CliUtil;
import org.apache.geode.management.internal.cli.GfshParseResult;
import org.apache.geode.management.internal.cli.LogWrapper;
import org.apache.geode.management.internal.cli.functions.CliFunctionResult;
import org.apache.geode.management.internal.cli.functions.ListFunctionFunction;
import org.apache.geode.management.internal.cli.functions.UnregisterFunction;
import org.apache.geode.management.internal.cli.functions.UserFunctionExecution;
import org.apache.geode.management.internal.cli.i18n.CliStrings;
import org.apache.geode.management.internal.cli.result.CommandResultException;
import org.apache.geode.management.internal.cli.result.CompositeResultData;
import org.apache.geode.management.internal.cli.result.ErrorResultData;
import org.apache.geode.management.internal.cli.result.ResultBuilder;
import org.apache.geode.management.internal.cli.result.TabularResultData;
import org.apache.geode.management.internal.cli.shell.Gfsh;
import org.apache.geode.management.internal.security.ResourceOperation;
import org.apache.geode.security.ResourcePermission.Operation;
import org.apache.geode.security.ResourcePermission.Resource;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
/**
*
* @since GemFire 7.0
*/
@SuppressWarnings("unused")
public class FunctionCommands implements CommandMarker {
private final ListFunctionFunction listFunctionFunction = new ListFunctionFunction();
private Gfsh getGfsh() {
return Gfsh.getCurrentInstance();
}
@CliCommand(value = CliStrings.EXECUTE_FUNCTION, help = CliStrings.EXECUTE_FUNCTION__HELP)
@CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_FUNCTION})
@ResourceOperation(resource = Resource.DATA, operation = Operation.WRITE)
public Result executeFunction(
// TODO: Add optioncontext for functionID
@CliOption(key = CliStrings.EXECUTE_FUNCTION__ID, mandatory = true,
help = CliStrings.EXECUTE_FUNCTION__ID__HELP) String functionId,
@CliOption(key = CliStrings.EXECUTE_FUNCTION__ONGROUPS,
unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE,
optionContext = ConverterHint.MEMBERGROUP,
help = CliStrings.EXECUTE_FUNCTION__ONGROUPS__HELP) String[] onGroups,
@CliOption(key = CliStrings.EXECUTE_FUNCTION__ONMEMBER,
unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE,
optionContext = ConverterHint.MEMBERIDNAME,
help = CliStrings.EXECUTE_FUNCTION__ONMEMBER__HELP) String onMember,
@CliOption(key = CliStrings.EXECUTE_FUNCTION__ONREGION,
unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE,
optionContext = ConverterHint.REGIONPATH,
help = CliStrings.EXECUTE_FUNCTION__ONREGION__HELP) String onRegion,
@CliOption(key = CliStrings.EXECUTE_FUNCTION__ARGUMENTS,
unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE,
help = CliStrings.EXECUTE_FUNCTION__ARGUMENTS__HELP) String[] arguments,
@CliOption(key = CliStrings.EXECUTE_FUNCTION__RESULTCOLLECTOR,
unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE,
help = CliStrings.EXECUTE_FUNCTION__RESULTCOLLECTOR__HELP) String resultCollector,
@CliOption(key = CliStrings.EXECUTE_FUNCTION__FILTER,
unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE,
help = CliStrings.EXECUTE_FUNCTION__FILTER__HELP) String filterString) {
Result result = null;
CompositeResultData executeFunctionResultTable = ResultBuilder.createCompositeResultData();
TabularResultData resultTable = executeFunctionResultTable.addSection().addTable("Table1");
String headerText = "Execution summary";
resultTable.setHeader(headerText);
ResultCollector resultCollectorInstance = null;
Function function;
Set filters = new HashSet();
Execution execution = null;
if (functionId != null) {
functionId = functionId.trim();
}
if (onRegion != null) {
onRegion = onRegion.trim();
}
if (onMember != null) {
onMember = onMember.trim();
}
if (filterString != null) {
filterString = filterString.trim();
}
try {
// validate otherwise return right away. no need to process anything
if (functionId == null || functionId.length() == 0) {
ErrorResultData errorResultData =
ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
.addLine(CliStrings.EXECUTE_FUNCTION__MSG__MISSING_FUNCTIONID);
result = ResultBuilder.buildResult(errorResultData);
return result;
}
if (onRegion != null && onMember != null && onGroups != null) {
ErrorResultData errorResultData =
ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
.addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
result = ResultBuilder.buildResult(errorResultData);
return result;
} else if (onRegion != null && onMember != null) {
ErrorResultData errorResultData =
ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
.addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
result = ResultBuilder.buildResult(errorResultData);
return result;
} else if (onMember != null && onGroups != null) {
ErrorResultData errorResultData =
ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
.addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
result = ResultBuilder.buildResult(errorResultData);
return result;
} else if (onRegion != null && onGroups != null) {
ErrorResultData errorResultData =
ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
.addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
result = ResultBuilder.buildResult(errorResultData);
return result;
} else if (onRegion != null && onMember != null && onGroups != null) {
ErrorResultData errorResultData =
ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
.addLine(CliStrings.EXECUTE_FUNCTION__MSG__OPTIONS);
result = ResultBuilder.buildResult(errorResultData);
return result;
} else if ((onRegion == null || onRegion.length() == 0) && (filterString != null)) {
ErrorResultData errorResultData = ResultBuilder.createErrorResultData()
.setErrorCode(ResultBuilder.ERRORCODE_DEFAULT)
.addLine(CliStrings.EXECUTE_FUNCTION__MSG__MEMBER_SHOULD_NOT_HAVE_FILTER_FOR_EXECUTION);
result = ResultBuilder.buildResult(errorResultData);
return result;
}
Cache cache = CacheFactory.getAnyInstance();
if (resultCollector != null) {
resultCollectorInstance =
(ResultCollector) ClassPathLoader.getLatest().forName(resultCollector).newInstance();
}
if (filterString != null && filterString.length() > 0) {
filters.add(filterString);
}
if (onRegion == null && onMember == null && onGroups == null) {
// run function on all the members excluding locators bug#46113
// if user wish to execute on locator then he can choose --member or --group option
Set dsMembers = CliUtil.getAllNormalMembers(cache);
if (dsMembers.size() > 0) {
function = new UserFunctionExecution();
LogWrapper.getInstance().info(CliStrings
.format(CliStrings.EXECUTE_FUNCTION__MSG__EXECUTING_0_ON_ENTIRE_DS, functionId));
for (DistributedMember member : dsMembers) {
executeAndGetResults(functionId, filterString, resultCollector, arguments, cache,
member, resultTable, onRegion);
}
return ResultBuilder.buildResult(resultTable);
} else {
return ResultBuilder
.createUserErrorResult(CliStrings.EXECUTE_FUNCTION__MSG__DS_HAS_NO_MEMBERS);
}
} else if (onRegion != null && onRegion.length() > 0) {
if (cache.getRegion(onRegion) == null) {
// find a member where region is present
DistributedRegionMXBean bean =
ManagementService.getManagementService(GemFireCacheImpl.getInstance())
.getDistributedRegionMXBean(onRegion);
if (bean == null) {
bean = ManagementService.getManagementService(GemFireCacheImpl.getInstance())
.getDistributedRegionMXBean(Region.SEPARATOR + onRegion);
if (bean == null) {
return ResultBuilder.createGemFireErrorResult(CliStrings
.format(CliStrings.EXECUTE_FUNCTION__MSG__MXBEAN_0_FOR_NOT_FOUND, onRegion));
}
}
DistributedMember member = null;
String[] membersName = bean.getMembers();
Set dsMembers = CliUtil.getAllMembers(cache);
Iterator it = dsMembers.iterator();
boolean matchFound = false;
if (membersName.length > 0) {
while (it.hasNext() && matchFound == false) {
DistributedMember dsmember = (DistributedMember) it.next();
for (String memberName : membersName) {
if (MBeanJMXAdapter.getMemberNameOrId(dsmember).equals(memberName)) {
member = dsmember;
matchFound = true;
break;
}
}
}
}
if (matchFound == true) {
executeAndGetResults(functionId, filterString, resultCollector, arguments, cache,
member, resultTable, onRegion);
return ResultBuilder.buildResult(resultTable);
} else {
return ResultBuilder.createGemFireErrorResult(CliStrings.format(
CliStrings.EXECUTE_FUNCTION__MSG__NO_ASSOCIATED_MEMBER_REGION, " " + onRegion));
}
} else {
execution = FunctionService.onRegion(cache.getRegion(onRegion));
if (execution != null) {
if (resultCollectorInstance != null) {
execution = execution.withCollector(resultCollectorInstance);
}
if (filters != null && filters.size() > 0) {
execution = execution.withFilter(filters);
}
if (arguments != null && arguments.length > 0) {
execution = execution.withArgs(arguments);
}
try {
List