-
Notifications
You must be signed in to change notification settings - Fork 7
CME-239 - Add GET reindexing tasks #1585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SammiChong
wants to merge
52
commits into
CME-238-patch
Choose a base branch
from
CME-239
base: CME-238-patch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
764e68a
Add reindex entity
SammiChong 8eae7a8
Add unit tests and metadata exception
SammiChong 6c0017e
Add GET
SammiChong db33162
Add case_type
SammiChong c186109
Cleanup
SammiChong 9e4eaa1
Add IT and clean up
SammiChong 434db9c
Format error message
SammiChong 0cb50c5
Revert testing
SammiChong 46b8b3f
Cleanup
SammiChong 8cecdf1
Merge branch 'master' into CME-239
SammiChong 6d27919
Cleanup
SammiChong 1b1835e
Add ActiveProfile to IT
SammiChong 41708b4
Update ElasticsearchBaseTest.java
SammiChong b14b75b
Update ElasticsearchBaseTest.java
SammiChong 36c9bb7
testing
SammiChong c9e1768
Add findByIndexName
SammiChong e3be5a6
Add ReindexPersistService
SammiChong aa1716a
Fix async transaction issue
SammiChong 29a26fa
Clean up
SammiChong 64d0e22
Replace overwrite with insert to DB
SammiChong 2e585ef
Cleanup - rename and add tests
SammiChong d4aa313
Add reindex_response
SammiChong 60fbf84
More cleanup
SammiChong 3297c20
Merge branch 'master' into CME-239
SammiChong 6ece42a
Add h2database
SammiChong 3e773cd
Use testcontainers
SammiChong 0d84d90
Add back ElasticsearchContainerInitializer
SammiChong 1061029
Merge branch 'master' into CME-239
SammiChong 6d68668
Return ReindexDTO instead of ReindexEntity
SammiChong c8bf2a9
Merge branch 'CME-239' of https://github.com/hmcts/ccd-definition-sto…
SammiChong b57eecb
Fix code smells
SammiChong d15f5d9
Revert !reindex
SammiChong 3044cf3
Merge branch 'master' into CME-239
SammiChong a4aafd0
Remove null check
SammiChong 453c3af
Address review comments
SammiChong 5246cf8
Rename ReindexDTO
SammiChong aa5a82f
Merge branch 'master' into CME-239
SammiChong f2358dd
Merge branch 'master' into CME-239
SammiChong 54b7265
Merge branch 'CME-238-patch' into CME-239
SammiChong 4046b65
Cleanup, add tests
SammiChong 956fa6b
Merge ReindexTaskService with ReindexService
SammiChong 4ccb25b
Checkstyle
SammiChong 35dbf88
Remove transactional
SammiChong 3deab9b
Update ReindexServiceImpl.java
SammiChong 20c2ffc
Merge branch 'CME-238-patch' into CME-239
SammiChong f7c1d3d
Fix tests, add reindexResponse
SammiChong 576425f
Merge branch 'master' into CME-239
SammiChong 283f295
Merge branch 'master' into CME-239
SammiChong 82d0749
Merge branch 'CME-238-patch' into CME-239
kiran-yenigala-hmcts 1a7d8bb
Clean import
SammiChong 00e5241
Remove buildResponse, add logging
SammiChong 73b2e5c
Remove reindex column
SammiChong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Cleanup, add tests
- Loading branch information
commit 4046b6588c96b522941c5a9c87e1c42023691e3c
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...upport/src/main/java/uk/gov/hmcts/ccd/definition/store/elastic/ElasticReindexService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package uk.gov.hmcts.ccd.definition.store.elastic; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.client.GetAliasesResponse; | ||
| import org.elasticsearch.index.reindex.BulkByScrollResponse; | ||
| import org.springframework.beans.factory.ObjectFactory; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Service; | ||
| import uk.gov.hmcts.ccd.definition.store.elastic.client.HighLevelCCDElasticClient; | ||
| import uk.gov.hmcts.ccd.definition.store.elastic.mapping.CaseMappingGenerator; | ||
| import uk.gov.hmcts.ccd.definition.store.elastic.service.ReindexDBService; | ||
| import uk.gov.hmcts.ccd.definition.store.event.DefinitionImportedEvent; | ||
| import uk.gov.hmcts.ccd.definition.store.repository.entity.CaseTypeEntity; | ||
| import uk.gov.hmcts.ccd.definition.store.repository.entity.ReindexEntity; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| public class ElasticReindexService { | ||
|
|
||
| private final CaseMappingGenerator mappingGenerator; | ||
|
|
||
| private final ObjectFactory<HighLevelCCDElasticClient> clientFactory; | ||
|
|
||
| private final ReindexDBService reindexDBService; | ||
|
|
||
| public ElasticReindexService(CaseMappingGenerator mappingGenerator, | ||
| ObjectFactory<HighLevelCCDElasticClient> clientFactory, | ||
| ReindexDBService reindexDBService) { | ||
| this.mappingGenerator = mappingGenerator; | ||
| this.clientFactory = clientFactory; | ||
| this.reindexDBService = reindexDBService; | ||
| } | ||
|
|
||
| @Async("reindexExecutor") | ||
| public void asyncReindex(DefinitionImportedEvent event, | ||
| String baseIndexName, | ||
| CaseTypeEntity caseType) throws IOException { | ||
| HighLevelCCDElasticClient elasticClient = clientFactory.getObject(); | ||
| GetAliasesResponse aliasResponse = elasticClient.getAlias(baseIndexName); | ||
| String indexName = aliasResponse.getAliases().keySet().iterator().next(); | ||
| String newIndexName = incrementIndexNumber(indexName); | ||
| boolean reindexStarted = false; | ||
|
|
||
| //prepare for db | ||
| ReindexEntity reindexEntity = reindexDBService.saveEntity(event.isReindex(), | ||
| event.isDeleteOldIndex(), caseType, newIndexName); | ||
|
|
||
| try { | ||
| //create new index with generated mapping and incremented case type name (no alias update yet) | ||
| String caseMapping = mappingGenerator.generateMapping(caseType); | ||
| log.debug("case mapping: {}", caseMapping); | ||
| elasticClient.setIndexReadOnly(baseIndexName, true); | ||
| elasticClient.createIndexAndMapping(newIndexName, caseMapping); | ||
|
|
||
| //initiate reindexing | ||
| reindexStarted = true; | ||
| handleReindexing(elasticClient, baseIndexName, indexName, newIndexName, event.isDeleteOldIndex()); | ||
| //dummy value for phase 1 | ||
| event.setTaskId("taskID"); | ||
| log.debug("reindexing successful for case type: {}", caseType.getReference()); | ||
| log.debug("task id returned from the import: {}", event.getTaskId()); | ||
| } catch (Exception e) { | ||
| if (!reindexStarted) { | ||
| reindexDBService.updateEntity(newIndexName, e); | ||
| } | ||
| throw new CompletionException(e); | ||
| } | ||
| } | ||
|
|
||
| private void handleReindexing(HighLevelCCDElasticClient elasticClient, String baseIndexName, | ||
| String oldIndexName, String newIndexName, | ||
| boolean deleteOldIndex) { | ||
| elasticClient.reindexData(oldIndexName, newIndexName, new ActionListener<>() { | ||
| @Override | ||
| public void onResponse(BulkByScrollResponse bulkByScrollResponse) { | ||
| try (elasticClient; HighLevelCCDElasticClient highLevelCCDElasticClient = clientFactory.getObject()) { | ||
| //if success set writable and update alias to new index | ||
| log.debug("updating alias from {} to {}", oldIndexName, newIndexName); | ||
| highLevelCCDElasticClient.setIndexReadOnly(baseIndexName, false); | ||
| highLevelCCDElasticClient.updateAlias(baseIndexName, oldIndexName, newIndexName); | ||
| if (deleteOldIndex) { | ||
| log.debug("deleting old index {}", oldIndexName); | ||
| highLevelCCDElasticClient.removeIndex(oldIndexName); | ||
| } | ||
| //set success status and end time for db | ||
| reindexDBService.updateEntity(newIndexName, bulkByScrollResponse.toString()); | ||
| log.info("saved reindex entity" | ||
| + " metadata for case type {} to DB", baseIndexName); | ||
| } catch (IOException e) { | ||
| log.error("failed to clean up after reindexing success", e); | ||
| reindexDBService.updateEntity(newIndexName, e); | ||
| throw new CompletionException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Exception ex) { | ||
| try (elasticClient; HighLevelCCDElasticClient highLevelCCDElasticClient = clientFactory.getObject()) { | ||
| //set failure status, end time and ex for db | ||
| reindexDBService.updateEntity(newIndexName, ex); | ||
|
|
||
| //if failed delete new index, set old index writable | ||
| log.debug("reindexing failed", ex); | ||
| highLevelCCDElasticClient.removeIndex(newIndexName); | ||
| log.debug("{} deleted", newIndexName); | ||
| highLevelCCDElasticClient.setIndexReadOnly(oldIndexName, false); | ||
| log.debug("{} set to writable", oldIndexName); | ||
| } catch (IOException e) { | ||
| log.error("failed to clean up after reindexing failure", e); | ||
| reindexDBService.updateEntity(newIndexName, e); | ||
| throw new CompletionException(e); | ||
| } | ||
| throw new CompletionException(ex); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| String incrementIndexNumber(String indexName) { | ||
| Pattern pattern = Pattern.compile("(.+_cases-)(\\d+)$"); | ||
| Matcher matcher = pattern.matcher(indexName); | ||
|
|
||
| if (!matcher.matches() || matcher.groupCount() < 2) { | ||
| throw new IllegalArgumentException("invalid index name format: " + indexName); | ||
| } | ||
|
|
||
| String prefix = matcher.group(1); | ||
| String numberStr = matcher.group(2); | ||
|
|
||
| int incremented = Integer.parseInt(numberStr) + 1; | ||
| String formattedNumber = StringUtils.leftPad(String.valueOf(incremented), numberStr.length(), '0'); | ||
|
|
||
| String incrementedIndexName = prefix + formattedNumber; | ||
| return incrementedIndexName; | ||
| } | ||
|
|
||
| } | ||
4 changes: 0 additions & 4 deletions
4
...earch-support/src/main/java/uk/gov/hmcts/ccd/definition/store/elastic/ReindexService.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import uk.gov.hmcts.ccd.definition.store.elastic.service.ReindexTaskService; | ||
| import uk.gov.hmcts.ccd.definition.store.elastic.service.ReindexDBService; | ||
| import uk.gov.hmcts.ccd.definition.store.repository.model.ReindexTask; | ||
|
|
||
| import java.util.List; | ||
|
|
@@ -22,11 +22,11 @@ public class ReindexTaskController { | |
|
|
||
| public static final String REINDEX_TASKS_URI = "/elastic-support/reindex/tasks"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| private final ReindexTaskService reindexTaskService; | ||
| private final ReindexDBService reindexDBService; | ||
|
|
||
| @Autowired | ||
| public ReindexTaskController(ReindexTaskService reindexTaskService) { | ||
| this.reindexTaskService = reindexTaskService; | ||
| public ReindexTaskController(ReindexDBService reindexDBService) { | ||
| this.reindexDBService = reindexDBService; | ||
| } | ||
|
|
||
| @GetMapping | ||
|
|
@@ -40,7 +40,7 @@ public ReindexTaskController(ReindexTaskService reindexTaskService) { | |
| public ResponseEntity<List<ReindexTask>> getReindexTasksByCaseType( | ||
| @RequestParam(value = "caseType", required = false) String caseType | ||
| ) { | ||
| List<ReindexTask> response = reindexTaskService.getTasksByCaseType(caseType); | ||
| List<ReindexTask> response = reindexDBService.getTasksByCaseType(caseType); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.