ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jonathanhur...@apache.org
Subject [15/17] ambari git commit: AMBARI-21025. Avoid unnecessary calls to load stage summaries (magyari_sandor)
Date Tue, 16 May 2017 17:15:25 GMT
AMBARI-21025. Avoid unnecessary calls to load stage summaries (magyari_sandor)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/8cebc18b
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/8cebc18b
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/8cebc18b

Branch: refs/heads/branch-feature-AMBARI-12556
Commit: 8cebc18b82e3e9df0c440675f26f8e6be32ebf06
Parents: b97d268
Author: Sandor Magyari <smagyari@hortonworks.com>
Authored: Wed May 10 16:22:07 2017 +0200
Committer: Sandor Magyari <smagyari@hortonworks.com>
Committed: Tue May 16 13:59:08 2017 +0200

----------------------------------------------------------------------
 .../internal/StageResourceProvider.java         | 81 +++-----------------
 1 file changed, 10 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/8cebc18b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StageResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StageResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StageResourceProvider.java
index db9a0e2..ec3688d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StageResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/StageResourceProvider.java
@@ -211,8 +211,7 @@ public class StageResourceProvider extends AbstractControllerResourceProvider
im
 
     // !!! poor mans cache.  toResource() shouldn't be calling the db
     // every time, when the request id is likely the same for each stageEntity
-    Map<Long, Map<Long, HostRoleCommandStatusSummaryDTO>> cache =
-      new HashMap<>();
+    Map<Long, Map<Long, HostRoleCommandStatusSummaryDTO>> cache = new HashMap<>();
 
     List<StageEntity> entities = dao.findAll(request, predicate);
     for (StageEntity entity : entities) {
@@ -232,8 +231,11 @@ public class StageResourceProvider extends AbstractControllerResourceProvider
im
 
       if (null != lr) {
         Collection<StageEntity> topologyManagerStages = lr.getStageEntities();
+        // preload summaries as it contains summaries for all stages within this request
+        Map<Long, HostRoleCommandStatusSummaryDTO> summary = topologyManager.getStageSummaries(requestId);
+        cache.put(requestId, summary);
         for (StageEntity entity : topologyManagerStages) {
-          Resource stageResource = toResource(entity, propertyIds);
+          Resource stageResource = toResource(cache, entity, propertyIds);
           if (predicate.evaluate(stageResource)) {
             results.add(stageResource);
           }
@@ -242,7 +244,11 @@ public class StageResourceProvider extends AbstractControllerResourceProvider
im
     } else {
       Collection<StageEntity> topologyManagerStages = topologyManager.getStages();
       for (StageEntity entity : topologyManagerStages) {
-        Resource stageResource = toResource(entity, propertyIds);
+        if (!cache.containsKey(entity.getRequestId())) {
+          Map<Long, HostRoleCommandStatusSummaryDTO> summary = topologyManager.getStageSummaries(entity.getRequestId());
+          cache.put(entity.getRequestId(), summary);
+        }
+        Resource stageResource = toResource(cache, entity, propertyIds);
         if (predicate.evaluate(stageResource)) {
           results.add(stageResource);
         }
@@ -351,71 +357,4 @@ public class StageResourceProvider extends AbstractControllerResourceProvider
im
     return resource;
   }
 
-  /**
-   * Converts the {@link StageEntity} to a {@link Resource}.
-   *
-   * @param entity        the entity to convert (not {@code null})
-   * @param requestedIds  the properties requested (not {@code null})
-   *
-   * @return the new resource
-   */
-  //todo: almost exactly the same as other toResource except how summaries are obtained
-  //todo: refactor to combine the two with the summary logic extracted
-  private Resource toResource(StageEntity entity, Set<String> requestedIds) {
-
-    Resource resource = new ResourceImpl(Resource.Type.Stage);
-
-    Long clusterId = entity.getClusterId();
-    if (clusterId != null && !clusterId.equals(Long.valueOf(-1L))) {
-      try {
-        Cluster cluster = clustersProvider.get().getClusterById(clusterId);
-
-        setResourceProperty(resource, STAGE_CLUSTER_NAME, cluster.getClusterName(), requestedIds);
-      } catch (Exception e) {
-        LOG.error("Can not get information for cluster " + clusterId + ".", e );
-      }
-    }
-
-    Map<Long, HostRoleCommandStatusSummaryDTO> summary =
-        topologyManager.getStageSummaries(entity.getRequestId());
-
-    setResourceProperty(resource, STAGE_STAGE_ID, entity.getStageId(), requestedIds);
-    setResourceProperty(resource, STAGE_REQUEST_ID, entity.getRequestId(), requestedIds);
-    setResourceProperty(resource, STAGE_CONTEXT, entity.getRequestContext(), requestedIds);
-
-    // this property is lazy loaded in JPA; don't use it unless requested
-    if (isPropertyRequested(STAGE_CLUSTER_HOST_INFO, requestedIds)) {
-      resource.setProperty(STAGE_CLUSTER_HOST_INFO, entity.getClusterHostInfo());
-    }
-
-    // this property is lazy loaded in JPA; don't use it unless requested
-    if (isPropertyRequested(STAGE_COMMAND_PARAMS, requestedIds)) {
-      resource.setProperty(STAGE_COMMAND_PARAMS, entity.getCommandParamsStage());
-    }
-
-    // this property is lazy loaded in JPA; don't use it unless requested
-    if (isPropertyRequested(STAGE_HOST_PARAMS, requestedIds)) {
-      resource.setProperty(STAGE_HOST_PARAMS, entity.getHostParamsStage());
-    }
-
-    setResourceProperty(resource, STAGE_SKIPPABLE, entity.isSkippable(), requestedIds);
-
-    Long startTime = Long.MAX_VALUE;
-    Long endTime = 0L;
-    if (summary.containsKey(entity.getStageId())) {
-      startTime = summary.get(entity.getStageId()).getStartTime();
-      endTime = summary.get(entity.getStageId()).getEndTime();
-    }
-
-    setResourceProperty(resource, STAGE_START_TIME, startTime, requestedIds);
-    setResourceProperty(resource, STAGE_END_TIME, endTime, requestedIds);
-
-    CalculatedStatus status = CalculatedStatus.statusFromStageSummary(summary, Collections.singleton(entity.getStageId()));
-
-    setResourceProperty(resource, STAGE_PROGRESS_PERCENT, status.getPercent(), requestedIds);
-    setResourceProperty(resource, STAGE_STATUS, status.getStatus(), requestedIds);
-    setResourceProperty(resource, STAGE_DISPLAY_STATUS, status.getDisplayStatus(), requestedIds);
-
-    return resource;
-  }
 }


Mime
View raw message