This is an automated email from the ASF dual-hosted git repository. diwu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push: new 8d65a627058 [Fix](show-frontends-disk)Fix NPE and macOS compatibility (#25565) 8d65a627058 is described below commit 8d65a627058bd7a4a18a10591619b6c0d585b144 Author: Calvin Kirs <acm_mas...@163.com> AuthorDate: Thu Oct 19 09:53:43 2023 +0800 [Fix](show-frontends-disk)Fix NPE and macOS compatibility (#25565) --- .../java/org/apache/doris/common/io/DiskUtils.java | 97 ++++++++++++++-------- 1 file changed, 63 insertions(+), 34 deletions(-) diff --git a/fe/fe-common/src/main/java/org/apache/doris/common/io/DiskUtils.java b/fe/fe-common/src/main/java/org/apache/doris/common/io/DiskUtils.java index e7b0ea414bd..4861f40fe8a 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/common/io/DiskUtils.java +++ b/fe/fe-common/src/main/java/org/apache/doris/common/io/DiskUtils.java @@ -23,18 +23,16 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; @Slf4j public class DiskUtils { public static class Df { public String fileSystem = ""; - public long blocks; - public long used; - public long available; - public int useRate; + public long blocks = 0L; + public long used = 0L; + public long available = 0L; + public int useRate = 0; public String mountedOn = ""; } @@ -46,52 +44,83 @@ public class DiskUtils { return df; } + Process process; try { - process = Runtime.getRuntime().exec("df " + dir); + process = Runtime.getRuntime().exec("df -k " + dir); InputStream inputStream = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - + Df df = new Df(); // Filesystem 1K-blocks Used Available Use% Mounted on // /dev/sdc 5814186096 5814169712 0 100% /home/spy-sd/sdc String titleLine = reader.readLine(); String dataLine = reader.readLine(); if (titleLine == null || dataLine == null) { - return null; + return df; } - String[] values = dataLine.split("\\s+"); - if (values.length != 6) { - return null; + String[] dfValues = dataLine.split("\\s+"); + if (os.startsWith("Mac")) { + parseMacOSDiskInfo(dfValues, df); + } else { + parseLinuxDiskInfo(dfValues, df); } - - Df df = new Df(); - df.fileSystem = values[0]; - df.blocks = Long.parseLong(values[1]); - df.used = Long.parseLong(values[2]); - df.available = Long.parseLong(values[3]); - df.useRate = Integer.parseInt(values[4].replace("%", "")); - df.mountedOn = values[5]; return df; } catch (IOException e) { log.info("failed to obtain disk information", e); - return null; + return new Df(); } } - private static List<String> getTitles(String titlesLine) { - List<String> titles = new ArrayList<>(); - String[] titleArray = titlesLine.split("\\s+"); - for (String title : titleArray) { - if (title.equalsIgnoreCase("on")) { - if (!titles.isEmpty()) { - int lastIdx = titles.size() - 1; - titles.set(lastIdx, titles.get(lastIdx) + "On"); - } - } else { - titles.add(title); - } + /** + * Linux df -k output + * Filesystem 1K-blocks Used Available Use% Mounted on + * /dev/sda1 8256952 2094712 5742232 27% / + */ + private static void parseLinuxDiskInfo(String[] dfValues, Df df) { + if (dfValues.length != 6) { + return; + } + df.fileSystem = dfValues[0]; + df.blocks = parseLongValue(dfValues[1]); + df.used = parseLongValue(dfValues[2]); + df.available = parseLongValue(dfValues[3]); + df.useRate = parseIntegerValue(dfValues[4].replace("%", "")); + df.mountedOn = dfValues[5]; + } + + /** + * MacOS df -k output + * Filesystem 1024-blocks Used Available Capacity iused ifree %iused Mounted on + * /dev/disk1s1 488555536 97511104 390044432 20% 121655 4884849711 0% / + */ + private static void parseMacOSDiskInfo(String[] dfValues, Df df) { + if (dfValues.length != 9) { + return; + } + df.fileSystem = dfValues[0]; + df.blocks = Long.parseLong(dfValues[1]); + df.used = parseLongValue(dfValues[2]); + df.available = parseLongValue(dfValues[3]); + df.useRate = parseIntegerValue(dfValues[4].replace("%", "")); + df.mountedOn = dfValues[8]; + } + + private static long parseLongValue(String value) { + try { + return Long.parseLong(value); + } catch (NumberFormatException e) { + log.info("failed to parse long value", e); + return 0L; + } + } + + private static int parseIntegerValue(String value) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + log.info("failed to parse integer value", e); + return 0; } - return titles; } private static String[] units = new String[]{"", "K", "M", "G", "T", "P"}; --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org