Skip to content

Commit

Permalink
feat(re-index): delete all records from consortium_instance on full r…
Browse files Browse the repository at this point in the history
…e-index (#575)

Closes: MSEARCH-744
  • Loading branch information
psmagin committed May 14, 2024
1 parent 109dc76 commit 65c3d80
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
18 changes: 4 additions & 14 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
## v3.2.6 2024-05-14
### Bug fixes
* Do additional search request on browse before getting backward succeeding in order to find preceding results ([MSEARCH-705](https://folio-org.atlassian.net/browse/MSEARCH-705))
---

## v3.2.5 2024-05-10
### Bug fixes
* Keep right context in resource-id thread ([MSEARCH-754](https://folio-org.atlassian.net/browse/MSEARCH-754))

---

## v3.2.4 2024-05-03
### Bug fixes
* Fix title count when on central tenant ([MSEARCH-745](https://issues.folio.org/browse/MSEARCH-745))
### Tech Dept
* Re-Index: delete all records from consortium_instance on full re-index ([MSEARCH-744](https://folio-org.atlassian.net/browse/MSEARCH-744))

---

## v3.2.3 2024-04-23
## v3.2.5 2024-05-10
### Bug fixes
* Instance search: make "all" search field option to search by full-text fields ([MSEARCH-606](https://issues.folio.org/browse/MSEARCH-606))

---
* Keep right context in resource-id thread ([MSEARCH-754](https://folio-org.atlassian.net/browse/MSEARCH-754))

## v3.2.2 2024-04-10
### Bug fixes
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/folio/search/service/IndexService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.folio.search.exception.SearchServiceException;
import org.folio.search.repository.IndexNameProvider;
import org.folio.search.repository.IndexRepository;
import org.folio.search.service.consortium.ConsortiumInstanceService;
import org.folio.search.service.consortium.TenantProvider;
import org.folio.search.service.es.SearchMappingsHelper;
import org.folio.search.service.es.SearchSettingsHelper;
Expand All @@ -44,6 +45,7 @@ public class IndexService {
private final SearchSettingsHelper settingsHelper;
private final ResourceReindexClient resourceReindexClient;
private final ResourceDescriptionService resourceDescriptionService;
private final ConsortiumInstanceService consortiumInstanceService;
private final IndexNameProvider indexNameProvider;
private final TenantProvider tenantProvider;

Expand Down Expand Up @@ -137,14 +139,17 @@ public void createIndexIfNotExist(String resourceName, String tenantId) {
*/
public ReindexJob reindexInventory(String tenantId, ReindexRequest reindexRequest) {
var resources = getResourceNamesToReindex(reindexRequest);
var resource = normalizeResourceName(resources.get(0));
if (reindexRequest != null && TRUE.equals(reindexRequest.getRecreateIndex())
&& notConsortiumMemberTenant(tenantId)) {
resources.forEach(resourceName -> {
dropIndex(resourceName, tenantId);
createIndex(resourceName, tenantId, reindexRequest.getIndexSettings());
if (INSTANCE_RESOURCE.equals(resource)) {
consortiumInstanceService.deleteAll();
}
});
}
var resource = normalizeResourceName(resources.get(0));
var reindexUri = fromUriString(RESOURCE_STORAGE_REINDEX_URI).buildAndExpand(resource).toUri();
return resourceReindexClient.submitReindex(reindexUri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class ConsortiumInstanceRepository {

private static final String SELECT_BY_ID_SQL = "SELECT * FROM %s WHERE instance_id IN (%s)";
private static final String DELETE_BY_TENANT_AND_ID_SQL = "DELETE FROM %s WHERE tenant_id = ? AND instance_id = ?;";
private static final String DELETE_ALL_SQL = "DELETE FROM %s;";
private static final String UPSERT_SQL = """
INSERT INTO %s (tenant_id, instance_id, json, created_date, updated_date)
VALUES (?, ?, ?::json, ?, ?)
Expand Down Expand Up @@ -112,6 +113,10 @@ public List<ConsortiumItem> fetchItems(ConsortiumSearchQueryBuilder searchQueryB
);
}

public void deleteAll() {
jdbcTemplate.update(DELETE_ALL_SQL.formatted(getTableName()));
}

private ConsortiumInstance toConsortiumInstance(ResultSet rs) throws SQLException {
var id = new ConsortiumInstanceId(rs.getString(TENANT_ID_COLUMN), rs.getString(INSTANCE_ID_COLUMN));
return new ConsortiumInstance(id, rs.getString(JSON_COLUMN));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public List<ResourceEvent> deleteInstances(List<ResourceEvent> instanceEvents) {
return consortiumTenantEventsMap.get(false);
}

public void deleteAll() {
consortiumTenantExecutor.run(repository::deleteAll);
}

public List<ResourceEvent> fetchInstances(Iterable<String> instanceIds) {
List<ResourceEvent> resourceEvents = new ArrayList<>();

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/folio/search/service/IndexServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.folio.search.exception.RequestValidationException;
import org.folio.search.repository.IndexNameProvider;
import org.folio.search.repository.IndexRepository;
import org.folio.search.service.consortium.ConsortiumInstanceService;
import org.folio.search.service.consortium.TenantProvider;
import org.folio.search.service.es.SearchMappingsHelper;
import org.folio.search.service.es.SearchSettingsHelper;
Expand Down Expand Up @@ -78,6 +79,8 @@ class IndexServiceTest {
private ResourceDescriptionService resourceDescriptionService;
@Mock
private IndexNameProvider indexNameProvider;
@Mock
private ConsortiumInstanceService consortiumInstanceService;

@Mock
private TenantProvider tenantProvider;
Expand Down Expand Up @@ -257,6 +260,7 @@ void reindexInventory_positive_recreateIndexIsTrue() {

assertThat(actual).isEqualTo(expectedResponse);
verify(indexRepository).dropIndex(indexName);
verify(consortiumInstanceService).deleteAll();
}

@Test
Expand All @@ -273,6 +277,7 @@ void reindexInventory_positive_recreateIndexIsTrue_memberTenant() {

assertThat(actual).isEqualTo(expectedResponse);
verifyNoInteractions(indexRepository);
verifyNoInteractions(consortiumInstanceService);
}

@Test
Expand All @@ -286,6 +291,7 @@ void reindexInventory_positive_recreateIndexIsFalse() {

var actual = indexService.reindexInventory(TENANT_ID, new ReindexRequest());
assertThat(actual).isEqualTo(expectedResponse);
verifyNoInteractions(consortiumInstanceService);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ void testDelete_positive_instanceDeletedByIdAndTenant() {
.containsExactly(tuple(instanceId2, TENANTS[0]), tuple(instanceId1, TENANTS[1]));
}

@Test
@SneakyThrows
void testDeleteAll_positive() {
var instanceId1 = randomId();
var instanceId2 = randomId();
var consortiumInstance1 = consortiumInstance(0, instanceId1, instance(instanceId1));
var consortiumInstance2 = consortiumInstance(0, instanceId2, instance(instanceId2));
var consortiumInstance3 = consortiumInstance(1, instanceId1, instance(instanceId2));

repository.save(List.of(consortiumInstance1, consortiumInstance2, consortiumInstance3));
assertThat(getDbRecords()).hasSize(3);

repository.deleteAll();

assertThat(getDbRecords())
.isEmpty();
}

@Test
@SneakyThrows
void testFetch_positive_instancesFetched() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ void deleteInstances_positive_shouldProcessConsortiumInstances() {
.containsExactlyInAnyOrder(resourceEvents.stream().map(ResourceEvent::getId).toArray(String[]::new));
}

@Test
void deleteAll_positive() {
service.deleteAll();
verify(repository).deleteAll();
verify(consortiumTenantExecutor).run(any());
}

@Test
void fetchInstances_positive_shouldMergeInstancesById() {
var instanceIds = List.of(randomId());
Expand Down

0 comments on commit 65c3d80

Please sign in to comment.