Skip to content

Commit

Permalink
HSEARCH-5068 Move loading strategy configuration and retrieval to poj…
Browse files Browse the repository at this point in the history
…o-base
  • Loading branch information
yrodiere committed Feb 9, 2024
1 parent d114775 commit 6133c60
Show file tree
Hide file tree
Showing 103 changed files with 1,214 additions and 1,364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@
import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
import org.hibernate.search.engine.environment.bean.BeanResolver;
import org.hibernate.search.engine.mapper.mapping.building.spi.MappingFinalizationContext;
import org.hibernate.search.engine.reporting.spi.ContextualFailureCollector;

class MappingFinalizationContextImpl implements MappingFinalizationContext {

private final ContextualFailureCollector failureCollector;
private final ConfigurationPropertySource propertySource;
private final BeanResolver beanResolver;

MappingFinalizationContextImpl(ConfigurationPropertySource propertySource, BeanResolver beanResolver) {
MappingFinalizationContextImpl(ContextualFailureCollector failureCollector,
ConfigurationPropertySource propertySource, BeanResolver beanResolver) {
this.failureCollector = failureCollector;
this.propertySource = propertySource;
this.beanResolver = beanResolver;
}

@Override
public ContextualFailureCollector failureCollector() {
return failureCollector;
}

@Override
public ConfigurationPropertySource configurationPropertySource() {
return propertySource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.hibernate.search.engine.reporting.FailureHandler;
import org.hibernate.search.engine.reporting.impl.EngineEventContextMessages;
import org.hibernate.search.engine.reporting.impl.FailSafeFailureHandlerWrapper;
import org.hibernate.search.engine.reporting.spi.ContextualFailureCollector;
import org.hibernate.search.engine.reporting.spi.RootFailureCollector;
import org.hibernate.search.util.common.AssertionFailure;
import org.hibernate.search.util.common.SearchException;
Expand Down Expand Up @@ -289,7 +288,7 @@ void partiallyBuildAndAddTo(Map<MappingKey<?, ?>, MappingPartialBuildState> mapp
mappings.put( mappingKey, partiallyBuiltMapping );
}
catch (MappingAbortedException e) {
handleMappingAborted( e );
e.collectSilentlyAndCheck( buildContext.failureCollector() );
}
}

Expand All @@ -299,33 +298,6 @@ public void closeOnFailure() {
}
}

private void handleMappingAborted(MappingAbortedException e) {
ContextualFailureCollector failureCollector = buildContext.failureCollector();

if ( !failureCollector.hasFailure() ) {
throw new AssertionFailure(
"Caught " + MappingAbortedException.class.getSimpleName()
+ ", but the mapper did not collect any failure.",
e
);
}

/*
* This generally shouldn't do anything, because we don't expect a cause nor suppressed exceptions
* in the MappingAbortedException, but ignoring exceptions can lead to
* spending some really annoying hours debugging.
* So let's be extra cautious not to lose these.
*/
Throwable cause = e.getCause();
if ( cause != null ) {
failureCollector.add( cause );
}
Throwable[] suppressed = e.getSuppressed();
for ( Throwable throwable : suppressed ) {
failureCollector.add( throwable );
}
}

private class MappingConfigurationCollectorImpl implements MappingConfigurationCollector<C> {
private final TypeMetadataContributorProvider.Builder<C> builder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import org.hibernate.search.engine.environment.bean.BeanResolver;
import org.hibernate.search.engine.environment.bean.spi.BeanProvider;
import org.hibernate.search.engine.environment.thread.impl.ThreadPoolProviderImpl;
import org.hibernate.search.engine.mapper.mapping.building.spi.MappingAbortedException;
import org.hibernate.search.engine.mapper.mapping.building.spi.MappingFinalizationContext;
import org.hibernate.search.engine.mapper.mapping.building.spi.MappingFinalizer;
import org.hibernate.search.engine.mapper.mapping.building.spi.MappingKey;
import org.hibernate.search.engine.mapper.mapping.building.spi.MappingPartialBuildState;
import org.hibernate.search.engine.mapper.mapping.spi.MappingImplementor;
import org.hibernate.search.engine.reporting.FailureHandler;
import org.hibernate.search.engine.reporting.impl.EngineEventContextMessages;
import org.hibernate.search.engine.reporting.spi.ContextualFailureCollector;
import org.hibernate.search.engine.reporting.spi.RootFailureCollector;
import org.hibernate.search.util.common.AssertionFailure;
import org.hibernate.search.util.common.impl.Closer;
Expand Down Expand Up @@ -125,6 +127,8 @@ public SearchIntegrationFinalizer finalizer(ConfigurationPropertySource property

private class SearchIntegrationFinalizerImpl implements SearchIntegrationFinalizer {

private final RootFailureCollector failureCollector =
new RootFailureCollector( EngineEventContextMessages.INSTANCE.bootstrap() );
private final ConfigurationPropertySource propertySource;
private final ConfigurationPropertyChecker propertyChecker;

Expand All @@ -146,10 +150,22 @@ public <PBM, M> M finalizeMapping(MappingKey<PBM, M> mappingKey,
);
}

ContextualFailureCollector mappingFailureCollector = failureCollector.withContext( mappingKey );
MappingFinalizationContext mappingFinalizationContext =
new MappingFinalizationContextImpl( propertySource, beanResolver );
new MappingFinalizationContextImpl( mappingFailureCollector, propertySource, beanResolver );

MappingImplementor<M> mapping = null;
try {
mapping = finalizer.finalizeMapping( mappingFinalizationContext, partiallyBuiltMapping );
}
catch (RuntimeException e) {
mappingFailureCollector.add( e );
}
catch (MappingAbortedException e) {
e.collectSilentlyAndCheck( mappingFailureCollector );
}
failureCollector.checkNoFailure();

MappingImplementor<M> mapping = finalizer.finalizeMapping( mappingFinalizationContext, partiallyBuiltMapping );
fullyBuiltNonStartedMappings.put( mappingKey, new MappingNonStartedState( mappingKey, mapping ) );
partiallyBuiltMappings.remove( mappingKey );

Expand All @@ -158,15 +174,14 @@ public <PBM, M> M finalizeMapping(MappingKey<PBM, M> mappingKey,

@Override
public SearchIntegration finalizeIntegration() {
failureCollector.checkNoFailure();

if ( !partiallyBuiltMappings.isEmpty() ) {
throw new AssertionFailure(
"Some mappings were not fully built. Partially built mappings: " + partiallyBuiltMappings
);
}

RootFailureCollector failureCollector =
new RootFailureCollector( EngineEventContextMessages.INSTANCE.bootstrap() );

// Start backends
for ( Map.Entry<String, BackendNonStartedState> entry : nonStartedBackends.entrySet() ) {
startedBackends.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package org.hibernate.search.engine.mapper.mapping.building.spi;

import org.hibernate.search.engine.reporting.spi.ContextualFailureCollector;

/**
* @param <MPBS> The Java type of the partial build state of the produced mapping.
*/
Expand Down Expand Up @@ -48,8 +46,7 @@ public interface Mapper<MPBS extends MappingPartialBuildState> {
* Called exactly once just after {@link #mapTypes(MappedIndexManagerFactory)}.
*
* @return The partially-built mapping.
* @throws MappingAbortedException When aborting the mapping due to
* {@link ContextualFailureCollector#add(Throwable) collected} failures.
* @throws MappingAbortedException If something went wrong when preparing the mapping.
*/
MPBS prepareBuild() throws MappingAbortedException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.engine.mapper.mapping.building.spi;

import org.hibernate.search.engine.reporting.spi.ContextualFailureCollector;
import org.hibernate.search.util.common.AssertionFailure;

/**
* An exception thrown by {@link Mapper#prepareBuild()} when detecting that failures were
Expand All @@ -15,4 +16,34 @@
* which would make the failure report unclear.
*/
public class MappingAbortedException extends Exception {
/**
* Collects the cause/suppressed of this exception, if any, in the failure collector,
* and throws an exception if there is nothing to explain the abortion
* (neither in the failure collector or in this exceptions' cause/suppressed).
* @param failureCollector A failure collector for the mapping that was just aborted.
*/
public void collectSilentlyAndCheck(ContextualFailureCollector failureCollector) {
/*
* This generally shouldn't do anything, because we don't expect a cause nor suppressed exceptions
* in the MappingAbortedException, but ignoring exceptions can lead to
* spending some really annoying hours debugging.
* So let's be extra cautious not to lose these.
*/
Throwable cause = getCause();
if ( cause != null ) {
failureCollector.add( cause );
}
Throwable[] suppressed = getSuppressed();
for ( Throwable throwable : suppressed ) {
failureCollector.add( throwable );
}

if ( !failureCollector.hasFailure() ) {
throw new AssertionFailure(
"Caught " + MappingAbortedException.class.getSimpleName()
+ ", but the mapper did not collect any failure.",
this
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
import org.hibernate.search.engine.environment.bean.BeanResolver;
import org.hibernate.search.engine.reporting.spi.ContextualFailureCollector;

/**
* The context passed to the mapper during the very last step of bootstrap.
*/
public interface MappingFinalizationContext {

ContextualFailureCollector failureCollector();

ConfigurationPropertySource configurationPropertySource();

BeanResolver beanResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public interface MappingFinalizer<PBM, M> {
* @param context The context, including configuration properties.
* @param partiallyBuiltMapping The partially built mapping.
* @return The fully-built mapping.
* @throws RuntimeException If something went wrong when finalizing the mapping.
* @throws MappingAbortedException If something went wrong when finalizing the mapping.
*/
MappingImplementor<M> finalizeMapping(MappingFinalizationContext context, PBM partiallyBuiltMapping);
MappingImplementor<M> finalizeMapping(MappingFinalizationContext context, PBM partiallyBuiltMapping)
throws MappingAbortedException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.jakarta.batch.core.massindexing.util.impl;

import java.util.List;
import java.util.Set;

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
Expand Down Expand Up @@ -79,7 +80,7 @@ public SelectionQuery<? super E> createLoadingQuery(SessionImplementor session,
}

private HibernateOrmQueryLoader<? super E, I> queryLoader(List<ConditionalExpression> conditions, String order) {
return loadingStrategy.createQueryLoader( sessionFactory, List.of( delegate ), conditions, order );
return loadingStrategy.createQueryLoader( sessionFactory, Set.of( delegate.delegate() ), conditions, order );
}

}
Loading

0 comments on commit 6133c60

Please sign in to comment.