|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.autoscaler; |
| 19 | + |
| 20 | +import org.apache.flink.autoscaler.event.AutoScalerEventHandler; |
| 21 | +import org.apache.flink.autoscaler.topology.ShipStrategy; |
| 22 | +import org.apache.flink.runtime.jobgraph.JobVertexID; |
| 23 | + |
| 24 | +import static org.apache.flink.autoscaler.JobVertexScaler.SCALE_LIMITED_MESSAGE_FORMAT; |
| 25 | +import static org.apache.flink.autoscaler.JobVertexScaler.SCALING_LIMITED; |
| 26 | +import static org.apache.flink.autoscaler.config.AutoScalerOptions.SCALING_EVENT_INTERVAL; |
| 27 | + |
| 28 | +/** |
| 29 | + * Component responsible adjusts the parallelism of a vertex. |
| 30 | + * |
| 31 | + * <p>When input vertex {@link ShipStrategy} is {@link ShipStrategy#HASH} or knows the number of |
| 32 | + * current partitions of vertex. We hope to adjust the parallelism of the current vertex according |
| 33 | + * to the number of key groups or partitions to achieve the goal of evenly distributing data among |
| 34 | + * subtasks or maximizing utilization. |
| 35 | + */ |
| 36 | +public class ParallelismAdjuster { |
| 37 | + |
| 38 | + public static <KEY, Context extends JobAutoScalerContext<KEY>> |
| 39 | + int adjustViaNumKeyGroupsOrPartitions( |
| 40 | + JobVertexID vertex, |
| 41 | + Context context, |
| 42 | + AutoScalerEventHandler<KEY, Context> eventHandler, |
| 43 | + int numKeyGroupsOrPartitions, |
| 44 | + int newParallelism, |
| 45 | + int upperBound, |
| 46 | + int parallelismLowerLimit) { |
| 47 | + var upperBoundForAlignment = |
| 48 | + Math.min( |
| 49 | + // Optimize the case where newParallelism <= maxParallelism / 2 |
| 50 | + newParallelism > numKeyGroupsOrPartitions / 2 |
| 51 | + ? numKeyGroupsOrPartitions |
| 52 | + : numKeyGroupsOrPartitions / 2, |
| 53 | + upperBound); |
| 54 | + |
| 55 | + // When the shuffle type of vertex inputs contains keyBy or vertex is a source, |
| 56 | + // we try to adjust the parallelism such that it divides |
| 57 | + // the numKeyGroupsOrPartitions without a remainder => data is evenly spread across subtasks |
| 58 | + for (int p = newParallelism; p <= upperBoundForAlignment; p++) { |
| 59 | + if (numKeyGroupsOrPartitions % p == 0) { |
| 60 | + return p; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // When adjusting the parallelism after rounding up cannot |
| 65 | + // find the right parallelism to meet requirements, |
| 66 | + // Try to find the smallest parallelism that can satisfy the current consumption rate. |
| 67 | + int p = |
| 68 | + calculateMinimumParallelism( |
| 69 | + numKeyGroupsOrPartitions, newParallelism, parallelismLowerLimit); |
| 70 | + var message = |
| 71 | + String.format( |
| 72 | + SCALE_LIMITED_MESSAGE_FORMAT, |
| 73 | + vertex, |
| 74 | + newParallelism, |
| 75 | + p, |
| 76 | + numKeyGroupsOrPartitions, |
| 77 | + upperBound, |
| 78 | + parallelismLowerLimit); |
| 79 | + eventHandler.handleEvent( |
| 80 | + context, |
| 81 | + AutoScalerEventHandler.Type.Warning, |
| 82 | + SCALING_LIMITED, |
| 83 | + message, |
| 84 | + SCALING_LIMITED + vertex + newParallelism, |
| 85 | + context.getConfiguration().get(SCALING_EVENT_INTERVAL)); |
| 86 | + return p; |
| 87 | + } |
| 88 | + |
| 89 | + private static int calculateMinimumParallelism( |
| 90 | + int numKeyGroupsOrPartitions, int newParallelism, int parallelismLowerLimit) { |
| 91 | + int p = newParallelism; |
| 92 | + for (; p > 0; p--) { |
| 93 | + if (numKeyGroupsOrPartitions / p > numKeyGroupsOrPartitions / newParallelism) { |
| 94 | + if (numKeyGroupsOrPartitions % p != 0) { |
| 95 | + p++; |
| 96 | + } |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + p = Math.max(p, parallelismLowerLimit); |
| 101 | + return p; |
| 102 | + } |
| 103 | +} |
0 commit comments