org.apache.flink.runtime.resourcemanager.autoscale.plugins.detectors.ClusterMemoryUsageDetector 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.plugins.detectors;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;
import org.apache.flink.metrics.Histogram;
import org.apache.flink.runtime.clusterframework.types.ResourceProfile;
import org.apache.flink.runtime.metrics.SimpleHistogram;
import org.apache.flink.runtime.resourcemanager.autoscale.ResourceAutoScaler;
import org.apache.flink.runtime.resourcemanager.autoscale.plugins.ResourceDetector;
import org.apache.flink.runtime.resourcemanager.autoscale.plugins.ResourceSymptom;
import org.apache.flink.runtime.resourcemanager.autoscale.plugins.symptoms.ClusterHighMemory;
import org.apache.flink.runtime.resourcemanager.autoscale.plugins.symptoms.ClusterLowMemory;
import org.apache.flink.runtime.resourcemanager.autoscale.utils.SlotManagerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
/**
* ClusterMemoryUsageDetector detects cluster memory usage.
* Detects {@link ClusterHighMemory} if the max avg memory usage of cluster
* is higher than threshold.
* Detects {@link ClusterLowMemory} if the max avg memory usage of cluster
* is lower than threshold.
*/
public class ClusterMemoryUsageDetector implements ResourceDetector {
private static final Logger LOGGER = LoggerFactory.getLogger(ClusterMemoryUsageDetector.class);
public static final ConfigOption HIGH_MEM_THRESHOLD =
ConfigOptions.key("session-auto-scale.cluster-mem-usage-detector.threashold.high").defaultValue(1.0);
public static final ConfigOption LOW_MEM_THRESHOLD =
ConfigOptions.key("session-auto-scale.cluster-mem-usage-detector.threashold.low").defaultValue(0.1);
private ResourceAutoScaler resourceAutoScaler;
private int checkInterval;
private double highThreshold;
private double lowThreshold;
private Histogram clusterMemoryAvailableHistogram;
private Histogram clusterMemoryCapacityHistogram;
@Override
public void open(ResourceAutoScaler resourceAutoScaler) {
this.resourceAutoScaler = resourceAutoScaler;
checkInterval = resourceAutoScaler.getConfig().getInteger(ResourceAutoScaler.METRIC_UPDATE_INTERVAL);
highThreshold = resourceAutoScaler.getConfig().getDouble(HIGH_MEM_THRESHOLD);
lowThreshold = resourceAutoScaler.getConfig().getDouble(LOW_MEM_THRESHOLD);
clusterMemoryAvailableHistogram = new SimpleHistogram(checkInterval);
clusterMemoryCapacityHistogram = new SimpleHistogram(checkInterval);
}
@Override
public void close() {
}
@Override
public void update(SlotManagerInfo slotManagerInfo, Collection> taskManagersProfiles) {
long memoryCapacity = 0;
long memeoryAvailable = 0;
for (Tuple2 resourceProfileTuple : taskManagersProfiles) {
memoryCapacity += resourceProfileTuple.f0.getMemoryInMB();
memeoryAvailable += resourceProfileTuple.f1.getMemoryInMB();
}
clusterMemoryAvailableHistogram.update(memeoryAvailable);
clusterMemoryCapacityHistogram.update(memoryCapacity);
}
@Override
public ResourceSymptom detect() throws Exception {
LOGGER.debug("Start detecting");
double capacity = clusterMemoryCapacityHistogram.getStatistics().getMean();
double available = clusterMemoryAvailableHistogram.getStatistics().getMean();
LOGGER.debug("Cluster memory capacity {}, available {}.", capacity, available);
if (capacity == 0.0) {
LOGGER.warn("Cluster has non memory resource, capacity is 0.");
return null;
}
double utility = (capacity - available) / capacity;
if (utility >= highThreshold) {
LOGGER.debug("Memory high detected for Cluster, capacity {}, available {}, utility {}", capacity, available, utility);
return new ClusterHighMemory(available, capacity, utility);
} else if (utility <= lowThreshold) {
LOGGER.debug("Memory low detected for Cluster, capacity {}, available {}, utility {}", capacity, available, utility);
return new ClusterLowMemory(available, capacity, utility);
}
return null;
}
}