zhijiangW commented on a change in pull request #8090: [FLINK-12067][network] 
Refactor the constructor of NetworkEnvironment
URL: https://github.com/apache/flink/pull/8090#discussion_r272008282
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/util/ConfigurationParserUtils.java
 ##########
 @@ -0,0 +1,159 @@
+/*
+ * 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.util;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.IllegalConfigurationException;
+import org.apache.flink.configuration.MemorySize;
+import org.apache.flink.configuration.TaskManagerOptions;
+import org.apache.flink.core.memory.MemoryType;
+import org.apache.flink.runtime.memory.MemoryManager;
+import org.apache.flink.util.MathUtils;
+
+import static org.apache.flink.configuration.MemorySize.MemoryUnit.MEGA_BYTES;
+import static org.apache.flink.util.MathUtils.checkedDownCast;
+
+/**
+ * Utility class to extract related parameters from {@link Configuration} and 
to
+ * sanity check them.
+ */
+public class ConfigurationParserUtils {
+
+       /**
+        * Parses the configuration to get the managed memory size and 
validates the value.
+        *
+        * @param configuration configuration object
+        * @return managed memory size (in megabytes)
+        */
+       public static long getManagedMemorySize(Configuration configuration) {
+               long managedMemorySize;
+               String managedMemorySizeDefaultVal = 
TaskManagerOptions.MANAGED_MEMORY_SIZE.defaultValue();
+               if 
(!configuration.getString(TaskManagerOptions.MANAGED_MEMORY_SIZE).equals(managedMemorySizeDefaultVal))
 {
+                       try {
+                               managedMemorySize = MemorySize.parse(
+                                       
configuration.getString(TaskManagerOptions.MANAGED_MEMORY_SIZE), 
MEGA_BYTES).getMebiBytes();
+                       } catch (IllegalArgumentException e) {
+                               throw new IllegalConfigurationException("Could 
not read " + TaskManagerOptions.MANAGED_MEMORY_SIZE.key(), e);
+                       }
+               } else {
+                       managedMemorySize = 
Long.valueOf(managedMemorySizeDefaultVal);
+               }
+
+               checkConfigParameter(configuration.getString(
+                       
TaskManagerOptions.MANAGED_MEMORY_SIZE).equals(TaskManagerOptions.MANAGED_MEMORY_SIZE.defaultValue())
 || managedMemorySize > 0,
+                       managedMemorySize, 
TaskManagerOptions.MANAGED_MEMORY_SIZE.key(),
+                       "MemoryManager needs at least one MB of memory. " +
+                               "If you leave this config parameter empty, the 
system automatically pick a fraction of the available memory.");
+
+               return managedMemorySize;
+       }
+
+       /**
+        * Parses the configuration to get the fraction of managed memory and 
validates the value.
+        *
+        * @param configuration configuration object
+        * @return fraction of managed memory
+        */
+       public static float getManagedMemoryFraction(Configuration 
configuration) {
+               float managedMemoryFraction = 
configuration.getFloat(TaskManagerOptions.MANAGED_MEMORY_FRACTION);
+
+               checkConfigParameter(managedMemoryFraction > 0.0f && 
managedMemoryFraction < 1.0f, managedMemoryFraction,
+                       TaskManagerOptions.MANAGED_MEMORY_FRACTION.key(),
+                       "MemoryManager fraction of the free memory must be 
between 0.0 and 1.0");
+
+               return managedMemoryFraction;
+       }
+
+       /**
+        * Parses the configuration to get the type of memory.
+        *
+        * @param configuration configuration object
+        * @return type of memory
+        */
+       public static MemoryType getMemoryType(Configuration configuration) {
+               // check whether we use heap or off-heap memory
+               final MemoryType memType;
+               if 
(configuration.getBoolean(TaskManagerOptions.MEMORY_OFF_HEAP)) {
+                       memType = MemoryType.OFF_HEAP;
+               } else {
+                       memType = MemoryType.HEAP;
+               }
+               return memType;
+       }
+
+       /**
+        * Parses the configuration to get the page size and validates the 
value.
+        *
+        * @param configuration configuration object
+        * @return size of memory segment
+        */
+       public static int getPageSize(Configuration configuration) {
 
 Review comment:
   I assumed this size would also be used in `MemoryManager`, but actually it 
is only used in `NetworkEnvironmentConfiguration` atm. Then I could remove it 
inside.

----------------------------------------------------------------
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

Reply via email to