Skip to content
Snippets Groups Projects
Commit 8515852e authored by harshit aggarwal's avatar harshit aggarwal
Browse files

Adding javaDoc

parent 636024a0
No related branches found
No related tags found
1 merge request!16Adding logic to handle failure scenario during Create Subscription
Pipeline #6569 failed
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
<dependency> <dependency>
<groupId>org.opengroup.osdu</groupId> <groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId> <artifactId>core-lib-azure</artifactId>
<version>0.0.18</version> <version>0.0.21</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.opengroup.osdu</groupId> <groupId>org.opengroup.osdu</groupId>
......
...@@ -64,6 +64,32 @@ public class SubscriptionRepository implements ISubscriptionRepository { ...@@ -64,6 +64,32 @@ public class SubscriptionRepository implements ISubscriptionRepository {
@Autowired @Autowired
private PushSubscription pushSubscription; private PushSubscription pushSubscription;
/**
* @param input Subscription Object
* @return Subscription Object
*
* This method creates a Record for the subscription in CosmosDb along
* with a Push Subscription in Event Grid.
*
* Expected Flow -> Create a record in Cosmos Db along with a Push Subscription in Event Grid
* Possible Failures ->
* 1) Creating PushSubscription in Event Grid
* How it is handled -> Delete the record in Cosmos Db as well
* Possible Issue -> What happens if this delete operation on Cosmos Db fails?
* Users won't be able to create a subscription again with same Topic and
* push endpoint combination since system will generate 409 on next retry
* because of presence of record with same Id in Cosmos Db
*
* How to resolve this?
* createPushSubscriptionIfDoesNotExist method takes care of that
* When Cosmos Db throws 409 while creating a new record we can check if a Push Subscription
* for that Record already exists in Event Grid or not.
* If present -> Return 409
* If not present -> We can try creating the Push Subscription again in Event Grid
* Also we will update the record in Cosmos Db in case other fields might
* be different like description etc.
*
*/
@Override @Override
public Subscription create(Subscription input) { public Subscription create(Subscription input) {
...@@ -78,11 +104,11 @@ public class SubscriptionRepository implements ISubscriptionRepository { ...@@ -78,11 +104,11 @@ public class SubscriptionRepository implements ISubscriptionRepository {
} }
catch (AppException e) { catch (AppException e) {
if(e.getError().getCode() == 409) { if(e.getError().getCode() == 409) {
// This case will handle the scenario where creating PushSubscription in EventGrid failed // This flow is added to handle the scenario where creating PushSubscription in EventGrid failed
// but we failed to delete the corresponding record from Cosmos Db. Since the operation // and deleting the corresponding record from Cosmos Db was also unsuccessful.
// failed last time throwing 500, user should be able to create the subscription with // This will result in an 500 Exception so the user should be able to create the subscription with
// the same topic and pushEndpoint combination. // the same topic and pushEndpoint combination again
SubscriptionDoc output = checkIfPushSubscriptionExists(doc); SubscriptionDoc output = createPushSubscriptionIfDoesNotExist(doc);
input.setNotificationId(output.getNotificationId()); input.setNotificationId(output.getNotificationId());
return input; return input;
} }
...@@ -136,6 +162,12 @@ public class SubscriptionRepository implements ISubscriptionRepository { ...@@ -136,6 +162,12 @@ public class SubscriptionRepository implements ISubscriptionRepository {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
/**
* @param id Identifier for the subscription
* @return true if subscription successfully deleted
* false if subscription not found
* Exception thrown -> AppException with Status Codes 409 and 500
*/
@Override @Override
public boolean delete(String id) { public boolean delete(String id) {
...@@ -195,17 +227,14 @@ public class SubscriptionRepository implements ISubscriptionRepository { ...@@ -195,17 +227,14 @@ public class SubscriptionRepository implements ISubscriptionRepository {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private SubscriptionDoc checkIfPushSubscriptionExists(SubscriptionDoc input) { private SubscriptionDoc createPushSubscriptionIfDoesNotExist(SubscriptionDoc input) {
try { try {
// We are fetching the record from cosmos db again because we want to use the original // We are fetching the record from cosmos db again because we want to use the original
// notification-id to create the Push Subscription // notification-id to create the Push Subscription
Optional<SubscriptionDoc> originalDoc = cosmosStore.findItem(dpsHeaders.getPartitionId(), azureBootstrapConfig.getCosmosDBName(), cosmosContainerConfig.getSubscriptionContainerName(), input.getId(), dpsHeaders.getPartitionId(), SubscriptionDoc.class); Optional<SubscriptionDoc> originalDoc = cosmosStore.findItem(dpsHeaders.getPartitionId(), azureBootstrapConfig.getCosmosDBName(), cosmosContainerConfig.getSubscriptionContainerName(), input.getId(), dpsHeaders.getPartitionId(), SubscriptionDoc.class);
if(!originalDoc.isPresent()) { originalDoc.ifPresent(subscriptionDoc -> input.setNotificationId(subscriptionDoc.getNotificationId()));
throw new AppException(500, "Server Error", "Unexpected error creating subscription");
}
input.setNotificationId(originalDoc.get().getNotificationId());
// We will check if Push Subscription does not exist we should try creating it again since // We will check if Push Subscription does not exist then we should try creating it again since
// the corresponding record in the Cosmos Db is already present // the corresponding record in the Cosmos Db is already present
pushSubscription.getPushSubscription(input.getNotificationId(), input.getTopic()); pushSubscription.getPushSubscription(input.getNotificationId(), input.getTopic());
logger.error("A subscriber already exists with the same topic and endpoint combination"); logger.error("A subscriber already exists with the same topic and endpoint combination");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment