1996fanrui commented on code in PR #726:
URL: 
https://github.com/apache/flink-kubernetes-operator/pull/726#discussion_r1422568938


##########
flink-autoscaler/src/main/java/org/apache/flink/autoscaler/RestApiMetricsCollector.java:
##########
@@ -78,19 +91,87 @@ protected Map<FlinkMetric, AggregatedMetric> 
queryAggregatedVertexMetrics(
                                     EmptyRequestBody.getInstance())
                             .get();
 
-            return responseBody.getMetrics().stream()
-                    .collect(
-                            Collectors.toMap(
-                                    m -> metrics.get(m.getId()),
-                                    m -> m,
-                                    (m1, m2) ->
-                                            new AggregatedMetric(
-                                                    m1.getId() + " merged with 
" + m2.getId(),
-                                                    Math.min(m1.getMin(), 
m2.getMin()),
-                                                    Math.max(m1.getMax(), 
m2.getMax()),
-                                                    // Average can't be 
computed
-                                                    Double.NaN,
-                                                    m1.getSum() + 
m2.getSum())));
+            return aggregateByFlinkMetric(metrics, responseBody);
         }
     }
+
+    protected Map<FlinkMetric, AggregatedMetric> queryTmMetrics(Context ctx) 
throws Exception {
+        try (var restClient = ctx.getRestClusterClient()) {
+            var metricNames = getTmMetricNames(restClient, ctx);
+            var metricNameMapping = new HashMap<String, FlinkMetric>();
+
+            REQUIRED_TM_METRICS.forEach(
+                    fm -> {
+                        var name =
+                                fm.findAny(metricNames)
+                                        .orElseThrow(
+                                                () ->
+                                                        new RuntimeException(
+                                                                "Could not 
find required TM metric "
+                                                                        + 
fm.name()));
+                        metricNameMapping.put(name, fm);
+                    });
+
+            TOTAL_GC_TIME_PER_SEC
+                    .findAny(metricNames)
+                    .ifPresent(
+                            m -> {
+                                LOG.debug("GC metrics found");
+                                metricNameMapping.put(m, 
TOTAL_GC_TIME_PER_SEC);
+                            });
+
+            var queriedMetrics =
+                    new HashMap<>(queryAggregatedTmMetrics(restClient, 
metricNameMapping));
+            availableTmMetricNames.put(ctx.getJobKey(), metricNames);

Review Comment:
   Could we cache `metricNameMapping` directly?
   
   After cached the `metricNames`, the availableTmMetricNames of each job won't 
changed forever. And the  `TOTAL_GC_TIME_PER_SEC` and `REQUIRED_TM_METRICS` 
aren't changed forever. 
   
   So, the `metricNameMapping` won't be changed forever.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to