Use try-with-resources with MockLogAppender (#1595)

I previously added a helper that started a MockLogAppender to ensure it
was never added to a Logger before it was started. I subsequently found
the opposite case in RolloverIT.java where the appender was stopped
before it was closed, therefore creating a race where a concurrently
running test in the same JVM could cause a logging failure. This seems
like a really easy mistake to make when writing a test or introduce when
refactoring a test. I've made a change to use try-with-resources to
ensure that proper setup and teardown is done. This should make it much
harder to introduce this particular test bug in the future.
Unfortunately, it did involve touching a lot of files. The changes here
are purely structural to leverage try-with-resources; no testing logic
has been changed.

Signed-off-by: Andrew Ross <andrross@amazon.com>
This commit is contained in:
Andrew Ross 2021-12-21 15:00:55 -06:00 committed by GitHub
parent ca40ba9c64
commit 96d55966a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 839 additions and 1021 deletions

View File

@ -36,7 +36,6 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.opensearch.OpenSearchNetty4IntegTestCase; import org.opensearch.OpenSearchNetty4IntegTestCase;
import org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest; import org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest;
import org.opensearch.common.logging.Loggers;
import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.InternalTestCluster; import org.opensearch.test.InternalTestCluster;
import org.opensearch.test.MockLogAppender; import org.opensearch.test.MockLogAppender;
@ -53,17 +52,15 @@ public class OpenSearchLoggingHandlerIT extends OpenSearchNetty4IntegTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
appender = MockLogAppender.createStarted(); appender = MockLogAppender.createForLoggers(
Loggers.addAppender(LogManager.getLogger(OpenSearchLoggingHandler.class), appender); LogManager.getLogger(OpenSearchLoggingHandler.class),
Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender); LogManager.getLogger(TransportLogger.class),
Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender); LogManager.getLogger(TcpTransport.class)
);
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {
Loggers.removeAppender(LogManager.getLogger(OpenSearchLoggingHandler.class), appender); appender.close();
Loggers.removeAppender(LogManager.getLogger(TransportLogger.class), appender);
Loggers.removeAppender(LogManager.getLogger(TcpTransport.class), appender);
appender.stop();
super.tearDown(); super.tearDown();
} }

View File

@ -37,7 +37,6 @@ import org.apache.logging.log4j.LogManager;
import org.opensearch.NioIntegTestCase; import org.opensearch.NioIntegTestCase;
import org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest; import org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest;
import org.opensearch.common.logging.Loggers;
import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.InternalTestCluster; import org.opensearch.test.InternalTestCluster;
import org.opensearch.test.MockLogAppender; import org.opensearch.test.MockLogAppender;
@ -54,15 +53,11 @@ public class NioTransportLoggingIT extends NioIntegTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
appender = MockLogAppender.createStarted(); appender = MockLogAppender.createForLoggers(LogManager.getLogger(TransportLogger.class), LogManager.getLogger(TcpTransport.class));
Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender);
Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender);
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {
Loggers.removeAppender(LogManager.getLogger(TransportLogger.class), appender); appender.close();
Loggers.removeAppender(LogManager.getLogger(TcpTransport.class), appender);
appender.stop();
super.tearDown(); super.tearDown();
} }

View File

