Search in sources :

Example 6 with MockLogAppender

use of org.opensearch.test.MockLogAppender in project OpenSearch by opensearch-project.

the class FsHealthServiceTests method testLoggingOnHungIO.

@TestLogging(value = "org.opensearch.monitor.fs:WARN", reason = "to ensure that we log on hung IO at WARN level")
public void testLoggingOnHungIO() throws Exception {
    long slowLogThreshold = randomLongBetween(100, 200);
    final Settings settings = Settings.builder().put(FsHealthService.SLOW_PATH_LOGGING_THRESHOLD_SETTING.getKey(), slowLogThreshold + "ms").build();
    FileSystem fileSystem = PathUtils.getDefaultFileSystem();
    TestThreadPool testThreadPool = new TestThreadPool(getClass().getName(), settings);
    FileSystemFsyncHungProvider disruptFileSystemProvider = new FileSystemFsyncHungProvider(fileSystem, randomLongBetween(slowLogThreshold + 1, 400), testThreadPool);
    fileSystem = disruptFileSystemProvider.getFileSystem(null);
    PathUtilsForTesting.installMock(fileSystem);
    final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(FsHealthService.class));
        NodeEnvironment env = newNodeEnvironment()) {
        FsHealthService fsHealthService = new FsHealthService(settings, clusterSettings, testThreadPool, env);
        int counter = 0;
        for (Path path : env.nodeDataPaths()) {
            mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test" + ++counter, FsHealthService.class.getCanonicalName(), Level.WARN, "health check of [" + path + "] took [*ms] which is above the warn threshold*"));
        }
        // disrupt file system
        disruptFileSystemProvider.injectIODelay.set(true);
        fsHealthService.new FsHealthMonitor().run();
        assertEquals(env.nodeDataPaths().length, disruptFileSystemProvider.getInjectedPathCount());
        assertBusy(mockAppender::assertAllExpectationsMatched);
    } finally {
        PathUtilsForTesting.teardown();
        ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
    }
}
Also used : Path(java.nio.file.Path) ClusterSettings(org.opensearch.common.settings.ClusterSettings) MockLogAppender(org.opensearch.test.MockLogAppender) NodeEnvironment(org.opensearch.env.NodeEnvironment) TestThreadPool(org.opensearch.threadpool.TestThreadPool) FileSystem(java.nio.file.FileSystem) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) TestLogging(org.opensearch.test.junit.annotations.TestLogging)

Example 7 with MockLogAppender

use of org.opensearch.test.MockLogAppender in project OpenSearch by opensearch-project.

the class InboundHandlerTests method testLogsSlowInboundProcessing.

public void testLogsSlowInboundProcessing() throws Exception {
    try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(InboundHandler.class))) {
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("expected message", InboundHandler.class.getCanonicalName(), Level.WARN, "handling inbound transport message "));
        handler.setSlowLogThreshold(TimeValue.timeValueMillis(5L));
        final Version remoteVersion = Version.CURRENT;
        final long requestId = randomNonNegativeLong();
        final Header requestHeader = new Header(between(0, 100), requestId, TransportStatus.setRequest(TransportStatus.setHandshake((byte) 0)), remoteVersion);
        final InboundMessage requestMessage = new InboundMessage(requestHeader, ReleasableBytesReference.wrap(BytesArray.EMPTY), () -> {
            try {
                TimeUnit.SECONDS.sleep(1L);
            } catch (InterruptedException e) {
                throw new AssertionError(e);
            }
        });
        requestHeader.actionName = TransportHandshaker.HANDSHAKE_ACTION_NAME;
        requestHeader.headers = Tuple.tuple(Collections.emptyMap(), Collections.emptyMap());
        requestHeader.features = org.opensearch.common.collect.Set.of();
        handler.inboundMessage(channel, requestMessage);
        assertNotNull(channel.getMessageCaptor().get());
        mockAppender.assertAllExpectationsMatched();
    }
}
Also used : MockLogAppender(org.opensearch.test.MockLogAppender) Version(org.opensearch.Version)

Example 8 with MockLogAppender

use of org.opensearch.test.MockLogAppender in project OpenSearch by opensearch-project.

the class TransportLoggerTests method testLoggingHandler.

