Contenu connexe
Similaire à [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)
Similaire à [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF) (20)
Plus de Amazon Web Services Japan
Plus de Amazon Web Services Japan (20)
[AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)
- 1. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWSマイスターシリーズ
Amazon Simple Workflow Service
2013.4.24
アマゾンデータサービスジャパン株式会社
部長/ソリューション アーキテクト
片山暁雄
- 2. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
2
Amazon Simple Workflow Serviceの
概要
- 3. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
3
Amazon Simple Workflow Serviceとは
スケーラブルで分散したワークフローを実現するための
実行基盤を提供するサービス
• 通称:Amazon SWF
• 全リージョンに展開
リージョンをまたいだワークフローや、AWSとオンプレミス
を連携したワークフローも実現
• 途中で人手が入るワークフローも実現可能
SWFが提供するのは、ワークフローの実行基盤のみ
• 例えば「企業の組織構造を登録しておいて、稟議書を課長が承認したら
部長に承認プロセスが回る」というような業務ワークフローはSWFの提
供範囲外
• 業務フローはAWS利用者が実装
- 4. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
4
Amazon Simple Workflow Serviceとは
ワークフローの管理、実行タスクの管理、タスク間の処理
ルーティング、一貫性の担保、実行履歴・リトライやタイム
アウトなどの状態管理はAWS側(SWF)が担当
ワークフローの登録、各タスク処理の実装および処理順番の
決定はAWS利用者が実施
SWF
SWF user
- 5. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
5
利用例
動画や音声のエンコーディング
Eコマースサイトの決済
バッチ処理
システム構築やバックアップ
途中で出荷処理などの人手が入る処理フロー
長時間に渡る処理
- 6. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
6
NASAの利用事例(画像処理)
NASA JPL
火星
AWS
- 7. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
7
Amazon SWFを使ったシステムの特徴
疎結合
ステートレス
非同期処理
柔軟なスケールアップ/ダウン
高い冗長性・可用性
- 8. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
8
Amazon SWFの動作原理
- 9. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
9
SWF利用時の用語説明
ドメイン
ワークフロー
ワークフローエグゼキューション
アクティビティタイプ
タスクリスト
• デシジョンタスクリスト
• アクティビティタスクリスト
ワークフロースターター
アクティビティ
デサイダー
ワーカー
• アクティビティワーカー
• ワークフローワーカー
SWFに登録するもの
/SWFが提供するもの
SWF利用者が動作させるもの
(アクター)
- 10. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
10
SWFに登録するもの/SWFが提供するもの(その1)
ドメイン
• トップレベルの論理区分
ワークフロー
• 実行する処理の一連の流れを定義する単位
• デサイダーと紐づく
ワークフローエグゼキューション
• ワークフローのプロセス。例えばEコマースサイトの場合、顧客が
購入ボタンをクリックするごとに、ワークフローエグゼキューショ
ンが生成されるイメージ
• 各々のワークフローエグゼキューションは、任意の
ID(WorkflowID)と自動生成ID(runID)を持つ。
• 任意のID(WorkflowID)は、ユーザーが採番し、通常、決済番号や
請求書番号などを設定する(重複時はエラーとなる)
• 自動生成ID(runID)は、システムが生成したものが付与される。
- 11. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
11
アクティビティタイプ
• ワークフロー中の処理の論理単位。各アクティビティタイプごとに
アクティビティが紐づき、実際の処理を行う
タスクリスト
• Amazon SQSのキューのようなもの
• タスクリストには、タスク(SQSのメッセージ相当)が入る
• デシジョンタスクリスト
• ワークフローの処理ロジックを動かすためのタスク(デシジョンタス
ク)を入れるリスト
• ワークフローエグゼキューションは、必ず1つのデシジョンタスクリス
トに紐づく
• アクティビティタスクリスト
• アクティビティに処理を指示するためのタスク(アクティビティタス
ク)を入れるリスト
• 「画像を変換する」「データをコピーする」など、アクティビティを
動かすためのタスクが入る
SWFに登録するもの/SWFが提供するもの(その2)
- 12. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
12
- 13. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
13
SWF利用者が動作させるもの(アクター)
ワークフロースターター
• ワークフローを開始するアクター
• 例えばアプリケーションサーバや、定期処理を行うcronサーバ
アクティビティ
• アクティビティタイプごとに発生するタスクを実際に処理する
アクター
• ビジネスロジック部分
• 例えば画像変換処理や、カード決済処理など
デサイダー
• タスク処理のルーティングを決めるアクター
• ワークフローの流れの定義を行なう部分
• 次に実行するアクティビティワーカーを決定する
ワーカー
• アクティビティおよびデサイダーを動かすプログラム
• アクティビティワーカー/ワークフローワーカー
• mainメソッドやデーモンプログラムなど
- 14. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
14
SWF - 動作のポイント
タスクリストを中心に、デサイダーとアクティビティが交互に動作
デサイダーとアクティビティは、ロングポーリングでタスクリスト
に接続
タスクリストにタスクが入ってくると、接続しているデサイダー/
アクティビティのうちの1つにタスクを返す
• SQSのように複数にメッセージを返さない
• 一貫性の担保
Amazon
SWF
タスクリストデサイダー アクティビティ
- 15. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
15
デサイダー
動画変換ワークフローの例
Amazon
SWF
2.ビデオ
エンコードのタス
クをタスクリスト
に入れる
6.処理の終了
デサイダー
Web
Server
LongPoll
アクティビ
ティタスク
(エンコー
ド処理)
デシジョン
タスク
アクティビ
ティタスク
(DRM処理)
アクティビティ
デシジョン
タスク
LongPoll
デシジョン
タスク
LongPoll
LongPoll
LongPoll
1.ワークフロー
開始
3.エンコード
処理を実施
4.DRM付与
タスクをタ
スクリスト
に入れる
5.DRMの追
加処理を実
施
処理フロー(処理開始>エンコーディング>DRM付与)
ワークフロー
スターター
アクティビティ デサイダー
アクティビティ
タスクリスト
デシジョン
タスクリスト
ワークフロー
エグゼキューション
ワーカー
- 16. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
16
SWF – 設計のポイント
処理の細分化
• 処理を細分化しておき、処理同士を疎結合にすることで、処理
の再利用やワークフロー間での共通利用が可能
• 細分化することで、特定の処理だけサーバを追加して、パ
フォーマンスを向上させることが可能
ステートレス
• デサイダーやアクティビティを作る際、データをローカルに持
たないように設計する
• 例えば画像処理の場合、画像データはS3などに入れ、各アク
ティビティが処理時にS3から取得するようにしておく
• ステートレスにしておくことで、容易に冗長性やパフォーマン
スを向上させることができる
- 17. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
17
開発
SWF実行までの流れ
アプリケーションやワーク
フローの設計、必要なアク
ティビティの洗い出し
ドメインの作成
ワークフローとアクティビ
ティタイプの設定
アクティビティの
実装
デサイダーの実装
ワーカー/
ワークフロー
スターターの実装
ワークフロース
ターターの実行
ワークフロー
エグゼキューショ
ン
デサイダーやアク
ティビティワー
カーのデプロイと
実行
設計 実行
- 18. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
18
Amazon SWFの機能
- 19. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
19
Amazon SWFの機能(1)
デサイダー/アクティビティへのタスクルーティング
(基本機能)
• デサイダー/アクティビティ間のデータ受け渡し
ワークフローエグゼキューションの実行履歴保持
• どのようなアクティビティが呼ばれたかの履歴を保持
• 渡された値や戻り値の情報も保持
タイムアウト・リトライの設定
• ワークフローエグゼキューション全体のタイムアウト時間
• アクティビティワーカーごとのタイムアウト時間
• リトライ回数/リトライポリシー
マニュアル実行のアクティビティワーカーのサポート
• メールでの承認作業や、発送処理などの手作業が入る場合
- 20. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
20
Amazon SWFの機能(2)
子プロセスサポート
• ワークフロー実行中にサブワークフローを実行
シグナル
• ワークフロー処理への割り込み
タグ
• ワークフローエグゼキューションにタグを付与(5つまで)
タイマー(ワークフロークロック)
• 指定時間後にデサイダーを再コール
実行中のワークフローのキャンセル
- 21. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
21
プログラミングモデル
- 22. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
22
AWS SDK
ワークフロースターター、デサイダー、アクティビティの
実装には、基本的にAWS SDKを使用
例:
• AWS SDK for Java
• AWS SDK for .NET
• AWS SDK for PHP
• AWS SDK for Ruby
SDKにはドメインやワークフローの登録、タスクリストの
ポーリング、実行履歴取得など、SWFの機能を利用する
APIが実装済み
AWS SDK for Javaは「AWS Flow Framework」を提供
- 23. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
23
AWS Flow Framework
ワークフロースターター、デサイダー、アクティビティ
ワーカーを実装するためのJavaフレームワーク
JavaのAPT機能を使用
• アクティビティワーカーの実装と、SWF固有処理を分離
• デサイダーのロジック実装から、非同期処理の実装を分離
• 各種スタブコードを生成
JUnitを使用したテストのサポート機能
- 24. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
24
クラス構成と実装順
MyActivityImpl
MyActivity
MyActivity
ClientImpl
MyActivityClient
APT
MyWorkflowClient
External
MyWorkflowClient
ExternalImpl
MyWorkflowClient
ExternalFactory
MyWorkflowClient
ExternalFactory
Impl
APT
APT
MyActivityWorker
MyWorkflowWorker
MyWorkflow
Starter
ActivityWorker
AmazonSimple
Workflow
MyWorkflowImpl
MyWorkflow
WorkflowWorker
認証情報
実装する部分
生成される部分
SDK提供部分
利用
アクティビティ
の実装
デサイダーの実装
ワーカー/ワークフロー
スターターの実装
- 25. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
25
アクティビティの実装
アクティビティのインターフェースを定義
@Activities(version = "1.0")
@ActivityRegistrationOptions(
defaultTaskList = “MyActivityTaskList”,
defaultTaskScheduleToStartTimeoutSeconds = 30,
defaultTaskStartToCloseTimeoutSeconds = 30)
public interface MyActivity {
String activity1();
}
インターフェースを実装
public class MyActivityImpl implements MyActivity {
@Override
public String activity1() {
return "a1";
}
}
- 26. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
26
デサイダーの実装
デサイダーのインターフェースを実装
@Workflow
@WorkflowRegistrationOptions(
defaultTaskList = Constants.DECIDER_TASK_LIST,
defaultExecutionStartToCloseTimeoutSeconds = 300,
defaultTaskStartToCloseTimeoutSeconds = 30)
public interface MyWorkflow{
@Execute(version = "1.0")
void execute();
}
インターフェースを実装
public class MyWorkflowImpl implements MyWorkflow {
private MyActivityClient client = new MyActivityClientImpl();
@Override
public void execute() {
client.activity1();
}
}
- 27. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
27
ワーカー/ワークフロースターターの実装
ワーカーを実装
public class MyActivityWorker {
public static void main(String[] args) throws Exception {
AmazonSimpleWorkflow swfClient = Helper.createClient();
String domain = CreateDomain.DOMAIN;
// アクティブワーカークラスをインスタンス化
ActivityWorker worker = new ActivityWorker(swfClient, domain,”ACTIVITY_TASK_LIST”);
// 実行するアクティビティを登録
MyActivityImpl ac = new MyActivityImpl();
worker.addActivitiesImplementation(ac);
// タスクの待ち受けを開始
worker.start();
}
}
public class MyWorkflowWorker {
public static void main(String[] args) throws Exception {
AmazonSimpleWorkflow swfClient = Helper.createClient();
String domain = CreateDomain.DOMAIN;
WorkflowWorker worker = new WorkflowWorker(swfClient, “DECIDER_TASKLIST”);
worker.addWorkflowImplementationType(MyWorkflowImpl.class);
worker.start();
}
}
ワークフロースターターを実装
public class MyWorkflowStarter {
public static void main(String[] args) throws Exception {
AmazonSimpleWorkflow swfClient = Helper.createClient();
String domain = CreateDomain.DOMAIN;
MyDeciderClientExternalFactory factory = new MyDeciderClientExternalFactoryImpl(swfClient, domain);
String workflowId = “DEMO” + new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date());
MyDeciderClientExternal client = factory.getClient(workflowId);
StartWorkflowOptions options = new StartWorkflowOptions();
options.setTaskList(Constants.DECIDER_TASK_LIST);
client.execute();
}
}
- 28. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
28
デサイダーの実装方法
デサイダーはワークフローのフローロジックの要
• 逐次処理、平行処理、フォーク/ジョインはデサイダーで実装
Promise<>と@Asynchronusが処理フロー実装の基本
• ActivityClientのメソッド、もしくは@Asynchronousのついた
メソッドにPromise<>を渡すと、そのPromise<>の状態が
Readyになるまで処理が実行されない
• 逐次処理をしたい場合は、直前の処理の戻り値(Promise<>)を、
次の処理の引数にする。並行処理の場合は、戻り値を渡さない
MyWorkflowImpl
MyWorkflow
MyActivity
ClientImpl
MyActivityClient
- 29. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
29
Flow ChartprocessStart
fileTransfer
dataProcessingA
combinResult
showReport
dataProcessingB dataProcessingC
dataProcessingD
- 30. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
30
public void processStart(final String srcImagePath) {
Promise<String> s3ImagePath = jpl.fileTransfer(srcImagePath);
//3分岐の処理
Promise<ProcessingResultA> resultA =
aws.dataProcessingA(s3ImagePath);
Promise<ProcessingResultB> resultB =
aws.dataProcessingB(s3ImagePath);
Promise<ProcessingResultC> resultC =
aws.dataProcessingC(s3ImagePath);
//dataProcessingCの終了後にDを実行
Promise<ProcessingResultD> resultD = aws.dataProcessingD(resultC);
//全てのdataProcessiongを待つ
Promise<CombineResult> combineResult
= jpl.combineResult(resultA, resultB, resultD);
Promise<Void> endResult = jpl.showResult(combineResult);
}
Jpl, awsは生成されたActivetyClientデサイダー実装例
- 31. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
31
デサイダーの実装方法(Tips)
その他分岐処理を実装するためのクラスやアノテーショ
ンを活用
• AndPromise<>,OrPromise<>クラス
• @Wait,@NoWait
• Settable<>クラス
• @Asynchronous、Taskクラス
• TryCatch、TryFinally、TryCatchFinallyクラス
• @Signal
- 32. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
32
利用料金
- 33. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
33
SWFの利用料金
1ワークフローエグゼキューションごとに$0.00012
• 初回1000回は無料
1ワークフローエグゼキューションの実行時間、24時間
ごとに$0.000006
• ただし最初の30000回、24時間までは無料
完了した1ワークフローの実行履歴を残すのに、1日当
たり$0.000006
• ただし最初の30000回、24時間までは無料
1タスク、マーカー、タイマー、シグナルごとに
$0.00003
• 最初の10000回は無料金
- 34. © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
34
Appendix
参考資料
• Amazon Simple Workflow Service
• http://aws.amazon.com/jp/swf/
• AWS Flow Framework Recipes
• http://aws.amazon.com/code/2535278400103493
• AWS Flow Framework samples for Amazon SWF
• http://aws.amazon.com/code/3015904745387737