@ -44,7 +44,6 @@ import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.metadata.AutoExpandReplicas; import org.opensearch.cluster.metadata.AutoExpandReplicas;
import org.opensearch.cluster.metadata.IndexMetadata; import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.routing.allocation.AllocationService; import org.opensearch.cluster.routing.allocation.AllocationService;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.time.DateFormatter; import org.opensearch.common.time.DateFormatter;
import org.opensearch.common.unit.ByteSizeUnit; import org.opensearch.common.unit.ByteSizeUnit;
@ -263,8 +262,8 @@ public class RolloverIT extends OpenSearchIntegTestCase {
ensureGreen(); ensureGreen();
Logger allocationServiceLogger = LogManager.getLogger(AllocationService.class); Logger allocationServiceLogger = LogManager.getLogger(AllocationService.class);
MockLogAppender appender = new MockLogAppender(); final RolloverResponse response;
appender.start(); try (MockLogAppender appender = MockLogAppender.createForLoggers(allocationServiceLogger)) {
appender.addExpectation( appender.addExpectation(
new MockLogAppender.UnseenEventExpectation( new MockLogAppender.UnseenEventExpectation(
"no related message logged on dry run", "no related message logged on dry run",
@ -273,13 +272,11 @@ public class RolloverIT extends OpenSearchIntegTestCase {
"*test_index*" "*test_index*"
) )
); );
Loggers.addAppender(allocationServiceLogger, appender);
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").dryRun(true).get(); response = client().admin().indices().prepareRolloverIndex("test_alias").dryRun(true).get();
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
appender.stop(); }
Loggers.removeAppender(allocationServiceLogger, appender);
assertThat(response.getOldIndex(), equalTo("test_index-1")); assertThat(response.getOldIndex(), equalTo("test_index-1"));
assertThat(response.getNewIndex(), equalTo("test_index-000002")); assertThat(response.getNewIndex(), equalTo("test_index-000002"));

View File

@ -59,7 +59,6 @@ import org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider
import org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider; import org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider;
import org.opensearch.common.Priority; import org.opensearch.common.Priority;
import org.opensearch.common.io.FileSystemUtils; import org.opensearch.common.io.FileSystemUtils;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.internal.io.IOUtils; import org.opensearch.core.internal.io.IOUtils;
@ -458,8 +457,7 @@ public class ClusterRerouteIT extends OpenSearchIntegTestCase {
Logger actionLogger = LogManager.getLogger(TransportClusterRerouteAction.class); Logger actionLogger = LogManager.getLogger(TransportClusterRerouteAction.class);
MockLogAppender dryRunMockLog = new MockLogAppender(); try (MockLogAppender dryRunMockLog = MockLogAppender.createForLoggers(actionLogger)) {
dryRunMockLog.start();
dryRunMockLog.addExpectation( dryRunMockLog.addExpectation(
new MockLogAppender.UnseenEventExpectation( new MockLogAppender.UnseenEventExpectation(
"no completed message logged on dry run", "no completed message logged on dry run",
@ -468,7 +466,6 @@ public class ClusterRerouteIT extends OpenSearchIntegTestCase {
"allocated an empty primary*" "allocated an empty primary*"
) )
); );
Loggers.addAppender(actionLogger, dryRunMockLog);
AllocationCommand dryRunAllocation = new AllocateEmptyPrimaryAllocationCommand(indexName, 0, nodeName1, true); AllocationCommand dryRunAllocation = new AllocateEmptyPrimaryAllocationCommand(indexName, 0, nodeName1, true);
ClusterRerouteResponse dryRunResponse = client().admin() ClusterRerouteResponse dryRunResponse = client().admin()
@ -485,11 +482,9 @@ public class ClusterRerouteIT extends OpenSearchIntegTestCase {
assertThat(dryRunResponse.getExplanations().getYesDecisionMessages().get(0), containsString("allocated an empty primary")); assertThat(dryRunResponse.getExplanations().getYesDecisionMessages().get(0), containsString("allocated an empty primary"));
dryRunMockLog.assertAllExpectationsMatched(); dryRunMockLog.assertAllExpectationsMatched();
dryRunMockLog.stop(); }
Loggers.removeAppender(actionLogger, dryRunMockLog);
MockLogAppender allocateMockLog = new MockLogAppender(); try (MockLogAppender allocateMockLog = MockLogAppender.createForLoggers(actionLogger)) {
allocateMockLog.start();
allocateMockLog.addExpectation( allocateMockLog.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"message for first allocate empty primary", "message for first allocate empty primary",
@ -506,7 +501,6 @@ public class ClusterRerouteIT extends OpenSearchIntegTestCase {
"allocated an empty primary*" + nodeName2 + "*" "allocated an empty primary*" + nodeName2 + "*"
) )
); );
Loggers.addAppender(actionLogger, allocateMockLog);
AllocationCommand yesDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand(indexName, 0, nodeName1, true); AllocationCommand yesDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand(indexName, 0, nodeName1, true);
AllocationCommand noDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand("noexist", 1, nodeName2, true); AllocationCommand noDecisionAllocation = new AllocateEmptyPrimaryAllocationCommand("noexist", 1, nodeName2, true);
@ -524,8 +518,7 @@ public class ClusterRerouteIT extends OpenSearchIntegTestCase {
assertThat(response.getExplanations().getYesDecisionMessages().get(0), containsString(nodeName1)); assertThat(response.getExplanations().getYesDecisionMessages().get(0), containsString(nodeName1));
allocateMockLog.assertAllExpectationsMatched(); allocateMockLog.assertAllExpectationsMatched();
allocateMockLog.stop(); }
Loggers.removeAppender(actionLogger, allocateMockLog);
} }
public void testClusterRerouteWithBlocks() { public void testClusterRerouteWithBlocks() {

View File

@ -39,7 +39,6 @@ import org.apache.logging.log4j.core.LogEvent;
import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.coordination.JoinHelper; import org.opensearch.cluster.coordination.JoinHelper;
import org.opensearch.cluster.service.ClusterService; import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.node.Node.DiscoverySettings; import org.opensearch.node.Node.DiscoverySettings;
import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.OpenSearchIntegTestCase;
@ -119,8 +118,8 @@ public class SingleNodeDiscoveryIT extends OpenSearchIntegTestCase {
} }
public void testCannotJoinNodeWithSingleNodeDiscovery() throws Exception { public void testCannotJoinNodeWithSingleNodeDiscovery() throws Exception {
MockLogAppender mockAppender = new MockLogAppender(); Logger clusterLogger = LogManager.getLogger(JoinHelper.class);
mockAppender.start(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(clusterLogger)) {
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation("test", JoinHelper.class.getCanonicalName(), Level.INFO, "failed to join") { new MockLogAppender.SeenEventExpectation("test", JoinHelper.class.getCanonicalName(), Level.INFO, "failed to join") {
@ -175,17 +174,10 @@ public class SingleNodeDiscoveryIT extends OpenSearchIntegTestCase {
Function.identity() Function.identity()
) )
) { ) {
Logger clusterLogger = LogManager.getLogger(JoinHelper.class);
Loggers.addAppender(clusterLogger, mockAppender);
try {
other.beforeTest(random(), 0); other.beforeTest(random(), 0);
final ClusterState first = internalCluster().getInstance(ClusterService.class).state(); final ClusterState first = internalCluster().getInstance(ClusterService.class).state();
assertThat(first.nodes().getSize(), equalTo(1)); assertThat(first.nodes().getSize(), equalTo(1));
assertBusy(() -> mockAppender.assertAllExpectationsMatched()); assertBusy(() -> mockAppender.assertAllExpectationsMatched());
} finally {
Loggers.removeAppender(clusterLogger, mockAppender);
mockAppender.stop();
} }
} }
} }

View File

@ -40,7 +40,6 @@ import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.lucene.util.Constants; import org.apache.lucene.util.Constants;
import org.opensearch.cluster.metadata.Metadata; import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.common.io.PathUtils; import org.opensearch.common.io.PathUtils;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.test.AbstractBootstrapCheckTestCase; import org.opensearch.test.AbstractBootstrapCheckTestCase;
import org.opensearch.test.MockLogAppender; import org.opensearch.test.MockLogAppender;
@ -157,8 +156,7 @@ public class MaxMapCountCheckTests extends AbstractBootstrapCheckTestCase {
final IOException ioException = new IOException("fatal"); final IOException ioException = new IOException("fatal");
when(reader.readLine()).thenThrow(ioException); when(reader.readLine()).thenThrow(ioException);
final Logger logger = LogManager.getLogger("testGetMaxMapCountIOException"); final Logger logger = LogManager.getLogger("testGetMaxMapCountIOException");
final MockLogAppender appender = new MockLogAppender(); try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
appender.start();
appender.addExpectation( appender.addExpectation(
new ParameterizedMessageLoggingExpectation( new ParameterizedMessageLoggingExpectation(
"expected logged I/O exception", "expected logged I/O exception",
@ -169,20 +167,17 @@ public class MaxMapCountCheckTests extends AbstractBootstrapCheckTestCase {
e -> ioException == e e -> ioException == e
) )
); );
Loggers.addAppender(logger, appender);
assertThat(check.getMaxMapCount(logger), equalTo(-1L)); assertThat(check.getMaxMapCount(logger), equalTo(-1L));
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
verify(reader).close(); verify(reader).close();
Loggers.removeAppender(logger, appender); }
appender.stop();
} }
{ {
reset(reader); reset(reader);
when(reader.readLine()).thenReturn("eof"); when(reader.readLine()).thenReturn("eof");
final Logger logger = LogManager.getLogger("testGetMaxMapCountNumberFormatException"); final Logger logger = LogManager.getLogger("testGetMaxMapCountNumberFormatException");
final MockLogAppender appender = new MockLogAppender(); try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
appender.start();
appender.addExpectation( appender.addExpectation(
new ParameterizedMessageLoggingExpectation( new ParameterizedMessageLoggingExpectation(
"expected logged number format exception", "expected logged number format exception",
@ -193,12 +188,10 @@ public class MaxMapCountCheckTests extends AbstractBootstrapCheckTestCase {
e -> e instanceof NumberFormatException && e.getMessage().equals("For input string: \"eof\"") e -> e instanceof NumberFormatException && e.getMessage().equals("For input string: \"eof\"")
) )
); );
Loggers.addAppender(logger, appender);
assertThat(check.getMaxMapCount(logger), equalTo(-1L)); assertThat(check.getMaxMapCount(logger), equalTo(-1L));
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
verify(reader).close(); verify(reader).close();
Loggers.removeAppender(logger, appender); }
appender.stop();
} }
} }

View File

