Skip to content

Commit

Permalink
[LoadBalance] Optimize find nics process. (apache#14340)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattisonchao committed Feb 18, 2022
1 parent 5123d8a commit 77d60b3
Showing 1 changed file with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -254,12 +249,13 @@ private double getTotalNicLimitKbps(List<String> 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);
Expand Down

0 comments on commit 77d60b3

Please sign in to comment.