Skip to content

Commit

Permalink
HSEARCH-4995 Skip rendering the context in RootFailureCollector when …
Browse files Browse the repository at this point in the history
…the log level is not TRACE
  • Loading branch information
yrodiere committed Nov 14, 2023
1 parent cd8d181 commit 3338661
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
import org.hibernate.search.util.common.SearchTimeoutException;
import org.hibernate.search.util.common.logging.impl.ClassFormatter;
import org.hibernate.search.util.common.logging.impl.DurationInSecondsAndFractionsFormatter;
import org.hibernate.search.util.common.logging.impl.EventContextFormatter;
import org.hibernate.search.util.common.logging.impl.EventContextNoPrefixFormatter;
import org.hibernate.search.util.common.logging.impl.MessageConstants;
import org.hibernate.search.util.common.logging.impl.SimpleNameClassFormatter;
import org.hibernate.search.util.common.reporting.EventContext;
import org.hibernate.search.util.common.reporting.spi.EventContextProvider;

import org.jboss.logging.BasicLogger;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -141,10 +143,12 @@ SearchException collectedFailures(String process, String renderedFailures,
value = "Hibernate Search encountered a failure during %1$s;"
+ " continuing for now to list all problems,"
+ " but the process will ultimately be aborted.\n"
+ "Context: %2$s\n"
+ "%2$s\n" // Context
+ "Failure:" // The stack trace follows
)
void newCollectedFailure(String process, String context, @Cause Throwable failure);
void newCollectedFailure(String process,
@FormatWith(EventContextFormatter.class) EventContextProvider contextProvider,
@Cause Throwable failure);

@LogMessage(level = Logger.Level.WARN)
@Message(id = ID_OFFSET + 22, value = "Exception while collecting a failure"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
*/
package org.hibernate.search.engine.reporting.spi;

import org.hibernate.search.util.common.reporting.spi.EventContextProvider;

/**
* A failure collector with an implicit context.
* <p>
* Implementations are thread-safe.
*
* @see FailureCollector
*/
public interface ContextualFailureCollector extends FailureCollector {
public interface ContextualFailureCollector extends FailureCollector, EventContextProvider {

boolean hasFailure();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -25,7 +24,6 @@
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
import org.hibernate.search.util.common.reporting.EventContext;
import org.hibernate.search.util.common.reporting.EventContextElement;
import org.hibernate.search.util.common.reporting.impl.CommonEventContextMessages;

public final class RootFailureCollector implements FailureCollector {

Expand Down Expand Up @@ -135,8 +133,8 @@ ContextualFailureCollectorImpl withDefaultContext() {
return withContext( EventContexts.defaultContext() );
}

void appendContextTo(StringJoiner joiner) {
// Nothing to do
EventContext createEventContext(EventContextElement contextElement) {
return EventContext.create( contextElement );
}

final void appendChildrenFailuresTo(List<Throwable> failures, ToStringTreeBuilder builder) {
Expand All @@ -152,20 +150,21 @@ final void appendChildrenFailuresTo(List<Throwable> failures, ToStringTreeBuilde
final Collection<ContextualFailureCollectorImpl> children() {
return children.values();
}

}

private static class ContextualFailureCollectorImpl extends NonRootFailureCollector implements ContextualFailureCollector {
private final NonRootFailureCollector parent;
private final EventContextElement context;
private final EventContextElement contextElement;

// Avoiding blocking implementations because we access this from reactive event loops
private final Collection<Throwable> failures = new ConcurrentLinkedDeque<>();
private final Collection<String> failureMessages = new ConcurrentLinkedDeque<>();

private ContextualFailureCollectorImpl(NonRootFailureCollector parent, EventContextElement context) {
private ContextualFailureCollectorImpl(NonRootFailureCollector parent, EventContextElement contextElement) {
super( parent );
this.parent = parent;
this.context = context;
this.contextElement = contextElement;
}

@Override
Expand Down Expand Up @@ -209,13 +208,17 @@ ContextualFailureCollectorImpl withDefaultContext() {
}

@Override
void appendContextTo(StringJoiner joiner) {
parent.appendContextTo( joiner );
joiner.add( context.render() );
public EventContext eventContext() {
return parent.createEventContext( contextElement );
}

@Override
EventContext createEventContext(EventContextElement contextElement) {
return eventContext().append( contextElement );
}

void appendFailuresTo(List<Throwable> failures, ToStringTreeBuilder builder) {
builder.startObject( context.render() );
builder.startObject( contextElement.render() );
failures.addAll( this.failures );
if ( !failureMessages.isEmpty() ) {
builder.attribute( EngineEventContextMessages.INSTANCE.failureReportFailures(), failureMessages );
Expand All @@ -225,9 +228,7 @@ void appendFailuresTo(List<Throwable> failures, ToStringTreeBuilder builder) {
}

private void doAdd(Throwable failure, String failureMessage) {
StringJoiner contextJoiner = new StringJoiner( CommonEventContextMessages.INSTANCE.contextSeparator() );
appendContextTo( contextJoiner );
log.newCollectedFailure( root.process, contextJoiner.toString(), failure );
log.newCollectedFailure( root.process, this, failure );

if ( root.shouldAddFailure() ) {
failureMessages.add( failureMessage );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
package org.hibernate.search.util.common.logging.impl;

import org.hibernate.search.util.common.reporting.EventContext;
import org.hibernate.search.util.common.reporting.spi.EventContextProvider;

public final class EventContextFormatter {

private final EventContext eventContext;

public EventContextFormatter(EventContextProvider eventContextProvider) {
this( eventContextProvider.eventContext() );
}

public EventContextFormatter(EventContext eventContext) {
this.eventContext = eventContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public String renderWithPrefix() {
return MESSAGES.contextPrefix() + render();
}

public EventContext append(EventContextElement other) {
return new EventContext( this, other );
}

public EventContext append(EventContext other) {
return other.appendTo( this );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public ContextualFailureCollector withContext(EventContext context) {
public ContextualFailureCollector withContext(EventContextElement contextElement) {
return fail( "Unexpected call to withContext(" + contextElement + ")" );
}

@Override
public EventContext eventContext() {
return fail( "Unexpected call to eventContext()" );
}
}

0 comments on commit 3338661

Please sign in to comment.