[LoadBalance] Optimize find nics process.
@@ -227,24 +227,19 @@ public class LinuxBrokerHostUsageImpl implements BrokerHostUsage {
}
private boolean isPhysicalNic(Path path) {
- if (!path.toString().contains("/virtual/")) {
+ if (path.toString().contains("/virtual/")) {
+ return false;
+ }
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();
+ // wireless NICs don't report speed, ignore them.
return Integer.parseInt(type) == 1;
- } catch (IOException ioe) {
- // wireless nics don't report speed, ignore them.
+ } catch (Exception e) {
+ // Read type got error.
return false;
}
}
- }
- return false;
- }
private Path getNicSpeedPath(String nic) {
return Paths.get(String.format("/sys/class/net/%s/speed", nic));
@@ -254,12 +249,13 @@ public class LinuxBrokerHostUsageImpl implements BrokerHostUsage {
// 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);
