org.apache.flink.runtime.healthmanager.plugins.actionselectors.RescaleResourcePriorActionSelector 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.actionselectors;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;
import org.apache.flink.runtime.healthmanager.HealthMonitor;
import org.apache.flink.runtime.healthmanager.plugins.Action;
import org.apache.flink.runtime.healthmanager.plugins.ActionSelector;
import org.apache.flink.runtime.healthmanager.plugins.actions.AdjustJobResource;
import org.apache.flink.runtime.healthmanager.plugins.actions.RescaleJobParallelism;
import org.apache.flink.runtime.healthmanager.plugins.utils.HealthMonitorOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* RescaleResourcePriorActionSelector prioritize combing and selecting AdjustJobResource actions
* than RescaleJobParallelism actions.
*/
public class RescaleResourcePriorActionSelector implements ActionSelector {
private static final Logger LOGGER = LoggerFactory.getLogger(RescaleResourcePriorActionSelector.class);
public static final ConfigOption ACTION_BLACK_LIST_INTERVAL =
ConfigOptions.key("action.selector.blacklist.interval.ms")
.defaultValue(1800_000L);
public static final ConfigOption ACTION_RESCALE_RESOURCE_FIRST =
ConfigOptions.key("action.selector.rescale.resource.first")
.defaultValue(false);
public static final ConfigOption ACTION_WAIT_CHECKPOINT =
ConfigOptions.key("action.selector.wait.checkpoint")
.defaultValue(true);
private Action lastFailedAction;
private long lastFailedTime;
private long blacklistThreshold;
private HealthMonitor monitor;
private boolean rescaleResourceFirst;
private boolean waitCheckpoint;
@Override
public void open(HealthMonitor monitor) {
this.blacklistThreshold = monitor.getConfig().getLong(ACTION_BLACK_LIST_INTERVAL);
this.rescaleResourceFirst = monitor.getConfig().getBoolean(ACTION_RESCALE_RESOURCE_FIRST);
this.waitCheckpoint = monitor.getConfig().getBoolean(ACTION_WAIT_CHECKPOINT);
this.monitor = monitor;
}
@Override
public void close() {
}
@Override
public Action accept(List actions) {
AdjustJobResource adjustJobResource = null;
RescaleJobParallelism rescaleJobParallelism = null;
Action firstOtherAction = null;
for (Action action: actions) {
if (action instanceof AdjustJobResource) {
if (adjustJobResource == null) {
adjustJobResource = (AdjustJobResource) action;
} else {
adjustJobResource = ((AdjustJobResource) action).merge(adjustJobResource);
}
continue;
} else if (action instanceof RescaleJobParallelism) {
rescaleJobParallelism = (RescaleJobParallelism) action;
} else if (firstOtherAction == null) {
firstOtherAction = action;
}
}
if (lastFailedAction != null && System.currentTimeMillis() - lastFailedTime < blacklistThreshold) {
if (rescaleJobParallelism != null && lastFailedAction instanceof RescaleJobParallelism) {
LOGGER.info("Ignore action because last failed action is the same type.");
rescaleJobParallelism = null;
}
if (adjustJobResource != null && lastFailedAction instanceof AdjustJobResource) {
LOGGER.info("Ignore action because last failed action is the same type.");
adjustJobResource = null;
}
if (firstOtherAction != null && firstOtherAction.getClass().equals(lastFailedAction.getClass())) {
LOGGER.info("Ignore action because last failed action is the same type.");
firstOtherAction = null;
}
}
if (!monitor.getConfig().getBoolean(HealthMonitorOptions.ENABLE_MINOR_SCALE_DOWN)){
if (adjustJobResource != null && adjustJobResource.isMinorScaleDown(monitor.getJobConfig(), monitor.getConfig())) {
adjustJobResource = null;
}
if (rescaleJobParallelism != null && rescaleJobParallelism.isMinorScaleDown(monitor.getJobConfig(), monitor.getConfig())) {
rescaleJobParallelism = null;
}
}
if (rescaleResourceFirst) {
if (adjustJobResource != null && adjustJobResource.getActionMode().executeImmediate()) {
if (!waitCheckpoint) {
adjustJobResource.setActionMode(Action.ActionMode.IMMEDIATE);
}
return adjustJobResource;
}
}
if (rescaleJobParallelism != null && rescaleJobParallelism.getActionMode().executeImmediate()){
if (!waitCheckpoint) {
rescaleJobParallelism.setActionMode(Action.ActionMode.IMMEDIATE);
}
return rescaleJobParallelism;
}
if (adjustJobResource != null && adjustJobResource.getActionMode().executeImmediate()) {
if (!waitCheckpoint) {
adjustJobResource.setActionMode(Action.ActionMode.IMMEDIATE);
}
return adjustJobResource;
}
if (firstOtherAction != null) {
return firstOtherAction;
}
return null;
}
@Override
public void actionFailed(Action action) {
lastFailedAction = action;
lastFailedTime = System.currentTimeMillis();
}
@Override
public void actionSucceed(Action action) {
lastFailedAction = null;
}
}