Skip to content

Commit b34afc1

Browse files
committed
Log at warn level for server errors, update tests
1 parent 78d58a4 commit b34afc1

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

server/src/main/java/org/elasticsearch/search/SearchService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,16 @@ static <T> ActionListener<T> maybeWrapListenerForStackTrace(
557557
}
558558
if (header == false) {
559559
return listener.delegateResponse((l, e) -> {
560-
logger.debug(() -> format("[%s]%s: failed to execute search request", nodeId, shardId), e);
560+
org.apache.logging.log4j.util.Supplier<String> messageSupplier = () -> format(
561+
"[%s]%s: failed to execute search request",
562+
nodeId,
563+
shardId
564+
);
565+
if (ExceptionsHelper.status(e).getStatus() < 500 || ExceptionsHelper.isNodeOrShardUnavailableTypeException(e)) {
566+
logger.debug(messageSupplier, e);
567+
} else {
568+
logger.warn(messageSupplier, e);
569+
}
561570
ExceptionsHelper.unwrapCausesAndSuppressed(e, err -> {
562571
err.setStackTrace(EMPTY_STACK_TRACE_ARRAY);
563572
return false;

server/src/test/java/org/elasticsearch/search/SearchServiceTests.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,21 @@ public void onFailure(Exception e) {
156156
listener.onFailure(e);
157157
}
158158

159-
public void testMaybeWrapListenerForStackTraceLogs() {
159+
public void testMaybeWrapListenerForStackTraceDebugLog() {
160160
final String nodeId = "node";
161161
final String index = "index";
162162
ShardId shardId = new ShardId(index, index, 0);
163163

164164
try (var mockLog = MockLog.capture(SearchService.class)) {
165-
Configurator.setLevel("org.elasticsearch.search.SearchService", Level.DEBUG);
165+
Configurator.setLevel(SearchService.class, Level.DEBUG);
166166
final String exceptionMessage = "test exception message";
167167
mockLog.addExpectation(
168-
new MockLog.PatternAndExceptionSeenEventExpectation(
169-
format("Tracking information ([%s]%s) and exception logged before stack trace cleared", nodeId, shardId),
168+
new MockLog.ExceptionSeenEventExpectation(
169+
format("\"[%s]%s: failed to execute search request\" and an exception logged", nodeId, shardId),
170170
SearchService.class.getCanonicalName(),
171171
Level.DEBUG,
172-
format("\\[%s\\]%s: failed to execute search request", nodeId, shardId),
173-
Exception.class,
172+
format("[%s]%s: failed to execute search request", nodeId, shardId),
173+
IllegalArgumentException.class,
174174
exceptionMessage
175175
)
176176
);
@@ -187,7 +187,45 @@ public void onFailure(Exception e) {
187187
mockLog.assertAllExpectationsMatched();
188188
}
189189
};
190-
Exception e = new Exception(exceptionMessage);
190+
// This is a 400-level exception, so it should only be debug logged
191+
IllegalArgumentException e = new IllegalArgumentException(exceptionMessage);
192+
listener = maybeWrapListenerForStackTrace(listener, TransportVersion.current(), nodeId, shardId, threadPool);
193+
listener.onFailure(e);
194+
}
195+
}
196+
197+
public void testMaybeWrapListenerForStackTraceWarnLog() {
198+
final String nodeId = "node";
199+
final String index = "index";
200+
ShardId shardId = new ShardId(index, index, 0);
201+
202+
try (var mockLog = MockLog.capture(SearchService.class)) {
203+
final String exceptionMessage = "test exception message";
204+
mockLog.addExpectation(
205+
new MockLog.ExceptionSeenEventExpectation(
206+
format("\"[%s]%s: failed to execute search request\" and an exception logged", nodeId, shardId),
207+
SearchService.class.getCanonicalName(),
208+
Level.WARN,
209+
format("[%s]%s: failed to execute search request", nodeId, shardId),
210+
IllegalStateException.class,
211+
exceptionMessage
212+
)
213+
);
214+
215+
// Tests the listener has logged if it is wrapped
216+
ActionListener<SearchPhaseResult> listener = new ActionListener<>() {
217+
@Override
218+
public void onResponse(SearchPhaseResult searchPhaseResult) {
219+
// noop - we only care about failure scenarios
220+
}
221+
222+
@Override
223+
public void onFailure(Exception e) {
224+
mockLog.assertAllExpectationsMatched();
225+
}
226+
};
227+
// This is a 400-level exception, so it should only be debug logged
228+
IllegalStateException e = new IllegalStateException(exceptionMessage);
191229
listener = maybeWrapListenerForStackTrace(listener, TransportVersion.current(), nodeId, shardId, threadPool);
192230
listener.onFailure(e);
193231
}

test/framework/src/main/java/org/elasticsearch/search/ErrorTraceHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void addSeenLoggingExpectations(int numShards, MockLog mockLog, St
7171
mockLog.addExpectation(
7272
new MockLog.PatternAndExceptionSeenEventExpectation(
7373
format(
74-
"Tracking information ([%s][%s][%d]) and exception logged before stack trace cleared",
74+
"\"[%s][%s][%d]: failed to execute search request\" and an exception logged",
7575
nodesDisjunction,
7676
errorTriggeringIndex,
7777
shard
@@ -99,7 +99,7 @@ public static void addUnseenLoggingExpectations(int numShards, MockLog mockLog,
9999
for (int shard = 0; shard < numShards; shard++) {
100100
mockLog.addExpectation(
101101
new MockLog.UnseenEventExpectation(
102-
"Tracking information ([nodeId][indexName][shard]) and exception logged before stack trace cleared",
102+
"\"[%s][%s][%d]: failed to execute search request\" and an exception logged",
103103
SearchService.class.getCanonicalName(),
104104
Level.DEBUG,
105105
format("[%s][%s][%d]: failed to execute search request", getNodeId(nodeName), errorTriggeringIndex, shard)

test/framework/src/main/java/org/elasticsearch/test/MockLog.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,10 @@ public void match(LogEvent event) {
344344
&& event.getThrown().getClass() == clazz
345345
&& event.getThrown().getMessage().equals(exceptionMessage);
346346

347-
if (patternMatches && exceptionMatches) {
348-
seenLatch.countDown();
347+
if (exceptionMatches) {
348+
if (patternMatches) {
349+
seenLatch.countDown();
350+
}
349351
}
350352
}
351353
}

0 commit comments

Comments
 (0)