Skip to content

Commit

Permalink
feat(re-index): recreate upload ranges each upload execution (#726)
Browse files Browse the repository at this point in the history
Closes: MSEARCH-934
  • Loading branch information
psmagin authored Jan 6, 2025
1 parent ada7406 commit e6f3f90
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Fix the "Invalid reference" appears after updating ownership ([MSEARCH-915](https://folio-org.atlassian.net/browse/MSEARCH-915))

### Tech Dept
* Recreate upload ranges each upload execution ([MSEARCH-934](https://folio-org.atlassian.net/browse/MSEARCH-934))
* Remove unnecessary LccnNormalizerStructureB ([MSEARCH-926](https://folio-org.atlassian.net/browse/MSEARCH-926))

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void prepareAndSendIndexRanges(ReindexEntityType entityType) {
var repository = Optional.ofNullable(repositories.get(entityType))
.orElseThrow(() -> new UnsupportedOperationException("No repository found for entity type: " + entityType));

var uploadRanges = repository.getUploadRanges(true);
var uploadRanges = repository.createUploadRanges();
statusService.updateReindexUploadStarted(entityType, uploadRanges.size());
indexRangeEventProducer.sendMessages(prepareEvents(uploadRanges));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ON CONFLICT (id)
""";

private static final String SELECT_UPLOAD_RANGE_BY_ENTITY_TYPE_SQL = "SELECT * FROM %s WHERE entity_type = ?;";
private static final String DELETE_UPLOAD_RANGE_SQL = "DELETE FROM %s WHERE entity_type = ?;";
private static final TypeReference<LinkedHashSet<InstanceSubResource>> VALUE_TYPE_REF = new TypeReference<>() { };

protected final ReindexConfigurationProperties reindexConfig;
Expand All @@ -54,14 +55,19 @@ protected UploadRangeRepository(JdbcTemplate jdbcTemplate,
this.reindexConfig = reindexConfig;
}

public List<UploadRangeEntity> getUploadRanges(boolean populateIfNotExist) {
public List<UploadRangeEntity> getUploadRanges() {
var fullTableName = getFullTableName(context, UPLOAD_RANGE_TABLE);
var sql = SELECT_UPLOAD_RANGE_BY_ENTITY_TYPE_SQL.formatted(fullTableName);
var uploadRanges = jdbcTemplate.query(sql, uploadRangeRowMapper(), entityType().getType());

return populateIfNotExist && uploadRanges.isEmpty()
? prepareAndSaveUploadRanges()
: uploadRanges;
return jdbcTemplate.query(sql, uploadRangeRowMapper(), entityType().getType());
}

public List<UploadRangeEntity> createUploadRanges() {
var fullTableName = getFullTableName(context, UPLOAD_RANGE_TABLE);
var deleteSql = DELETE_UPLOAD_RANGE_SQL.formatted(fullTableName);
jdbcTemplate.update(deleteSql, entityType().getType());

return prepareAndSaveUploadRanges();
}

public List<Map<String, Object>> fetchByIdRange(String lower, String upper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void setUp() {
@Test
void prepareAndSendIndexRanges_positive(@Random UploadRangeEntity uploadRange) {
// arrange
when(repository.getUploadRanges(true)).thenReturn(List.of(uploadRange));
when(repository.createUploadRanges()).thenReturn(List.of(uploadRange));

// act
service.prepareAndSendIndexRanges(ReindexEntityType.INSTANCE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.jdbc.Sql;

@IntegrationTest
Expand All @@ -35,9 +35,9 @@
class ReindexJdbcRepositoriesIT {

private @Autowired JdbcTemplate jdbcTemplate;
private @MockBean FolioExecutionContext context;
private @MockBean ReindexConfigurationProperties reindexConfig;
private @MockBean ConsortiumTenantProvider tenantProvider;
private @MockitoBean FolioExecutionContext context;
private @MockitoBean ReindexConfigurationProperties reindexConfig;
private @MockitoBean ConsortiumTenantProvider tenantProvider;
private MergeInstanceRepository mergeRepository;
private UploadInstanceRepository uploadRepository;

Expand Down Expand Up @@ -77,15 +77,15 @@ void updateRangeStatus() {
var mergeRange = mergeRepository.getMergeRanges().stream()
.filter(range -> range.getEntityType().equals(ReindexEntityType.INSTANCE))
.findFirst();
var uploadRange = uploadRepository.getUploadRanges(false).stream()
var uploadRange = uploadRepository.getUploadRanges().stream()
.filter(range -> range.getEntityType().equals(ReindexEntityType.INSTANCE))
.findFirst();

assertThat(mergeRange).isPresent().get()
.matches(range -> timestamp.getTime() == range.getFinishedAt().getTime()
&& ReindexRangeStatus.SUCCESS == range.getStatus() && failCause.equals(range.getFailCause()));
&& ReindexRangeStatus.SUCCESS == range.getStatus() && failCause.equals(range.getFailCause()));
assertThat(uploadRange).isPresent().get()
.matches(range -> timestamp.getTime() == range.getFinishedAt().getTime()
&& ReindexRangeStatus.FAIL == range.getStatus() && failCause.equals(range.getFailCause()));
&& ReindexRangeStatus.FAIL == range.getStatus() && failCause.equals(range.getFailCause()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.springframework.test.context.jdbc.Sql;

@IntegrationTest
Expand All @@ -46,8 +46,8 @@
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class SubjectRepositoryIT {

private @SpyBean JdbcTemplate jdbcTemplate;
private @MockBean FolioExecutionContext context;
private @MockitoSpyBean JdbcTemplate jdbcTemplate;
private @MockitoBean FolioExecutionContext context;
private SubjectRepository repository;
private ReindexConfigurationProperties properties;

Expand All @@ -71,22 +71,12 @@ public String getDBSchemaName(String tenantId) {
}

@Test
void getUploadRanges_returnEmptyList_whenNoUploadRangesAndNotPopulate() {
// act
var ranges = repository.getUploadRanges(false);

// assert
assertThat(ranges).isEmpty();
}

@Test
@Sql("/sql/populate-subjects.sql")
void getUploadRanges_returnList_whenNoUploadRangesAndNotPopulate() {
void getUploadRanges_returnList() {
// arrange
properties.setUploadRangeLevel(1);

// act
var ranges = repository.getUploadRanges(true);
var ranges = repository.createUploadRanges();

// assert
assertThat(ranges)
Expand Down Expand Up @@ -139,7 +129,7 @@ void deleteByInstanceIds_oneSubjectRemovedAndOneInstanceCounterDecremented() {
.contains(
tuple("Sci-Fi", List.of(
Map.of("count", 1, "shared", true, "tenantId", "consortium"),
Map.of("count", 1, "shared", false, "tenantId", "member_tenant"))));
Map.of("count", 1, "shared", false, "tenantId", "member_tenant"))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String getDBSchemaName(String tenantId) {
@Sql({"/sql/populate-instances.sql"})
void getUploadRanges_shouldNotPopulateStatus() {
// act
var uploadRanges = uploadRepository.getUploadRanges(true);
var uploadRanges = uploadRepository.createUploadRanges();
System.out.println(uploadRanges.size());

// assert
Expand Down

0 comments on commit e6f3f90

Please sign in to comment.