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

org.apache.flink.runtime.healthmanager.plugins.executors.DryRunActionExecutor 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.healthmanager.plugins.executors;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.healthmanager.HealthMonitor;
import org.apache.flink.runtime.healthmanager.RestServerClient;
import org.apache.flink.runtime.healthmanager.plugins.Action;
import org.apache.flink.runtime.healthmanager.plugins.ActionExecutor;
import org.apache.flink.runtime.healthmanager.plugins.actions.AdjustJobConfig;
import org.apache.flink.runtime.healthmanager.plugins.actions.AdjustJobResource;
import org.apache.flink.runtime.healthmanager.plugins.actions.RescaleJobParallelism;
import org.apache.flink.runtime.jobgraph.JobVertexID;

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

import java.util.HashMap;
import java.util.Map;

/**
 * Dry run Action Executor.
 */
public class DryRunActionExecutor implements ActionExecutor {
	private static final Logger LOGGER = LoggerFactory.getLogger(DryRunActionExecutor.class);

	private HealthMonitor healthMonitor;

	private Configuration config;
	private RestServerClient.JobConfig lastDryRunConfig;
	private Map currentParallelism = new HashMap<>();

	@Override
	public void open(HealthMonitor monitor) {
		healthMonitor = monitor;
		config = monitor.getConfig();
		lastDryRunConfig = monitor.getJobConfig();
		for (Map.Entry entry : lastDryRunConfig.getVertexConfigs().entrySet()) {
			currentParallelism.put(entry.getKey(), entry.getValue().getParallelism());
		}
	}

	@Override
	public boolean execute(Action action) throws Throwable {

		if (action instanceof AdjustJobConfig) {
			// refine the action based on last dry run config.
			if (action instanceof AdjustJobResource) {
				for (JobVertexID vertexID : ((AdjustJobResource) action).getAffectedVertex()) {
					RestServerClient.VertexConfig currentConfig = lastDryRunConfig.getVertexConfigs().get(vertexID);
					((AdjustJobResource) action).addVertex(vertexID,
							currentConfig.getParallelism(),
							currentConfig.getParallelism(),
							currentConfig.getResourceSpec(),
							((AdjustJobResource) action).getTargetResource(vertexID));
				}
			}

			if (action instanceof RescaleJobParallelism) {
				for (JobVertexID vertexID : ((RescaleJobParallelism) action).getAffectedVertex()) {
					RestServerClient.VertexConfig currentConfig = lastDryRunConfig.getVertexConfigs().get(vertexID);
					((RescaleJobParallelism) action).addVertex(vertexID,
							currentConfig.getParallelism(),
							((RescaleJobParallelism) action).getTargetParallelism(vertexID),
							currentConfig.getResourceSpec(),
							currentConfig.getResourceSpec());
				}
			}

			AdjustJobConfig adjustJobConfig = (AdjustJobConfig) action;

			// exclude changes similar with last action.
			adjustJobConfig.excludeMinorDiffVertices(true, config);

			if (adjustJobConfig.isEmpty() || adjustJobConfig.isMinorScaleDown(lastDryRunConfig, config)) {
				// there is no big changes compared with last update.
				LOGGER.info("Action is ignored.");
				adjustJobConfig.clear();
				return true;
			}

			RestServerClient.JobConfig resultConfig = adjustJobConfig.getAppliedJobConfig(lastDryRunConfig);

			// recompute final action.
			for (Map.Entry entry : resultConfig.getVertexConfigs().entrySet()) {

				currentParallelism.put(entry.getKey(), entry.getValue().getParallelism());

				adjustJobConfig.addVertex(
						entry.getKey(),
						healthMonitor.getJobConfig().getVertexConfigs().get(entry.getKey()).getParallelism(),
						entry.getValue().getParallelism(),
						healthMonitor.getJobConfig().getVertexConfigs().get(entry.getKey()).getResourceSpec(),
						entry.getValue().getResourceSpec());
			}

			lastDryRunConfig = resultConfig;

		}
		LOGGER.info("New Action in dry run triggered: {}.", action);
		return true;
	}

	@Override
	public void close() {

	}

	public Map getCurrentParallelism() {
		return currentParallelism;
	}

	@VisibleForTesting
	public RestServerClient.JobConfig getLastDryRunConfig() {
		return lastDryRunConfig;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy