zhuzhurk commented on a change in pull request #17952: URL: https://github.com/apache/flink/pull/17952#discussion_r764648448
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/adaptivebatch/DefaultVertexParallelismDecider.java ########## @@ -0,0 +1,133 @@ +/* + * 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.scheduler.adaptivebatch; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.JobManagerOptions; +import org.apache.flink.configuration.MemorySize; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** Default implementation of {@link VertexParallelismDecider}. */ +public class DefaultVertexParallelismDecider implements VertexParallelismDecider { + + private static final Logger LOG = + LoggerFactory.getLogger(DefaultVertexParallelismDecider.class); + + /** + * The cap ratio of broadcast bytes to data volume per task. The cap ratio is 0.5 currently + * because we usually expect the broadcast dataset to be smaller than non-broadcast. We can make + * it configurable later if we see users requesting for it. + */ + private static final double CAP_RATIO_OF_BROADCAST = 0.5; + + private final int maxParallelism; + private final int minParallelism; + private final long dataVolumePerTask; + private final int defaultSourceParallelism; + + private DefaultVertexParallelismDecider( + int maxParallelism, + int minParallelism, + MemorySize dataVolumePerTask, + int defaultSourceParallelism) { + + checkArgument(minParallelism > 0, "Invalid minimum parallelism."); + checkArgument( + maxParallelism >= minParallelism, + "Maximum parallelism should greater than minimum parallelism."); + checkArgument(defaultSourceParallelism > 0, "Invalid default source parallelism."); + checkNotNull(dataVolumePerTask); + + this.maxParallelism = maxParallelism; + this.minParallelism = minParallelism; + this.dataVolumePerTask = dataVolumePerTask.getBytes(); + this.defaultSourceParallelism = defaultSourceParallelism; + } + + @Override + public int decideParallelismForVertex(List<BlockingResultInfo> consumedResults) { + + if (consumedResults.isEmpty()) { + // source job vertex + return defaultSourceParallelism; + } else { + return calculateParallelism(consumedResults); + } + } + + private int calculateParallelism(List<BlockingResultInfo> consumedResults) { + + long broadcastBytes = + consumedResults.stream() + .filter(BlockingResultInfo::isBroadcast) + .mapToLong( + consumedResult -> + consumedResult.getBlockingPartitionSizes().stream() + .reduce(0L, Long::sum)) + .sum(); + + long nonBroadcastBytes = + consumedResults.stream() + .filter(consumedResult -> !consumedResult.isBroadcast()) + .mapToLong( + consumedResult -> + consumedResult.getBlockingPartitionSizes().stream() + .reduce(0L, Long::sum)) + .sum(); + + long expectedMaxBroadcastBytes = + (long) Math.ceil((dataVolumePerTask * CAP_RATIO_OF_BROADCAST)); + + if (broadcastBytes > expectedMaxBroadcastBytes) { + LOG.info( + "The number of broadcast bytes: {} is larger than the expected maximum value: {} ('{}' * {})." + + " Use the expected maximum value as the number of broadcast bytes to decide the parallelism.", + broadcastBytes, + expectedMaxBroadcastBytes, + JobManagerOptions.ADAPTIVE_BATCH_SCHEDULER_DATA_VOLUME_PER_TASK.key(), + CAP_RATIO_OF_BROADCAST); + + broadcastBytes = expectedMaxBroadcastBytes; + } + + int parallelism = + (int) Math.ceil((double) nonBroadcastBytes / (dataVolumePerTask - broadcastBytes)); + parallelism = Math.max(parallelism, minParallelism); Review comment: Maybe log info about the revision of the parallelism? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
