org.apache.flink.runtime.healthmanager.plugins.resolvers.DryRunParallelismResolver Maven / Gradle / Ivy
/*
* 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.healthmanager.plugins.resolvers;
import org.apache.flink.runtime.healthmanager.HealthMonitor;
import org.apache.flink.runtime.healthmanager.plugins.Resolver;
import org.apache.flink.runtime.healthmanager.plugins.Symptom;
import org.apache.flink.runtime.healthmanager.plugins.actions.RescaleJobParallelism;
import org.apache.flink.runtime.healthmanager.plugins.executors.DryRunActionExecutor;
import org.apache.flink.runtime.healthmanager.plugins.symptoms.DryRunCheckSignal;
import org.apache.flink.runtime.healthmanager.plugins.utils.HealthMonitorOptions;
import org.apache.flink.runtime.healthmanager.plugins.utils.TaskMetrics;
import org.apache.flink.runtime.jobgraph.JobVertexID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Resolver for dry run.
*/
public class DryRunParallelismResolver implements Resolver {
private static final Logger LOGGER = LoggerFactory.getLogger(DryRunParallelismResolver.class);
private HealthMonitor monitor;
private ParallelismScaler parallelismScaler = new ParallelismScaler();
private ParallelismResolverUtils resolverUtils;
private double scaleTpsRatio;
private double maxRatio;
private double minDiffParallelismRatio;
@Override
public void open(HealthMonitor monitor) {
this.monitor = monitor;
this.parallelismScaler.open(monitor);
this.resolverUtils = new ParallelismResolverUtils(monitor.getJobTopologyAnalyzer(), monitor.getConfig());
this.scaleTpsRatio = monitor.getConfig().getDouble(HealthMonitorOptions.PARALLELISM_MIN_RATIO);
this.maxRatio = monitor.getConfig().getDouble(HealthMonitorOptions.PARALLELISM_MAX_RATIO);
this.minDiffParallelismRatio = monitor.getConfig().getDouble(HealthMonitorOptions.PARALLELISM_SCALE_MIN_DIFF_RATIO);
}
@Override
public void close() {
parallelismScaler.close();
}
@Override
public RescaleJobParallelism resolve(List symptomList) {
boolean triggered = false;
for (Symptom symptom : symptomList) {
if (symptom == DryRunCheckSignal.INSTANCE) {
triggered = true;
break;
}
}
if (!triggered) {
return null;
}
parallelismScaler.diagnose(symptomList);
Map taskMetrics = parallelismScaler.getTaskMetrics();
if (taskMetrics == null || !parallelismScaler.canRescale(taskMetrics)) {
// only work with task metrics.
LOGGER.debug("Can not rescale now, task metrics {}", taskMetrics);
return null;
}
for (JobVertexID vertexID : taskMetrics.keySet()) {
LOGGER.debug("Task Metrics {}", taskMetrics.get(vertexID));
if (taskMetrics.get(vertexID).getWorkload() < 0) {
LOGGER.debug("Can not rescale for vertex {} workload is invalid.", vertexID);
return null;
}
}
Map targetWorkload =
resolverUtils.estimateVertexWorkload(taskMetrics);
LOGGER.debug("Estimated target workload: {}", targetWorkload);
Map minParallelisms = parallelismScaler.getMinParallelism(taskMetrics);
LOGGER.debug("min parallelism: {}", minParallelisms);
DryRunActionExecutor dryRunActionExecutor = (DryRunActionExecutor) monitor.getActionExecutor();
Map currentParallelism = dryRunActionExecutor.getCurrentParallelism();
LOGGER.debug("current parallelism: {}", currentParallelism);
Set vertexToRescale = new HashSet<>();
for (JobVertexID vertexID : targetWorkload.keySet()) {
// vertex need to scale up.
if (currentParallelism.get(vertexID) < targetWorkload.get(vertexID) * (1 + minDiffParallelismRatio)) {
vertexToRescale.add(vertexID);
}
// vertex need to scale down.
if (currentParallelism.get(vertexID) > Math.ceil(taskMetrics.get(vertexID).getWorkload() * maxRatio) &&
currentParallelism.get(vertexID) - minParallelisms.get(vertexID) > Math.ceil(currentParallelism.get(vertexID) * minDiffParallelismRatio)) {
vertexToRescale.add(vertexID);
}
}
LOGGER.debug("rescaling {}", vertexToRescale);
Map targetParallelisms =
getVertexTargetParallelisms(targetWorkload, vertexToRescale);
LOGGER.debug("target parallelism before apply constraint {}", targetParallelisms);
parallelismScaler.updateTargetParallelismsSubjectToConstraints(
targetParallelisms,
minParallelisms,
monitor.getJobConfig());
LOGGER.debug("target parallelism after apply constraint {}", targetParallelisms);
return parallelismScaler.generateRescaleParallelismAction(
targetParallelisms, minParallelisms, monitor.getJobConfig());
}
private Map getVertexTargetParallelisms(Map targetStatus, Set vertexToRescale) {
Map result = new HashMap<>();
for (JobVertexID vertexID : vertexToRescale) {
result.put(vertexID, (int) Math.ceil(targetStatus.get(vertexID) * scaleTpsRatio));
}
return result;
}
}