org.apache.flink.runtime.resourcemanager.autoscale.utils.HotUpdateConfiguration 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.resourcemanager.autoscale.utils;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.resourcemanager.autoscale.ResourceAutoScaler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Configuration supported in hot update of {@link org.apache.flink.runtime.resourcemanager.autoscale.ResourceAutoScaler}.
*/
public class HotUpdateConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(
HotUpdateConfiguration.class);
private int maxTaskExecutors;
private int minTaskExecutors;
private int desireTaskExecutors;
public HotUpdateConfiguration(
int maxTaskExecutors,
int minTaskExecutors,
int desireTaskExecutors) {
this.maxTaskExecutors = maxTaskExecutors;
this.minTaskExecutors = minTaskExecutors;
this.desireTaskExecutors = desireTaskExecutors;
}
public static HotUpdateConfiguration fromConfiguration(Configuration configuration) {
if (!checkSanity(configuration)) {
LOG.warn("The updated configuration are not legal. Will not trigger hot update.");
return new HotUpdateConfiguration(-1, -1, -1);
}
int maxTaskExecutors = -1;
if (configuration.contains(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MAX_TM)) {
maxTaskExecutors = configuration.getInteger(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MAX_TM);
if (maxTaskExecutors < 0) {
maxTaskExecutors = -1;
}
}
int minTaskExecutors = -1;
if (configuration.contains(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MIN_TM)) {
minTaskExecutors = configuration.getInteger(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MIN_TM);
if (minTaskExecutors < 0) {
minTaskExecutors = -1;
}
}
int desireTaskExecutors = -1;
if (configuration.contains(ResourceAutoScaler.RESOURCE_DESIRE_TM)) {
desireTaskExecutors = configuration.getInteger(ResourceAutoScaler.RESOURCE_DESIRE_TM);
if (desireTaskExecutors < 0) {
desireTaskExecutors = -1;
}
}
return new HotUpdateConfiguration(maxTaskExecutors, minTaskExecutors, desireTaskExecutors);
}
private static boolean checkSanity(Configuration configuration) {
if (configuration.contains(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MAX_TM)
&& !StringUtils.isNumeric(configuration.getValue(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MAX_TM))) {
return false;
}
if (configuration.contains(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MIN_TM)
&& !StringUtils.isNumeric(configuration.getValue(ResourceAutoScaler.RESOURCE_LIMIT_CLUSTER_MIN_TM))) {
return false;
}
if (configuration.contains(ResourceAutoScaler.RESOURCE_DESIRE_TM)
&& !StringUtils.isNumeric(configuration.getValue(ResourceAutoScaler.RESOURCE_DESIRE_TM))) {
return false;
}
return true;
}
public boolean shouldTriggerRescale() {
return containMaxTaskExecutors() || containMinTaskExecutors() || containDesireTaskExecutors();
}
public int getMaxTaskExecutors() {
return maxTaskExecutors;
}
public int getDesireTaskExecutors() {
return desireTaskExecutors;
}
public int getMinTaskExecutors() {
return minTaskExecutors;
}
public boolean containMaxTaskExecutors() {
return maxTaskExecutors != -1;
}
public boolean containMinTaskExecutors() {
return minTaskExecutors != -1;
}
public boolean containDesireTaskExecutors() {
return desireTaskExecutors != -1;
}
}