com.gemstone.gemfire.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 gemfire-core Show documentation
Show all versions of gemfire-core Show documentation
TIBCO ComputeDB store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.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.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;
import com.gemstone.gemfire.SystemFailure;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.execute.Execution;
import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionException;
import com.gemstone.gemfire.cache.execute.FunctionService;
import com.gemstone.gemfire.cache.execute.ResultCollector;
import com.gemstone.gemfire.distributed.DistributedMember;
import com.gemstone.gemfire.internal.ClassPathLoader;
import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
import com.gemstone.gemfire.management.DistributedRegionMXBean;
import com.gemstone.gemfire.management.ManagementService;
import com.gemstone.gemfire.management.cli.CliMetaData;
import com.gemstone.gemfire.management.cli.ConverterHint;
import com.gemstone.gemfire.management.cli.Result;
import com.gemstone.gemfire.management.cli.Result.Status;
import com.gemstone.gemfire.management.internal.MBeanJMXAdapter;
import com.gemstone.gemfire.management.internal.cli.AbstractCliAroundInterceptor;
import com.gemstone.gemfire.management.internal.cli.CliUtil;
import com.gemstone.gemfire.management.internal.cli.GfshParseResult;
import com.gemstone.gemfire.management.internal.cli.LogWrapper;
import com.gemstone.gemfire.management.internal.cli.functions.CliFunctionResult;
import com.gemstone.gemfire.management.internal.cli.functions.ListFunctionFunction;
import com.gemstone.gemfire.management.internal.cli.functions.UnregisterFunction;
import com.gemstone.gemfire.management.internal.cli.functions.UserFunctionExecution;
import com.gemstone.gemfire.management.internal.cli.i18n.CliStrings;
import com.gemstone.gemfire.management.internal.cli.result.CommandResultException;
import com.gemstone.gemfire.management.internal.cli.result.CompositeResultData;
import com.gemstone.gemfire.management.internal.cli.result.ErrorResultData;
import com.gemstone.gemfire.management.internal.cli.result.ResultBuilder;
import com.gemstone.gemfire.management.internal.cli.result.TabularResultData;
import com.gemstone.gemfire.management.internal.cli.shell.Gfsh;
/**
* @author David Hoots
*
* @since 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_GEMFIRE_FUNCTION })
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__ONGROUP,
unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE,
optionContext = ConverterHint.MEMBERGROUP,
help = CliStrings.EXECUTE_FUNCTION__ONGROUP__HELP) String onGroup,
@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 && onGroup != 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 && onGroup != 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 && onGroup != 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 && onGroup != 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 && onGroup == 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) {
return ResultBuilder
.createGemFireErrorResult(CliStrings.format(CliStrings.EXECUTE_FUNCTION__MSG__MXBEAN_0_FOR_NOT_FOUND,
onRegion));
} else {
DistributedMember member = null;
String[] membersName = bean.getMembers();
Set dsMembers = CliUtil.getAllMembers(cache);
Iterator it = dsMembers.iterator();
boolean matchFound = false;
if (membersName.length > 1) {
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