public void testLoggingHandler() throws Exception {
    try (MockLogAppender appender = MockLogAppender.createForLoggers(LogManager.getLogger(TransportLogger.class))) {
        final String writePattern = ".*\\[length: \\d+" + ", request id: \\d+" + ", type: request" + ", version: .*" + ", header size: \\d+B" + ", action: cluster:monitor/stats]" + " WRITE: \\d+B";
        final MockLogAppender.LoggingExpectation writeExpectation = new MockLogAppender.PatternSeenEventExpectation("hot threads request", TransportLogger.class.getCanonicalName(), Level.TRACE, writePattern);
        final String readPattern = ".*\\[length: \\d+" + ", request id: \\d+" + ", type: request" + ", version: .*" + ", header size: \\d+B" + ", action: cluster:monitor/stats]" + " READ: \\d+B";
        final MockLogAppender.LoggingExpectation readExpectation = new MockLogAppender.PatternSeenEventExpectation("cluster monitor request", TransportLogger.class.getCanonicalName(), Level.TRACE, readPattern);
        appender.addExpectation(writeExpectation);
        appender.addExpectation(readExpectation);
        BytesReference bytesReference = buildRequest();
        TransportLogger.logInboundMessage(mock(TcpChannel.class), bytesReference.slice(6, bytesReference.length() - 6));
        TransportLogger.logOutboundMessage(mock(TcpChannel.class), bytesReference);
        appender.assertAllExpectationsMatched();
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) MockLogAppender(org.opensearch.test.MockLogAppender)

Example 9 with MockLogAppender

use of org.opensearch.test.MockLogAppender in project OpenSearch by opensearch-project.

the class NodeConnectionsServiceTests method testDebugLogging.

@TestLogging(reason = "testing that DEBUG-level logging is reasonable", value = "org.opensearch.cluster.NodeConnectionsService:DEBUG")
public void testDebugLogging() throws IllegalAccessException {
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(builder().put(NODE_NAME_SETTING.getKey(), "node").build(), random());
    MockTransport transport = new MockTransport(deterministicTaskQueue.getThreadPool());
    TestTransportService transportService = new TestTransportService(transport, deterministicTaskQueue.getThreadPool());
    transportService.start();
    transportService.acceptIncomingRequests();
    final NodeConnectionsService service = new NodeConnectionsService(Settings.EMPTY, deterministicTaskQueue.getThreadPool(), transportService);
    service.start();
    final List<DiscoveryNode> allNodes = generateNodes();
    final DiscoveryNodes targetNodes = discoveryNodesFromList(randomSubsetOf(allNodes));
    service.connectToNodes(targetNodes, () -> {
    });
    deterministicTaskQueue.runAllRunnableTasks();
    // periodic reconnections to unexpectedly-disconnected nodes are logged
    final Set<DiscoveryNode> disconnectedNodes = new HashSet<>(randomSubsetOf(allNodes));
    for (DiscoveryNode disconnectedNode : disconnectedNodes) {
        transportService.disconnectFromNode(disconnectedNode);
    }
    final Logger logger = LogManager.getLogger("org.opensearch.cluster.NodeConnectionsService");
    try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
        for (DiscoveryNode targetNode : targetNodes) {
            if (disconnectedNodes.contains(targetNode)) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connecting to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connected to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            } else {
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connecting to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connected to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            }
        }
        runTasksUntil(deterministicTaskQueue, CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.get(Settings.EMPTY).millis());
        appender.assertAllExpectationsMatched();
    }
    for (DiscoveryNode disconnectedNode : disconnectedNodes) {
        transportService.disconnectFromNode(disconnectedNode);
    }
    // changes to the expected set of nodes are logged, including reconnections to any unexpectedly-disconnected nodes
    final DiscoveryNodes newTargetNodes = discoveryNodesFromList(randomSubsetOf(allNodes));
    for (DiscoveryNode disconnectedNode : disconnectedNodes) {
        transportService.disconnectFromNode(disconnectedNode);
    }
    try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
        for (DiscoveryNode targetNode : targetNodes) {
            if (disconnectedNodes.contains(targetNode) && newTargetNodes.get(targetNode.getId()) != null) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connecting to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connected to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            } else {
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connecting to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.UnseenEventExpectation("connected to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            }
            if (newTargetNodes.get(targetNode.getId()) == null) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("disconnected from " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "disconnected from " + targetNode));
            }
        }
        for (DiscoveryNode targetNode : newTargetNodes) {
            appender.addExpectation(new MockLogAppender.UnseenEventExpectation("disconnected from " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "disconnected from " + targetNode));
            if (targetNodes.get(targetNode.getId()) == null) {
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connecting to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connecting to " + targetNode));
                appender.addExpectation(new MockLogAppender.SeenEventExpectation("connected to " + targetNode, "org.opensearch.cluster.NodeConnectionsService", Level.DEBUG, "connected to " + targetNode));
            }
        }
        service.disconnectFromNodesExcept(newTargetNodes);
        service.connectToNodes(newTargetNodes, () -> {
        });
        deterministicTaskQueue.runAllRunnableTasks();
        appender.assertAllExpectationsMatched();
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockLogAppender(org.opensearch.test.MockLogAppender) Logger(org.apache.logging.log4j.Logger) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet) TestLogging(org.opensearch.test.junit.annotations.TestLogging)

