Skip to content

Commit

Permalink
add orchestrator
Browse files Browse the repository at this point in the history
  • Loading branch information
Besok committed Nov 2, 2019
1 parent c4c37d7 commit 3a4677c
Show file tree
Hide file tree
Showing 16 changed files with 554 additions and 108 deletions.
17 changes: 16 additions & 1 deletion saga/src/main/java/com/iluwatar/saga/orchestration/Chapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,28 @@
package com.iluwatar.saga.orchestration;

/**
* Chapter including into Saga
* Chapter is an interface representing a contract for an external service.
* @param <K> is type for passing params
*/
public interface Chapter<K> {

/**
* @return service name.
*/
String getName();

/**
* The operation executed in general case.
* @param value incoming value
* @return result {@link ChapterResult}
*/
ChapterResult<K> process(K value);

/**
* The operation executed in rollback case.
* @param value incoming value
* @return result {@link ChapterResult}
*/
ChapterResult<K> rollback(K value);

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
/*
* The MIT License
* Copyright © 2014-2019 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.saga.orchestration;

/**
* Executing result for chapter
* @param <K> incoming value
*/
public class ChapterResult<K> {
K value;
State state;
private K value;
private State state;

public ChapterResult(K value, State state) {
public K getValue() {
return value;
}

ChapterResult(K value, State state) {
this.value = value;
this.state = state;
}

public boolean isSuccess(){
return state == State.SUCCESS;
}

public static <K> ChapterResult<K> success(K val) {
return new ChapterResult<>(val, State.SUCCESS);
}

public static <K> ChapterResult<K> failure(K val) {
return new ChapterResult<>(val, State.FAILURE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
/*
* The MIT License
* Copyright © 2014-2019 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.saga.orchestration;

/**
* Class representing a service to book a fly
*/
public class FlyBookingService extends Service<String> {
@Override
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
/*
* The MIT License
* Copyright © 2014-2019 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.saga.orchestration;

/**
* Class representing a service to book a hotel
*/
public class HotelBookingService extends Service<String> {
@Override
public String getName() {
return "booking a Hotel";
}


@Override
public ChapterResult<String> rollback(String value) {
if(value.equals("crashed_order")){
logger.info("The Rollback for a chapter '{}' has been started. " +
"The data {} has been failed.The saga has been crashed.",
getName(), value);

return ChapterResult.failure(value);
}

logger.info("The Rollback for a chapter '{}' has been started. The data {} has been rollbacked successfully",
getName(), value);

return super.rollback(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
/*
* The MIT License
* Copyright © 2014-2019 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.saga.orchestration;

/**
* Class representing a service to init a new order.
*/
public class OrderService extends Service<String> {
@Override
public String getName() {
Expand Down
40 changes: 17 additions & 23 deletions saga/src/main/java/com/iluwatar/saga/orchestration/Saga.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,50 @@
import java.util.ArrayList;
import java.util.List;

/**
* Saga representation.
* Saca consists of chapters.
* Every Chapter is executed a certain service.
*/
public class Saga {

private List<Chapter> chapters;
private Result result;

public Saga() {
this.chapters = new ArrayList<>();
this.result = Result.INPROCESS;
}

public void setResult(Result result) {
this.result = result;
}

public boolean isSuccess(){
return result == Result.FINISHED;
}

public boolean isFinished(){
return result != Result.INPROCESS;
}

public Saga chapter(String name, int timeoutInSec) {
this.chapters.add(new Chapter(name, timeoutInSec));
public Saga chapter(String name) {
this.chapters.add(new Chapter(name));
return this;
}


public Chapter get(int idx) {
if (chapters.size() < idx || idx < 0) {
throw new SagaException("idx shoud be less then ch size or more then 0");
}
return chapters.get(idx);
}

public boolean isPresent(int idx) {
return idx >= 0 && idx < chapters.size();
}


public static Saga create() {
return new Saga();
}

public enum Result{
FINISHED,CANCELED, INPROCESS;
public enum Result {
FINISHED, ROLLBACK, CRASHED
}
private static class Chapter {

public static class Chapter {
String name;
int timeoutInSec;

public Chapter(String name, int timeoutInSec) {
public Chapter(String name) {
this.name = name;
this.timeoutInSec = timeoutInSec;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,48 @@
package com.iluwatar.saga.orchestration;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This pattern is used in distributed services to perform a group of operations atomically.
* This is an analog of transaction in a database but in terms of microservices architecture this is executed
* in a distributed environment
*
* A saga is a sequence of local transactions in a certain context. If one transaction fails for some reason,
* the saga executes compensating transactions(rollbacks) to undo the impact of the preceding transactions.
*
* In this approach, there is an orchestrator @see {@link SagaOrchestrator} that manages all the transactions and directs
* the participant services to execute local transactions based on events.
*
* @see Saga
* @see SagaOrchestrator
* @see Service
*/
public class SagaApplication {
private static final Logger logger = LoggerFactory.getLogger(SagaApplication.class);

public static void main(String[] args) {
SagaOrchestrator sagaOrchestrator = new SagaOrchestrator(newSaga(), serviceDiscovery());

}
Saga.Result goodOrder = sagaOrchestrator.execute("god_order");
Saga.Result badOrder = sagaOrchestrator.execute("bad_order");
Saga.Result crashedOrder = sagaOrchestrator.execute("crashed_order");

logger.info("orders: goodOrder is {}, badOrder is {},crashedOrder is {}",goodOrder,badOrder,crashedOrder);
}


private Saga createSaga() {
return Saga.create()
.chapter("init an order",10)
.chapter("booking a Fly",10)
.chapter("booking a Hotel",10)
.chapter("withdrawing Money",10);
private static Saga newSaga() {
return Saga
.create()
.chapter("init an order")
.chapter("booking a Fly")
.chapter("booking a Hotel")
.chapter("withdrawing Money");
}

private static ServiceDiscoveryService sd() {
private static ServiceDiscoveryService serviceDiscovery() {
return
new ServiceDiscoveryService()
.discover(new OrderService())
Expand Down

This file was deleted.

0 comments on commit 3a4677c

Please sign in to comment.