Search in sources :

Example 6 with MockLogAppender

use of org.elasticsearch.test.MockLogAppender in project crate by crate.

the class PersistedClusterStateServiceTests method assertExpectedLogs.

private void assertExpectedLogs(long currentTerm, ClusterState previousState, ClusterState clusterState, PersistedClusterStateService.Writer writer, MockLogAppender.LoggingExpectation expectation) throws IllegalAccessException, IOException {
    MockLogAppender mockAppender = new MockLogAppender();
    mockAppender.start();
    mockAppender.addExpectation(expectation);
    Logger classLogger = LogManager.getLogger(PersistedClusterStateService.class);
    Loggers.addAppender(classLogger, mockAppender);
    try {
        if (previousState == null) {
            writer.writeFullStateAndCommit(currentTerm, clusterState);
        } else {
            writer.writeIncrementalStateAndCommit(currentTerm, previousState, clusterState);
        }
    } finally {
        Loggers.removeAppender(classLogger, mockAppender);
        mockAppender.stop();
    }
    mockAppender.assertAllExpectationsMatched();
}
Also used : MockLogAppender(org.elasticsearch.test.MockLogAppender) Logger(org.apache.logging.log4j.Logger)

Example 7 with MockLogAppender

use of org.elasticsearch.test.MockLogAppender in project crate by crate.

the class CopyFromFailFastITest method assertExpectedLogMessages.

private void assertExpectedLogMessages(Runnable command, MockLogAppender.LoggingExpectation... expectations) throws IllegalAccessException {
    Logger testLogger = LogManager.getLogger("io.crate.execution.dml.upsert");
    MockLogAppender appender = new MockLogAppender();
    Loggers.addAppender(testLogger, appender);
    try {
        appender.start();
        Arrays.stream(expectations).forEach(appender::addExpectation);
        command.run();
        appender.assertAllExpectationsMatched();
    } finally {
        Loggers.removeAppender(testLogger, appender);
    }
}
Also used : MockLogAppender(org.elasticsearch.test.MockLogAppender) Logger(org.apache.logging.log4j.Logger)

Example 8 with MockLogAppender

use of org.elasticsearch.test.MockLogAppender in project crate by crate.

the class IncrementalClusterStateWriterTests method assertExpectedLogs.

private void assertExpectedLogs(ClusterState clusterState, IncrementalClusterStateWriter incrementalClusterStateWriter, MockLogAppender.LoggingExpectation expectation) throws IllegalAccessException, WriteStateException {
    MockLogAppender mockAppender = new MockLogAppender();
    mockAppender.start();
    mockAppender.addExpectation(expectation);
    Logger classLogger = LogManager.getLogger(IncrementalClusterStateWriter.class);
    Loggers.addAppender(classLogger, mockAppender);
    try {
        incrementalClusterStateWriter.updateClusterState(clusterState);
    } finally {
        Loggers.removeAppender(classLogger, mockAppender);
        mockAppender.stop();
    }
    mockAppender.assertAllExpectationsMatched();
}
Also used : MockLogAppender(org.elasticsearch.test.MockLogAppender) Logger(org.apache.logging.log4j.Logger)

Example 9 with MockLogAppender

use of org.elasticsearch.test.MockLogAppender in project elasticsearch by elastic.

the class ClusterServiceTests method testLongClusterStateUpdateLogging.