@ -34,6 +34,7 @@ package org.opensearch.cluster;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchTimeoutException; import org.opensearch.OpenSearchTimeoutException;
import org.opensearch.Version; import org.opensearch.Version;
import org.opensearch.action.ActionListener; import org.opensearch.action.ActionListener;
@ -46,7 +47,6 @@ import org.opensearch.common.CheckedRunnable;
import org.opensearch.common.UUIDs; import org.opensearch.common.UUIDs;
import org.opensearch.common.component.Lifecycle; import org.opensearch.common.component.Lifecycle;
import org.opensearch.common.component.LifecycleListener; import org.opensearch.common.component.LifecycleListener;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.transport.BoundTransportAddress; import org.opensearch.common.transport.BoundTransportAddress;
import org.opensearch.common.transport.TransportAddress; import org.opensearch.common.transport.TransportAddress;
@ -352,10 +352,8 @@ public class NodeConnectionsServiceTests extends OpenSearchTestCase {
for (DiscoveryNode disconnectedNode : disconnectedNodes) { for (DiscoveryNode disconnectedNode : disconnectedNodes) {
transportService.disconnectFromNode(disconnectedNode); transportService.disconnectFromNode(disconnectedNode);
} }
MockLogAppender appender = new MockLogAppender(); final Logger logger = LogManager.getLogger("org.opensearch.cluster.NodeConnectionsService");
try { try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
appender.start();
Loggers.addAppender(LogManager.getLogger("org.opensearch.cluster.NodeConnectionsService"), appender);
for (DiscoveryNode targetNode : targetNodes) { for (DiscoveryNode targetNode : targetNodes) {
if (disconnectedNodes.contains(targetNode)) { if (disconnectedNodes.contains(targetNode)) {
appender.addExpectation( appender.addExpectation(
@ -396,9 +394,6 @@ public class NodeConnectionsServiceTests extends OpenSearchTestCase {
runTasksUntil(deterministicTaskQueue, CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.get(Settings.EMPTY).millis()); runTasksUntil(deterministicTaskQueue, CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.get(Settings.EMPTY).millis());
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(LogManager.getLogger("org.opensearch.cluster.NodeConnectionsService"), appender);
appender.stop();
} }
for (DiscoveryNode disconnectedNode : disconnectedNodes) { for (DiscoveryNode disconnectedNode : disconnectedNodes) {
transportService.disconnectFromNode(disconnectedNode); transportService.disconnectFromNode(disconnectedNode);
@ -409,10 +404,8 @@ public class NodeConnectionsServiceTests extends OpenSearchTestCase {
for (DiscoveryNode disconnectedNode : disconnectedNodes) { for (DiscoveryNode disconnectedNode : disconnectedNodes) {
transportService.disconnectFromNode(disconnectedNode); transportService.disconnectFromNode(disconnectedNode);
} }
appender = new MockLogAppender();
try { try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
appender.start();
Loggers.addAppender(LogManager.getLogger("org.opensearch.cluster.NodeConnectionsService"), appender);
for (DiscoveryNode targetNode : targetNodes) { for (DiscoveryNode targetNode : targetNodes) {
if (disconnectedNodes.contains(targetNode) && newTargetNodes.get(targetNode.getId()) != null) { if (disconnectedNodes.contains(targetNode) && newTargetNodes.get(targetNode.getId()) != null) {
appender.addExpectation( appender.addExpectation(
@ -493,9 +486,6 @@ public class NodeConnectionsServiceTests extends OpenSearchTestCase {
service.connectToNodes(newTargetNodes, () -> {}); service.connectToNodes(newTargetNodes, () -> {});
deterministicTaskQueue.runAllRunnableTasks(); deterministicTaskQueue.runAllRunnableTasks();
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(LogManager.getLogger("org.opensearch.cluster.NodeConnectionsService"), appender);
appender.stop();
} }
} }

View File

@ -33,7 +33,6 @@ package org.opensearch.cluster.coordination;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.opensearch.OpenSearchException; import org.opensearch.OpenSearchException;
import org.opensearch.Version; import org.opensearch.Version;
@ -48,7 +47,6 @@ import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodeRole; import org.opensearch.cluster.node.DiscoveryNodeRole;
import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.regex.Regex; import org.opensearch.common.regex.Regex;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.Settings.Builder; import org.opensearch.common.settings.Settings.Builder;
@ -1265,19 +1263,12 @@ public class CoordinatorTests extends AbstractCoordinatorTestCase {
cluster1.clusterNodes.add(newNode); cluster1.clusterNodes.add(newNode);
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(JoinHelper.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation("test1", JoinHelper.class.getCanonicalName(), Level.INFO, "*failed to join*") 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"); cluster1.runFor(DEFAULT_STABILISATION_TIME, "failing join validation");
try {
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(joinLogger, mockAppender);
mockAppender.stop();
} }
assertEquals(0, newNode.getLastAppliedClusterState().version()); assertEquals(0, newNode.getLastAppliedClusterState().version());
@ -1554,10 +1545,11 @@ public class CoordinatorTests extends AbstractCoordinatorTestCase {
} }
for (int i = scaledRandomIntBetween(1, 10); i >= 0; i--) { for (int i = scaledRandomIntBetween(1, 10); i >= 0; i--) {
final MockLogAppender mockLogAppender = new MockLogAppender(); try (
try { MockLogAppender mockLogAppender = MockLogAppender.createForLoggers(
mockLogAppender.start(); LogManager.getLogger(ClusterFormationFailureHelper.class)
Loggers.addAppender(LogManager.getLogger(ClusterFormationFailureHelper.class), mockLogAppender); )
) {
mockLogAppender.addExpectation(new MockLogAppender.LoggingExpectation() { mockLogAppender.addExpectation(new MockLogAppender.LoggingExpectation() {
final Set<DiscoveryNode> nodesLogged = new HashSet<>(); final Set<DiscoveryNode> nodesLogged = new HashSet<>();
@ -1599,9 +1591,6 @@ public class CoordinatorTests extends AbstractCoordinatorTestCase {
}); });
cluster.runFor(warningDelayMillis + DEFAULT_DELAY_VARIABILITY, "waiting for warning to be emitted"); cluster.runFor(warningDelayMillis + DEFAULT_DELAY_VARIABILITY, "waiting for warning to be emitted");
mockLogAppender.assertAllExpectationsMatched(); mockLogAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(LogManager.getLogger(ClusterFormationFailureHelper.class), mockLogAppender);
mockLogAppender.stop();
} }
} }
} }
@ -1613,11 +1602,12 @@ public class CoordinatorTests extends AbstractCoordinatorTestCase {
cluster.stabilise(); cluster.stabilise();
final ClusterNode brokenNode = cluster.getAnyNodeExcept(cluster.getAnyLeader()); final ClusterNode brokenNode = cluster.getAnyNodeExcept(cluster.getAnyLeader());
final MockLogAppender mockLogAppender = new MockLogAppender(); try (
try { MockLogAppender mockLogAppender = MockLogAppender.createForLoggers(
mockLogAppender.start(); LogManager.getLogger(Coordinator.CoordinatorPublication.class),
Loggers.addAppender(LogManager.getLogger(Coordinator.CoordinatorPublication.class), mockLogAppender); LogManager.getLogger(LagDetector.class)
Loggers.addAppender(LogManager.getLogger(LagDetector.class), mockLogAppender); )
) {
mockLogAppender.addExpectation( mockLogAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
@ -1683,10 +1673,6 @@ public class CoordinatorTests extends AbstractCoordinatorTestCase {
); );
mockLogAppender.assertAllExpectationsMatched(); mockLogAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(LogManager.getLogger(Coordinator.CoordinatorPublication.class), mockLogAppender);
Loggers.removeAppender(LogManager.getLogger(LagDetector.class), mockLogAppender);
mockLogAppender.stop();
} }
} }
} }

View File

@ -33,7 +33,6 @@ package org.opensearch.cluster.routing.allocation;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.Version; import org.opensearch.Version;
import org.opensearch.action.ActionListener; import org.opensearch.action.ActionListener;
import org.opensearch.cluster.ClusterInfo; import org.opensearch.cluster.ClusterInfo;
@ -51,7 +50,6 @@ import org.opensearch.cluster.routing.RoutingTable;
import org.opensearch.cluster.routing.ShardRoutingState; import org.opensearch.cluster.routing.ShardRoutingState;
import org.opensearch.common.Priority; import org.opensearch.common.Priority;
import org.opensearch.common.collect.ImmutableOpenMap; import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.index.shard.ShardId; import org.opensearch.index.shard.ShardId;
@ -659,25 +657,30 @@ public class DiskThresholdMonitorTests extends OpenSearchAllocationTestCase {
private void assertNoLogging(DiskThresholdMonitor monitor, ImmutableOpenMap<String, DiskUsage> diskUsages) private void assertNoLogging(DiskThresholdMonitor monitor, ImmutableOpenMap<String, DiskUsage> diskUsages)
throws IllegalAccessException { throws IllegalAccessException {
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(DiskThresholdMonitor.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.UnseenEventExpectation("any INFO message", DiskThresholdMonitor.class.getCanonicalName(), Level.INFO, "*") new MockLogAppender.UnseenEventExpectation(
"any INFO message",
DiskThresholdMonitor.class.getCanonicalName(),
Level.INFO,
"*"
)
); );
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.UnseenEventExpectation("any WARN message", DiskThresholdMonitor.class.getCanonicalName(), Level.WARN, "*") new MockLogAppender.UnseenEventExpectation(
"any WARN message",
DiskThresholdMonitor.class.getCanonicalName(),
Level.WARN,
"*"
)
); );
Logger diskThresholdMonitorLogger = LogManager.getLogger(DiskThresholdMonitor.class);
Loggers.addAppender(diskThresholdMonitorLogger, mockAppender);
for (int i = between(1, 3); i >= 0; i--) { for (int i = between(1, 3); i >= 0; i--) {
monitor.onNewInfo(clusterInfo(diskUsages)); monitor.onNewInfo(clusterInfo(diskUsages));
} }
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
Loggers.removeAppender(diskThresholdMonitorLogger, mockAppender); }
mockAppender.stop();
} }
private void assertRepeatedWarningMessages(DiskThresholdMonitor monitor, ImmutableOpenMap<String, DiskUsage> diskUsages, String message) private void assertRepeatedWarningMessages(DiskThresholdMonitor monitor, ImmutableOpenMap<String, DiskUsage> diskUsages, String message)
@ -701,7 +704,7 @@ public class DiskThresholdMonitorTests extends OpenSearchAllocationTestCase {
private void assertLogging(DiskThresholdMonitor monitor, ImmutableOpenMap<String, DiskUsage> diskUsages, Level level, String message) private void assertLogging(DiskThresholdMonitor monitor, ImmutableOpenMap<String, DiskUsage> diskUsages, Level level, String message)
throws IllegalAccessException { throws IllegalAccessException {
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(DiskThresholdMonitor.class))) {
mockAppender.start(); mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation("expected message", DiskThresholdMonitor.class.getCanonicalName(), level, message) new MockLogAppender.SeenEventExpectation("expected message", DiskThresholdMonitor.class.getCanonicalName(), level, message)
@ -715,14 +718,10 @@ public class DiskThresholdMonitorTests extends OpenSearchAllocationTestCase {
) )
); );
Logger diskThresholdMonitorLogger = LogManager.getLogger(DiskThresholdMonitor.class);
Loggers.addAppender(diskThresholdMonitorLogger, mockAppender);
monitor.onNewInfo(clusterInfo(diskUsages)); monitor.onNewInfo(clusterInfo(diskUsages));
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
Loggers.removeAppender(diskThresholdMonitorLogger, mockAppender); }
mockAppender.stop();
} }
private static ClusterInfo clusterInfo(ImmutableOpenMap<String, DiskUsage> diskUsages) { private static ClusterInfo clusterInfo(ImmutableOpenMap<String, DiskUsage> diskUsages) {

View File

@ -34,7 +34,6 @@ package org.opensearch.cluster.service;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.Version; import org.opensearch.Version;
import org.opensearch.cluster.ClusterName; import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.ClusterState; import org.opensearch.cluster.ClusterState;
@ -47,7 +46,6 @@ import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider; import org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider;
import org.opensearch.cluster.service.ClusterApplier.ClusterApplyListener; import org.opensearch.cluster.service.ClusterApplier.ClusterApplyListener;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
@ -131,8 +129,7 @@ public class ClusterApplierServiceTests extends OpenSearchTestCase {
@TestLogging(value = "org.opensearch.cluster.service:TRACE", reason = "to ensure that we log cluster state events on TRACE level") @TestLogging(value = "org.opensearch.cluster.service:TRACE", reason = "to ensure that we log cluster state events on TRACE level")
public void testClusterStateUpdateLogging() throws Exception { public void testClusterStateUpdateLogging() throws Exception {
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(ClusterApplierService.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"test1", "test1",
@ -158,9 +155,6 @@ public class ClusterApplierServiceTests extends OpenSearchTestCase {
) )
); );
Logger clusterLogger = LogManager.getLogger(ClusterApplierService.class);
Loggers.addAppender(clusterLogger, mockAppender);
try {
clusterApplierService.currentTimeOverride = threadPool.relativeTimeInMillis(); clusterApplierService.currentTimeOverride = threadPool.relativeTimeInMillis();
clusterApplierService.runOnApplierThread( clusterApplierService.runOnApplierThread(
"test1", "test1",
@ -198,16 +192,12 @@ public class ClusterApplierServiceTests extends OpenSearchTestCase {
} }
}); });
assertBusy(mockAppender::assertAllExpectationsMatched); assertBusy(mockAppender::assertAllExpectationsMatched);
} finally {
Loggers.removeAppender(clusterLogger, mockAppender);
mockAppender.stop();
} }
} }
@TestLogging(value = "org.opensearch.cluster.service:WARN", reason = "to ensure that we log cluster state events on WARN level") @TestLogging(value = "org.opensearch.cluster.service:WARN", reason = "to ensure that we log cluster state events on WARN level")
public void testLongClusterStateUpdateLogging() throws Exception { public void testLongClusterStateUpdateLogging() throws Exception {
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(ClusterApplierService.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.UnseenEventExpectation( new MockLogAppender.UnseenEventExpectation(
"test1 shouldn't see because setting is too low", "test1 shouldn't see because setting is too low",
@ -235,9 +225,6 @@ public class ClusterApplierServiceTests extends OpenSearchTestCase {
) )
); );
Logger clusterLogger = LogManager.getLogger(ClusterApplierService.class);
Loggers.addAppender(clusterLogger, mockAppender);
try {
final CountDownLatch latch = new CountDownLatch(4); final CountDownLatch latch = new CountDownLatch(4);
final CountDownLatch processedFirstTask = new CountDownLatch(1); final CountDownLatch processedFirstTask = new CountDownLatch(1);
clusterApplierService.currentTimeOverride = threadPool.relativeTimeInMillis(); clusterApplierService.currentTimeOverride = threadPool.relativeTimeInMillis();
@ -301,12 +288,9 @@ public class ClusterApplierServiceTests extends OpenSearchTestCase {
} }
}); });
latch.await(); latch.await();
} finally {
Loggers.removeAppender(clusterLogger, mockAppender);
mockAppender.stop();
}
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} }
}
public void testLocalNodeMasterListenerCallbacks() { public void testLocalNodeMasterListenerCallbacks() {
TimedClusterApplierService timedClusterApplierService = createTimedClusterService(false); TimedClusterApplierService timedClusterApplierService = createTimedClusterService(false);

View File

@ -34,7 +34,6 @@ package org.opensearch.cluster.service;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException; import org.opensearch.OpenSearchException;
import org.opensearch.Version; import org.opensearch.Version;
import org.opensearch.cluster.AckedClusterStateUpdateTask; import org.opensearch.cluster.AckedClusterStateUpdateTask;
@ -54,7 +53,6 @@ import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.Nullable; import org.opensearch.common.Nullable;
import org.opensearch.common.Priority; import org.opensearch.common.Priority;
import org.opensearch.common.collect.Tuple; import org.opensearch.common.collect.Tuple;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
@ -327,8 +325,7 @@ public class MasterServiceTests extends OpenSearchTestCase {
@TestLogging(value = "org.opensearch.cluster.service:TRACE", reason = "to ensure that we log cluster state events on TRACE level") @TestLogging(value = "org.opensearch.cluster.service:TRACE", reason = "to ensure that we log cluster state events on TRACE level")
public void testClusterStateUpdateLogging() throws Exception { public void testClusterStateUpdateLogging() throws Exception {
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(MasterService.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"test1 start", "test1 start",
@ -421,8 +418,6 @@ public class MasterServiceTests extends OpenSearchTestCase {
) )
); );
Logger clusterLogger = LogManager.getLogger(MasterService.class);
Loggers.addAppender(clusterLogger, mockAppender);
try (MasterService masterService = createMasterService(true)) { try (MasterService masterService = createMasterService(true)) {
masterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() { masterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() {
@Override @Override
@ -486,9 +481,7 @@ public class MasterServiceTests extends OpenSearchTestCase {
} }
}); });
assertBusy(mockAppender::assertAllExpectationsMatched); assertBusy(mockAppender::assertAllExpectationsMatched);
} finally { }
Loggers.removeAppender(clusterLogger, mockAppender);
mockAppender.stop();
} }
} }
@ -741,8 +734,7 @@ public class MasterServiceTests extends OpenSearchTestCase {
@TestLogging(value = "org.opensearch.cluster.service:WARN", reason = "to ensure that we log cluster state events on WARN level") @TestLogging(value = "org.opensearch.cluster.service:WARN", reason = "to ensure that we log cluster state events on WARN level")
public void testLongClusterStateUpdateLogging() throws Exception { public void testLongClusterStateUpdateLogging() throws Exception {
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(MasterService.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.UnseenEventExpectation( new MockLogAppender.UnseenEventExpectation(
"test1 shouldn't log because it was fast enough", "test1 shouldn't log because it was fast enough",
@ -792,8 +784,6 @@ public class MasterServiceTests extends OpenSearchTestCase {
) )
); );
Logger clusterLogger = LogManager.getLogger(MasterService.class);
Loggers.addAppender(clusterLogger, mockAppender);
try ( try (
MasterService masterService = new MasterService( MasterService masterService = new MasterService(
Settings.builder() Settings.builder()
@ -819,12 +809,12 @@ public class MasterServiceTests extends OpenSearchTestCase {
final AtomicReference<ClusterState> clusterStateRef = new AtomicReference<>(initialClusterState); final AtomicReference<ClusterState> clusterStateRef = new AtomicReference<>(initialClusterState);
masterService.setClusterStatePublisher((event, publishListener, ackListener) -> { masterService.setClusterStatePublisher((event, publishListener, ackListener) -> {
if (event.source().contains("test5")) { if (event.source().contains("test5")) {
relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY).millis() relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY)
+ randomLongBetween(1, 1000000); .millis() + randomLongBetween(1, 1000000);
} }
if (event.source().contains("test6")) { if (event.source().contains("test6")) {
relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY).millis() relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY)
+ randomLongBetween(1, 1000000); .millis() + randomLongBetween(1, 1000000);
throw new OpenSearchException("simulated error during slow publication which should trigger logging"); throw new OpenSearchException("simulated error during slow publication which should trigger logging");
} }
clusterStateRef.set(event.state()); clusterStateRef.set(event.state());
@ -861,8 +851,8 @@ public class MasterServiceTests extends OpenSearchTestCase {
masterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() { masterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() {
@Override @Override
public ClusterState execute(ClusterState currentState) { public ClusterState execute(ClusterState currentState) {
relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY).millis() relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY)
+ randomLongBetween(1, 1000000); .millis() + randomLongBetween(1, 1000000);
throw new IllegalArgumentException("Testing handling of exceptions in the cluster state task"); throw new IllegalArgumentException("Testing handling of exceptions in the cluster state task");
} }
@ -879,8 +869,8 @@ public class MasterServiceTests extends OpenSearchTestCase {
masterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() { masterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() {
@Override @Override
public ClusterState execute(ClusterState currentState) { public ClusterState execute(ClusterState currentState) {
relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY).millis() relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY)
+ randomLongBetween(1, 1000000); .millis() + randomLongBetween(1, 1000000);
return ClusterState.builder(currentState).incrementVersion().build(); return ClusterState.builder(currentState).incrementVersion().build();
} }
@ -897,8 +887,8 @@ public class MasterServiceTests extends OpenSearchTestCase {
masterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() { masterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() {
@Override @Override
public ClusterState execute(ClusterState currentState) { public ClusterState execute(ClusterState currentState) {
relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY).millis() relativeTimeInMillis += MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING.get(Settings.EMPTY)
+ randomLongBetween(1, 1000000); .millis() + randomLongBetween(1, 1000000);
return currentState; return currentState;
} }
@ -963,12 +953,10 @@ public class MasterServiceTests extends OpenSearchTestCase {
} }
}); });
latch.await(); latch.await();
} finally {
Loggers.removeAppender(clusterLogger, mockAppender);
mockAppender.stop();
} }
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} }
}
public void testAcking() throws InterruptedException { public void testAcking() throws InterruptedException {
final DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); final DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);

View File

@ -37,7 +37,6 @@ import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.opensearch.cluster.metadata.IndexMetadata; import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.common.collect.Tuple; import org.opensearch.common.collect.Tuple;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.AbstractScopedSettings.SettingUpdater; import org.opensearch.common.settings.AbstractScopedSettings.SettingUpdater;
import org.opensearch.common.settings.Setting.Property; import org.opensearch.common.settings.Setting.Property;
import org.opensearch.common.unit.ByteSizeUnit; import org.opensearch.common.unit.ByteSizeUnit;
@ -1479,7 +1478,8 @@ public class SettingTests extends OpenSearchTestCase {
); );
final IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY); final IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
final MockLogAppender mockLogAppender = new MockLogAppender(); final Logger logger = LogManager.getLogger(IndexScopedSettings.class);
try (MockLogAppender mockLogAppender = MockLogAppender.createForLoggers(logger)) {
mockLogAppender.addExpectation( mockLogAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"message", "message",
@ -1493,18 +1493,11 @@ public class SettingTests extends OpenSearchTestCase {
} }
} }
); );
mockLogAppender.start();
final Logger logger = LogManager.getLogger(IndexScopedSettings.class);
try {
Loggers.addAppender(logger, mockLogAppender);
settings.updateIndexMetadata( settings.updateIndexMetadata(
newIndexMeta("index1", Settings.builder().put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s").build()) newIndexMeta("index1", Settings.builder().put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s").build())
); );
mockLogAppender.assertAllExpectationsMatched(); mockLogAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(logger, mockLogAppender);
mockLogAppender.stop();
} }
} }
} }

View File

@ -35,7 +35,6 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.opensearch.common.Strings; import org.opensearch.common.Strings;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Setting.Property; import org.opensearch.common.settings.Setting.Property;
import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.json.JsonXContent; import org.opensearch.common.xcontent.json.JsonXContent;
@ -143,14 +142,10 @@ public class SettingsFilterTests extends OpenSearchTestCase {
private void assertExpectedLogMessages(Consumer<Logger> consumer, MockLogAppender.LoggingExpectation... expectations) private void assertExpectedLogMessages(Consumer<Logger> consumer, MockLogAppender.LoggingExpectation... expectations)
throws IllegalAccessException { throws IllegalAccessException {
Logger testLogger = LogManager.getLogger("org.opensearch.test"); Logger testLogger = LogManager.getLogger("org.opensearch.test");
MockLogAppender appender = MockLogAppender.createStarted(); try (MockLogAppender appender = MockLogAppender.createForLoggers(testLogger)) {
Loggers.addAppender(testLogger, appender);
try {
Arrays.stream(expectations).forEach(appender::addExpectation); Arrays.stream(expectations).forEach(appender::addExpectation);
consumer.accept(testLogger); consumer.accept(testLogger);
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(testLogger, appender);
} }
} }

View File

@ -42,7 +42,6 @@ import org.opensearch.action.ActionListener;
import org.opensearch.cluster.ClusterName; import org.opensearch.cluster.ClusterName;
import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.Nullable; import org.opensearch.common.Nullable;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.transport.TransportAddress; import org.opensearch.common.transport.TransportAddress;
import org.opensearch.test.OpenSearchTestCase; import org.opensearch.test.OpenSearchTestCase;
@ -174,8 +173,8 @@ public class HandshakingTransportAddressConnectorTests extends OpenSearchTestCas
FailureListener failureListener = new FailureListener(); FailureListener failureListener = new FailureListener();
MockLogAppender mockAppender = new MockLogAppender(); Logger targetLogger = LogManager.getLogger(HandshakingTransportAddressConnector.class);
mockAppender.start(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(targetLogger)) {
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"message", "message",
@ -184,16 +183,10 @@ public class HandshakingTransportAddressConnectorTests extends OpenSearchTestCas
"*completed handshake with [*] but followup connection failed*" "*completed handshake with [*] but followup connection failed*"
) )
); );
Logger targetLogger = LogManager.getLogger(HandshakingTransportAddressConnector.class);
Loggers.addAppender(targetLogger, mockAppender);
try {
handshakingTransportAddressConnector.connectToRemoteMasterNode(discoveryAddress, failureListener); handshakingTransportAddressConnector.connectToRemoteMasterNode(discoveryAddress, failureListener);
failureListener.assertFailure(); failureListener.assertFailure();
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(targetLogger, mockAppender);
mockAppender.stop();
} }
} }

