This is an automated email from the ASF dual-hosted git repository.

sewen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 4a361e17fc5c1cfa957920da8a92556af81ad141
Author: Stephan Ewen <se...@apache.org>
AuthorDate: Tue Feb 18 09:53:16 2020 +0100

    [FLINK-14086][core] (follow-up) Clean-up / unify processor and memory 
architecture enums
    
    This closes #9768
---
 .../apache/flink/util/ProcessorArchitecture.java   | 142 ++++++++++++++-------
 .../flink/util/ProcessorArchitectureTest.java      |  13 +-
 .../tests/PrometheusReporterEndToEndITCase.java    |  34 ++---
 .../network/partition/ResultPartitionFactory.java  |   3 +-
 4 files changed, 118 insertions(+), 74 deletions(-)

diff --git 
a/flink-core/src/main/java/org/apache/flink/util/ProcessorArchitecture.java 
b/flink-core/src/main/java/org/apache/flink/util/ProcessorArchitecture.java
index 47e3094..9b34eca 100644
--- a/flink-core/src/main/java/org/apache/flink/util/ProcessorArchitecture.java
+++ b/flink-core/src/main/java/org/apache/flink/util/ProcessorArchitecture.java
@@ -19,79 +19,133 @@
 package org.apache.flink.util;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
 /**
- * The memory architecture (32 bit / 64 bit) of the current process.
- * Note that this might be different than the actual operating system's 
architecture, for example
- * when installing a 32 bit JRE in a 64 bit OS.
+ * The processor architecture of the this system.
+ *
+ * <p>Note that the memory address size might be different than the actual 
hardware architecture,
+ * due to the installed OS (32bit OS) or when installing a 32 bit JRE in a 64 
bit OS.
  */
 public enum ProcessorArchitecture {
+
+       /**
+        * The Intel x86 processor architecture.
+        */
+       X86(MemoryAddressSize._32_BIT, "x86", "i386", "i486", "i586", "i686"),
+
        /**
-        * X86 platform.
+        * The AMD 64 bit processor architecture.
         */
-       X86,
+       AMD64(MemoryAddressSize._64_BIT, "amd64", "x86_64"),
 
        /**
-        * arm platform.
+        * The ARM 32 bit processor architecture.
         */
-       ARM,
+       ARMv7(MemoryAddressSize._32_BIT, "armv7", "arm"),
 
        /**
-        * 32 bit memory address size.
+        * The 64 bit ARM processor architecture.
         */
-       _32_BIT,
+       AARCH64(MemoryAddressSize._64_BIT, "aarch64"),
+
+       /**
+        * Unknown architecture, could not be determined. This one 
conservatively assumes 32 bit,
+        * because 64 bit platforms typically support 32 bit memory spaces.
+        */
+       UNKNOWN(MemoryAddressSize._32_BIT, "unknown");
+
+       // 
------------------------------------------------------------------------
+
+       private static final ProcessorArchitecture CURRENT = 
readArchFromSystemProperties();
+
+       private final MemoryAddressSize addressSize;
+
+       private final String name;
+
+       private final List<String> alternativeNames;
+
+       ProcessorArchitecture(MemoryAddressSize addressSize, String name, 
String... alternativeNames) {
+               this.addressSize = addressSize;
+               this.name = name;
+               this.alternativeNames = 
Collections.unmodifiableList(Arrays.asList(alternativeNames));
+       }
+
+       /**
+        * Gets the address size of the memory (32 bit, 64 bit).
+        */
+       public MemoryAddressSize getAddressSize() {
+               return addressSize;
+       }
 
        /**
-        * 64 bit memory address size.
+        * Gets the primary name of the processor architecture.
+        * The primary name would for example be "x86" or "amd64".
         */
-       _64_BIT,
+       public String getArchitectureName() {
+               return name;
+       }
 
        /**
-        * Unknown architecture, could not be determined.
+        * Gets the alternative names for the processor architecture.
+        * Alternative names are for example "i586" for "x86", or "x86_64" for 
"amd64".
         */
-       UNKNOWN;
+       public List<String> getAlternativeNames() {
+               return alternativeNames;
+       }
+
+       // 
------------------------------------------------------------------------
 
+       /**
+        * Gets the ProcessorArchitecture of the system running this process.
+        */
+       public static ProcessorArchitecture getProcessorArchitecture() {
+               return CURRENT;
+       }
 
-       private static final ProcessorArchitecture arch = 
readArchFromSystemProperties();
-       private static final ProcessorArchitecture size = 
readSizeFromSystemProperties();
+       /**
+        * Gets the MemorySize of the ProcessorArchitecture of this process.
+        *
+        * <p>Note that the memory address size might be different than the 
actual hardware architecture,
+        * due to the installed OS (32bit OS) or when installing a 32 bit JRE 
in a 64 bit OS.
+        */
+       public static MemoryAddressSize getMemoryAddressSize() {
+               return getProcessorArchitecture().getAddressSize();
+       }
 
        private static ProcessorArchitecture readArchFromSystemProperties() {
-               final List<String> namesX86 = Arrays.asList("amd64", "x86_64", 
"x86", "i386", "i486", "i586", "i686");
-               final List<String> namesArm = Arrays.asList("arm", "aarch64");
-               final String arch = System.getProperty("os.arch");
-
-               if (namesX86.contains(arch)) {
-                       return X86;
-               } else if (namesArm.contains(arch)) {
-                       return ARM;
-               } else {
+               final String sysArchName = System.getProperty("os.arch");
+               if (sysArchName == null) {
                        return UNKNOWN;
                }
-       }
 
-       private static ProcessorArchitecture readSizeFromSystemProperties() {
-               // putting these into the method to avoid having objects on the 
heap that are not needed
-               // any more after initialization
-               final List<String> names64bit = Arrays.asList("amd64", 
"x86_64", "aarch64");
-               final List<String> names32bit = Arrays.asList("x86", "i386", 
"i486", "i586", "i686");
-               final String arch = System.getProperty("os.arch");
-
-               if (names64bit.contains(arch)) {
-                       return _64_BIT;
-               } else if (names32bit.contains(arch)) {
-                       return _32_BIT;
-               } else {
-                       return UNKNOWN;
+               for (ProcessorArchitecture arch : values()) {
+                       if (sysArchName.equalsIgnoreCase(arch.name)) {
+                               return arch;
+                       }
+
+                       for (String altName : arch.alternativeNames) {
+                               if (sysArchName.equalsIgnoreCase(altName)) {
+                                       return arch;
+                               }
+                       }
                }
-       }
 
-       public static ProcessorArchitecture getCurrentOperatingSystemArch() {
-               return arch;
+               return UNKNOWN;
        }
 
-       public static ProcessorArchitecture getCurrentOperatingSystemSize() {
-               return size;
-       }
+       // 
------------------------------------------------------------------------
 
+       /**
+        * The memory address size of the processor.
+        */
+       public enum MemoryAddressSize {
+
+               /** 32 bit memory address size. */
+               _32_BIT,
+
+               /** 64 bit memory address size. */
+               _64_BIT,
+       }
 }
diff --git 
a/flink-core/src/test/java/org/apache/flink/util/ProcessorArchitectureTest.java 
b/flink-core/src/test/java/org/apache/flink/util/ProcessorArchitectureTest.java
index 369b913..9ec2c74 100644
--- 
a/flink-core/src/test/java/org/apache/flink/util/ProcessorArchitectureTest.java
+++ 
b/flink-core/src/test/java/org/apache/flink/util/ProcessorArchitectureTest.java
@@ -21,6 +21,7 @@ package org.apache.flink.util;
 import org.junit.Test;
 
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
 
 /**
  * Tests for the {@link ProcessorArchitecture}.
@@ -29,10 +30,14 @@ public class ProcessorArchitectureTest {
 
        @Test
        public void testArchitectureNotUnknown() {
-               final ProcessorArchitecture arch = 
ProcessorArchitecture.getCurrentOperatingSystemArch();
-               final ProcessorArchitecture size = 
ProcessorArchitecture.getCurrentOperatingSystemSize();
-
+               final ProcessorArchitecture arch = 
ProcessorArchitecture.getProcessorArchitecture();
                assertNotEquals(ProcessorArchitecture.UNKNOWN, arch);
-               assertNotEquals(ProcessorArchitecture.UNKNOWN, size);
+       }
+
+       @Test
+       public void testNamesNotNull() {
+               final ProcessorArchitecture arch = 
ProcessorArchitecture.getProcessorArchitecture();
+               assertNotNull(arch.getArchitectureName());
+               assertNotNull(arch.getAlternativeNames());
        }
 }
diff --git 
a/flink-end-to-end-tests/flink-metrics-reporter-prometheus-test/src/test/java/org/apache/flink/metrics/prometheus/tests/PrometheusReporterEndToEndITCase.java
 
b/flink-end-to-end-tests/flink-metrics-reporter-prometheus-test/src/test/java/org/apache/flink/metrics/prometheus/tests/PrometheusReporterEndToEndITCase.java
index c70a5198..fe8fe10 100644
--- 
a/flink-end-to-end-tests/flink-metrics-reporter-prometheus-test/src/test/java/org/apache/flink/metrics/prometheus/tests/PrometheusReporterEndToEndITCase.java
+++ 
b/flink-end-to-end-tests/flink-metrics-reporter-prometheus-test/src/test/java/org/apache/flink/metrics/prometheus/tests/PrometheusReporterEndToEndITCase.java
@@ -82,32 +82,18 @@ public class PrometheusReporterEndToEndITCase extends 
TestLogger {
                                os = "linux";
                                break;
                }
-               switch (ProcessorArchitecture.getCurrentOperatingSystemArch()) {
+               switch (ProcessorArchitecture.getProcessorArchitecture()) {
                        case X86:
-                               switch 
(ProcessorArchitecture.getCurrentOperatingSystemSize()) {
-                                       case _32_BIT:
-                                               platform = "386";
-                                               break;
-                                       case _64_BIT:
-                                               platform = "amd64";
-                                               break;
-                                       default:
-                                               platform = "Unknown";
-                                               break;
-                               }
+                               platform = "386";
                                break;
-                       case ARM:
-                               switch 
(ProcessorArchitecture.getCurrentOperatingSystemSize()) {
-                                       case _32_BIT:
-                                               platform = "armv7";
-                                               break;
-                                       case _64_BIT:
-                                               platform = "arm64";
-                                               break;
-                                       default:
-                                               platform = "Unknown";
-                                               break;
-                               }
+                       case AMD64:
+                               platform = "amd64";
+                               break;
+                       case ARMv7:
+                               platform = "armv7";
+                               break;
+                       case AARCH64:
+                               platform = "arm64";
                                break;
                        default:
                                platform = "Unknown";
diff --git 
a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/ResultPartitionFactory.java
 
b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/ResultPartitionFactory.java
index 604164d..e924bd0 100755
--- 
a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/ResultPartitionFactory.java
+++ 
b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/ResultPartitionFactory.java
@@ -220,12 +220,11 @@ public class ResultPartitionFactory {
        }
 
        static BoundedBlockingSubpartitionType getBoundedBlockingType() {
-               switch (ProcessorArchitecture.getCurrentOperatingSystemSize()) {
+               switch (ProcessorArchitecture.getMemoryAddressSize()) {
                        case _64_BIT:
                                return 
BoundedBlockingSubpartitionType.FILE_MMAP;
                        case _32_BIT:
                                return BoundedBlockingSubpartitionType.FILE;
-                       case UNKNOWN:
                        default:
                                LOG.warn("Cannot determine memory architecture. 
Using pure file-based shuffle.");
                                return BoundedBlockingSubpartitionType.FILE;

Reply via email to