Skip to content

Commit

Permalink
- fix truncation 2
Browse files Browse the repository at this point in the history
  • Loading branch information
psmagin committed Sep 4, 2024
1 parent 04d6ea0 commit c6d772b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.folio.search.service.reindex;

import static java.util.function.Function.identity;
import static org.folio.search.service.reindex.ReindexConstants.RESOURCE_NAME_MAP;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.log4j.Log4j2;
import org.folio.search.model.types.ReindexEntityType;
import org.folio.search.service.IndexService;
import org.folio.search.service.reindex.jdbc.ReindexJdbcRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Log4j2
@Service
public class ReindexCommonService {

private final Map<ReindexEntityType, ReindexJdbcRepository> repositories;
private final IndexService indexService;

public ReindexCommonService(List<ReindexJdbcRepository> repositories, IndexService indexService) {
this.repositories = repositories.stream()
.collect(Collectors.toMap(ReindexJdbcRepository::entityType, identity()));
this.indexService = indexService;
}

@Transactional
public void deleteAllRecords() {
for (ReindexEntityType entityType : ReindexEntityType.values()) {
repositories.get(entityType).truncate();
}
}

public void recreateIndex(ReindexEntityType reindexEntityType, String tenantId) {
try {
var resourceType = RESOURCE_NAME_MAP.get(reindexEntityType);
indexService.dropIndex(resourceType, tenantId);
indexService.createIndex(resourceType, tenantId);
} catch (Exception e) {
log.warn("Index cannot be recreated for resource={}, message={}", reindexEntityType, e.getMessage());
}
}
}
24 changes: 6 additions & 18 deletions src/main/java/org/folio/search/service/reindex/ReindexService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.folio.search.service.reindex;

import static org.folio.search.service.reindex.ReindexConstants.MERGE_RANGE_ENTITY_TYPES;
import static org.folio.search.service.reindex.ReindexConstants.RESOURCE_NAME_MAP;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -17,7 +16,6 @@
import org.folio.search.model.reindex.MergeRangeEntity;
import org.folio.search.model.types.ReindexEntityType;
import org.folio.search.model.types.ReindexStatus;
import org.folio.search.service.IndexService;
import org.folio.search.service.consortium.ConsortiumTenantService;
import org.folio.spring.service.SystemUserScopedExecutionService;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -36,7 +34,7 @@ public class ReindexService {
private final ExecutorService reindexFullExecutor;
private final ExecutorService reindexUploadExecutor;
private final ReindexEntityTypeMapper entityTypeMapper;
private final IndexService indexService;
private final ReindexCommonService reindexCommonService;

public ReindexService(ConsortiumTenantService consortiumService,
SystemUserScopedExecutionService executionService,
Expand All @@ -47,7 +45,7 @@ public ReindexService(ConsortiumTenantService consortiumService,
@Qualifier("reindexFullExecutor") ExecutorService reindexFullExecutor,
@Qualifier("reindexUploadExecutor") ExecutorService reindexUploadExecutor,
ReindexEntityTypeMapper entityTypeMapper,
IndexService indexService) {
ReindexCommonService reindexCommonService) {
this.consortiumService = consortiumService;
this.executionService = executionService;
this.mergeRangeService = mergeRangeService;
Expand All @@ -57,7 +55,7 @@ public ReindexService(ConsortiumTenantService consortiumService,
this.reindexFullExecutor = reindexFullExecutor;
this.reindexUploadExecutor = reindexUploadExecutor;
this.entityTypeMapper = entityTypeMapper;
this.indexService = indexService;
this.reindexCommonService = reindexCommonService;
}

public CompletableFuture<Void> submitFullReindex(String tenantId) {
Expand All @@ -66,10 +64,10 @@ public CompletableFuture<Void> submitFullReindex(String tenantId) {
validateTenant(tenantId);

for (var reindexEntityType : ReindexEntityType.values()) {
recreateIndex(reindexEntityType, tenantId);
reindexCommonService.recreateIndex(reindexEntityType, tenantId);
}

uploadRangeService.deleteAllRecords();
reindexCommonService.deleteAllRecords();
statusService.recreateMergeStatusRecords();

var future = CompletableFuture.runAsync(() -> {
Expand Down Expand Up @@ -102,7 +100,7 @@ public CompletableFuture<Void> submitUploadReindex(String tenantId,

for (var reindexEntityType : reindexEntityTypes) {
statusService.recreateUploadStatusRecord(reindexEntityType);
recreateIndex(reindexEntityType, tenantId);
reindexCommonService.recreateIndex(reindexEntityType, tenantId);
}

var futures = new ArrayList<>();
Expand All @@ -123,16 +121,6 @@ public CompletableFuture<Void> submitUploadReindex(String tenantId,
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
}

private void recreateIndex(ReindexEntityType reindexEntityType, String tenantId) {
try {
var resourceType = RESOURCE_NAME_MAP.get(reindexEntityType);
indexService.dropIndex(resourceType, tenantId);
indexService.createIndex(resourceType, tenantId);
} catch (Exception e) {
log.warn("Index cannot be recreated for resource={}, message={}", reindexEntityType, e.getMessage());
}
}

private List<MergeRangeEntity> processForConsortium(String tenantId) {
List<MergeRangeEntity> mergeRangeEntities = new ArrayList<>();
var memberTenants = consortiumService.getConsortiumTenants(tenantId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.folio.search.service.reindex.jdbc.UploadRangeRepository;
import org.folio.spring.tools.kafka.FolioMessageProducer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ReindexUploadRangeIndexService {
Expand Down Expand Up @@ -62,14 +61,6 @@ public void updateFinishDate(ReindexRangeIndexEvent event) {
repository.setIndexRangeFinishDate(event.getId(), Timestamp.from(Instant.now()));
}


@Transactional
public void deleteAllRecords() {
for (ReindexEntityType entityType : ReindexEntityType.values()) {
repositories.get(entityType).truncate();
}
}

private List<ReindexRangeIndexEvent> prepareEvents(List<UploadRangeEntity> uploadRanges) {
return uploadRanges.stream()
.map(range -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.UUID;
import org.folio.search.model.reindex.MergeRangeEntity;
import org.folio.search.model.types.ReindexEntityType;
import org.folio.search.utils.JdbcUtils;
import org.folio.search.utils.JsonConverter;
import org.folio.spring.FolioExecutionContext;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -34,10 +33,6 @@ protected MergeRangeRepository(JdbcTemplate jdbcTemplate,
super(jdbcTemplate, jsonConverter, context);
}

public void truncateMergeRanges() {
JdbcUtils.truncateTable(MERGE_RANGE_TABLE, jdbcTemplate, context);
}

@Transactional
public void saveMergeRanges(List<MergeRangeEntity> mergeRanges) {
var fullTableName = getFullTableName(context, MERGE_RANGE_TABLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ReindexServiceTest {
private ExecutorService reindexExecutor;
@Mock
private ReindexEntityTypeMapper entityTypeMapper;
@Mock
private ReindexCommonService reindexCommonService;
@InjectMocks
private ReindexService reindexService;

Expand Down Expand Up @@ -98,7 +100,7 @@ void submitFullReindex_positive() throws InterruptedException {
reindexService.submitFullReindex(tenant);
ThreadUtils.sleep(Duration.ofSeconds(1));

verify(uploadRangeService).deleteAllRecords();
verify(reindexCommonService).deleteAllRecords();
verify(statusService).recreateMergeStatusRecords();
verify(mergeRangeService).createMergeRanges(tenant);
verify(mergeRangeService).saveMergeRanges(anyList());
Expand Down

0 comments on commit c6d772b

Please sign in to comment.