All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.resourcemanager.autoscale.plugins.detectors.ClusterBlockRequestDetector Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * 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.runtime.clusterframework.types.ResourceProfile;
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.ClusterHighBlockRequests;
import org.apache.flink.runtime.resourcemanager.autoscale.plugins.symptoms.ClusterZeroBlockRequests;
import org.apache.flink.runtime.resourcemanager.autoscale.utils.SlotManagerInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * ClusterBlockRequestDetector detects block request state of cluster.
 * Detects {@link ClusterHighBlockRequests} if the number of new block requests
 * is higher than threshold.
 * Detects {@link ClusterZeroBlockRequests} if no new block requests.
 */
public class ClusterBlockRequestDetector implements ResourceDetector {

	private static final Logger LOGGER = LoggerFactory.getLogger(ClusterBlockRequestDetector.class);

	public static final ConfigOption HIGH_BLOCK_REQUEST_THRESHOLD =
		ConfigOptions.key("session-auto-scale.cluster-block-request-detector.threashold.high").defaultValue(10L);

	private ResourceAutoScaler resourceAutoScaler;
	private double highThreshold;
	private long lastBlockNum;
	private long lastCheckTime;
	private long nowBlockNum;
	private long nowTime;

	@Override
	public void open(ResourceAutoScaler resourceAutoScaler) {
		this.resourceAutoScaler = resourceAutoScaler;
		this.highThreshold =  resourceAutoScaler.getConfig().getLong(HIGH_BLOCK_REQUEST_THRESHOLD);
		this.lastBlockNum = 0;
		this.lastCheckTime = 0;
		this.nowBlockNum = 0;
		this.nowTime = 0;
	}

	@Override
	public void close() {
	}

	@Override
	public void update(SlotManagerInfo slotManagerInfo, Collection> taskManagersProfiles) {
		nowBlockNum = slotManagerInfo.getBlockRequestsNumber();
		nowTime = System.currentTimeMillis();
	}

	@Override
	public ResourceSymptom detect() throws Exception {
		LOGGER.debug("Start detecting");

		LOGGER.debug("Cluster block requests {}.", nowBlockNum);
		if (lastCheckTime != 0 && nowTime > lastCheckTime) {
			long newBlockNum = nowBlockNum - lastBlockNum;
			if (newBlockNum >= highThreshold) {
				LOGGER.debug("Block requests number high detected for Cluster, new block {}, time gap {}",
					newBlockNum, nowTime - lastCheckTime);
				lastCheckTime = nowTime;
				lastBlockNum = nowBlockNum;
				return new ClusterHighBlockRequests(newBlockNum, nowTime - lastCheckTime);
			} else if (newBlockNum == 0) {
				LOGGER.debug("Block requests number zero detected for Cluster, new block {}, time gap {}",
					newBlockNum, nowTime - lastCheckTime);
				lastCheckTime = nowTime;
				lastBlockNum = nowBlockNum;
				return new ClusterZeroBlockRequests(nowTime - lastCheckTime);
			} else if (newBlockNum < 0) {
				LOGGER.error("New block request could not small than the previous level.");
			}
		}

		lastCheckTime = nowTime;
		lastBlockNum = nowBlockNum;
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy