SE457 – Service-Oriented Architecture
Homework 4
Due Date: May 25, 2021 5:30PM
SE457 – Service-Oriented Architecture Spring 2021
Homework 4
Due Date: May 25, 2021 5:30PM
100 points (10% of final grade)
Submit your answers to D2L by May 25, 2021 5:30PM. Late homework will not be accepted.
As mentioned in lecture, a saga is a way to implement a transaction that spans services without using 2-phase commit. The purpose of this assignment is to give you practical experience with sagas by writing a simple saga framework and a number of sagas which use them.
Programming
In this section, you will write a simple orchestration-based saga framework and a simulated distributed trans- action which uses this framework. The saga framework will run each activity within the saga successively. If any saga stage fails, the framework will roll back the entire saga by executing each successful action’s compensating transaction.
For this section, a saga represents the entire distributed transaction. A saga consists of one or more saga activitys, which represent the individual steps within the saga (as well as their compensating actions in the case of a rollback). A saga builder is a class which is used to make constructing a saga easier.
For the saga framework, you must implement the following classes with the following signatures:
// Represents an individual step in a saga which can be rolled back.
interface SagaActivity
{
// Attempts to perform the activity. Throws an Exception if the attempt
// fails for whatever reason (e.g. a network failure)
void runAction() throws Exception;
// Undoes a previously-successful activity. For example, if a saga
// runAction() created a file, runCompensatingAction() would delete
// the created file. For the purposes of this assignment, this
// compensating action is assumed to never fail.
void runCompensatingAction();
}
// Represents an entire distributed transaction. Consists of one or more
// SagaActivitys.
class Saga {
// Any constructors you deem appropriate
// Executes the entire Saga by running each activity one-by-one. If
// any SagaActivity in the Saga fails, undoes all previously executed
// activties by running their compensating actions. Compensating actions
// must run in reverse order of their original application. Returns
// true if the entire Saga completed successfully or false otherwise.
public boolean run { … }
}
// A utility class to make constructing sagas easier.
class SagaBuilder {
// Add the specified activity to the to-be-built saga.
public SagaBuilder activity(SagaActivity activity) { … }
Page 1 of 2
SE457 – Service-Oriented Architecture Homework 4 Due Date: May 25, 2021 5:30PM
// Constructs and returns the saga.
public Saga build() { … }
}
In addition to the above saga framework, you must create a console program which constructs a Saga that consists of the following simulated activities:
1. Reserve a car 2. Book a hotel 3. Book a flight
For this assignment, each simulated activity can simply print to the console that it performed the asso- ciated action (e.g. Reserved a car! or Cancelled the car reservation!). (In a real application, each step might perform a REST API call to perform the action for real.)
Your console program must use SagaBuilder to construct and execute two sagas:
1. One in which all three of the above steps succeed.
2. One in which the last step (book a flight) fails, and you can demonstrate that your saga framework
runs the appropriate compensating activites correctly.
Questions
After writing the above code, answer the below questions in a separate Word document or PDF file:
1. What would happen in the above framework if the compensating action failed? How would you change the Saga framework to handle this?
2. What could happen if a saga activity was written as a ”if resource X does not exist, create it” and the compensating action was ”delete resource X”? How would you change the Saga framework to handle this?
3. The above Saga framework does not allow any data to be passed from one activity to another. How would you change the Saga framework to allow passing of data from one activity to another?
4. What would happen if the Saga orchestrator failed? How would you change the Saga framework to handle this?
5. The above Saga framework requires each activity to be executed one after another. How would you change the Saga framework to allow multiple, independent activities to be run in parallel? (Be sure to consider if these activities both depended on the same first activity)
Submission
Submit the following in a single ZIP file:
• The source code to the above program
• A text file, Word document, or PDF demonstrating the behavior of your application • A text file, Word document, or PDF with the answers to the above questions
Page 2 of 2