Example 10 with MockLogAppender

use of org.opensearch.test.MockLogAppender in project OpenSearch by opensearch-project.

the class MaxMapCountCheckTests method testGetMaxMapCount.

public void testGetMaxMapCount() throws IOException, IllegalAccessException {
    final long procSysVmMaxMapCount = randomIntBetween(1, Integer.MAX_VALUE);
    final BufferedReader reader = mock(BufferedReader.class);
    when(reader.readLine()).thenReturn(Long.toString(procSysVmMaxMapCount));
    final Path procSysVmMaxMapCountPath = PathUtils.get("/proc/sys/vm/max_map_count");
    BootstrapChecks.MaxMapCountCheck check = new BootstrapChecks.MaxMapCountCheck() {

        @Override
        BufferedReader getBufferedReader(Path path) throws IOException {
            assertEquals(path, procSysVmMaxMapCountPath);
            return reader;
        }
    };
    assertThat(check.getMaxMapCount(), equalTo(procSysVmMaxMapCount));
    verify(reader).close();
    {
        reset(reader);
        final IOException ioException = new IOException("fatal");
        when(reader.readLine()).thenThrow(ioException);
        final Logger logger = LogManager.getLogger("testGetMaxMapCountIOException");
        try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
            appender.addExpectation(new ParameterizedMessageLoggingExpectation("expected logged I/O exception", "testGetMaxMapCountIOException", Level.WARN, "I/O exception while trying to read [{}]", new Object[] { procSysVmMaxMapCountPath }, e -> ioException == e));
            assertThat(check.getMaxMapCount(logger), equalTo(-1L));
            appender.assertAllExpectationsMatched();
            verify(reader).close();
        }
    }
    {
        reset(reader);
        when(reader.readLine()).thenReturn("eof");
        final Logger logger = LogManager.getLogger("testGetMaxMapCountNumberFormatException");
        try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
            appender.addExpectation(new ParameterizedMessageLoggingExpectation("expected logged number format exception", "testGetMaxMapCountNumberFormatException", Level.WARN, "unable to parse vm.max_map_count [{}]", new Object[] { "eof" }, e -> e instanceof NumberFormatException && e.getMessage().equals("For input string: \"eof\"")));
            assertThat(check.getMaxMapCount(logger), equalTo(-1L));
            appender.assertAllExpectationsMatched();
            verify(reader).close();
        }
    }
}
Also used : Path(java.nio.file.Path) MockLogAppender(org.opensearch.test.MockLogAppender) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Logger(org.apache.logging.log4j.Logger)

Aggregations

MockLogAppender (org.opensearch.test.MockLogAppender)29 Logger (org.apache.logging.log4j.Logger)11 TestLogging (org.opensearch.test.junit.annotations.TestLogging)10 Matchers.containsString (org.hamcrest.Matchers.containsString)8 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)5 OpenSearchException (org.opensearch.OpenSearchException)4 ClusterState (org.opensearch.cluster.ClusterState)4 ClusterSettings (org.opensearch.common.settings.ClusterSettings)4 Settings (org.opensearch.common.settings.Settings)4 Path (java.nio.file.Path)3 HashSet (java.util.HashSet)3 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 LogEvent (org.apache.logging.log4j.core.LogEvent)3 Version (org.opensearch.Version)3 ClusterNode (org.opensearch.cluster.coordination.AbstractCoordinatorTestCase.Cluster.ClusterNode)3 IOException (java.io.IOException)2 InetSocketAddress (java.net.InetSocketAddress)2 UnknownHostException (java.net.UnknownHostException)2