diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java index fa18795000316..53d8d3e379da3 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java @@ -227,23 +227,18 @@ public int getNicCount() { } private boolean isPhysicalNic(Path path) { - if (!path.toString().contains("/virtual/")) { - try { - Files.readAllBytes(path.resolve("speed")); - return true; - } catch (Exception e) { - // In some cases, VMs in EC2 won't have the speed reported on the NIC and will give a read-error. - // Check the type to make sure it's ethernet (type "1") - try { - String type = new String(Files.readAllBytes(path.resolve("type")), StandardCharsets.UTF_8).trim(); - return Integer.parseInt(type) == 1; - } catch (IOException ioe) { - // wireless nics don't report speed, ignore them. - return false; - } - } + if (path.toString().contains("/virtual/")) { + return false; + } + try { + // Check the type to make sure it's ethernet (type "1") + String type = new String(Files.readAllBytes(path.resolve("type")), StandardCharsets.UTF_8).trim(); + // wireless NICs don't report speed, ignore them. + return Integer.parseInt(type) == 1; + } catch (Exception e) { + // Read type got error. + return false; } - return false; } private Path getNicSpeedPath(String nic) { @@ -254,12 +249,13 @@ private double getTotalNicLimitKbps(List nics) { // Use the override value as configured. Return the total max speed across all available NICs, converted // from Gbps into Kbps return overrideBrokerNicSpeedGbps.map(aDouble -> aDouble * nics.size() * 1024 * 1024) - .orElseGet(() -> nics.stream().mapToDouble(s -> { + .orElseGet(() -> nics.stream().mapToDouble(nicPath -> { // Nic speed is in Mbits/s, return kbits/s try { - return Double.parseDouble(new String(Files.readAllBytes(getNicSpeedPath(s)))); + return Double.parseDouble(new String(Files.readAllBytes(getNicSpeedPath(nicPath)))); } catch (IOException e) { - log.error("Failed to read speed for nic " + s, e); + log.error(String.format("Failed to read speed for nic %s, maybe you can set broker" + + " config [loadBalancerOverrideBrokerNicSpeedGbps] to override it.", nicPath), e); return 0d; } }).sum() * 1024);