View File

@ -51,7 +51,6 @@ import org.opensearch.cluster.routing.RoutingTable;
import org.opensearch.cluster.routing.allocation.AllocationService; import org.opensearch.cluster.routing.allocation.AllocationService;
import org.opensearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider; import org.opensearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider;
import org.opensearch.common.collect.Tuple; import org.opensearch.common.collect.Tuple;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.NamedXContentRegistry;
@ -568,18 +567,11 @@ public class IncrementalClusterStateWriterTests extends OpenSearchAllocationTest
IncrementalClusterStateWriter incrementalClusterStateWriter, IncrementalClusterStateWriter incrementalClusterStateWriter,
MockLogAppender.LoggingExpectation expectation MockLogAppender.LoggingExpectation expectation
) throws IllegalAccessException, WriteStateException { ) throws IllegalAccessException, WriteStateException {
MockLogAppender mockAppender = new MockLogAppender();
mockAppender.start();
mockAppender.addExpectation(expectation);
Logger classLogger = LogManager.getLogger(IncrementalClusterStateWriter.class); Logger classLogger = LogManager.getLogger(IncrementalClusterStateWriter.class);
Loggers.addAppender(classLogger, mockAppender); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(classLogger)) {
mockAppender.addExpectation(expectation);
try {
incrementalClusterStateWriter.updateClusterState(clusterState); incrementalClusterStateWriter.updateClusterState(clusterState);
} finally {
Loggers.removeAppender(classLogger, mockAppender);
mockAppender.stop();
}
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} }
} }
}

