Skip to content

Commit

Permalink
MSEARCH-794: set status entity status at creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mukhiddin-yusuf committed Aug 15, 2024
1 parent 795fca4 commit d60ee76
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,10 +63,6 @@ public List<MergeRangeEntity> fetchMergeRanges(ReindexEntityType entityType) {
return repositories.get(entityType).getMergeRanges();
}

public int fetchRangeEntitiesCount(ReindexEntityType entityType) {
return Optional.ofNullable(repositories.get(entityType).countRangeEntities()).orElse(0);
}

private List<MergeRangeEntity> constructRecordMergeRanges(int recordsCount,
int rangeSize,
InventoryRecordType recordType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +42,7 @@ public void initFullReindex(String tenantId) {
}

mergeRangeService.deleteAllRangeRecords();
statusService.recreateStatusRecords();
statusService.recreateStatusRecords(ReindexStatus.MERGE_IN_PROGRESS);

CompletableFuture.runAsync(() -> {
mergeRangeService.createMergeRanges(tenantId);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -32,9 +33,9 @@ public void updateMergeRangesFailed(ReindexEntityType entityType) {
statusRepository.setReindexMergeFailed(entityType);
}

private List<ReindexStatusEntity> constructNewStatusRecords() {
private List<ReindexStatusEntity> constructNewStatusRecords(ReindexStatus status) {
return Arrays.stream(ReindexEntityType.values())
.map(ReindexStatusEntity::new)
.map(entityType -> new ReindexStatusEntity(entityType, status))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,12 +57,6 @@ public List<MergeRangeEntity> 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<MergeRangeEntity> mergeRangeEntityRowMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ReindexStatusRepository {

private static final String UPDATE_STATUS_SQL = """
UPDATE %s
SET status = ?, %s
SET %s
WHERE entity_type = ?;
""";

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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));
}
}

0 comments on commit d60ee76

Please sign in to comment.