Dennis-Mircea commented on code in PR #1099:
URL:
https://github.com/apache/flink-kubernetes-operator/pull/1099#discussion_r3426487262
##########
flink-autoscaler/src/main/java/org/apache/flink/autoscaler/ScalingMetricEvaluator.java:
##########
@@ -588,4 +635,76 @@ protected static double computeEdgeDataRate(
to);
return getRate(ScalingMetric.NUM_RECORDS_OUT, from, metricsHistory);
}
+
+ /**
+ * Executes the provided custom metric evaluator for the given job vertex.
Calls {@link
+ * FlinkAutoscalerEvaluator#evaluateVertexMetrics} to evaluate scaling
metrics.
+ *
+ * @param vertex The job vertex being evaluated.
+ * @param evaluatedMetrics Current evaluated metrics.
+ * @param customEvaluationSession A tuple containing the custom metric
evaluator and evaluation
+ * context.
+ * @return A map of scaling metrics, with its corresponding evaluated
scaling metric.
+ */
+ @VisibleForTesting
+ protected static Map<ScalingMetric, EvaluatedScalingMetric>
runCustomEvaluator(
+ JobVertexID vertex,
+ Map<ScalingMetric, EvaluatedScalingMetric> evaluatedMetrics,
+ Tuple2<FlinkAutoscalerEvaluator, FlinkAutoscalerEvaluator.Context>
+ customEvaluationSession) {
+ try {
+ return customEvaluationSession.f0.evaluateVertexMetrics(
+ vertex, evaluatedMetrics, customEvaluationSession.f1);
+ } catch (UnsupportedOperationException e) {
+ LOG.warn(
+ "Custom metric evaluator {} tried accessing an
un-modifiable view.",
+ customEvaluationSession.f0.getClass(),
+ e);
+ } catch (Exception e) {
+ LOG.warn(
+ "Custom metric evaluator {} threw an exception.",
+ customEvaluationSession.f0.getClass(),
+ e);
+ }
+
+ return Collections.emptyMap();
+ }
+
+ /**
+ * Merges the incoming evaluated metrics into actual evaluated metrics.
+ *
+ * @param actual The target evaluated metrics map to merge into.
+ * @param incoming The incoming map containing new evaluated metrics map
to be merged
+ * (nullable).
+ */
+ @VisibleForTesting
+ protected static void mergeEvaluatedMetricsMaps(
+ Map<ScalingMetric, EvaluatedScalingMetric> actual,
+ @Nullable Map<ScalingMetric, EvaluatedScalingMetric> incoming) {
+ Optional.ofNullable(incoming)
+ .ifPresent(
+ customEvaluatedMetric ->
+ customEvaluatedMetric.forEach(
+ (scalingMetric,
evaluatedScalingMetric) ->
+ actual.merge(
+ scalingMetric,
+ evaluatedScalingMetric,
+ ScalingMetricEvaluator
+
::mergeEvaluatedScalingMetric)));
+ }
+
+ /**
+ * Merges two {@link EvaluatedScalingMetric} instances.
+ *
+ * @param actual The existing evaluated scaling metric.
+ * @param incoming The incoming evaluated scaling metric.
+ * @return A new {@link EvaluatedScalingMetric} instance with merged
values.
+ */
Review Comment:
These two are the actual override step, so I'd keep them. The custom
evaluator returns only the metrics it wants to change, and
`mergeEvaluatedScalingMetric` does a NaN-aware, per-field merge
(`current`/`average`) of the returned `EvaluatedScalingMetric` onto the
internally computed one, while `mergeEvaluatedMetricsMaps` applies that across
the returned map. A plain `putAll` would force evaluators to re-supply every
field (and both `current` and `average`) just to override one, and would break
internal values with NaN where they only meant to set one side. This is what
makes "augment/override specific `ScalingMetric` values" work as documented.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]