[GitHub] [geode] DonalEvans commented on a change in pull request #5249: Refactor Restore Redundancy Command

2020-06-17 Thread GitBox


DonalEvans commented on a change in pull request #5249:
URL: https://github.com/apache/geode/pull/5249#discussion_r441782824



##
File path: 
geode-core/src/main/java/org/apache/geode/management/internal/operation/RestoreRedundancyPerformer.java
##
@@ -0,0 +1,179 @@
+/*
+ * 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.operation;
+
+import static org.apache.geode.cache.Region.SEPARATOR;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.geode.annotations.Immutable;
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.execute.Function;
+import org.apache.geode.cache.execute.ResultCollector;
+import org.apache.geode.distributed.DistributedMember;
+import 
org.apache.geode.distributed.internal.membership.InternalDistributedMember;
+import org.apache.geode.internal.cache.InternalCache;
+import org.apache.geode.internal.serialization.Version;
+import org.apache.geode.management.ManagementService;
+import 
org.apache.geode.management.internal.functions.RestoreRedundancyFunction;
+import org.apache.geode.management.internal.util.ManagementUtils;
+import org.apache.geode.management.operation.RestoreRedundancyRequest;
+import org.apache.geode.management.runtime.RestoreRedundancyResults;
+
+public class RestoreRedundancyPerformer
+implements OperationPerformer {
+  @Immutable
+  public static final Version ADDED_VERSION = Version.GEODE_1_13_0;
+  public static final String NO_MEMBERS_WITH_VERSION_FOR_REGION =
+  "No members with a version greater than or equal to %s were found for 
region %s";
+  public static final String EXCEPTION_MEMBER_MESSAGE = "Exception occurred on 
member %s: %s";
+
+  @Override
+  public RestoreRedundancyResults perform(Cache cache, 
RestoreRedundancyRequest operation) {
+return perform(cache, operation, false);
+  }
+
+  public RestoreRedundancyResults perform(Cache cache, 
RestoreRedundancyRequest operation,
+  boolean checkStatus) {
+List membersForEachRegion = new 
ArrayList<>();
+List includedRegionsWithNoMembers = new ArrayList<>();
+
+populateLists(membersForEachRegion, includedRegionsWithNoMembers, 
operation.getIncludeRegions(),
+operation.getExcludeRegions(), (InternalCache) cache);
+
+for (RebalanceOperationPerformer.MemberPRInfo prInfo : 
membersForEachRegion) {
+  // Filter out any members using older versions of Geode
+  List viableMembers = filterViableMembers(prInfo);
+
+  if (viableMembers.size() != 0) {
+// Update the MemberPRInfo with the viable members
+prInfo.dsMemberList = viableMembers;
+  } else {
+RestoreRedundancyResultsImpl results = new 
RestoreRedundancyResultsImpl();
+
results.setStatusMessage(String.format(NO_MEMBERS_WITH_VERSION_FOR_REGION,
+ADDED_VERSION.getName(), prInfo.region));
+results.setSuccess(false);
+return results;
+  }
+}
+
+List functionResults = new ArrayList<>();
+Object[] functionArgs = new Object[] {operation, checkStatus};
+List completedMembers = new ArrayList<>();
+for (RebalanceOperationPerformer.MemberPRInfo memberPRInfo : 
membersForEachRegion) {
+  // Check to see if an earlier function execution has already targeted a 
member hosting this
+  // region. If one has, there is no point sending a function for this 
region as it has already
+  // had redundancy restored
+  if (!Collections.disjoint(completedMembers, memberPRInfo.dsMemberList)) {
+continue;
+  }
+  // Try the function on the first member for this region
+  DistributedMember targetMember = memberPRInfo.dsMemberList.get(0);
+  RestoreRedundancyResults functionResult = 
executeFunctionAndGetFunctionResult(
+  new RestoreRedundancyFunction(), functionArgs, targetMember);
+  if (!functionResult.getSuccess()) {
+// Record the error and then give up
+RestoreRedundancyResultsImpl results = new 
RestoreRedundancyResultsImpl();
+results.setSuccess(false);
+String errorString =
+String.format(EXCEPTION_MEMBER_MESSAGE, 

[GitHub] [geode] DonalEvans commented on a change in pull request #5249: Refactor Restore Redundancy Command

2020-06-16 Thread GitBox


DonalEvans commented on a change in pull request #5249:
URL: https://github.com/apache/geode/pull/5249#discussion_r441065594



##
File path: 
geode-core/src/main/java/org/apache/geode/management/internal/operation/RebalanceOperationPerformer.java
##
@@ -189,16 +189,16 @@ public static DistributedMember 
getAssociatedMembers(String region, final Intern
 
 String[] membersName = bean.getMembers();
 Set dsMembers = ManagementUtils.getAllMembers(cache);
-Iterator it = dsMembers.iterator();
+Iterator it = dsMembers.iterator();
 
 boolean matchFound = false;
 
 if (membersName.length > 1) {
   while (it.hasNext() && !matchFound) {
-DistributedMember dsmember = (DistributedMember) it.next();
+DistributedMember DSMember = it.next();

Review comment:
   I think that variable names should start with a lower-case letter.

##
File path: 
geode-core/src/main/java/org/apache/geode/management/internal/functions/RestoreRedundancyFunction.java
##
@@ -0,0 +1,105 @@
+/*
+ * 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.functions;
+
+import static 
org.apache.geode.management.runtime.RestoreRedundancyResults.Status.ERROR;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.cache.control.RestoreRedundancyOperation;
+import org.apache.geode.cache.execute.FunctionContext;
+import 
org.apache.geode.internal.cache.control.SerializableRestoreRedundancyResultsImpl;
+import org.apache.geode.internal.cache.execute.InternalFunction;
+import org.apache.geode.logging.internal.log4j.api.LogService;
+import 
org.apache.geode.management.internal.operation.RestoreRedundancyResultsImpl;
+import org.apache.geode.management.operation.RestoreRedundancyRequest;
+
+
+public class RestoreRedundancyFunction implements InternalFunction {
+  private static final Logger logger = LogService.getLogger();
+
+  public static final String ID = RestoreRedundancyFunction.class.getName();
+
+
+  private static final long serialVersionUID = 1L;
+
+  @Override
+  // this would return the RestoreRedundancyResults if successful,
+  // it will return an exception to the caller if status is failure or any 
exception happens
+  public void execute(FunctionContext context) {
+Object[] arguments = context.getArguments();
+RestoreRedundancyRequest request = (RestoreRedundancyRequest) arguments[0];
+boolean isStatusCommand = (boolean) arguments[1];
+RestoreRedundancyOperation redundancyOperation =
+
context.getCache().getResourceManager().createRestoreRedundancyOperation();
+Set includeRegionsSet = null;
+if (request.getIncludeRegions() != null) {
+  includeRegionsSet = new HashSet<>(request.getIncludeRegions());
+}
+Set excludeRegionsSet = null;
+if (request.getExcludeRegions() != null) {
+  excludeRegionsSet = new HashSet<>(request.getExcludeRegions());
+}
+redundancyOperation.includeRegions(includeRegionsSet);
+redundancyOperation.excludeRegions(excludeRegionsSet);
+RestoreRedundancyResultsImpl results;
+
+try {
+  if (isStatusCommand) {
+results = (RestoreRedundancyResultsImpl) 
redundancyOperation.redundancyStatus();
+  } else {
+
redundancyOperation.shouldReassignPrimaries(request.getReassignPrimaries());
+results = (RestoreRedundancyResultsImpl) 
redundancyOperation.start().join();
+  }
+  if (results.getRegionOperationStatus().equals(ERROR)) {
+Exception e = new Exception(results.getRegionOperationMessage());
+throw e;
+  }
+  results.setSuccess(true);
+  results.setStatusMessage("Success"); // MLH change this

Review comment:
   This comment should be removed.

##
File path: 
geode-core/src/main/java/org/apache/geode/management/internal/functions/RestoreRedundancyFunction.java
##
@@ -0,0 +1,105 @@
+/*
+ * 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