Skip to content

Commit

Permalink
Fix integration tests failure (#2067) (#2090)
Browse files Browse the repository at this point in the history
Fixed integration tests failure on Linux with Kernel 5.16.x

Signed-off-by: Andrey Pleskach <[email protected]>
(cherry picked from commit 27ed6fc)

Co-authored-by: Andrey Pleskach <[email protected]>
  • Loading branch information
1 parent ff4af0f commit 55eb372
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
29 changes: 23 additions & 6 deletions server/src/main/java/org/opensearch/monitor/os/OsProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -401,6 +402,7 @@ private OsStats.Cgroup.CpuStat getCgroupCpuAcctCpuStat(final String controlGroup
long numberOfPeriods = -1;
long numberOfTimesThrottled = -1;
long timeThrottledNanos = -1;

for (final String line : lines) {
final String[] fields = line.split("\\s+");
switch (fields[0]) {
Expand All @@ -415,9 +417,17 @@ private OsStats.Cgroup.CpuStat getCgroupCpuAcctCpuStat(final String controlGroup
break;
}
}
assert numberOfPeriods != -1;
assert numberOfTimesThrottled != -1;
assert timeThrottledNanos != -1;
if (isCpuStatWarningsLogged.getAndSet(true) == false) {
if (numberOfPeriods == -1) {
logger.warn("Expected to see nr_periods filed but found nothing");
}
if (numberOfTimesThrottled == -1) {
logger.warn("Expected to see nr_throttled filed but found nothing");
}
if (timeThrottledNanos == -1) {
logger.warn("Expected to see throttled_time filed but found nothing");
}
}
return new OsStats.Cgroup.CpuStat(numberOfPeriods, numberOfTimesThrottled, timeThrottledNanos);
}

Expand All @@ -440,7 +450,7 @@ private OsStats.Cgroup.CpuStat getCgroupCpuAcctCpuStat(final String controlGroup
@SuppressForbidden(reason = "access /sys/fs/cgroup/cpu")
List<String> readSysFsCgroupCpuAcctCpuStat(final String controlGroup) throws IOException {
final List<String> lines = Files.readAllLines(PathUtils.get("/sys/fs/cgroup/cpu", controlGroup, "cpu.stat"));
assert lines != null && lines.size() == 3;
assert lines != null && lines.isEmpty() == false;
return lines;
}

Expand Down Expand Up @@ -588,11 +598,18 @@ public static OsProbe getInstance() {
return OsProbeHolder.INSTANCE;
}

OsProbe() {
private final Logger logger;

private AtomicBoolean isCpuStatWarningsLogged = new AtomicBoolean(false);

OsProbe() {
this(LogManager.getLogger(OsProbe.class));
}

private final Logger logger = LogManager.getLogger(getClass());
/*For testing purpose*/
OsProbe(final Logger logger) {
this.logger = logger;
}

OsInfo osInfo(long refreshInterval, int allocatedProcessors) throws IOException {
return new OsInfo(
Expand Down
27 changes: 27 additions & 0 deletions server/src/test/java/org/opensearch/monitor/os/OsProbeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,17 @@
import java.util.Locale;
import java.util.stream.Collectors;

import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.Constants;
import org.opensearch.test.OpenSearchTestCase;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class OsProbeTests extends OpenSearchTestCase {

public void testOsInfo() throws IOException {
Expand Down Expand Up @@ -277,6 +285,24 @@ public void testCgroupProbeWithMissingMemory() {
assertNull(cgroup);
}

public void testLogWarnCpuMessageOnlyOnes() {
final Logger logger = mock(Logger.class);

final OsProbe noCpuStatsOsProbe = new OsProbe(logger) {
@Override
List<String> readSysFsCgroupCpuAcctCpuStat(String controlGroup) throws IOException {
return Collections.singletonList("nr_periods 1");
}
};

noCpuStatsOsProbe.osStats();
// no nr_throttled and throttled_time
verify(logger, times(2)).warn(anyString());
reset(logger);
noCpuStatsOsProbe.osStats();
verify(logger, never()).warn(anyString());
}

private static List<String> getProcSelfGroupLines(String hierarchy) {
return Arrays.asList(
"10:freezer:/",
Expand Down Expand Up @@ -361,4 +387,5 @@ boolean areCgroupStatsAvailable() {
}
};
}

}

0 comments on commit 55eb372

Please sign in to comment.