Skip to content

Commit cc571f4

Browse files
besokiluwatar
authored andcommitted
Saga pattern (iluwatar#1062)
* init repo for role object * add to init * add to init * add first impl * add pattern * add license * add changes * add saga init dsc * add init saga dsc * add changes to dsc * add * add orchestrator * add ch * separate pkgs * add info * add choreogr * rem space * change according to cgeckstyle * add changes according to google style
1 parent 50467c9 commit cc571f4

29 files changed

+1859
-1
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@
183183
<module>data-locality</module>
184184
<module>subclass-sandbox</module>
185185
<module>circuit-breaker</module>
186+
<module>role-object</module>
187+
<module>saga</module>
186188
<module>double-buffer</module>
187189
<module>sharding</module>
188190
</modules>

role-object/src/main/java/com/iluwatar/roleobject/InvestorRole.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public void setAmountToInvest(long amountToInvest) {
4343
}
4444

4545
public String invest() {
46-
return String.format("Investor %s has invested %d dollars", name, amountToInvest);
46+
return String.format("Investor %s has invested %d dollars", name, amountToInvest);
4747
}
4848
}

saga/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
layout: pattern
3+
title: Saga
4+
folder: saga
5+
permalink: /patterns/saga/
6+
categories: Behavioral
7+
tags:
8+
- Java
9+
- Difficulty-Expert
10+
- Idiom
11+
- Distributed communication
12+
---
13+
14+
## Also known as
15+
This pattern has a similar goal with two-phase commit (XA transaction)
16+
17+
## Intent
18+
This pattern is used in distributed services to perform a group of operations atomically.
19+
This is an analog of transaction in a database but in terms of microservices architecture this is executed
20+
in a distributed environment
21+
22+
## Explanation
23+
A saga is a sequence of local transactions in a certain context. If one transaction fails for some reason,
24+
the saga executes compensating transactions(rollbacks) to undo the impact of the preceding transactions.
25+
There are two types of Saga:
26+
27+
- Choreography-Based Saga.
28+
In this approach, there is no central orchestrator.
29+
Each service participating in the Saga performs their transaction and publish events.
30+
The other services act upon those events and perform their transactions.
31+
Also, they may or not publish other events based on the situation.
32+
33+
- Orchestration-Based Saga
34+
In this approach, there is a Saga orchestrator that manages all the transactions and directs
35+
the participant services to execute local transactions based on events.
36+
This orchestrator can also be though of as a Saga Manager.
37+
38+
## Applicability
39+
Use the Saga pattern, if:
40+
- you need to perform a group of operations related to different microservices atomically
41+
- you need to rollback changes in different places in case of failure one of the operation
42+
- you need to take care of data consistency in different places including different databases
43+
- you can not use 2PC(two phase commit)
44+
45+
## Credits
46+
- [Pattern: Saga](https://microservices.io/patterns/data/saga.html)

saga/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
27+
xmlns="http://maven.apache.org/POM/4.0.0"
28+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.22.0-SNAPSHOT</version>
34+
</parent>
35+
36+
<artifactId>saga</artifactId>
37+
<dependencies>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.saga.choreography;
25+
26+
27+
/**
28+
* ChoreographyChapter is an interface representing a contract for an external service.
29+
* In that case, a service needs to make a decision what to do further
30+
* hence the server needs to get all context representing {@link Saga}
31+
*/
32+
public interface ChoreographyChapter {
33+
34+
/**
35+
* In that case, every method is responsible to make a decision on what to do then.
36+
*
37+
* @param saga incoming saga
38+
* @return saga result
39+
*/
40+
Saga execute(Saga saga);
41+
42+
/**
43+
* get name method.
44+
* @return service name.
45+
*/
46+
String getName();
47+
48+
/**
49+
* The operation executed in general case.
50+
*
51+
* @param saga incoming saga
52+
* @return result {@link Saga}
53+
*/
54+
Saga process(Saga saga);
55+
56+
/**
57+
* The operation executed in rollback case.
58+
*
59+
* @param saga incoming saga
60+
* @return result {@link Saga}
61+
*/
62+
Saga rollback(Saga saga);
63+
64+
65+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.saga.choreography;
25+
26+
27+
/**
28+
* Class representing a service to book a fly.
29+
*/
30+
public class FlyBookingService extends Service {
31+
public FlyBookingService(ServiceDiscoveryService service) {
32+
super(service);
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return "booking a Fly";
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.saga.choreography;
25+
26+
27+
/**
28+
* Class representing a service to book a hotel.
29+
*/
30+
public class HotelBookingService extends Service {
31+
public HotelBookingService(ServiceDiscoveryService service) {
32+
super(service);
33+
}
34+
35+
@Override
36+
public String getName() {
37+
return "booking a Hotel";
38+
}
39+
40+
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.saga.choreography;
25+
26+
27+
/**
28+
* Class representing a service to init a new order.
29+
*/
30+
public class OrderService extends Service {
31+
32+
public OrderService(ServiceDiscoveryService service) {
33+
super(service);
34+
}
35+
36+
@Override
37+
public String getName() {
38+
return "init an order";
39+
}
40+
}

0 commit comments

Comments
 (0)