gyfora commented on code in PR #194: URL: https://github.com/apache/flink-kubernetes-operator/pull/194#discussion_r865594193
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/metrics/FlinkDeploymentMetrics.java: ########## @@ -0,0 +1,68 @@ +/* + * 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.flink.kubernetes.operator.metrics; + +import org.apache.flink.kubernetes.operator.crd.FlinkDeployment; +import org.apache.flink.kubernetes.operator.crd.status.JobManagerDeploymentStatus; +import org.apache.flink.metrics.MetricGroup; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** FlinkDeployment metrics. */ +public class FlinkDeploymentMetrics { + + private final Map<JobManagerDeploymentStatus, Set<String>> statuses = new HashMap<>(); + public static final String NS_SCOPE_KEY = "ns"; + public static final String METRIC_GROUP_NAME = "FlinkDeployment"; + + public FlinkDeploymentMetrics(MetricGroup parentMetricGroup) { + MetricGroup flinkDeploymentMetrics = parentMetricGroup.addGroup(METRIC_GROUP_NAME); + for (JobManagerDeploymentStatus status : JobManagerDeploymentStatus.values()) { + statuses.put(status, ConcurrentHashMap.newKeySet()); + } + for (JobManagerDeploymentStatus status : JobManagerDeploymentStatus.values()) { + statuses.put(status, new HashSet<>()); + MetricGroup metricGroup = flinkDeploymentMetrics.addGroup(status.toString()); + metricGroup.gauge("Count", () -> statuses.get(status).size()); + } + flinkDeploymentMetrics.gauge( + "Count", () -> statuses.values().stream().mapToInt(Set::size).sum()); + } + + public void update(FlinkDeployment flinkApp) { + remove(flinkApp); + statuses.get(flinkApp.getStatus().getJobManagerDeploymentStatus()) + .add(flinkApp.getMetadata().getName()); + } + + public void remove(FlinkDeployment flinkApp) { + statuses.values() + .forEach( + deployments -> { + deployments.remove(flinkApp.getMetadata().getName()); + }); + } + + public boolean isEmpty() { + return statuses.values().stream().mapToInt(Set::size).sum() == 0; Review Comment: If you store the Count gauge into a field you can simply read the value here and compare it to 0 ########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkSessionJobController.java: ########## @@ -266,4 +276,14 @@ private Optional<String> validateSessionJob(FlinkSessionJob sessionJob, Context } return validationError; } + + private void addMetricsIfAbsent(FlinkSessionJob sessionJob) { Review Comment: Same comment as I added to the deployment controller ########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkDeploymentController.java: ########## @@ -169,4 +181,14 @@ private Optional<String> validateDeployment(FlinkDeployment deployment) { } return validationError; } + + private void addMetricsIfAbsent(FlinkDeployment flinkApp) { Review Comment: We could call this methid `getMetrics` and simply use it everywhere instead of manuall getting from the map. That would potentially avoid some freak race conditions and null pointers between adding and getting. -- 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]
