Skip to content

Commit

Permalink
HSEARCH-4487 Use StatelessSession where relevant in the Jakarta Batch…
Browse files Browse the repository at this point in the history
… mass indexing job

No need for a full-blown session just to count entities or scroll on
identifiers.
  • Loading branch information
yrodiere committed Sep 29, 2023
1 parent af421b7 commit 936c089
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.LockModeType;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.StatelessSession;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.search.jakarta.batch.core.logging.impl.Log;
import org.hibernate.search.jakarta.batch.core.massindexing.MassIndexingJobParameters;
import org.hibernate.search.jakarta.batch.core.massindexing.impl.JobContextData;
Expand Down Expand Up @@ -77,7 +75,7 @@ public void beforeStep() throws IOException, ClassNotFoundException {
ConditionalExpression reindexOnly =
SerializationUtil.parseReindexOnlyParameters( reindexOnlyHql, serializedReindexOnlyParameters );

try ( Session session = PersistenceUtil.openSession( emf, tenantId ) ) {
try ( StatelessSession session = PersistenceUtil.openStatelessSession( emf, tenantId ) ) {
for ( EntityTypeDescriptor<?, ?> type : jobData.getEntityTypeDescriptors() ) {
Long rowCount = countAll( session, type, reindexOnly );
log.rowsToIndex( type.jpaEntityName(), rowCount );
Expand All @@ -98,14 +96,12 @@ public void afterStep() {
stepContext.setPersistentUserData( stepProgress );
}

private static Long countAll(Session session, EntityTypeDescriptor<?, ?> type, ConditionalExpression reindexOnly) {
return type.createCountQuery( session.unwrap( SessionImplementor.class ),
private static Long countAll(StatelessSession session, EntityTypeDescriptor<?, ?> type, ConditionalExpression reindexOnly) {
return type.createCountQuery( (SharedSessionContractImplementor) session,
reindexOnly == null ? List.of() : List.of( reindexOnly ) )
.setReadOnly( true )
.setCacheable( false )
.setLockMode( LockModeType.NONE )
.setCacheMode( CacheMode.IGNORE )
.setHibernateFlushMode( FlushMode.MANUAL )
.uniqueResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.LockModeType;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.StatelessSession;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.SelectionQuery;
import org.hibernate.search.jakarta.batch.core.context.jpa.spi.EntityManagerFactoryRegistry;
import org.hibernate.search.jakarta.batch.core.inject.scope.spi.HibernateSearchPartitionScoped;
Expand Down Expand Up @@ -260,7 +258,7 @@ private JobContextData getOrCreateJobContextData() {
}

private class ChunkState implements AutoCloseable {
private Session session;
private StatelessSession session;
private ScrollableResults<?> scroll;

private CheckpointInfo lastCheckpointInfo;
Expand Down Expand Up @@ -319,16 +317,16 @@ public void close() {
scroll = null;
}
if ( session != null ) {
closer.push( Session::close, session );
closer.push( StatelessSession::close, session );
session = null;
}
}
}

private void start() {
session = PersistenceUtil.openSession( emf, tenantId );
session = PersistenceUtil.openStatelessSession( emf, tenantId );
try {
scroll = createScroll( type, session.unwrap( SessionImplementor.class ) );
scroll = createScroll( type, session );
}
catch (Throwable t) {
try {
Expand All @@ -341,7 +339,7 @@ private void start() {
}
}

private <E, I> ScrollableResults<I> createScroll(EntityTypeDescriptor<E, I> type, SessionImplementor session) {
private <E, I> ScrollableResults<I> createScroll(EntityTypeDescriptor<E, I> type, StatelessSession session) {
List<ConditionalExpression> conditions = new ArrayList<>();
if ( reindexOnly != null ) {
conditions.add( reindexOnly );
Expand All @@ -357,7 +355,7 @@ else if ( lowerBound != null ) {
conditions.add( type.idOrder().idGreaterOrEqual( "HIBERNATE_SEARCH_PARTITION_LOWER_BOUND_", lowerBound ) );
}

SelectionQuery<I> query = type.createIdentifiersQuery( session, conditions );
SelectionQuery<I> query = type.createIdentifiersQuery( (SharedSessionContractImplementor) session, conditions );

if ( maxResults != null ) {
int remaining;
Expand All @@ -374,8 +372,6 @@ else if ( lowerBound != null ) {
.setReadOnly( true )
.setCacheable( false )
.setLockMode( LockModeType.NONE )
.setCacheMode( CacheMode.IGNORE )
.setHibernateFlushMode( FlushMode.MANUAL )
.setFetchSize( idFetchSize )
.scroll( ScrollMode.FORWARD_ONLY );
}
Expand Down

0 comments on commit 936c089

Please sign in to comment.