Skip to content

Commit

Permalink
MSEARCH-794: merge latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mukhiddin-yusuf committed Aug 19, 2024
1 parent f0ca4c8 commit 054de8d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import org.folio.search.rest.resource.IndexManagementApi;
import org.folio.search.service.IndexService;
import org.folio.search.service.ResourceService;
import org.folio.search.service.reindex.ReindexService;
import org.folio.search.service.reindex.ReindexRangeIndexService;
import org.folio.search.service.reindex.ReindexService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import org.folio.search.model.event.ReindexRangeIndexEvent;
import org.folio.search.model.reindex.UploadRangeEntity;
import org.folio.search.model.types.ReindexEntityType;
import org.folio.search.service.reindex.jdbc.UploadRangeRepository;
import org.folio.search.service.consortium.ConsortiumTenantService;
import org.folio.search.service.reindex.jdbc.ReindexJdbcRepository;
import org.folio.search.service.consortium.UserTenantsService;
import org.folio.search.service.reindex.jdbc.ReindexStatusRepository;
import org.folio.search.service.reindex.jdbc.UploadRangeRepository;
import org.folio.spring.integration.XOkapiHeaders;
import org.folio.spring.tools.kafka.FolioMessageProducer;
import org.springframework.stereotype.Service;
Expand All @@ -36,19 +35,19 @@ public class ReindexRangeIndexService {
private final FolioMessageProducer<ReindexRangeIndexEvent> indexRangeEventProducer;
private final ReindexStatusRepository statusRepository;
private final ReindexStatusMapper reindexStatusMapper;
private final ConsortiumTenantService tenantService;
private final UserTenantsService userTenantsService;

public ReindexRangeIndexService(List<UploadRangeRepository> repositories,
FolioMessageProducer<ReindexRangeIndexEvent> indexRangeEventProducer,
ReindexStatusRepository statusRepository,
ReindexStatusMapper reindexStatusMapper,
ConsortiumTenantService tenantService) {
UserTenantsService userTenantsService) {
this.repositories = repositories.stream()
.collect(Collectors.toMap(UploadRangeRepository::entityType, identity()));
this.indexRangeEventProducer = indexRangeEventProducer;
this.statusRepository = statusRepository;
this.reindexStatusMapper = reindexStatusMapper;
this.tenantService = tenantService;
this.userTenantsService = userTenantsService;
}

public void prepareAndSendIndexRanges(ReindexEntityType entityType) {
Expand Down Expand Up @@ -77,7 +76,7 @@ public void updateFinishDate(ReindexRangeIndexEvent event) {
}

public List<ReindexStatusItem> getReindexStatuses(String tenantId) {
var centralTenant = tenantService.getCentralTenant(tenantId);
var centralTenant = userTenantsService.getCentralTenant(tenantId);
if (centralTenant.isPresent() && !centralTenant.get().equals(tenantId)) {
throw new RequestValidationException(REQUEST_NOT_ALLOWED_MSG, XOkapiHeaders.TENANT, tenantId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void updateMergeRangesStarted(ReindexEntityType entityType, int totalMerg
}

public void updateMergeRangesFailed(List<ReindexEntityType> entityTypes) {
statusRepository.setReindexMergeFailed(entityTypes);
statusRepository.setMergeReindexFailed(entityTypes);
}

public void updateMergeRangesFailed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import static org.folio.search.model.reindex.ReindexStatusEntity.STATUS_COLUMN;
import static org.folio.search.model.reindex.ReindexStatusEntity.TOTAL_MERGE_RANGES_COLUMN;
import static org.folio.search.model.reindex.ReindexStatusEntity.TOTAL_UPLOAD_RANGES_COLUMN;
import static org.folio.search.model.reindex.ReindexStatusEntity.START_TIME_MERGE_COLUMN;
import static org.folio.search.model.reindex.ReindexStatusEntity.STATUS_COLUMN;
import static org.folio.search.model.reindex.ReindexStatusEntity.TOTAL_MERGE_RANGES_COLUMN;
import static org.folio.search.service.reindex.ReindexConstants.REINDEX_STATUS_TABLE;
import static org.folio.search.utils.JdbcUtils.getFullTableName;

Expand Down Expand Up @@ -50,11 +47,7 @@ public class ReindexStatusRepository {
""";

private static final String SELECT_REINDEX_STATUS_BY_REINDEX_ID_SQL = "SELECT * FROM %s;";
private static final String UPDATE_REINDEX_STATUS_SQL = """
UPDATE %s
SET status = ?, %s = ?
WHERE entity_type = ?;
""";

private static final String UPDATE_REINDEX_COUNTS_SQL = """
UPDATE %s
SET processed_merge_ranges = processed_merge_ranges + ?,
Expand All @@ -74,7 +67,8 @@ public List<ReindexStatusEntity> getReindexStatuses() {

public void setReindexUploadFailed(ReindexEntityType entityType) {
var fullTableName = getFullTableName(context, REINDEX_STATUS_TABLE);
var sql = UPDATE_REINDEX_STATUS_SQL.formatted(fullTableName, END_TIME_UPLOAD_COLUMN);
var sql = UPDATE_SQL.formatted(
fullTableName, "%s = ?, %s = ?".formatted(STATUS_COLUMN, START_TIME_MERGE_COLUMN));

jdbcTemplate.update(sql, ReindexStatus.UPLOAD_FAILED.name(), Timestamp.from(Instant.now()), entityType.name());
}
Expand All @@ -87,23 +81,6 @@ public void addReindexCounts(ReindexEntityType entityType, int processedMergeRan
jdbcTemplate.update(sql, processedMergeRanges, processedUploadRanges, entityType.name());
}

private RowMapper<ReindexStatusEntity> reindexStatusRowMapper() {
return (rs, rowNum) -> {
var reindexStatus = new ReindexStatusEntity(
ReindexEntityType.valueOf(rs.getString(ReindexStatusEntity.ENTITY_TYPE_COLUMN)),
ReindexStatus.valueOf(rs.getString(STATUS_COLUMN))
);
reindexStatus.setTotalMergeRanges(rs.getInt(TOTAL_MERGE_RANGES_COLUMN));
reindexStatus.setProcessedMergeRanges(rs.getInt(PROCESSED_MERGE_RANGES_COLUMN));
reindexStatus.setTotalUploadRanges(rs.getInt(TOTAL_UPLOAD_RANGES_COLUMN));
reindexStatus.setProcessedUploadRanges(rs.getInt(PROCESSED_UPLOAD_RANGES_COLUMN));
reindexStatus.setStartTimeMerge(rs.getTimestamp(START_TIME_MERGE_COLUMN));
reindexStatus.setEndTimeMerge(rs.getTimestamp(END_TIME_MERGE_COLUMN));
reindexStatus.setStartTimeUpload(rs.getTimestamp(START_TIME_UPLOAD_COLUMN));
reindexStatus.setEndTimeUpload(rs.getTimestamp(END_TIME_UPLOAD_COLUMN));
return reindexStatus;
};
}
public void truncate() {
var fullTableName = getFullTableName(context, REINDEX_STATUS_TABLE);
String sql = "TRUNCATE TABLE %s;".formatted(fullTableName);
Expand All @@ -118,7 +95,7 @@ public void setMergeReindexStarted(ReindexEntityType entityType, int totalMergeR
jdbcTemplate.update(sql, totalMergeRanges, Timestamp.from(Instant.now()), entityType.name());
}

public void setReindexMergeFailed(List<ReindexEntityType> entityTypes) {
public void setMergeReindexFailed(List<ReindexEntityType> entityTypes) {
var inTypes = entityTypes.stream()
.map(entityType -> "'%s'".formatted(entityType.name()))
.collect(Collectors.joining(","));
Expand Down Expand Up @@ -146,4 +123,21 @@ public void saveReindexStatusRecords(List<ReindexStatusEntity> statusRecords) {
});
}

private RowMapper<ReindexStatusEntity> reindexStatusRowMapper() {
return (rs, rowNum) -> {
var reindexStatus = new ReindexStatusEntity(
ReindexEntityType.valueOf(rs.getString(ReindexStatusEntity.ENTITY_TYPE_COLUMN)),
ReindexStatus.valueOf(rs.getString(STATUS_COLUMN))
);
reindexStatus.setTotalMergeRanges(rs.getInt(TOTAL_MERGE_RANGES_COLUMN));
reindexStatus.setProcessedMergeRanges(rs.getInt(PROCESSED_MERGE_RANGES_COLUMN));
reindexStatus.setTotalUploadRanges(rs.getInt(TOTAL_UPLOAD_RANGES_COLUMN));
reindexStatus.setProcessedUploadRanges(rs.getInt(PROCESSED_UPLOAD_RANGES_COLUMN));
reindexStatus.setStartTimeMerge(rs.getTimestamp(START_TIME_MERGE_COLUMN));
reindexStatus.setEndTimeMerge(rs.getTimestamp(END_TIME_MERGE_COLUMN));
reindexStatus.setStartTimeUpload(rs.getTimestamp(START_TIME_UPLOAD_COLUMN));
reindexStatus.setEndTimeUpload(rs.getTimestamp(END_TIME_UPLOAD_COLUMN));
return reindexStatus;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.folio.search.model.types.ReindexEntityType;
import org.folio.search.service.IndexService;
import org.folio.search.service.ResourceService;
import org.folio.search.service.reindex.ReindexService;
import org.folio.search.service.reindex.ReindexRangeIndexService;
import org.folio.search.service.reindex.ReindexService;
import org.folio.spring.integration.XOkapiHeaders;
import org.folio.spring.testing.type.UnitTest;
import org.hibernate.validator.internal.engine.ConstraintViolationImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import org.folio.search.model.reindex.ReindexStatusEntity;
import org.folio.search.model.reindex.UploadRangeEntity;
import org.folio.search.model.types.ReindexEntityType;
import org.folio.search.service.reindex.jdbc.UploadRangeRepository;
import org.folio.search.model.types.ReindexStatus;
import org.folio.search.service.consortium.ConsortiumTenantService;
import org.folio.search.service.reindex.jdbc.ReindexJdbcRepository;
import org.folio.search.service.consortium.UserTenantsService;
import org.folio.search.service.reindex.jdbc.ReindexStatusRepository;
import org.folio.search.service.reindex.jdbc.UploadRangeRepository;
import org.folio.spring.integration.XOkapiHeaders;
import org.folio.spring.testing.extension.Random;
import org.folio.spring.testing.extension.impl.RandomParametersExtension;
Expand All @@ -49,7 +48,7 @@ class ReindexRangeIndexServiceTest {
private @Mock FolioMessageProducer<ReindexRangeIndexEvent> indexRangeEventProducer;
private @Mock ReindexStatusRepository statusRepository;
private @Mock ReindexStatusMapper reindexStatusMapper;
private @Mock ConsortiumTenantService tenantService;
private @Mock UserTenantsService tenantService;
private ReindexRangeIndexService service;

@BeforeEach
Expand Down

0 comments on commit 054de8d

Please sign in to comment.