Skip to content

Commit

Permalink
pass application instance id to generate accession
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi committed Apr 26, 2024
1 parent 777b892 commit 67b73ef
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface AccessioningService<MODEL, HASH, ACCESSION> {
* @return List of wrapper objects containing the accessioned objects and their associated accessions and hashes
* @throws AccessionCouldNotBeGeneratedException when accession could not be generated
*/
List<GetOrCreateAccessionWrapper<MODEL, HASH, ACCESSION>> getOrCreate(List<? extends MODEL> messages)
List<GetOrCreateAccessionWrapper<MODEL, HASH, ACCESSION>> getOrCreate(List<? extends MODEL> messages, String applicationInstanceId)
throws AccessionCouldNotBeGeneratedException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ public BasicAccessioningService(AccessionGenerator<MODEL, ACCESSION> accessionGe
}

@Override
public List<GetOrCreateAccessionWrapper<MODEL, HASH, ACCESSION>> getOrCreate(List<? extends MODEL> messages)
public List<GetOrCreateAccessionWrapper<MODEL, HASH, ACCESSION>> getOrCreate(List<? extends MODEL> messages,
String applicationInstanceId)
throws AccessionCouldNotBeGeneratedException {
return saveAccessions(accessionGenerator.generateAccessions(mapHashOfMessages(messages)));
return saveAccessions(accessionGenerator.generateAccessions(mapHashOfMessages(messages), applicationInstanceId));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public DecoratedAccessioningService(AccessioningService<MODEL, HASH, DB_ACCESSIO
}

@Override
public List<GetOrCreateAccessionWrapper<MODEL, HASH, ACCESSION>> getOrCreate(List<? extends MODEL> messages)
public List<GetOrCreateAccessionWrapper<MODEL, HASH, ACCESSION>> getOrCreate(List<? extends MODEL> messages, String applicationInstanceId)
throws AccessionCouldNotBeGeneratedException {
return getOrCreateDecorate(service.getOrCreate(messages));
return getOrCreateDecorate(service.getOrCreate(messages, applicationInstanceId));
}

private List<GetOrCreateAccessionWrapper<MODEL, HASH, ACCESSION>> getOrCreateDecorate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface AccessionGenerator<MODEL, ACCESSION> {
* @return List of wrapper objects containing the accessioned objects and their associated accessions and hashes
* @throws AccessionCouldNotBeGeneratedException when accession could not be generated
*/
<HASH> List<AccessionWrapper<MODEL, HASH, ACCESSION>> generateAccessions(Map<HASH, MODEL> messages)
<HASH> List<AccessionWrapper<MODEL, HASH, ACCESSION>> generateAccessions(Map<HASH, MODEL> messages, String applicationInstanceId)
throws AccessionCouldNotBeGeneratedException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SingleAccessionGenerator(Function<MODEL, ACCESSION> generateAccessionFunc
}

@Override
public <HASH> List<AccessionWrapper<MODEL, HASH, ACCESSION>> generateAccessions(Map<HASH, MODEL> messages) {
public <HASH> List<AccessionWrapper<MODEL, HASH, ACCESSION>> generateAccessions(Map<HASH, MODEL> messages, String applicationInstanceId) {
return messages.entrySet()
.stream()
.map(entry -> new AccessionWrapper<>(generateAccessionFunction.apply(entry.getValue()), entry.getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ protected Function<MODEL, DTO> getModelToDTO() {
return modelToDTO;
}

@RequestMapping(method = RequestMethod.POST, produces = "application/json",
@RequestMapping(value = "/{applicationInstanceId}", method = RequestMethod.POST, produces = "application/json",
consumes = "application/json")
public List<GetOrCreateAccessionResponseDTO<DTO, MODEL, HASH, ACCESSION>> generateAccessions(
@RequestBody @Valid List<DTO> dtos) throws AccessionCouldNotBeGeneratedException {
return service.getOrCreate(dtos).stream()
@PathVariable String applicationInstanceId, @RequestBody @Valid List<DTO> dtos)
throws AccessionCouldNotBeGeneratedException {
return service.getOrCreate(dtos, applicationInstanceId).stream()
.map(accessionModel -> new GetOrCreateAccessionResponseDTO<>(accessionModel, modelToDTO))
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.junit.Assert.assertEquals;

public class SingleAccessionGeneratorTest {
private static String APPLICATION_INSTANCE_ID = "TEST_APPPLICATION_INSTANCE_ID";

private class TestUser {

Expand All @@ -56,7 +57,7 @@ public void testSingleAccessionGenerator() {
hashToModel.put("hash1", new TestUser("test_name1", "test_surname1"));
hashToModel.put("hash2", new TestUser("test_name2", "test_surname2"));

List<AccessionWrapper<TestUser, String, String>> accessions = generator.generateAccessions(hashToModel);
List<AccessionWrapper<TestUser, String, String>> accessions = generator.generateAccessions(hashToModel, APPLICATION_INSTANCE_ID);
assertEquals(3, accessions.size());
assertAccession(0, accessions, null);
}
Expand Down Expand Up @@ -85,7 +86,7 @@ public void testOfHashAccession() {
hashToModel.put("hash1", new TestUser("test_name1", "test_surname1"));
hashToModel.put("hash2", new TestUser("test_name2", "test_surname2"));

List<AccessionWrapper<TestUser, String, String>> accessions = generator.generateAccessions(hashToModel);
List<AccessionWrapper<TestUser, String, String>> accessions = generator.generateAccessions(hashToModel, APPLICATION_INSTANCE_ID);
assertEquals(3, accessions.size());
assertAccession(0, accessions, new SHA1HashingFunction());
}
Expand All @@ -101,7 +102,7 @@ public void testOfSHA1HashAccession() {
hashToModel.put("hash1", new TestUser("test_name1", "test_surname1"));
hashToModel.put("hash2", new TestUser("test_name2", "test_surname2"));

List<AccessionWrapper<TestUser, String, String>> accessions = generator.generateAccessions(hashToModel);
List<AccessionWrapper<TestUser, String, String>> accessions = generator.generateAccessions(hashToModel, APPLICATION_INSTANCE_ID);
assertEquals(3, accessions.size());
assertAccession(0, accessions, new SHA1HashingFunction());
}
Expand All @@ -117,7 +118,7 @@ public void testPostSaveAction() {
hashToModel.put("hash1", new TestUser("test_name1", "test_surname1"));
hashToModel.put("hash2", new TestUser("test_name2", "test_surname2"));

List<AccessionWrapper<TestUser, String, String>> modelAccessions = generator.generateAccessions(hashToModel);
List<AccessionWrapper<TestUser, String, String>> modelAccessions = generator.generateAccessions(hashToModel, APPLICATION_INSTANCE_ID);
Set<String> accessions = modelAccessions.stream().map(AccessionWrapper::getAccession).collect(Collectors.toSet());
generator.postSave(new SaveResponse<>(accessions, new HashSet<>()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
@DataJpaTest
@ContextConfiguration(classes = {TestJpaDatabaseServiceTestConfiguration.class})
public class BasicAccessioningServiceTest {

private static String APPLICATION_INSTANCE_ID = "TEST_APPPLICATION_INSTANCE_ID";
@Autowired
private TestRepository repository;

Expand All @@ -62,7 +62,7 @@ public void accessionNotRepeatedElements() throws AccessionCouldNotBeGeneratedEx
TestModel.of("service-test-1"),
TestModel.of("service-test-2"),
TestModel.of("service-test-3")
));
), APPLICATION_INSTANCE_ID);
assertEquals(3, accessions.size());
}

Expand All @@ -74,7 +74,7 @@ public void accessionWithRepeatedElementsReturnsUnique() throws AccessionCouldNo
TestModel.of("service-test-2"),
TestModel.of("service-test-2"),
TestModel.of("service-test-3")
));
), APPLICATION_INSTANCE_ID);
assertEquals(3, accessions.size());
}

Expand All @@ -94,7 +94,7 @@ public void getAlreadyGeneratedAccessionsReturnsGeneratedOnly() throws Accession
accessioningService.getOrCreate(
Arrays.asList(
TestModel.of("service-test-3")
));
), APPLICATION_INSTANCE_ID);

List<AccessionWrapper<TestModel, String, String>> accessions = accessioningService.get(Arrays.asList(
TestModel.of("service-test-1"),
Expand All @@ -111,13 +111,13 @@ public void accessioningMultipleTimesTheSameObjectReturnsTheSameAccession()
List<GetOrCreateAccessionWrapper<TestModel, String, String>> accession1 = accessioningService.getOrCreate(
Arrays.asList(
TestModel.of("service-test-3")
));
), APPLICATION_INSTANCE_ID);
TestTransaction.end();

List<GetOrCreateAccessionWrapper<TestModel, String, String>> accession2 = accessioningService.getOrCreate(
Arrays.asList(
TestModel.of("service-test-3")
));
), APPLICATION_INSTANCE_ID);
assertEquals(1, accession2.size());
assertEquals(accession1.get(0).getAccession(), accession2.get(0).getAccession());

Expand All @@ -136,14 +136,14 @@ public void updateFailsWhenAccessionDoesNotExist() throws AccessionDoesNotExistE
@Test(expected = HashAlreadyExistsException.class)
public void updateFailsWhenAccessionAlreadyExists() throws AccessionDoesNotExistException,
HashAlreadyExistsException, AccessionCouldNotBeGeneratedException, AccessionMergedException, AccessionDeprecatedException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-3"), TestModel.of("test-4")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-3"), TestModel.of("test-4")), APPLICATION_INSTANCE_ID);
accessioningService.update("id-service-test-3", 1, TestModel.of("test-4"));
}

