tillrohrmann commented on a change in pull request #9760: [FLINK-13982][runtime] Implement memory calculation logics URL: https://github.com/apache/flink/pull/9760#discussion_r329055349
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/clusterframework/TaskExecutorResourceUtils.java ########## @@ -0,0 +1,333 @@ +/* + * 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.clusterframework; + +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.api.java.tuple.Tuple6; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.MemorySize; +import org.apache.flink.configuration.TaskManagerOptions; +import org.apache.flink.util.ConfigurationException; + +import static org.apache.flink.util.Preconditions.checkArgument; + +/** + * Utility class for TaskExecutor memory configurations. + */ +public class TaskExecutorResourceUtils { + + private TaskExecutorResourceUtils() {} + + // ------------------------------------------------------------------------ + // Memory Configuration Calculations + // ------------------------------------------------------------------------ + + public static TaskExecutorResourceSpec resourceSpecFromConfig(final Configuration config) throws ConfigurationException { + if (isTaskHeapMemorySizeExplicitlyConfigured(config) && isManagedMemorySizeExplicitlyConfigured(config)) { + // both task heap memory and managed memory is configured, use these to derive total flink memory + return deriveResourceSpecWithExplicitTaskAndManagedMemory(config); + } else if (isTotalFlinkMemorySizeExplicitlyConfigured(config)) { + // total flink memory is configured, but not task heap and managed memory, derive from total flink memory + return deriveResourceSpecWithTotalFlinkMemory(config); + } else if (isTotalProcessMemorySizeExplicitlyConfigured(config)) { + return deriveResourceSpecWithTotalProcessMemory(config); + } else { + throw new ConfigurationException("Either Task Heap Memory size and Managed Memory size, or Total Flink" + + " Memory size, or Total Process Memory size need to be configured explicitly."); + } + } + + private static TaskExecutorResourceSpec deriveResourceSpecWithExplicitTaskAndManagedMemory(final Configuration config) { + // derive total flink internal memory sizes from explicitly configure task heap memory size and managed memory size + + final MemorySize frameworkHeapMemorySize = getFrameworkHeapMemorySize(config); + final MemorySize taskHeapMemorySize = getTaskHeapMemorySize(config); + final MemorySize taskOffHeapMemorySize = getTaskOffHeapMemorySize(config); + + final MemorySize managedMemorySize = getManagedMemorySize(config); + final Tuple2<MemorySize, MemorySize> managedMemorySizeTuple2 = deriveOnHeapAndOffHeapManagedMemorySizeFromManagedMemorySize(config, managedMemorySize); + final MemorySize onHeapManagedMemorySize = managedMemorySizeTuple2.f0; + final MemorySize offHeapManagedMemorySize = managedMemorySizeTuple2.f1; + + final MemorySize shuffleMemorySize = deriveShuffleMemoryWithInverseFraction(config, + frameworkHeapMemorySize.add(taskHeapMemorySize).add(taskOffHeapMemorySize).add(managedMemorySize)); + + // derive total flink external memory sizes from derived total flink memory size + + final MemorySize totalFlinkMemorySize = frameworkHeapMemorySize + .add(taskHeapMemorySize) + .add(taskOffHeapMemorySize) + .add(shuffleMemorySize) + .add(managedMemorySize); + + final Tuple2<MemorySize, MemorySize> totalFlinkExternalMemorySizeTuple2 = deriveTotalFlinkExternalMemorySizes(config, totalFlinkMemorySize); + + return new TaskExecutorResourceSpec( + frameworkHeapMemorySize, + taskHeapMemorySize, + taskOffHeapMemorySize, + shuffleMemorySize, + onHeapManagedMemorySize, + offHeapManagedMemorySize, + totalFlinkExternalMemorySizeTuple2.f0, // jvmMetaspaceSize + totalFlinkExternalMemorySizeTuple2.f1); // jvmOverheadSize + } + + private static TaskExecutorResourceSpec deriveResourceSpecWithTotalFlinkMemory(final Configuration config) { + // derive total flink internal memory sizes from explicitly configured total flink memory size + + final MemorySize totalFlinkMemorySize = getTotalFlinkMemorySize(config); + final Tuple6<MemorySize, MemorySize, MemorySize, MemorySize, MemorySize, MemorySize> totalFLinkInternalMemorySizeTuple6 = Review comment: I would suggest to not use `Tuples` for the sake of maintainability. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services