View File

@ -51,7 +51,6 @@ import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.common.UUIDs; import org.opensearch.common.UUIDs;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.BigArrays; import org.opensearch.common.util.BigArrays;
@ -1178,24 +1177,17 @@ public class PersistedClusterStateServiceTests extends OpenSearchTestCase {
PersistedClusterStateService.Writer writer, PersistedClusterStateService.Writer writer,
MockLogAppender.LoggingExpectation expectation MockLogAppender.LoggingExpectation expectation
) throws IllegalAccessException, IOException { ) throws IllegalAccessException, IOException {
MockLogAppender mockAppender = new MockLogAppender();
mockAppender.start();
mockAppender.addExpectation(expectation);
Logger classLogger = LogManager.getLogger(PersistedClusterStateService.class); Logger classLogger = LogManager.getLogger(PersistedClusterStateService.class);
Loggers.addAppender(classLogger, mockAppender); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(classLogger)) {
mockAppender.addExpectation(expectation);
try {
if (previousState == null) { if (previousState == null) {
writer.writeFullStateAndCommit(currentTerm, clusterState); writer.writeFullStateAndCommit(currentTerm, clusterState);
} else { } else {
writer.writeIncrementalStateAndCommit(currentTerm, previousState, clusterState); writer.writeIncrementalStateAndCommit(currentTerm, previousState, clusterState);
} }
} finally {
Loggers.removeAppender(classLogger, mockAppender);
mockAppender.stop();
}
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} }
}
@Override @Override
public Settings buildEnvSettings(Settings settings) { public Settings buildEnvSettings(Settings settings) {

View File

@ -37,7 +37,6 @@ import org.apache.logging.log4j.LogManager;
import org.opensearch.common.UUIDs; import org.opensearch.common.UUIDs;
import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.network.NetworkService; import org.opensearch.common.network.NetworkService;
import org.opensearch.common.network.NetworkUtils; import org.opensearch.common.network.NetworkUtils;
import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.ClusterSettings;
@ -268,12 +267,8 @@ public class AbstractHttpServerTransportTests extends OpenSearchTestCase {
.put(HttpTransportSettings.SETTING_HTTP_TRACE_LOG_EXCLUDE.getKey(), excludeSettings) .put(HttpTransportSettings.SETTING_HTTP_TRACE_LOG_EXCLUDE.getKey(), excludeSettings)
.build() .build()
); );
MockLogAppender appender = new MockLogAppender();
final String traceLoggerName = "org.opensearch.http.HttpTracer"; final String traceLoggerName = "org.opensearch.http.HttpTracer";
try { try (MockLogAppender appender = MockLogAppender.createForLoggers(LogManager.getLogger(traceLoggerName))) {
appender.start();
Loggers.addAppender(LogManager.getLogger(traceLoggerName), appender);
final String opaqueId = UUIDs.randomBase64UUID(random()); final String opaqueId = UUIDs.randomBase64UUID(random());
appender.addExpectation( appender.addExpectation(
new MockLogAppender.PatternSeenEventExpectation( new MockLogAppender.PatternSeenEventExpectation(
@ -342,9 +337,6 @@ public class AbstractHttpServerTransportTests extends OpenSearchTestCase {
transport.incomingRequest(fakeRestRequestExcludedPath.getHttpRequest(), fakeRestRequestExcludedPath.getHttpChannel()); transport.incomingRequest(fakeRestRequestExcludedPath.getHttpRequest(), fakeRestRequestExcludedPath.getHttpChannel());
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(LogManager.getLogger(traceLoggerName), appender);
appender.stop();
} }
} }
} }

View File

@ -34,7 +34,6 @@ package org.opensearch.ingest;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.SetOnce; import org.apache.lucene.util.SetOnce;
import org.opensearch.OpenSearchParseException; import org.opensearch.OpenSearchParseException;
import org.opensearch.ResourceNotFoundException; import org.opensearch.ResourceNotFoundException;
@ -59,7 +58,6 @@ import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.service.ClusterService; import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.OpenSearchExecutors; import org.opensearch.common.util.concurrent.OpenSearchExecutors;
import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentBuilder;
@ -544,8 +542,7 @@ public class IngestServiceTests extends OpenSearchTestCase {
); );
ClusterState previousClusterState = clusterState; ClusterState previousClusterState = clusterState;
clusterState = IngestService.innerPut(putRequest, clusterState); clusterState = IngestService.innerPut(putRequest, clusterState);
MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(IngestService.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"test1", "test1",
@ -554,14 +551,8 @@ public class IngestServiceTests extends OpenSearchTestCase {
"failed to update ingest pipelines" "failed to update ingest pipelines"
) )
); );
Logger ingestLogger = LogManager.getLogger(IngestService.class);
Loggers.addAppender(ingestLogger, mockAppender);
try {
ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(ingestLogger, mockAppender);
mockAppender.stop();
} }
pipeline = ingestService.getPipeline(id); pipeline = ingestService.getPipeline(id);
assertNotNull(pipeline); assertNotNull(pipeline);

View File

