diff --git a/src/main/java/org/folio/search/model/reindex/ReindexStatusEntity.java b/src/main/java/org/folio/search/model/reindex/ReindexStatusEntity.java index deddfe91f..c7bdc0b7a 100644 --- a/src/main/java/org/folio/search/model/reindex/ReindexStatusEntity.java +++ b/src/main/java/org/folio/search/model/reindex/ReindexStatusEntity.java @@ -20,7 +20,7 @@ public class ReindexStatusEntity { public static final String END_TIME_UPLOAD_COLUMN = "end_time_upload"; private final ReindexEntityType entityType; - private ReindexStatus status; + private final ReindexStatus status; private int totalMergeRanges; private int processedMergeRanges; private int totalUploadRanges; diff --git a/src/main/java/org/folio/search/service/reindex/ReindexMergeRangeIndexService.java b/src/main/java/org/folio/search/service/reindex/ReindexMergeRangeIndexService.java index 63b49d9ee..1c678c744 100644 --- a/src/main/java/org/folio/search/service/reindex/ReindexMergeRangeIndexService.java +++ b/src/main/java/org/folio/search/service/reindex/ReindexMergeRangeIndexService.java @@ -8,7 +8,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; @@ -64,10 +63,6 @@ public List fetchMergeRanges(ReindexEntityType entityType) { return repositories.get(entityType).getMergeRanges(); } - public int fetchRangeEntitiesCount(ReindexEntityType entityType) { - return Optional.ofNullable(repositories.get(entityType).countRangeEntities()).orElse(0); - } - private List constructRecordMergeRanges(int recordsCount, int rangeSize, InventoryRecordType recordType, diff --git a/src/main/java/org/folio/search/service/reindex/ReindexService.java b/src/main/java/org/folio/search/service/reindex/ReindexService.java index e2f6847d7..c2b81a7e5 100644 --- a/src/main/java/org/folio/search/service/reindex/ReindexService.java +++ b/src/main/java/org/folio/search/service/reindex/ReindexService.java @@ -7,6 +7,7 @@ import org.folio.search.exception.FolioIntegrationException; import org.folio.search.exception.RequestValidationException; import org.folio.search.integration.InventoryService; +import org.folio.search.model.types.ReindexStatus; import org.folio.search.service.consortium.ConsortiumTenantsService; import org.folio.spring.service.SystemUserScopedExecutionService; import org.springframework.stereotype.Service; @@ -41,7 +42,7 @@ public void initFullReindex(String tenantId) { } mergeRangeService.deleteAllRangeRecords(); - statusService.recreateStatusRecords(); + statusService.recreateStatusRecords(ReindexStatus.MERGE_IN_PROGRESS); CompletableFuture.runAsync(() -> { mergeRangeService.createMergeRanges(tenantId); @@ -67,8 +68,7 @@ private void processForConsortium(String tenantId) { private void publishRecordsRange() { for (var entityType : MERGE_RANGE_ENTITY_TYPES) { var rangeEntities = mergeRangeService.fetchMergeRanges(entityType); - var count = mergeRangeService.fetchRangeEntitiesCount(entityType); - statusService.updateMergeRangesStarted(entityType, count); + statusService.updateMergeRangesStarted(entityType, rangeEntities.size()); for (var rangeEntity : rangeEntities) { try { diff --git a/src/main/java/org/folio/search/service/reindex/ReindexStatusService.java b/src/main/java/org/folio/search/service/reindex/ReindexStatusService.java index 20d7fc538..bfbc0bed2 100644 --- a/src/main/java/org/folio/search/service/reindex/ReindexStatusService.java +++ b/src/main/java/org/folio/search/service/reindex/ReindexStatusService.java @@ -4,6 +4,7 @@ import java.util.List; import org.folio.search.model.reindex.ReindexStatusEntity; import org.folio.search.model.types.ReindexEntityType; +import org.folio.search.model.types.ReindexStatus; import org.folio.search.service.reindex.jdbc.ReindexStatusRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,8 +19,8 @@ public ReindexStatusService(ReindexStatusRepository statusRepository) { } @Transactional - public void recreateStatusRecords() { - var statusRecords = constructNewStatusRecords(); + public void recreateStatusRecords(ReindexStatus status) { + var statusRecords = constructNewStatusRecords(status); statusRepository.truncate(); statusRepository.saveReindexStatusRecords(statusRecords); } @@ -32,9 +33,9 @@ public void updateMergeRangesFailed(ReindexEntityType entityType) { statusRepository.setReindexMergeFailed(entityType); } - private List constructNewStatusRecords() { + private List constructNewStatusRecords(ReindexStatus status) { return Arrays.stream(ReindexEntityType.values()) - .map(ReindexStatusEntity::new) + .map(entityType -> new ReindexStatusEntity(entityType, status)) .toList(); } } diff --git a/src/main/java/org/folio/search/service/reindex/jdbc/MergeRangeRepository.java b/src/main/java/org/folio/search/service/reindex/jdbc/MergeRangeRepository.java index ac8a72bae..b2548fc93 100644 --- a/src/main/java/org/folio/search/service/reindex/jdbc/MergeRangeRepository.java +++ b/src/main/java/org/folio/search/service/reindex/jdbc/MergeRangeRepository.java @@ -22,7 +22,6 @@ public abstract class MergeRangeRepository extends ReindexJdbcRepository { """; private static final String SELECT_MERGE_RANGES_BY_ENTITY_TYPE = "SELECT * FROM %s WHERE entity_type = ?;"; - private static final String COUNT_SQL_BY_ENTITY_TYPE = "SELECT COUNT(*) FROM %s WHERE entity_type = ?;"; private static final int BATCH_OPERATION_SIZE = 100; @@ -58,12 +57,6 @@ public List getMergeRanges() { return jdbcTemplate.query(sql, mergeRangeEntityRowMapper(), entityType().getType()); } - public Integer countRangeEntities() { - var fullTableName = getFullTableName(context, entityTable()); - var sql = COUNT_SQL_BY_ENTITY_TYPE.formatted(fullTableName); - return jdbcTemplate.queryForObject(sql, Integer.class); - } - public abstract ReindexEntityType entityType(); private RowMapper mergeRangeEntityRowMapper() { diff --git a/src/main/java/org/folio/search/service/reindex/jdbc/ReindexStatusRepository.java b/src/main/java/org/folio/search/service/reindex/jdbc/ReindexStatusRepository.java index 64d2b23c4..cad9a95cd 100644 --- a/src/main/java/org/folio/search/service/reindex/jdbc/ReindexStatusRepository.java +++ b/src/main/java/org/folio/search/service/reindex/jdbc/ReindexStatusRepository.java @@ -29,7 +29,7 @@ public class ReindexStatusRepository { private static final String UPDATE_STATUS_SQL = """ UPDATE %s - SET status = ?, %s + SET %s WHERE entity_type = ?; """; @@ -47,8 +47,7 @@ public void setMergeReindexStarted(ReindexEntityType entityType, int totalMergeR var sql = UPDATE_STATUS_SQL.formatted( fullTableName, "%s = ?, %s = ?".formatted(TOTAL_MERGE_RANGES_COLUMN, START_TIME_MERGE_COLUMN)); - jdbcTemplate.update(sql, ReindexStatus.MERGE_IN_PROGRESS.name(), totalMergeRanges, Timestamp.from(Instant.now()), - entityType.name()); + jdbcTemplate.update(sql, totalMergeRanges, Timestamp.from(Instant.now()), entityType.name()); } public void setReindexUploadFailed(ReindexEntityType entityType) { diff --git a/src/test/java/org/folio/search/service/reindex/ReindexServiceTest.java b/src/test/java/org/folio/search/service/reindex/ReindexServiceTest.java index 1ade067c3..5e4f97fb0 100644 --- a/src/test/java/org/folio/search/service/reindex/ReindexServiceTest.java +++ b/src/test/java/org/folio/search/service/reindex/ReindexServiceTest.java @@ -22,6 +22,7 @@ import org.folio.search.model.reindex.MergeRangeEntity; import org.folio.search.model.types.InventoryRecordType; import org.folio.search.model.types.ReindexEntityType; +import org.folio.search.model.types.ReindexStatus; import org.folio.search.service.consortium.ConsortiumTenantsService; import org.folio.spring.service.SystemUserScopedExecutionService; import org.folio.spring.testing.type.UnitTest; @@ -63,25 +64,22 @@ void initFullReindex_positive() throws InterruptedException { var id = UUID.randomUUID(); var rangeEntity = new MergeRangeEntity(id, InventoryRecordType.INSTANCE, tenant, id, id, Timestamp.from(Instant.now())); - var rangeEntitiesCount = 10; when(consortiumService.isMemberTenantInConsortium(tenant)).thenReturn(false); when(consortiumService.getConsortiumTenants(tenant)).thenReturn(List.of(member)); when(mergeRangeService.fetchMergeRanges(any(ReindexEntityType.class))).thenReturn(List.of(rangeEntity)); - when(mergeRangeService.fetchRangeEntitiesCount(any(ReindexEntityType.class))).thenReturn(rangeEntitiesCount); final var expectedCallsCount = MERGE_RANGE_ENTITY_TYPES.size(); reindexService.initFullReindex(tenant); ThreadUtils.sleep(Duration.ofSeconds(1)); verify(mergeRangeService).deleteAllRangeRecords(); - verify(statusService).recreateStatusRecords(); + verify(statusService).recreateStatusRecords(ReindexStatus.MERGE_IN_PROGRESS); verify(mergeRangeService).createMergeRanges(tenant); verify(executionService).executeAsyncSystemUserScoped(eq(member), any(Runnable.class)); verify(statusService, times(expectedCallsCount)) - .updateMergeRangesStarted(any(ReindexEntityType.class), eq(rangeEntitiesCount)); + .updateMergeRangesStarted(any(ReindexEntityType.class), eq(1)); verify(mergeRangeService, times(expectedCallsCount)).fetchMergeRanges(any(ReindexEntityType.class)); - verify(mergeRangeService, times(expectedCallsCount)).fetchRangeEntitiesCount(any(ReindexEntityType.class)); verify(inventoryService, times(expectedCallsCount)).publishReindexRecordsRange(rangeEntity); verifyNoMoreInteractions(mergeRangeService); } @@ -93,21 +91,18 @@ void initFullReindex_negative_abortMergeAndSetFailedStatusWhenPublishingRangesFa var id = UUID.randomUUID(); var rangeEntity = new MergeRangeEntity(id, InventoryRecordType.INSTANCE, tenant, id, id, Timestamp.from(Instant.now())); - var rangeEntitiesCount = 10; when(consortiumService.isMemberTenantInConsortium(tenant)).thenReturn(false); when(consortiumService.getConsortiumTenants(tenant)).thenReturn(List.of(member)); when(mergeRangeService.fetchMergeRanges(any(ReindexEntityType.class))).thenReturn(List.of(rangeEntity)); - when(mergeRangeService.fetchRangeEntitiesCount(any(ReindexEntityType.class))).thenReturn(rangeEntitiesCount); doThrow(FolioIntegrationException.class).when(inventoryService).publishReindexRecordsRange(rangeEntity); reindexService.initFullReindex(tenant); ThreadUtils.sleep(Duration.ofSeconds(1)); verify(statusService) - .updateMergeRangesStarted(any(ReindexEntityType.class), eq(rangeEntitiesCount)); + .updateMergeRangesStarted(any(ReindexEntityType.class), eq(1)); verify(mergeRangeService).fetchMergeRanges(any(ReindexEntityType.class)); - verify(mergeRangeService).fetchRangeEntitiesCount(any(ReindexEntityType.class)); verify(statusService).updateMergeRangesFailed(any(ReindexEntityType.class)); } }