@Test
public void testUpdate() throws AccessionDoesNotExistException,
HashAlreadyExistsException, AccessionCouldNotBeGeneratedException, AccessionMergedException, AccessionDeprecatedException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-3")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-3")), APPLICATION_INSTANCE_ID);
final AccessionVersionsWrapper<TestModel, String, String> updatedAccession =
accessioningService.update("id-service-test-3", 1, TestModel.of("test-3b"));

Expand All @@ -163,7 +163,7 @@ public void testUpdate() throws AccessionDoesNotExistException,
@Test
public void testPatch() throws AccessionCouldNotBeGeneratedException, AccessionDeprecatedException,
AccessionDoesNotExistException, AccessionMergedException, HashAlreadyExistsException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-3")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-3")), APPLICATION_INSTANCE_ID);
final AccessionVersionsWrapper<TestModel, String, String> accession =
accessioningService.patch("id-service-test-3", TestModel.of("test-3b"));
assertEquals(2, accession.getModelWrappers().size());
Expand All @@ -182,7 +182,7 @@ public void testPatch() throws AccessionCouldNotBeGeneratedException, AccessionD
@Test
public void testGetAccessionVersion() throws AccessionCouldNotBeGeneratedException, AccessionDoesNotExistException,
HashAlreadyExistsException, AccessionMergedException, AccessionDeprecatedException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-accession-version")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-accession-version")), APPLICATION_INSTANCE_ID);
accessioningService.patch("id-service-test-accession-version", TestModel.of("test-accession-version-b"));

final AccessionWrapper<TestModel, String, String> version1 = accessioningService
Expand All @@ -196,7 +196,7 @@ public void testGetAccessionVersion() throws AccessionCouldNotBeGeneratedExcepti
@Test
public void testDeprecate() throws AccessionCouldNotBeGeneratedException, AccessionMergedException,
AccessionDoesNotExistException, AccessionDeprecatedException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-version")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-version")), APPLICATION_INSTANCE_ID);
doDeprecateAndAssert("id-service-test-deprecate-version");
}

Expand All @@ -220,15 +220,15 @@ public void testDeprecateAccessionDoesNotExist() throws AccessionCouldNotBeGener
@Test(expected = AccessionDeprecatedException.class)
public void testDeprecateTwice() throws AccessionCouldNotBeGeneratedException, AccessionMergedException,
AccessionDoesNotExistException, AccessionDeprecatedException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-version-2")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-version-2")), APPLICATION_INSTANCE_ID);
accessioningService.deprecate("id-service-test-deprecate-version-2", "Reasons");
accessioningService.deprecate("id-service-test-deprecate-version-2", "Reasons");
}

@Test
public void testDeprecateUpdated() throws AccessionCouldNotBeGeneratedException, AccessionMergedException,
AccessionDoesNotExistException, AccessionDeprecatedException, HashAlreadyExistsException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-update-version")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-update-version")), APPLICATION_INSTANCE_ID);
accessioningService.update("id-service-test-deprecate-update-version", 1,
TestModel.of("test-deprecate-update-version-updated!"));
doDeprecateAndAssert("id-service-test-deprecate-update-version");
Expand All @@ -237,7 +237,7 @@ public void testDeprecateUpdated() throws AccessionCouldNotBeGeneratedException,
@Test
public void testDeprecatePatched() throws AccessionCouldNotBeGeneratedException, AccessionMergedException,
AccessionDoesNotExistException, AccessionDeprecatedException, HashAlreadyExistsException {
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-patch-version")));
accessioningService.getOrCreate(Arrays.asList(TestModel.of("test-deprecate-patch-version")), APPLICATION_INSTANCE_ID);
accessioningService.patch("id-service-test-deprecate-patch-version",
TestModel.of("test-deprecate-update-version-patched!"));
doDeprecateAndAssert("id-service-test-deprecate-patch-version");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
@DataJpaTest
@ContextConfiguration(classes = {TestJpaDatabaseServiceTestConfiguration.class})
public class DecoratedAccessioningServiceTest {

private static String APPLICATION_INSTANCE_ID = "TEST_APPPLICATION_INSTANCE_ID";
@Autowired
private AccessioningService<TestModel, String, String> accessioningService;

Expand All @@ -52,7 +52,7 @@ public void assertGetOrCreate() throws AccessionCouldNotBeGeneratedException {
List<GetOrCreateAccessionWrapper<TestModel, String, String>> accessions = getPrefixedService().getOrCreate(
Arrays.asList(
TestModel.of("service-test-1")
));
), APPLICATION_INSTANCE_ID);
assertEquals(1, accessions.size());
assertEquals("prefix-id-service-service-test-1", accessions.get(0).getAccession());
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public void assertMerge() throws AccessionMergedException, AccessionDoesNotExist
Arrays.asList(
TestModel.of("service-test-1"),
TestModel.of("service-test-2")
));
), APPLICATION_INSTANCE_ID);
assertEquals(2, accessions.size());
getPrefixedService().merge("prefix-id-service-service-test-1", "prefix-id-service-service-test-2", "reason");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@ public class MonotonicAccessionGenerator<MODEL> implements AccessionGenerator<MO

