use of org.apache.geode.distributed.DistributedMember in project geode by apache.
the class CreateAlterDestroyRegionCommands method getRegionAttributes.
private static <K, V> FetchRegionAttributesFunctionResult<K, V> getRegionAttributes(InternalCache cache, String regionPath) {
if (!isClusterWideSameConfig(cache, regionPath)) {
throw new IllegalStateException(CliStrings.format(CliStrings.CREATE_REGION__MSG__USE_ATTRIBUTES_FORM_REGIONS_EXISTS_BUT_DIFFERENT_SCOPE_OR_DATAPOLICY_USE_DESCRIBE_REGION_FOR_0, regionPath));
}
FetchRegionAttributesFunctionResult<K, V> attributes = null;
// need to use FetchRegionAttributesFunction to fetch RegionAttributes
try {
attributes = FetchRegionAttributesFunction.getRegionAttributes(regionPath);
} catch (IllegalArgumentException e) {
/* region doesn't exist on the manager */
}
if (attributes == null) {
// find first member which has the region
Set<DistributedMember> regionAssociatedMembers = CliUtil.getRegionAssociatedMembers(regionPath, cache, false);
if (regionAssociatedMembers != null && !regionAssociatedMembers.isEmpty()) {
DistributedMember distributedMember = regionAssociatedMembers.iterator().next();
ResultCollector<?, ?> resultCollector = CliUtil.executeFunction(FetchRegionAttributesFunction.INSTANCE, regionPath, distributedMember);
List<?> resultsList = (List<?>) resultCollector.getResult();
if (resultsList != null && !resultsList.isEmpty()) {
for (Object object : resultsList) {
if (object instanceof IllegalArgumentException) {
throw (IllegalArgumentException) object;
} else if (object instanceof Throwable) {
Throwable th = (Throwable) object;
LogWrapper.getInstance().info(CliUtil.stackTraceAsString((th)));
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__COULD_NOT_RETRIEVE_REGION_ATTRS_FOR_PATH_0_REASON_1, new Object[] { regionPath, th.getMessage() }));
} else {
// has to be RegionAttributes
// to avoid warning :(
@SuppressWarnings("unchecked") FetchRegionAttributesFunctionResult<K, V> regAttr = ((FetchRegionAttributesFunctionResult<K, V>) object);
if (attributes == null) {
attributes = regAttr;
break;
}
// attributes null check
}
// not IllegalArgumentException or other throwable
}
// iterate over list - there should be only one result in the list
}
// result list is not null or mpty
}
// regionAssociatedMembers is not-empty
}
return attributes;
}
use of org.apache.geode.distributed.DistributedMember in project geode by apache.
the class CreateAlterDestroyRegionCommands method destroyRegion.
@CliCommand(value = { CliStrings.DESTROY_REGION }, help = CliStrings.DESTROY_REGION__HELP)
@CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION)
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result destroyRegion(@CliOption(key = CliStrings.DESTROY_REGION__REGION, optionContext = ConverterHint.REGION_PATH, mandatory = true, help = CliStrings.DESTROY_REGION__REGION__HELP) String regionPath) {
if (regionPath == null) {
return ResultBuilder.createInfoResult(CliStrings.DESTROY_REGION__MSG__SPECIFY_REGIONPATH_TO_DESTROY);
}
if (StringUtils.isBlank(regionPath) || regionPath.equals(Region.SEPARATOR)) {
return ResultBuilder.createInfoResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__REGIONPATH_0_NOT_VALID, new Object[] { regionPath }));
}
Result result;
AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
try {
InternalCache cache = getCache();
ManagementService managementService = ManagementService.getExistingManagementService(cache);
String regionPathToUse = regionPath;
if (!regionPathToUse.startsWith(Region.SEPARATOR)) {
regionPathToUse = Region.SEPARATOR + regionPathToUse;
}
Set<DistributedMember> regionMembersList = findMembersForRegion(cache, managementService, regionPathToUse);
if (regionMembersList.size() == 0) {
return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__COULDNOT_FIND_REGIONPATH_0_IN_GEODE, new Object[] { regionPath, "jmx-manager-update-rate milliseconds" }));
}
CliFunctionResult destroyRegionResult;
ResultCollector<?, ?> resultCollector = CliUtil.executeFunction(RegionDestroyFunction.INSTANCE, regionPath, regionMembersList);
List<CliFunctionResult> resultsList = (List<CliFunctionResult>) resultCollector.getResult();
String message = CliStrings.format(CliStrings.DESTROY_REGION__MSG__REGION_0_1_DESTROYED, new Object[] { regionPath, "" });
// Only if there is an error is this set to false
boolean isRegionDestroyed = true;
for (CliFunctionResult aResultsList : resultsList) {
destroyRegionResult = aResultsList;
if (destroyRegionResult.isSuccessful()) {
xmlEntity.set(destroyRegionResult.getXmlEntity());
} else if (destroyRegionResult.getThrowable() != null) {
Throwable t = destroyRegionResult.getThrowable();
LogWrapper.getInstance().info(t.getMessage(), t);
message = CliStrings.format(CliStrings.DESTROY_REGION__MSG__ERROR_OCCURRED_WHILE_DESTROYING_0_REASON_1, new Object[] { regionPath, t.getMessage() });
isRegionDestroyed = false;
} else {
message = CliStrings.format(CliStrings.DESTROY_REGION__MSG__UNKNOWN_RESULT_WHILE_DESTROYING_REGION_0_REASON_1, new Object[] { regionPath, destroyRegionResult.getMessage() });
isRegionDestroyed = false;
}
}
if (isRegionDestroyed) {
result = ResultBuilder.createInfoResult(message);
} else {
result = ResultBuilder.createUserErrorResult(message);
}
} catch (IllegalStateException e) {
result = ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__ERROR_WHILE_DESTROYING_REGION_0_REASON_1, new Object[] { regionPath, e.getMessage() }));
} catch (Exception e) {
result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__ERROR_WHILE_DESTROYING_REGION_0_REASON_1, new Object[] { regionPath, e.getMessage() }));
}
if (xmlEntity.get() != null) {
persistClusterConfiguration(result, () -> getSharedConfiguration().deleteXmlEntity(xmlEntity.get(), null));
}
return result;
}
use of org.apache.geode.distributed.DistributedMember in project geode by apache.
the class CreateAlterDestroyRegionCommands method validateGroups.
private void validateGroups(InternalCache cache, String[] groups) {
if (groups != null && groups.length != 0) {
Set<String> existingGroups = new HashSet<>();
Set<DistributedMember> members = CliUtil.getAllNormalMembers(cache);
for (DistributedMember distributedMember : members) {
List<String> memberGroups = distributedMember.getGroups();
existingGroups.addAll(memberGroups);
}
List<String> groupsList = new ArrayList<>(Arrays.asList(groups));
groupsList.removeAll(existingGroups);
if (!groupsList.isEmpty()) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__GROUPS_0_ARE_INVALID, new Object[] { String.valueOf(groupsList) }));
}
}
}
use of org.apache.geode.distributed.DistributedMember in project geode by apache.
the class DataCommands method importData.
@CliCommand(value = CliStrings.IMPORT_DATA, help = CliStrings.IMPORT_DATA__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION })
public Result importData(@CliOption(key = CliStrings.IMPORT_DATA__REGION, optionContext = ConverterHint.REGION_PATH, mandatory = true, help = CliStrings.IMPORT_DATA__REGION__HELP) String regionName, @CliOption(key = CliStrings.IMPORT_DATA__FILE, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.IMPORT_DATA__FILE__HELP) String filePath, @CliOption(key = CliStrings.IMPORT_DATA__MEMBER, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.IMPORT_DATA__MEMBER__HELP) String memberNameOrId, @CliOption(key = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS, unspecifiedDefaultValue = "false", help = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS__HELP) boolean invokeCallbacks) {
this.securityService.authorizeRegionWrite(regionName);
Result result;
try {
final DistributedMember targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId);
if (!filePath.endsWith(CliStrings.GEODE_DATA_FILE_EXTENSION)) {
return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.INVALID_FILE_EXTENSION, CliStrings.GEODE_DATA_FILE_EXTENSION));
}
if (targetMember != null) {
final Object[] args = { regionName, filePath, invokeCallbacks };
ResultCollector<?, ?> rc = CliUtil.executeFunction(importDataFunction, args, targetMember);
List<Object> results = (List<Object>) rc.getResult();
if (results != null) {
Object resultObj = results.get(0);
if (resultObj instanceof String) {
result = ResultBuilder.createInfoResult((String) resultObj);
} else if (resultObj instanceof Exception) {
result = ResultBuilder.createGemFireErrorResult(((Exception) resultObj).getMessage());
} else {
result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
}
} else {
result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
}
} else {
result = ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.IMPORT_DATA__MEMBER__NOT__FOUND, memberNameOrId));
}
} catch (CacheClosedException e) {
result = ResultBuilder.createGemFireErrorResult(e.getMessage());
} catch (FunctionInvocationTargetException e) {
result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
}
return result;
}
use of org.apache.geode.distributed.DistributedMember in project geode by apache.
the class QueryDataFunction method callFunction.
private static Object callFunction(final Object functionArgs, final Set<DistributedMember> members, final boolean zipResult) throws Exception {
try {
if (members.size() == 1) {
DistributedMember member = members.iterator().next();
ResultCollector collector = FunctionService.onMember(member).setArguments(functionArgs).execute(ManagementConstants.QUERY_DATA_FUNCTION);
List list = (List) collector.getResult();
Object object = null;
if (list.size() > 0) {
object = list.get(0);
}
if (object instanceof Throwable) {
throw (Throwable) object;
}
QueryDataFunctionResult result = (QueryDataFunctionResult) object;
if (zipResult) {
// The result is already compressed
return result.compressedBytes;
} else {
Object[] functionArgsList = (Object[]) functionArgs;
boolean showMember = (Boolean) functionArgsList[DISPLAY_MEMBERWISE];
if (showMember) {
// Added to show a single member similar to multiple
// member.
// Note , if no member is selected this is the code path executed. A
// random associated member is chosen.
List<String> decompressedList = new ArrayList<>();
decompressedList.add(BeanUtilFuncs.decompress(result.compressedBytes));
return wrapResult(decompressedList.toString());
}
return BeanUtilFuncs.decompress(result.compressedBytes);
}
} else {
// More than 1 Member
ResultCollector coll = FunctionService.onMembers(members).setArguments(functionArgs).execute(ManagementConstants.QUERY_DATA_FUNCTION);
List list = (List) coll.getResult();
Object object = list.get(0);
if (object instanceof Throwable) {
throw (Throwable) object;
}
Iterator<QueryDataFunctionResult> it = list.iterator();
List<String> decompressedList = new ArrayList<>();
while (it.hasNext()) {
String decompressedStr;
decompressedStr = BeanUtilFuncs.decompress(it.next().compressedBytes);
decompressedList.add(decompressedStr);
}
if (zipResult) {
return BeanUtilFuncs.compress(wrapResult(decompressedList.toString()));
} else {
return wrapResult(decompressedList.toString());
}
}
} catch (FunctionException fe) {
throw new Exception(ManagementStrings.QUERY__MSG__QUERY_EXEC.toLocalizedString(fe.getMessage()));
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable e) {
SystemFailure.checkFailure();
throw new Exception(ManagementStrings.QUERY__MSG__QUERY_EXEC.toLocalizedString(e.getMessage()));
}
}
Aggregations