|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.search.integrationtest.mapper.pojo.massindexing; |
| 6 | + |
| 7 | +import static org.assertj.core.api.Fail.fail; |
| 8 | + |
| 9 | +import java.lang.invoke.MethodHandles; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | +import java.util.function.Consumer; |
| 12 | + |
| 13 | +import org.hibernate.search.engine.backend.work.execution.DocumentCommitStrategy; |
| 14 | +import org.hibernate.search.engine.backend.work.execution.DocumentRefreshStrategy; |
| 15 | +import org.hibernate.search.integrationtest.mapper.pojo.testsupport.loading.PersistenceTypeKey; |
| 16 | +import org.hibernate.search.integrationtest.mapper.pojo.testsupport.loading.StubEntityLoadingBinder; |
| 17 | +import org.hibernate.search.integrationtest.mapper.pojo.testsupport.loading.StubLoadingContext; |
| 18 | +import org.hibernate.search.mapper.pojo.loading.mapping.annotation.EntityLoadingBinderRef; |
| 19 | +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId; |
| 20 | +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField; |
| 21 | +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed; |
| 22 | +import org.hibernate.search.mapper.pojo.mapping.definition.annotation.SearchEntity; |
| 23 | +import org.hibernate.search.mapper.pojo.massindexing.DefaultMassIndexingMonitor; |
| 24 | +import org.hibernate.search.mapper.pojo.standalone.mapping.SearchMapping; |
| 25 | +import org.hibernate.search.mapper.pojo.standalone.massindexing.MassIndexer; |
| 26 | +import org.hibernate.search.mapper.pojo.standalone.session.SearchSession; |
| 27 | +import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock; |
| 28 | +import org.hibernate.search.util.impl.integrationtest.mapper.pojo.standalone.StandalonePojoMappingSetupHelper; |
| 29 | +import org.hibernate.search.util.impl.test.extension.ExpectedLog4jLog; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.ValueSource; |
| 35 | + |
| 36 | +import org.apache.logging.log4j.Level; |
| 37 | + |
| 38 | +class MassIndexingDefaultMonitorIT { |
| 39 | + |
| 40 | + private static final int COUNT = 100; |
| 41 | + @RegisterExtension |
| 42 | + public final BackendMock backendMock = BackendMock.create(); |
| 43 | + |
| 44 | + @RegisterExtension |
| 45 | + public final StandalonePojoMappingSetupHelper setupHelper = |
| 46 | + StandalonePojoMappingSetupHelper.withBackendMock( MethodHandles.lookup(), backendMock ); |
| 47 | + |
| 48 | + @RegisterExtension |
| 49 | + public ExpectedLog4jLog logged = ExpectedLog4jLog.create(); |
| 50 | + |
| 51 | + private final StubLoadingContext loadingContext = new StubLoadingContext(); |
| 52 | + |
| 53 | + @ValueSource(booleans = { true, false }) |
| 54 | + @ParameterizedTest |
| 55 | + void countOnBeforeType(boolean doCounts) { |
| 56 | + actualTest( () -> { |
| 57 | + logged.expectEvent( Level.INFO, "Mass indexing complete in ", ". Indexed 100/100 entities" ); |
| 58 | + if ( doCounts ) { |
| 59 | + logged.expectEvent( Level.INFO, "Mass indexing is going to index 100 entities" ).once(); |
| 60 | + } |
| 61 | + else { |
| 62 | + logged.expectEvent( Level.INFO, "Mass indexing is going to index 100 entities" ).never(); |
| 63 | + } |
| 64 | + }, indexer -> indexer.monitor( DefaultMassIndexingMonitor.builder().countOnBeforeType( doCounts ).build() ) ); |
| 65 | + } |
| 66 | + |
| 67 | + @ValueSource(booleans = { true, false }) |
| 68 | + @ParameterizedTest |
| 69 | + void countOnStart(boolean doCounts) { |
| 70 | + actualTest( () -> { |
| 71 | + logged.expectEvent( Level.INFO, "Mass indexing complete in ", ". Indexed 100/100 entities" ); |
| 72 | + logged.expectEvent( Level.INFO, "Mass indexing is going to index 100 entities" ).once(); |
| 73 | + if ( doCounts ) { |
| 74 | + logged.expectEvent( Level.INFO, |
| 75 | + "Mass indexing is going to index approx. 100 entities ([ Book ]). Actual number may change once the indexing starts." ) |
| 76 | + .once(); |
| 77 | + } |
| 78 | + else { |
| 79 | + logged.expectEvent( Level.INFO, |
| 80 | + "Mass indexing is going to index approx. 100 entities ([ Book ]). Actual number may change once the indexing starts." ) |
| 81 | + .never(); |
| 82 | + } |
| 83 | + }, indexer -> indexer.monitor( DefaultMassIndexingMonitor.builder().countOnStart( doCounts ).build() ) ); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void noCountsAtAll() { |
| 88 | + actualTest( () -> { |
| 89 | + logged.expectEvent( Level.INFO, "Mass indexing complete in ", ". Indexed 100/100 entities" ).once(); |
| 90 | + logged.expectEvent( Level.INFO, "Mass indexing is going to index 100 entities" ).never(); |
| 91 | + logged.expectEvent( Level.INFO, |
| 92 | + "Mass indexing is going to index approx. 100 entities ([ Book ]). Actual number may change once the indexing starts." ) |
| 93 | + .never(); |
| 94 | + }, indexer -> indexer |
| 95 | + .monitor( DefaultMassIndexingMonitor.builder().countOnStart( false ).countOnBeforeType( false ).build() ) ); |
| 96 | + } |
| 97 | + |
| 98 | + private void actualTest(Runnable expectedLogs, Consumer<MassIndexer> massIndexerConfiguration) { |
| 99 | + backendMock.expectAnySchema( Book.NAME ); |
| 100 | + |
| 101 | + SearchMapping mapping = setupHelper.start() |
| 102 | + .expectCustomBeans() |
| 103 | + .setup( Book.class ); |
| 104 | + |
| 105 | + backendMock.verifyExpectationsMet(); |
| 106 | + |
| 107 | + initData(); |
| 108 | + |
| 109 | + expectedLogs.run(); |
| 110 | + |
| 111 | + try ( SearchSession searchSession = mapping.createSession() ) { |
| 112 | + MassIndexer indexer = searchSession.massIndexer() |
| 113 | + // Simulate passing information to connect to a DB, ... |
| 114 | + .context( StubLoadingContext.class, loadingContext ); |
| 115 | + |
| 116 | + CompletableFuture<?> indexingFuture = new CompletableFuture<>(); |
| 117 | + indexingFuture.completeExceptionally( new SimulatedFailure( "Indexing error" ) ); |
| 118 | + |
| 119 | + // add operations on indexes can follow any random order, |
| 120 | + // since they are executed by different threads |
| 121 | + BackendMock.DocumentWorkCallListContext expectWorks = backendMock.expectWorks( |
| 122 | + Book.NAME, DocumentCommitStrategy.NONE, DocumentRefreshStrategy.NONE |
| 123 | + ); |
| 124 | + for ( int i = 0; i < COUNT; i++ ) { |
| 125 | + final String id = Integer.toString( i ); |
| 126 | + expectWorks |
| 127 | + .add( id, b -> b |
| 128 | + .field( "title", "TITLE_" + id ) |
| 129 | + .field( "author", "AUTHOR_" + id ) |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + // purgeAtStart and mergeSegmentsAfterPurge are enabled by default, |
| 134 | + // so we expect 1 purge, 1 mergeSegments and 1 flush calls in this order: |
| 135 | + backendMock.expectIndexScaleWorks( Book.NAME, searchSession.tenantIdentifierValue() ) |
| 136 | + .purge() |
| 137 | + .mergeSegments() |
| 138 | + .flush() |
| 139 | + .refresh(); |
| 140 | + |
| 141 | + try { |
| 142 | + massIndexerConfiguration.accept( indexer ); |
| 143 | + indexer.startAndWait(); |
| 144 | + } |
| 145 | + catch (InterruptedException e) { |
| 146 | + fail( "Unexpected InterruptedException: " + e.getMessage() ); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + backendMock.verifyExpectationsMet(); |
| 151 | + } |
| 152 | + |
| 153 | + private void initData() { |
| 154 | + for ( int i = 0; i < COUNT; i++ ) { |
| 155 | + persist( new Book( i, "TITLE_" + i, "AUTHOR_" + i ) ); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private void persist(Book book) { |
| 160 | + loadingContext.persistenceMap( Book.PERSISTENCE_KEY ).put( book.id, book ); |
| 161 | + } |
| 162 | + |
| 163 | + @SearchEntity(name = Book.NAME, |
| 164 | + loadingBinder = @EntityLoadingBinderRef(type = StubEntityLoadingBinder.class)) |
| 165 | + @Indexed(index = Book.NAME) |
| 166 | + public static class Book { |
| 167 | + |
| 168 | + public static final String NAME = "Book"; |
| 169 | + public static final PersistenceTypeKey<Book, Integer> PERSISTENCE_KEY = |
| 170 | + new PersistenceTypeKey<>( Book.class, Integer.class ); |
| 171 | + |
| 172 | + @DocumentId |
| 173 | + private Integer id; |
| 174 | + |
| 175 | + @GenericField |
| 176 | + private String title; |
| 177 | + |
| 178 | + @GenericField |
| 179 | + private String author; |
| 180 | + |
| 181 | + public Book() { |
| 182 | + } |
| 183 | + |
| 184 | + public Book(Integer id, String title, String author) { |
| 185 | + this.id = id; |
| 186 | + this.title = title; |
| 187 | + this.author = author; |
| 188 | + } |
| 189 | + |
| 190 | + public Integer getId() { |
| 191 | + return id; |
| 192 | + } |
| 193 | + |
| 194 | + public String getTitle() { |
| 195 | + return title; |
| 196 | + } |
| 197 | + |
| 198 | + public String getAuthor() { |
| 199 | + return author; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private static class SimulatedFailure extends RuntimeException { |
| 204 | + SimulatedFailure(String message) { |
| 205 | + super( message ); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments