From 27ed6fc82c7db7a3a741499f0dbd7722fa053f9d Mon Sep 17 00:00:00 2001 From: Andrey Pleskach Date: Fri, 11 Feb 2022 15:40:04 +0100 Subject: [PATCH] Fix integration tests failure (#2067) Fixed integration tests failure on Linux with Kernel 5.16.x Signed-off-by: Andrey Pleskach --- .../org/opensearch/monitor/os/OsProbe.java | 29 +++++++++++++++---- .../opensearch/monitor/os/OsProbeTests.java | 27 +++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/org/opensearch/monitor/os/OsProbe.java b/server/src/main/java/org/opensearch/monitor/os/OsProbe.java index 5097d1b0f4c..eda86c49539 100644 --- a/server/src/main/java/org/opensearch/monitor/os/OsProbe.java +++ b/server/src/main/java/org/opensearch/monitor/os/OsProbe.java @@ -51,6 +51,7 @@ import java.util.HashMap; 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; @@ -401,6 +402,7 @@ public class OsProbe { long numberOfPeriods = -1; long numberOfTimesThrottled = -1; long timeThrottledNanos = -1; + for (final String line : lines) { final String[] fields = line.split("\\s+"); switch (fields[0]) { @@ -415,9 +417,17 @@ public class OsProbe { 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); } @@ -440,7 +450,7 @@ public class OsProbe { @SuppressForbidden(reason = "access /sys/fs/cgroup/cpu") List readSysFsCgroupCpuAcctCpuStat(final String controlGroup) throws IOException { final List 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; } @@ -588,11 +598,18 @@ public class OsProbe { 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( diff --git a/server/src/test/java/org/opensearch/monitor/os/OsProbeTests.java b/server/src/test/java/org/opensearch/monitor/os/OsProbeTests.java index 140143ad4e0..a9df203c49a 100644 --- a/server/src/test/java/org/opensearch/monitor/os/OsProbeTests.java +++ b/server/src/test/java/org/opensearch/monitor/os/OsProbeTests.java @@ -50,9 +50,17 @@ import java.util.List; 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 { @@ -277,6 +285,24 @@ public class OsProbeTests extends OpenSearchTestCase { assertNull(cgroup); } + public void testLogWarnCpuMessageOnlyOnes() { + final Logger logger = mock(Logger.class); + + final OsProbe noCpuStatsOsProbe = new OsProbe(logger) { + @Override + List 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 getProcSelfGroupLines(String hierarchy) { return Arrays.asList( "10:freezer:/", @@ -361,4 +387,5 @@ public class OsProbeTests extends OpenSearchTestCase { } }; } + }