@ -34,13 +34,11 @@ package org.opensearch.monitor.fs;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.mockfile.FilterFileChannel; import org.apache.lucene.mockfile.FilterFileChannel;
import org.apache.lucene.mockfile.FilterFileSystemProvider; import org.apache.lucene.mockfile.FilterFileSystemProvider;
import org.opensearch.cluster.coordination.DeterministicTaskQueue; import org.opensearch.cluster.coordination.DeterministicTaskQueue;
import org.opensearch.common.io.PathUtils; import org.opensearch.common.io.PathUtils;
import org.opensearch.common.io.PathUtilsForTesting; import org.opensearch.common.io.PathUtilsForTesting;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.env.NodeEnvironment; import org.opensearch.env.NodeEnvironment;
@ -158,12 +156,10 @@ public class FsHealthServiceTests extends OpenSearchTestCase {
PathUtilsForTesting.installMock(fileSystem); PathUtilsForTesting.installMock(fileSystem);
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
MockLogAppender mockAppender = new MockLogAppender(); try (
mockAppender.start(); MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(FsHealthService.class));
NodeEnvironment env = newNodeEnvironment()
Logger logger = LogManager.getLogger(FsHealthService.class); ) {
Loggers.addAppender(logger, mockAppender);
try (NodeEnvironment env = newNodeEnvironment()) {
FsHealthService fsHealthService = new FsHealthService(settings, clusterSettings, testThreadPool, env); FsHealthService fsHealthService = new FsHealthService(settings, clusterSettings, testThreadPool, env);
int counter = 0; int counter = 0;
for (Path path : env.nodeDataPaths()) { for (Path path : env.nodeDataPaths()) {
@ -183,8 +179,6 @@ public class FsHealthServiceTests extends OpenSearchTestCase {
assertEquals(env.nodeDataPaths().length, disruptFileSystemProvider.getInjectedPathCount()); assertEquals(env.nodeDataPaths().length, disruptFileSystemProvider.getInjectedPathCount());
assertBusy(mockAppender::assertAllExpectationsMatched); assertBusy(mockAppender::assertAllExpectationsMatched);
} finally { } finally {
Loggers.removeAppender(logger, mockAppender);
mockAppender.stop();
PathUtilsForTesting.teardown(); PathUtilsForTesting.teardown();
ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS); ThreadPool.terminate(testThreadPool, 500, TimeUnit.MILLISECONDS);
} }

View File

@ -32,9 +32,7 @@
package org.opensearch.plugins; package org.opensearch.plugins;
import org.opensearch.common.logging.Loggers;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.lucene.util.Constants; import org.apache.lucene.util.Constants;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
@ -737,8 +735,7 @@ public class PluginsServiceTests extends OpenSearchTestCase {
public void testFindPluginDirs() throws Exception { public void testFindPluginDirs() throws Exception {
final Path plugins = createTempDir(); final Path plugins = createTempDir();
final MockLogAppender mockLogAppender = new MockLogAppender(); try (MockLogAppender mockLogAppender = MockLogAppender.createForLoggers(LogManager.getLogger(PluginsService.class))) {
mockLogAppender.start();
mockLogAppender.addExpectation( mockLogAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"[.test] warning", "[.test] warning",
@ -747,8 +744,6 @@ public class PluginsServiceTests extends OpenSearchTestCase {
"Non-plugin file located in the plugins folder with the following name: [.DS_Store]" "Non-plugin file located in the plugins folder with the following name: [.DS_Store]"
) )
); );
final Logger testLogger = LogManager.getLogger(PluginsService.class);
Loggers.addAppender(testLogger, mockLogAppender);
final Path fake = plugins.resolve("fake"); final Path fake = plugins.resolve("fake");
Path testFile = plugins.resolve(".DS_Store"); Path testFile = plugins.resolve(".DS_Store");
@ -777,6 +772,7 @@ public class PluginsServiceTests extends OpenSearchTestCase {
assertThat(PluginsService.findPluginDirs(plugins), containsInAnyOrder(fake)); assertThat(PluginsService.findPluginDirs(plugins), containsInAnyOrder(fake));
mockLogAppender.assertAllExpectationsMatched(); mockLogAppender.assertAllExpectationsMatched();
} }
}
public void testExistingMandatoryClasspathPlugin() { public void testExistingMandatoryClasspathPlugin() {
final Settings settings = Settings.builder() final Settings settings = Settings.builder()

View File

@ -34,7 +34,6 @@ package org.opensearch.transport;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException; import org.opensearch.OpenSearchException;
import org.opensearch.Version; import org.opensearch.Version;
import org.opensearch.action.ActionListener; import org.opensearch.action.ActionListener;
@ -46,7 +45,6 @@ import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.InputStreamStreamInput; import org.opensearch.common.io.stream.InputStreamStreamInput;
import org.opensearch.common.io.stream.NamedWriteableRegistry; import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue; import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.BigArrays; import org.opensearch.common.util.BigArrays;
@ -260,8 +258,7 @@ public class InboundHandlerTests extends OpenSearchTestCase {
// response so we must just close the connection on an error. To avoid the failure disappearing into a black hole we at least log // response so we must just close the connection on an error. To avoid the failure disappearing into a black hole we at least log
// it. // it.
final MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(InboundHandler.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"expected message", "expected message",
@ -270,10 +267,7 @@ public class InboundHandlerTests extends OpenSearchTestCase {
"could not send error response to handshake" "could not send error response to handshake"
) )
); );
final Logger inboundHandlerLogger = LogManager.getLogger(InboundHandler.class);
Loggers.addAppender(inboundHandlerLogger, mockAppender);
try {
final AtomicBoolean isClosed = new AtomicBoolean(); final AtomicBoolean isClosed = new AtomicBoolean();
channel.addCloseListener(ActionListener.wrap(() -> assertTrue(isClosed.compareAndSet(false, true)))); channel.addCloseListener(ActionListener.wrap(() -> assertTrue(isClosed.compareAndSet(false, true))));
@ -293,15 +287,11 @@ public class InboundHandlerTests extends OpenSearchTestCase {
assertTrue(isClosed.get()); assertTrue(isClosed.get());
assertNull(channel.getMessageCaptor().get()); assertNull(channel.getMessageCaptor().get());
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(inboundHandlerLogger, mockAppender);
mockAppender.stop();
} }
} }
public void testLogsSlowInboundProcessing() throws Exception { public void testLogsSlowInboundProcessing() throws Exception {
final MockLogAppender mockAppender = new MockLogAppender(); try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(InboundHandler.class))) {
mockAppender.start();
mockAppender.addExpectation( mockAppender.addExpectation(
new MockLogAppender.SeenEventExpectation( new MockLogAppender.SeenEventExpectation(
"expected message", "expected message",
@ -310,11 +300,8 @@ public class InboundHandlerTests extends OpenSearchTestCase {
"handling inbound transport message " "handling inbound transport message "
) )
); );
final Logger inboundHandlerLogger = LogManager.getLogger(InboundHandler.class);
Loggers.addAppender(inboundHandlerLogger, mockAppender);
handler.setSlowLogThreshold(TimeValue.timeValueMillis(5L)); handler.setSlowLogThreshold(TimeValue.timeValueMillis(5L));
try {
final Version remoteVersion = Version.CURRENT; final Version remoteVersion = Version.CURRENT;
final long requestId = randomNonNegativeLong(); final long requestId = randomNonNegativeLong();
final Header requestHeader = new Header( final Header requestHeader = new Header(
@ -336,9 +323,6 @@ public class InboundHandlerTests extends OpenSearchTestCase {
handler.inboundMessage(channel, requestMessage); handler.inboundMessage(channel, requestMessage);
assertNotNull(channel.getMessageCaptor().get()); assertNotNull(channel.getMessageCaptor().get());
mockAppender.assertAllExpectationsMatched(); mockAppender.assertAllExpectationsMatched();
} finally {
Loggers.removeAppender(inboundHandlerLogger, mockAppender);
mockAppender.stop();
} }
} }

View File

@ -40,7 +40,6 @@ import org.opensearch.action.support.PlainActionFuture;
import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.component.Lifecycle; import org.opensearch.common.component.Lifecycle;
import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.network.NetworkService; import org.opensearch.common.network.NetworkService;
import org.opensearch.common.network.NetworkUtils; import org.opensearch.common.network.NetworkUtils;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
@ -524,12 +523,7 @@ public class TcpTransportTests extends OpenSearchTestCase {
MockLogAppender.LoggingExpectation... expectations MockLogAppender.LoggingExpectation... expectations
) throws IllegalAccessException { ) throws IllegalAccessException {
final TestThreadPool testThreadPool = new TestThreadPool("test"); final TestThreadPool testThreadPool = new TestThreadPool("test");
MockLogAppender appender = new MockLogAppender(); try (MockLogAppender appender = MockLogAppender.createForLoggers(LogManager.getLogger(TcpTransport.class))) {
try {
appender.start();
Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender);
for (MockLogAppender.LoggingExpectation expectation : expectations) { for (MockLogAppender.LoggingExpectation expectation : expectations) {
appender.addExpectation(expectation); appender.addExpectation(expectation);
} }
@ -568,8 +562,6 @@ public class TcpTransportTests extends OpenSearchTestCase {
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
} finally { } finally {
Loggers.removeAppender(LogManager.getLogger(TcpTransport.class), appender);
appender.stop();
ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS); ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
} }
} }

View File

@ -38,7 +38,6 @@ import org.opensearch.action.admin.cluster.stats.ClusterStatsAction;
import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest; import org.opensearch.action.admin.cluster.stats.ClusterStatsRequest;
import org.opensearch.common.bytes.BytesReference; import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext; import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.test.OpenSearchTestCase; import org.opensearch.test.OpenSearchTestCase;
@ -51,22 +50,8 @@ import static org.mockito.Mockito.mock;
@TestLogging(value = "org.opensearch.transport.TransportLogger:trace", reason = "to ensure we log network events on TRACE level") @TestLogging(value = "org.opensearch.transport.TransportLogger:trace", reason = "to ensure we log network events on TRACE level")
public class TransportLoggerTests extends OpenSearchTestCase { public class TransportLoggerTests extends OpenSearchTestCase {
public void testLoggingHandler() throws Exception {
private MockLogAppender appender; try (MockLogAppender appender = MockLogAppender.createForLoggers(LogManager.getLogger(TransportLogger.class))) {
public void setUp() throws Exception {
super.setUp();
appender = MockLogAppender.createStarted();
Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender);
}
public void tearDown() throws Exception {
Loggers.removeAppender(LogManager.getLogger(TransportLogger.class), appender);
appender.stop();
super.tearDown();
}
public void testLoggingHandler() throws IOException {
final String writePattern = ".*\\[length: \\d+" final String writePattern = ".*\\[length: \\d+"
+ ", request id: \\d+" + ", request id: \\d+"
+ ", type: request" + ", type: request"
@ -103,6 +88,7 @@ public class TransportLoggerTests extends OpenSearchTestCase {
TransportLogger.logOutboundMessage(mock(TcpChannel.class), bytesReference); TransportLogger.logOutboundMessage(mock(TcpChannel.class), bytesReference);
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();
} }
}
private BytesReference buildRequest() throws IOException { private BytesReference buildRequest() throws IOException {
boolean compress = randomBoolean(); boolean compress = randomBoolean();

View File

@ -32,11 +32,15 @@
package org.opensearch.test; package org.opensearch.test;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.filter.RegexFilter; import org.apache.logging.log4j.core.filter.RegexFilter;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.regex.Regex; import org.opensearch.common.regex.Regex;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -47,32 +51,43 @@ import static org.hamcrest.MatcherAssert.assertThat;
/** /**
* Test appender that can be used to verify that certain events were logged correctly * Test appender that can be used to verify that certain events were logged correctly
*/ */
public class MockLogAppender extends AbstractAppender { public class MockLogAppender extends AbstractAppender implements AutoCloseable {
private static final String COMMON_PREFIX = System.getProperty("opensearch.logger.prefix", "org.opensearch."); private static final String COMMON_PREFIX = System.getProperty("opensearch.logger.prefix", "org.opensearch.");
private final List<LoggingExpectation> expectations; private final List<LoggingExpectation> expectations;
private final List<Logger> loggers;
/** /**
* Creates and starts a MockLogAppender. Generally preferred over using the constructor * Creates an instance and adds it as an appender to the given Loggers. Upon
* directly because adding an unstarted appender to the static logging context can cause * closure, this instance will then remove itself from the Loggers it was added
* difficult-to-identify errors in the tests and this method makes it impossible to do * to. It is strongly recommended to use this class in a try-with-resources block
* that. * to guarantee that it is properly removed from all Loggers. Since the logging
* state is static and therefore global within a JVM, it can cause unrelated
* tests to fail if, for example, they trigger a logging statement that tried to
* write to a closed MockLogAppender instance.
*/ */
public static MockLogAppender createStarted() throws IllegalAccessException { public static MockLogAppender createForLoggers(Logger... loggers) throws IllegalAccessException {
final MockLogAppender appender = new MockLogAppender(); final MockLogAppender appender = new MockLogAppender(
RegexFilter.createFilter(".*(\n.*)*", new String[0], false, null, null),
Collections.unmodifiableList(Arrays.asList(loggers))
);
appender.start(); appender.start();
for (Logger logger : loggers) {
Loggers.addAppender(logger, appender);
}
return appender; return appender;
} }
public MockLogAppender() throws IllegalAccessException { private MockLogAppender(RegexFilter filter, List<Logger> loggers) {
super("mock", RegexFilter.createFilter(".*(\n.*)*", new String[0], false, null, null), null); super("mock", filter, null);
/* /*
* We use a copy-on-write array list since log messages could be appended while we are setting up expectations. When that occurs, * We use a copy-on-write array list since log messages could be appended while we are setting up expectations. When that occurs,
* we would run into a concurrent modification exception from the iteration over the expectations in #append, concurrent with a * we would run into a concurrent modification exception from the iteration over the expectations in #append, concurrent with a
* modification from #addExpectation. * modification from #addExpectation.
*/ */
expectations = new CopyOnWriteArrayList<>(); this.expectations = new CopyOnWriteArrayList<>();
this.loggers = loggers;
} }
public void addExpectation(LoggingExpectation expectation) { public void addExpectation(LoggingExpectation expectation) {
@ -92,6 +107,14 @@ public class MockLogAppender extends AbstractAppender {
} }
} }
@Override
public void close() {
for (Logger logger : loggers) {
Loggers.removeAppender(logger, this);
}
this.stop();
}
public interface LoggingExpectation { public interface LoggingExpectation {
void match(LogEvent event); void match(LogEvent event);

View File

@ -34,6 +34,7 @@ package org.opensearch.transport;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier; import org.apache.logging.log4j.util.Supplier;
import org.apache.lucene.util.CollectionUtil; import org.apache.lucene.util.CollectionUtil;
@ -51,7 +52,6 @@ import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.network.CloseableChannel; import org.opensearch.common.network.CloseableChannel;
import org.opensearch.common.network.NetworkAddress; import org.opensearch.common.network.NetworkAddress;
import org.opensearch.common.network.NetworkUtils; import org.opensearch.common.network.NetworkUtils;
@ -1202,10 +1202,8 @@ public abstract class AbstractSimpleTransportTestCase extends OpenSearchTestCase
.build() .build()
); );
MockLogAppender appender = new MockLogAppender(); final Logger logger = LogManager.getLogger("org.opensearch.transport.TransportService.tracer");
try { try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
appender.start();
Loggers.addAppender(LogManager.getLogger("org.opensearch.transport.TransportService.tracer"), appender);
final String requestSent = ".*\\[internal:test].*sent to.*\\{TS_B}.*"; final String requestSent = ".*\\[internal:test].*sent to.*\\{TS_B}.*";
final MockLogAppender.LoggingExpectation requestSentExpectation = new MockLogAppender.PatternSeenEventExpectation( final MockLogAppender.LoggingExpectation requestSentExpectation = new MockLogAppender.PatternSeenEventExpectation(
"sent request", "sent request",
@ -1291,9 +1289,6 @@ public abstract class AbstractSimpleTransportTestCase extends OpenSearchTestCase
future.txGet(); future.txGet();
assertBusy(appender::assertAllExpectationsMatched); assertBusy(appender::assertAllExpectationsMatched);
} finally {
Loggers.removeAppender(LogManager.getLogger("org.opensearch.transport.TransportService.tracer"), appender);
appender.stop();
} }
} }

View File

@ -35,7 +35,6 @@ package org.opensearch.transport.nio;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.opensearch.common.CheckedRunnable; import org.opensearch.common.CheckedRunnable;
import org.opensearch.common.logging.Loggers;
import org.opensearch.common.settings.Settings; import org.opensearch.common.settings.Settings;
import org.opensearch.nio.ServerChannelContext; import org.opensearch.nio.ServerChannelContext;
import org.opensearch.nio.SocketChannelContext; import org.opensearch.nio.SocketChannelContext;
@ -54,22 +53,8 @@ import static org.mockito.Mockito.mock;
public class TestEventHandlerTests extends OpenSearchTestCase { public class TestEventHandlerTests extends OpenSearchTestCase {
private MockLogAppender appender;
public void setUp() throws Exception {
super.setUp();
appender = new MockLogAppender();
Loggers.addAppender(LogManager.getLogger(MockNioTransport.class), appender);
appender.start();
}
public void tearDown() throws Exception {
Loggers.removeAppender(LogManager.getLogger(MockNioTransport.class), appender);
appender.stop();
super.tearDown();
}
public void testLogOnElapsedTime() throws Exception { public void testLogOnElapsedTime() throws Exception {
try (MockLogAppender appender = MockLogAppender.createForLoggers(LogManager.getLogger(MockNioTransport.class))) {
long start = System.nanoTime(); long start = System.nanoTime();
long end = start + TimeUnit.MILLISECONDS.toNanos(400); long end = start + TimeUnit.MILLISECONDS.toNanos(400);
AtomicBoolean isStart = new AtomicBoolean(true); AtomicBoolean isStart = new AtomicBoolean(true);
@ -124,3 +109,4 @@ public class TestEventHandlerTests extends OpenSearchTestCase {
} }
} }
} }
}