zentol commented on a change in pull request #18566: URL: https://github.com/apache/flink/pull/18566#discussion_r799535017
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/metrics/DeploymentStateTimeMetrics.java ########## @@ -0,0 +1,168 @@ +/* + * 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.runtime.scheduler.metrics; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.configuration.MetricOptions; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.runtime.execution.ExecutionState; +import org.apache.flink.runtime.executiongraph.ExecutionAttemptID; +import org.apache.flink.runtime.executiongraph.ExecutionStateUpdateListener; +import org.apache.flink.runtime.jobgraph.JobType; +import org.apache.flink.util.clock.Clock; +import org.apache.flink.util.clock.SystemClock; + +import java.util.HashSet; +import java.util.Set; +import java.util.function.Predicate; + +/** + * Metrics that capture how long a job was deploying tasks. + * + * <p>These metrics differentiate between batch & streaming use-cases: + * + * <p>Batch: Measures from the start of the first deployment until the first task has been deployed. + * From that point the job is making progress. + * + * <p>Streaming: Measures from the start of the first deployment until all tasks have been deployed. + * From that point on checkpoints can be triggered, and thus progress be made. + */ +public class DeploymentStateTimeMetrics + implements ExecutionStateUpdateListener, StateTimeMetric, MetricsRegistrar { + + private static final long NOT_STARTED = -1L; + + private final Predicate<Integer> deploymentStartPredicate; + private final Predicate<Integer> deploymentEndPredicate; + private final MetricOptions.JobStatusMetricsSettings stateTimeMetricsSettings; + private final Clock clock; + + // deployment book-keeping + private final Set<ExecutionAttemptID> expectedDeployments = new HashSet<>(); + private int pendingDeployments = 0; + private int completedDeployments = 0; + + // metrics state + private long deploymentStart = NOT_STARTED; + private long deploymentTimeTotal = 0L; + + public DeploymentStateTimeMetrics( + JobType semantic, MetricOptions.JobStatusMetricsSettings stateTimeMetricsSettings) { + this(semantic, stateTimeMetricsSettings, SystemClock.getInstance()); + } + + @VisibleForTesting + DeploymentStateTimeMetrics( + JobType semantic, + MetricOptions.JobStatusMetricsSettings stateTimeMetricsSettings, + Clock clock) { + this.stateTimeMetricsSettings = stateTimeMetricsSettings; + this.clock = clock; + + switch (semantic) { + case BATCH: + deploymentStartPredicate = completedDeployments -> completedDeployments == 0; + deploymentEndPredicate = completedDeployments -> completedDeployments > 0; + break; + default: + deploymentStartPredicate = completedDeployments -> true; + deploymentEndPredicate = + completedDeployments -> completedDeployments == expectedDeployments.size(); + break; + } + } + + @Override + public long getCurrentTime() { + return deploymentStart == NOT_STARTED + ? 0L + : Math.max(0, clock.absoluteTimeMillis() - deploymentStart); + } + + @Override + public long getTotalTime() { + return getCurrentTime() + deploymentTimeTotal; + } + + @Override + public long getBinary() { + return deploymentStart == NOT_STARTED ? 0L : 1L; + } + + @Override + public void registerMetrics(MetricGroup metricGroup) { + StateTimeMetric.register(stateTimeMetricsSettings, metricGroup, this, "deploying"); + } + + @Override + public void onStateUpdate( + ExecutionAttemptID execution, ExecutionState previousState, ExecutionState newState) { Review comment: I'd rather formalize the state transitions elsewhere than check for them here. -- 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]