// To ensure that we log cluster state events on WARN level
@TestLogging("org.elasticsearch.cluster.service:WARN")
public void testLongClusterStateUpdateLogging() throws Exception {
    MockLogAppender mockAppender = new MockLogAppender();
    mockAppender.start();
    mockAppender.addExpectation(new MockLogAppender.UnseenEventExpectation("test1 shouldn't see because setting is too low", "org.elasticsearch.cluster.service.ClusterServiceTests$TimedClusterService", Level.WARN, "*cluster state update task [test1] took [*] above the warn threshold of *"));
    mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2", "org.elasticsearch.cluster.service.ClusterServiceTests$TimedClusterService", Level.WARN, "*cluster state update task [test2] took [32s] above the warn threshold of *"));
    mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3", "org.elasticsearch.cluster.service.ClusterServiceTests$TimedClusterService", Level.WARN, "*cluster state update task [test3] took [33s] above the warn threshold of *"));
    mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test4", "org.elasticsearch.cluster.service.ClusterServiceTests$TimedClusterService", Level.WARN, "*cluster state update task [test4] took [34s] above the warn threshold of *"));
    Logger clusterLogger = Loggers.getLogger("org.elasticsearch.cluster.service");
    Loggers.addAppender(clusterLogger, mockAppender);
    try {
        final CountDownLatch latch = new CountDownLatch(5);
        final CountDownLatch processedFirstTask = new CountDownLatch(1);
        clusterService.currentTimeOverride = System.nanoTime();
        clusterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) throws Exception {
                clusterService.currentTimeOverride += TimeValue.timeValueSeconds(1).nanos();
                return currentState;
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                latch.countDown();
                processedFirstTask.countDown();
            }

            @Override
            public void onFailure(String source, Exception e) {
                fail();
            }
        });
        processedFirstTask.await();
        clusterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) throws Exception {
                clusterService.currentTimeOverride += TimeValue.timeValueSeconds(32).nanos();
                throw new IllegalArgumentException("Testing handling of exceptions in the cluster state task");
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                fail();
            }

            @Override
            public void onFailure(String source, Exception e) {
                latch.countDown();
            }
        });
        clusterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) throws Exception {
                clusterService.currentTimeOverride += TimeValue.timeValueSeconds(33).nanos();
                return ClusterState.builder(currentState).incrementVersion().build();
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                latch.countDown();
            }

            @Override
            public void onFailure(String source, Exception e) {
                fail();
            }
        });
        clusterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) throws Exception {
                clusterService.currentTimeOverride += TimeValue.timeValueSeconds(34).nanos();
                return currentState;
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                latch.countDown();
            }

            @Override
            public void onFailure(String source, Exception e) {
                fail();
            }
        });
        // Additional update task to make sure all previous logging made it to the loggerName
        // We don't check logging for this on since there is no guarantee that it will occur before our check
        clusterService.submitStateUpdateTask("test5", new ClusterStateUpdateTask() {

            @Override
            public ClusterState execute(ClusterState currentState) {
                return currentState;
            }

            @Override
            public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                latch.countDown();
            }

            @Override
            public void onFailure(String source, Exception e) {
                fail();
            }
        });
        latch.await();
    } finally {
        Loggers.removeAppender(clusterLogger, mockAppender);
        mockAppender.stop();
    }
    mockAppender.assertAllExpectationsMatched();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) MockLogAppender(org.elasticsearch.test.MockLogAppender) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Logger(org.apache.logging.log4j.Logger) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 10 with MockLogAppender

use of org.elasticsearch.test.MockLogAppender in project crate by crate.

the class CoordinatorTests method testCannotJoinClusterWithDifferentUUID.

public void testCannotJoinClusterWithDifferentUUID() throws IllegalAccessException {
    try (Cluster cluster1 = new Cluster(randomIntBetween(1, 3))) {
        cluster1.runRandomly();
        cluster1.stabilise();
        final ClusterNode nodeInOtherCluster;
        try (Cluster cluster2 = new Cluster(3)) {
            cluster2.runRandomly();
            cluster2.stabilise();
            nodeInOtherCluster = randomFrom(cluster2.clusterNodes);
        }
        final ClusterNode newNode = cluster1.new ClusterNode(nextNodeIndex.getAndIncrement(), nodeInOtherCluster.getLocalNode(), n -> cluster1.new MockPersistedState(n, nodeInOtherCluster.persistedState, Function.identity(), Function.identity()), nodeInOtherCluster.nodeSettings);
        cluster1.clusterNodes.add(newNode);
        MockLogAppender mockAppender = new MockLogAppender();
        mockAppender.start();
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1", JoinHelper.class.getCanonicalName(), Level.INFO, "*failed to join*"));
        Logger joinLogger = LogManager.getLogger(JoinHelper.class);
        Loggers.addAppender(joinLogger, mockAppender);
        cluster1.runFor(DEFAULT_STABILISATION_TIME, "failing join validation");
        try {
            mockAppender.assertAllExpectationsMatched();
        } finally {
            Loggers.removeAppender(joinLogger, mockAppender);
            mockAppender.stop();
        }
        assertEquals(0, newNode.getLastAppliedClusterState().version());
        newNode.close();
        final ClusterNode detachedNode = newNode.restartedNode(DetachClusterCommand::updateMetadata, term -> DetachClusterCommand.updateCurrentTerm(), newNode.nodeSettings);
        cluster1.clusterNodes.replaceAll(cn -> cn == newNode ? detachedNode : cn);
        cluster1.stabilise();
    }
}
Also used : ClusterNode(org.elasticsearch.cluster.coordination.AbstractCoordinatorTestCase.Cluster.ClusterNode) MockLogAppender(org.elasticsearch.test.MockLogAppender) Logger(org.apache.logging.log4j.Logger)

Aggregations

MockLogAppender (org.elasticsearch.test.MockLogAppender)14 Logger (org.apache.logging.log4j.Logger)10 Matchers.containsString (org.hamcrest.Matchers.containsString)6 TestLogging (org.elasticsearch.test.junit.annotations.TestLogging)5 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClusterNode (org.elasticsearch.cluster.coordination.AbstractCoordinatorTestCase.Cluster.ClusterNode)3 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)3 HashSet (java.util.HashSet)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 AckedClusterStateUpdateTask (org.elasticsearch.cluster.AckedClusterStateUpdateTask)2 FailedToCommitClusterStateException (org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException)2 Matchers.hasToString (org.hamcrest.Matchers.hasToString)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1