private final BlockManager blockManager;
private final String categoryId;
private final String applicationInstanceId;
private final ContiguousIdBlockService blockService;
private MonotonicDatabaseService monotonicDatabaseService;

private boolean SHUTDOWN = false;
private boolean RUN_RECOVERY = true;

public MonotonicAccessionGenerator(String categoryId,
String applicationInstanceId,
ContiguousIdBlockService contiguousIdBlockService,
MonotonicDatabaseService monotonicDatabaseService) {
this.categoryId = categoryId;
this.applicationInstanceId = applicationInstanceId;
this.blockService = contiguousIdBlockService;
this.monotonicDatabaseService = monotonicDatabaseService;
assertBlockParametersAreInitialized(blockService, categoryId);
Expand All @@ -70,7 +67,7 @@ private static void assertBlockParametersAreInitialized(ContiguousIdBlockService
}
}

private void recoverState() {
private void recoverState(String applicationInstanceId) {
if (RUN_RECOVERY && monotonicDatabaseService != null) {
List<ContiguousIdBlock> uncompletedBlocks = blockService
.reserveUncompletedBlocksForCategoryIdAndApplicationInstanceId(categoryId, applicationInstanceId);
Expand Down Expand Up @@ -103,12 +100,12 @@ private void recoverStateForElements(long[] committedElements) throws AccessionI
blockService.save(blockManager.recoverState(committedElements));
}

public synchronized long[] generateAccessions(int numAccessionsToGenerate)
public synchronized long[] generateAccessions(int numAccessionsToGenerate, String applicationInstanceId)
throws AccessionCouldNotBeGeneratedException {
checkAccessionGeneratorNotShutDown();
recoverState();
recoverState(applicationInstanceId);
long[] accessions = new long[numAccessionsToGenerate];
reserveNewBlocksUntilSizeIs(numAccessionsToGenerate);
reserveNewBlocksUntilSizeIs(numAccessionsToGenerate, applicationInstanceId);

int i = 0;
while (i < numAccessionsToGenerate) {
Expand All @@ -127,7 +124,7 @@ public synchronized long[] generateAccessions(int numAccessionsToGenerate)
*
* @param totalAccessionsToGenerate
*/
private synchronized void reserveNewBlocksUntilSizeIs(int totalAccessionsToGenerate) {
private synchronized void reserveNewBlocksUntilSizeIs(int totalAccessionsToGenerate, String applicationInstanceId) {
while (!blockManager.hasAvailableAccessions(totalAccessionsToGenerate)) {
ExponentialBackOff.execute(() -> reserveNewBlock(categoryId, applicationInstanceId), 10, 30);
}
Expand All @@ -153,10 +150,10 @@ public synchronized MonotonicRangePriorityQueue getAvailableRanges() {
}

@Override
public <HASH> List<AccessionWrapper<MODEL, HASH, Long>> generateAccessions(Map<HASH, MODEL> messages)
public <HASH> List<AccessionWrapper<MODEL, HASH, Long>> generateAccessions(Map<HASH, MODEL> messages, String applicationInstanceId)
throws AccessionCouldNotBeGeneratedException {
checkAccessionGeneratorNotShutDown();
long[] accessions = generateAccessions(messages.size());
long[] accessions = generateAccessions(messages.size(), applicationInstanceId);
int i = 0;
List<AccessionWrapper<MODEL, HASH, Long>> accessionedModels = new ArrayList<>();
for (Map.Entry<HASH, ? extends MODEL> entry : messages.entrySet()) {
Expand Down
Loading

0 comments on commit 67b73ef

Please sign in to comment.