Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LOGMGR-343] If the AsyncHandler's worker thread and the current thread are the same, do not add the record the queue #443

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ protected void doPublish(final ExtLogRecord record) {
// Copy the MDC over
record.copyMdc();
}
if (Thread.currentThread() == thread) {
publishToNestedHandlers(record);
return;
}
if (overflowAction == OverflowAction.DISCARD) {
recordQueue.offer(record);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setup() {
handler = new BlockingQueueHandler();

asyncHandler = new AsyncHandler();
asyncHandler.setOverflowAction(OverflowAction.DISCARD);
asyncHandler.setOverflowAction(OverflowAction.BLOCK);
asyncHandler.addHandler(handler);
}

Expand Down Expand Up @@ -99,6 +99,37 @@ public void testMdc() throws Exception {
Assertions.assertEquals(mdcValue, handler.getFirst());
}

@Test
public void reentry() throws Exception {
final ExtHandler reLogHandler = new ExtHandler() {
private final ThreadLocal<Boolean> entered = ThreadLocal.withInitial(() -> false);

@Override
protected void doPublish(final ExtLogRecord record) {
if (entered.get()) {
return;
}
try {
entered.set(true);
super.doPublish(record);
// Create a new record and act is if this was through a logger
asyncHandler.publish(createRecord());
} finally {
entered.set(false);
}
}
};
handler.addHandler(reLogHandler);
handler.setFormatter(new PatternFormatter("%s"));
asyncHandler.publish(createRecord());
// We should end up with two messages and a third should not happen
Assertions.assertEquals("Test message", handler.getFirst());
Assertions.assertEquals("Test message", handler.getFirst());
// This should time out. Then we end up with a null value. We could instead sleep for a shorter time and check
// if the queue is empty. However, a 5 second delay does not seem too long.
Assertions.assertNull(handler.getFirst(), () -> "Expected no more entries, but found " + handler.queue);
}

static ExtLogRecord createRecord() {
return new ExtLogRecord(Level.INFO, "Test message", AsyncHandlerTests.class.getName());
}
Expand All @@ -107,13 +138,14 @@ static class BlockingQueueHandler extends ExtHandler {
private final BlockingDeque<String> queue;

BlockingQueueHandler() {
queue = new LinkedBlockingDeque<String>();
queue = new LinkedBlockingDeque<>();
setErrorManager(AssertingErrorManager.of());
}

@Override
protected void doPublish(final ExtLogRecord record) {
queue.addLast(getFormatter().format(record));
publishToNestedHandlers(record);
super.doPublish(record);
}

Expand Down