Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -100,7 +101,7 @@ public SystemResourceUsage getBrokerHostUsage() {

@Override
public void calculateBrokerHostUsage() {
List<String> nics = getNics();
List<String> nics = getUsablePhysicalNICs();
double totalNicLimit = getTotalNicLimitKbps(nics);
double totalNicUsageTx = getTotalNicUsageTxKb(nics);
double totalNicUsageRx = getTotalNicUsageRxKb(nics);
Expand Down Expand Up @@ -212,9 +213,11 @@ private ResourceUsage getMemUsage() {
return new ResourceUsage(total - free, total);
}

private List<String> getNics() {
private List<String> getUsablePhysicalNICs() {
try (Stream<Path> stream = Files.list(Paths.get("/sys/class/net/"))) {
return stream.filter(this::isPhysicalNic).map(path -> path.getFileName().toString())
return stream.filter(this::isPhysicalNic)
.filter(this::isUsable)
.map(path -> path.getFileName().toString())
.collect(Collectors.toList());
} catch (IOException e) {
log.error("Failed to find NICs", e);
Expand All @@ -223,7 +226,7 @@ private List<String> getNics() {
}

public int getNicCount() {
return getNics().size();
return getUsablePhysicalNICs().size();
}

private boolean isPhysicalNic(Path path) {
Expand All @@ -241,6 +244,25 @@ private boolean isPhysicalNic(Path path) {
}
}

private boolean isUsable(Path nicPath) {
try {
String operstate = readTrimStringFromFile(nicPath.resolve("operstate"));
Operstate operState = Operstate.valueOf(operstate.toUpperCase(Locale.ROOT));
switch (operState) {
case UP:
case UNKNOWN:
case DORMANT:
return true;
default:
return false;
}
} catch (Exception e) {
log.warn("[LinuxInfo] Failed to read {} NIC operstate, the detail is: {}", nicPath, e.getMessage());
// Read operstate got error.
return false;
}
}

private Path getNicSpeedPath(String nic) {
return Paths.get(String.format("/sys/class/net/%s/speed", nic));
}
Expand Down Expand Up @@ -294,4 +316,32 @@ private double getTotalNicUsageTxKb(List<String> nics) {
private static long readLongFromFile(String path) throws IOException {
return Long.parseLong(new String(Files.readAllBytes(Paths.get(path)), Charsets.UTF_8).trim());
}

private static String readTrimStringFromFile(Path path) throws IOException {
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim();
}


/**
* TLV IFLA_OPERSTATE
* contains RFC2863 state of the interface in numeric representation:
* See <a href="https://www.kernel.org/doc/Documentation/networking/operstates.txt">...</a>
*/
enum Operstate {
// Interface is in unknown state, neither driver nor userspace has set
// operational state. Interface must be considered for user data as
// setting operational state has not been implemented in every driver.
UNKNOWN,
// Interface is unable to transfer data on L1, f.e. ethernet is not
// plugged or interface is ADMIN down.
DOWN,
// Interfaces stacked on an interface that is IF_OPER_DOWN show this
// state (f.e. VLAN).
LOWERLAYERDOWN,
// Interface is L1 up, but waiting for an external event, f.e. for a
// protocol to establish. (802.1X)
DORMANT,
// Interface is operational up and can be used.
UP
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

@Test(groups = "broker")
public class LoadReportNetworkLimitTest extends MockedPulsarServiceBaseTest {
int nicCount;
int usableNicCount;

@BeforeClass
@Override
Expand All @@ -39,7 +39,7 @@ public void setup() throws Exception {
super.internalSetup();

if (SystemUtils.IS_OS_LINUX) {
nicCount = new LinuxBrokerHostUsageImpl(pulsar).getNicCount();
usableNicCount = new LinuxBrokerHostUsageImpl(pulsar).getNicCount();
}
}

Expand All @@ -56,8 +56,8 @@ public void checkLoadReportNicSpeed() throws Exception {
LoadManagerReport report = admin.brokerStats().getLoadReport();

if (SystemUtils.IS_OS_LINUX) {
assertEquals(report.getBandwidthIn().limit, nicCount * 5.4 * 1000 * 1000);
assertEquals(report.getBandwidthOut().limit, nicCount * 5.4 * 1000 * 1000);
assertEquals(report.getBandwidthIn().limit, usableNicCount * 5.4 * 1000 * 1000);
assertEquals(report.getBandwidthOut().limit, usableNicCount * 5.4 * 1000 * 1000);
} else {
// On non-Linux system we don't report the network usage
assertEquals(report.getBandwidthIn().limit, -1.0);
Expand Down