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

wuzhiguo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/bigtop-manager.git


The following commit(s) were added to refs/heads/main by this push:
     new bfdf372d BIGTOP-4398: Fix JDK installation failure caused by arch 
replacement error (#198)
bfdf372d is described below

commit bfdf372dd661d4084338627826cd1d8cd309439e
Author: ChunFuWu <[email protected]>
AuthorDate: Mon Apr 7 17:13:27 2025 +0800

    BIGTOP-4398: Fix JDK installation failure caused by arch replacement error 
(#198)
---
 .../grpc/service/SetupJdkServiceGrpcImpl.java      | 20 ++++++--
 .../manager/common/utils/os/OSDetection.java       | 54 +++++++++++++++++++---
 2 files changed, 63 insertions(+), 11 deletions(-)

diff --git 
a/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/service/SetupJdkServiceGrpcImpl.java
 
b/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/service/SetupJdkServiceGrpcImpl.java
index 58c7f5a3..bdb0d048 100644
--- 
a/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/service/SetupJdkServiceGrpcImpl.java
+++ 
b/bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/grpc/service/SetupJdkServiceGrpcImpl.java
@@ -89,16 +89,28 @@ public class SetupJdkServiceGrpcImpl extends 
SetupJdkServiceGrpc.SetupJdkService
     }
 
     private String getPkgName(String arch) {
-        String replacedArch = arch.equals("x86_64") ? "x64" : arch;
-        replacedArch = replacedArch.equals("arm64") ? "aarch64" : arch;
-        return MessageFormat.format("jdk-8u431-linux-{0}.tar.gz", 
replacedArch);
+        String replacedArch =
+                switch (arch) {
+                    case "x86_64" -> "x64";
+                    case "arm64", "aarch64" -> "aarch64";
+                    default -> {
+                        log.error("Unsupported architecture: {}", arch);
+                        throw new IllegalArgumentException("Unsupported 
architecture: " + arch);
+                    }
+                };
+        String pkgName = MessageFormat.format("jdk-8u431-linux-{0}.tar.gz", 
replacedArch);
+        log.debug("Generated package name: {}", pkgName);
+        return pkgName;
     }
 
     private String getChecksum(String arch) {
         return switch (arch) {
             case "x64", "x86_64" -> 
"SHA-256:b396978a716b7d23ccccabfe5c47c3b75d2434d7f8f7af690bc648172382720d";
             case "arm64", "aarch64" -> 
"SHA-256:e68d3e31ffcf7f05a4de65d04974843073bdff238bb6524adb272de9e616be7c";
-            default -> throw new RuntimeException("Unknown arch for jdk: " + 
arch);
+            default -> {
+                log.error("Unknown arch for jdk: {}", arch);
+                throw new IllegalArgumentException("Unknown arch for jdk: " + 
arch);
+            }
         };
     }
 }
diff --git 
a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/os/OSDetection.java
 
b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/os/OSDetection.java
index 593e5b3a..d11fe3a7 100644
--- 
a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/os/OSDetection.java
+++ 
b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/os/OSDetection.java
@@ -27,6 +27,7 @@ import lombok.extern.slf4j.Slf4j;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -68,9 +69,18 @@ public class OSDetection {
 
     public static String getArch() {
         if (SystemUtils.IS_OS_LINUX) {
-            return getOSArch();
+            try {
+                String arch = getOSArch();
+                return standardizeArch(arch);
+            } catch (Exception e) {
+                log.warn("Failed to get OS architecture using 'arch' command, 
falling back to os.arch", e);
+                String arch = System.getProperty("os.arch");
+                return standardizeArch(arch);
+            }
         } else {
-            return System.getProperty("os.arch");
+            String arch = System.getProperty("os.arch").toLowerCase();
+            log.debug("Detected non-Linux architecture: {}", arch);
+            return standardizeArch(arch);
         }
     }
 
@@ -139,16 +149,46 @@ public class OSDetection {
     }
 
     private static String getOSArch() {
-        List<String> builderParameters = new ArrayList<>();
-        builderParameters.add("arch");
-
         try {
+            List<String> builderParameters = new ArrayList<>();
+            builderParameters.add("arch");
             ShellResult shellResult = 
ShellExecutor.execCommand(builderParameters);
             String output = shellResult.getOutput().replace("\n", "");
             log.debug("getArch: {}", output);
             return output;
-        } catch (IOException e) {
-            throw new RuntimeException(e);
+        } catch (Exception e) {
+            log.warn("Failed to execute 'arch' command, falling back to 
/proc/cpuinfo");
+            try {
+                String cpuInfo = new String(Files.readAllBytes(new 
File("/proc/cpuinfo").toPath()));
+                Pattern pattern = Pattern.compile("model name\\s*:.*?(\\w+)$", 
Pattern.MULTILINE);
+                Matcher matcher = pattern.matcher(cpuInfo);
+                if (matcher.find()) {
+                    String output = matcher.group(1).toLowerCase();
+                    log.debug("getArch: {}", output);
+                    return output;
+                }
+            } catch (IOException ex) {
+                log.error("Failed to read /proc/cpuinfo: {}", ex.getMessage(), 
ex);
+            }
+            throw new RuntimeException("Unable to detect OS architecture");
         }
     }
+
+    private static String standardizeArch(String arch) {
+        return switch (arch) {
+            case "amd64" -> "x86_64";
+            case "arm64", "aarch64" -> "aarch64";
+            case "x86" -> "i386";
+            case "arm" -> "armv7l";
+            case "ppc64le" -> "ppc64le";
+            case "s390x" -> "s390x";
+            case "riscv64" -> "riscv64";
+            case "mips" -> "mips";
+            case "mips64" -> "mips64";
+            default -> {
+                log.warn("Detected unknown architecture: {}", arch);
+                throw new UnsupportedOperationException("Unsupported 
architecture: " + arch);
+            }
+        };
+    }
 }

Reply via email to