-
Notifications
You must be signed in to change notification settings - Fork 12
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
EVA-3570 Added recovery agent for running recovery of blocks #88
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...ac/ebi/ampt2d/commons/accession/generators/monotonic/MonotonicAccessionRecoveryAgent.java
This file contains 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,69 @@ | ||
package uk.ac.ebi.ampt2d.commons.accession.generators.monotonic; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.entities.ContiguousIdBlock; | ||
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.service.ContiguousIdBlockService; | ||
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.service.MonotonicDatabaseService; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class MonotonicAccessionRecoveryAgent { | ||
private final static Logger logger = LoggerFactory.getLogger(MonotonicAccessionRecoveryAgent.class); | ||
|
||
private final ContiguousIdBlockService blockService; | ||
private final MonotonicDatabaseService monotonicDatabaseService; | ||
|
||
public MonotonicAccessionRecoveryAgent(ContiguousIdBlockService blockService, | ||
MonotonicDatabaseService monotonicDatabaseService) { | ||
this.blockService = blockService; | ||
this.monotonicDatabaseService = monotonicDatabaseService; | ||
} | ||
|
||
public void runRecovery(String categoryId, String applicationInstanceId, LocalDateTime lastUpdatedTime) { | ||
logger.info("Starting recovering of blocks for category " + categoryId); | ||
List<ContiguousIdBlock> blocksToRecover = blockService.allBlocksForCategoryIdReservedBeforeTheGivenTimeFrame(categoryId, lastUpdatedTime); | ||
logger.info("List of block ids to recover : " + blocksToRecover.stream().map(b -> Long.toString(b.getId())) | ||
.collect(Collectors.joining(","))); | ||
for (ContiguousIdBlock block : blocksToRecover) { | ||
logger.info("Recovering Block: " + block); | ||
if (block.getLastCommitted() == block.getLastValue()) { | ||
logger.info("Block is already completely used, not need to run recovery. Releasing the block."); | ||
setAppInstanceIdAndReleaseBlock(applicationInstanceId, block); | ||
continue; | ||
} | ||
|
||
// run recover state for a block using BlockManager's recover state method | ||
Set<ContiguousIdBlock> blockSet = recoverStateForBlock(block); | ||
|
||
if (blockSet.isEmpty()) { | ||
// if block's last committed is correctly set, BlockManager's recover method will return an empty set | ||
logger.info("Block's last committed is correct. No updates to last_committed. Releasing the block."); | ||
setAppInstanceIdAndReleaseBlock(applicationInstanceId, block); | ||
} else { | ||
ContiguousIdBlock blockToUpdate = blockSet.iterator().next(); | ||
logger.info("Recovery ran successfully for block. Last committed updated to " + block.getLastCommitted() | ||
+ ". Saving and releasing the block."); | ||
setAppInstanceIdAndReleaseBlock(applicationInstanceId, blockToUpdate); | ||
} | ||
} | ||
} | ||
|
||
private Set<ContiguousIdBlock> recoverStateForBlock(ContiguousIdBlock block) { | ||
BlockManager blockManager = new BlockManager(); | ||
blockManager.addBlock(block); | ||
MonotonicRange monotonicRange = blockManager.getAvailableRanges().poll(); | ||
long[] committedElements = monotonicDatabaseService.getAccessionsInRanges(Collections.singletonList(monotonicRange)); | ||
return blockManager.recoverState(committedElements); | ||
} | ||
|
||
private void setAppInstanceIdAndReleaseBlock(String applicationInstanceId, ContiguousIdBlock block) { | ||
block.setApplicationInstanceId(applicationInstanceId); | ||
block.releaseReserved(); | ||
blockService.save(block); | ||
} | ||
} |
This file contains 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 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 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
102 changes: 102 additions & 0 deletions
102
...bi/ampt2d/commons/accession/generators/monotonic/MonotonicAccessionRecoveryAgentTest.java
This file contains 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,102 @@ | ||
package uk.ac.ebi.ampt2d.commons.accession.generators.monotonic; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import uk.ac.ebi.ampt2d.commons.accession.core.models.AccessionWrapper; | ||
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.entities.ContiguousIdBlock; | ||
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.repositories.ContiguousIdBlockRepository; | ||
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.service.ContiguousIdBlockService; | ||
import uk.ac.ebi.ampt2d.commons.accession.service.BasicSpringDataRepositoryMonotonicDatabaseService; | ||
import uk.ac.ebi.ampt2d.test.configuration.MonotonicAccessionGeneratorTestConfiguration; | ||
import uk.ac.ebi.ampt2d.test.configuration.TestMonotonicDatabaseServiceTestConfiguration; | ||
import uk.ac.ebi.ampt2d.test.models.TestModel; | ||
import uk.ac.ebi.ampt2d.test.persistence.TestMonotonicEntity; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.LongStream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(SpringRunner.class) | ||
@DataJpaTest | ||
@ContextConfiguration(classes = {MonotonicAccessionGeneratorTestConfiguration.class, TestMonotonicDatabaseServiceTestConfiguration.class}) | ||
public class MonotonicAccessionRecoveryAgentTest { | ||
private static final String TEST_CATEGORY = "TEST_CATEGORY"; | ||
private static final String TEST_APP_INSTANCE_ID = "TEST_APP_INSTANCE_ID"; | ||
private static final String TEST_RECOVERY_AGENT_APP_INSTANCE_ID = "TEST_RECOVERY_AGENT_APP_INSTANCE_ID"; | ||
|
||
@Autowired | ||
private BasicSpringDataRepositoryMonotonicDatabaseService<TestModel, TestMonotonicEntity> monotonicDBService; | ||
@Autowired | ||
private ContiguousIdBlockRepository repository; | ||
@Autowired | ||
private ContiguousIdBlockService service; | ||
|
||
@Test | ||
public void testRunRecovery() throws InterruptedException { | ||
// block1 does not have any accessions used | ||
ContiguousIdBlock block1 = new ContiguousIdBlock(TEST_CATEGORY, TEST_APP_INSTANCE_ID, 0, 100); | ||
repository.save(block1); | ||
|
||
// block2 is full and has all accessions used | ||
ContiguousIdBlock block2 = new ContiguousIdBlock(TEST_CATEGORY, TEST_APP_INSTANCE_ID, 100, 100); | ||
block2.setLastCommitted(199); | ||
repository.save(block2); | ||
|
||
// block3 has some of the accessions used but not captured in the block's table | ||
ContiguousIdBlock block3 = new ContiguousIdBlock(TEST_CATEGORY, TEST_APP_INSTANCE_ID, 200, 100); | ||
repository.save(block3); | ||
// save some accessions in db that are not captured in block3 | ||
List<AccessionWrapper<TestModel, String, Long>> accessionsSet = LongStream.range(200l, 225l) | ||
.boxed() | ||
.map(longAcc -> new AccessionWrapper<>(longAcc, "hash-1" + longAcc, TestModel.of("test-obj-1-" + longAcc))) | ||
.collect(Collectors.toList()); | ||
monotonicDBService.save(accessionsSet); | ||
|
||
// block4 should not be recovered as it is after the recover cut off time | ||
Thread.sleep(2000); | ||
ContiguousIdBlock block4 = new ContiguousIdBlock(TEST_CATEGORY, TEST_APP_INSTANCE_ID, 300, 100); | ||
repository.save(block4); | ||
|
||
// run recovery through recovery agent | ||
LocalDateTime recoverCutOffTime = block3.getLastUpdatedTimestamp(); | ||
MonotonicAccessionRecoveryAgent recoveryAgent = new MonotonicAccessionRecoveryAgent(service, monotonicDBService); | ||
recoveryAgent.runRecovery(TEST_CATEGORY, TEST_RECOVERY_AGENT_APP_INSTANCE_ID, recoverCutOffTime); | ||
|
||
|
||
List<ContiguousIdBlock> blockList = StreamSupport.stream(repository.findAll().spliterator(), false) | ||
.sorted(Comparator.comparing(ContiguousIdBlock::getFirstValue)) | ||
.collect(Collectors.toList()); | ||
assertEquals(4, blockList.size()); | ||
|
||
assertEquals(TEST_RECOVERY_AGENT_APP_INSTANCE_ID, blockList.get(0).getApplicationInstanceId()); | ||
assertEquals(0, blockList.get(0).getFirstValue()); | ||
assertEquals(-1, blockList.get(0).getLastCommitted()); | ||
assertTrue(blockList.get(0).isNotReserved()); | ||
|
||
assertEquals(TEST_RECOVERY_AGENT_APP_INSTANCE_ID, blockList.get(1).getApplicationInstanceId()); | ||
assertEquals(100, blockList.get(1).getFirstValue()); | ||
assertEquals(199, blockList.get(1).getLastCommitted()); | ||
assertTrue(blockList.get(1).isNotReserved()); | ||
|
||
assertEquals(TEST_RECOVERY_AGENT_APP_INSTANCE_ID, blockList.get(2).getApplicationInstanceId()); | ||
assertEquals(200, blockList.get(2).getFirstValue()); | ||
assertEquals(224, blockList.get(2).getLastCommitted()); | ||
assertTrue(blockList.get(2).isNotReserved()); | ||
|
||
assertEquals(TEST_APP_INSTANCE_ID, blockList.get(3).getApplicationInstanceId()); | ||
assertEquals(300, blockList.get(3).getFirstValue()); | ||
assertEquals(299, blockList.get(3).getLastCommitted()); | ||
assertTrue(blockList.get(3).isReserved()); | ||
} | ||
|
||
} |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like we should add logging statement or have a way to report how many blocks were processed and how many blocks were recovered in each category.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added logger statements