AMBARI-22144. Hitting pause during upgrade jumps progress to 100% (ncole)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/5370297d Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/5370297d Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/5370297d Branch: refs/heads/branch-feature-AMBARI-14714 Commit: 5370297d982330cd43673afbc9a0cb341ea027f4 Parents: ed378b7 Author: Nate Cole <[email protected]> Authored: Thu Oct 5 12:03:00 2017 -0400 Committer: Nate Cole <[email protected]> Committed: Thu Oct 5 12:03:00 2017 -0400 ---------------------------------------------------------------------- .../internal/UpgradeResourceProvider.java | 72 +++++++++++++++++++- .../internal/UpgradeResourceProviderTest.java | 33 ++++++++- 2 files changed, 102 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/5370297d/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java index 33ce25e..85f3a1b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java @@ -198,7 +198,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider private static final String REQUEST_END_TIME_ID = "Upgrade/end_time"; private static final String REQUEST_EXCLUSIVE_ID = "Upgrade/exclusive"; - private static final String REQUEST_PROGRESS_PERCENT_ID = "Upgrade/progress_percent"; + protected static final String REQUEST_PROGRESS_PERCENT_ID = "Upgrade/progress_percent"; private static final String REQUEST_STATUS_PROPERTY_ID = "Upgrade/request_status"; private static final Set<String> PK_PROPERTY_IDS = new HashSet<>( @@ -416,14 +416,82 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider CalculatedStatus calc = CalculatedStatus.statusFromStageSummary(summary, summary.keySet()); + if (calc.getStatus() == HostRoleStatus.ABORTED && entity.isSuspended()) { + double percent = calculateAbortedProgress(summary); + setResourceProperty(r, REQUEST_PROGRESS_PERCENT_ID, percent*100, requestPropertyIds); + } else { + setResourceProperty(r, REQUEST_PROGRESS_PERCENT_ID, calc.getPercent(), requestPropertyIds); + } + setResourceProperty(r, REQUEST_STATUS_PROPERTY_ID, calc.getStatus(), requestPropertyIds); - setResourceProperty(r, REQUEST_PROGRESS_PERCENT_ID, calc.getPercent(), requestPropertyIds); } } return results; } + /** + * Unlike in CalculatedStatus, we can't use ABORTED here as a COMPLETED state. + * Therefore, the values will be slightly off since in CalulatedStatus, ABORTED + * contributes all of its progress to the overall progress, but here it + * contributes none of it. + * + * Since this is specifically for ABORTED upgrades that are + * also suspended, the percentages should come out pretty close after ABORTED move back + * to PENDING. + * + * @return the percent complete, counting ABORTED as zero percent. + */ + private double calculateAbortedProgress(Map<Long, HostRoleCommandStatusSummaryDTO> summary) { + // !!! use the raw states to determine percent completes + Map<HostRoleStatus, Integer> countTotals = new HashMap<>(); + int totalTasks = 0; + + + for (HostRoleCommandStatusSummaryDTO statusSummary : summary.values()) { + totalTasks += statusSummary.getTaskTotal(); + for (Map.Entry<HostRoleStatus, Integer> entry : statusSummary.getCounts().entrySet()) { + if (!countTotals.containsKey(entry.getKey())) { + countTotals.put(entry.getKey(), Integer.valueOf(0)); + } + countTotals.put(entry.getKey(), countTotals.get(entry.getKey()) + entry.getValue()); + } + } + + double percent = 0d; + + for (HostRoleStatus status : HostRoleStatus.values()) { + if (!countTotals.containsKey(status)) { + countTotals.put(status, Integer.valueOf(0)); + } + double countValue = (double) countTotals.get(status); + + // !!! calculation lifted from CalculatedStatus + switch (status) { + case ABORTED: + // !!! see javadoc + break; + case HOLDING: + case HOLDING_FAILED: + case HOLDING_TIMEDOUT: + case IN_PROGRESS: + case PENDING: // shouldn't be any, we're supposed to be ABORTED + percent += countValue * 0.35d; + break; + case QUEUED: + percent += countValue * 0.09d; + break; + default: + if (status.isCompletedState()) { + percent += countValue / (double) totalTasks; + } + break; + } + } + + return percent; + } + @Override public RequestStatus updateResources(final Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, http://git-wip-us.apache.org/repos/asf/ambari/blob/5370297d/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java index fea56d9..20adac2 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java @@ -871,9 +871,40 @@ public class UpgradeResourceProviderTest extends EasyMockSupport { UpgradeResourceProvider urp = createProvider(amc); - // !!! make sure we can. actual abort is tested elsewhere Request req = PropertyHelper.getUpdateRequest(requestProps, null); urp.updateResources(req, null); + + List<HostRoleCommandEntity> commands = hrcDAO.findByRequest(id); + + int i = 0; + for (HostRoleCommandEntity command : commands) { + if (i < 3) { + command.setStatus(HostRoleStatus.COMPLETED); + } else { + command.setStatus(HostRoleStatus.ABORTED); + } + hrcDAO.merge(command); + i++; + } + + req = PropertyHelper.getReadRequest( + UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, + UpgradeResourceProvider.UPGRADE_ID, + UpgradeResourceProvider.REQUEST_PROGRESS_PERCENT_ID); + + Predicate pred = new PredicateBuilder() + .property(UpgradeResourceProvider.UPGRADE_REQUEST_ID).equals(id.toString()) + .and() + .property(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME).equals("c1") + .toPredicate(); + + Set<Resource> resources = urp.getResources(req, pred); + assertEquals(1, resources.size()); + res = resources.iterator().next(); + + Double value = (Double) res.getPropertyValue(UpgradeResourceProvider.REQUEST_PROGRESS_PERCENT_ID); + + assertEquals(37.5d, value, 0.1d); }
