[awsマイスターシリーズ]amazon simple workflow service (swf)

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. AWSマイスターシリーズ Amazon Simple Workflow Service 2013.4.24 アマゾンデータサービスジャパン株式会社 部長/ソリューション アーキテクト 片山暁雄

Upload: amazon-web-services-japan

Post on 30-Jun-2015

5.979 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

アマゾンデータサービスジャパン株式会社

部長/ソリューション アーキテクト

片山暁雄

Page 2: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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の 概要

Page 3: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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利用者が実装

Page 4: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

Page 5: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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コマースサイトの決済

バッチ処理

システム構築やバックアップ

途中で出荷処理などの人手が入る処理フロー

長時間に渡る処理

Page 6: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

Page 7: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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を使ったシステムの特徴

疎結合

ステートレス

非同期処理

柔軟なスケールアップ/ダウン

高い冗長性・可用性

Page 8: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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の動作原理

Page 9: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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利用者が動作させるもの (アクター)

Page 10: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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)は、システムが生成したものが付与される。

Page 11: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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)

Page 12: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

Page 13: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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メソッドやデーモンプログラムなど

Page 14: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

タスクリスト デサイダー アクティビティ

Page 15: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

Lo

ng

Po

ll

アクティビティタスク(エンコード処理)

デシジョンタスク

アクティビティタスク(DRM処理)

アクティビティ

デシジョンタスク

Lo

ng

Po

ll

デシジョンタスク

Lo

ng

Po

ll

Lo

ng

Po

ll

Lo

ng

Po

ll

1.ワークフロー開始

3.エンコード処理を実施

4.DRM付与タスクをタスクリストに入れる

5.DRMの追加処理を実施

処理フロー(処理開始>エンコーディング>DRM付与)

ワークフロー スターター

アクティビティ デサイダー

アクティビティ タスクリスト

デシジョン タスクリスト

ワークフロー エグゼキューション

ワーカー

Page 16: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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から取得するようにしておく

• ステートレスにしておくことで、容易に冗長性やパフォーマンスを向上させることができる

Page 17: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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実行までの流れ

アプリケーションやワークフローの設計、必要なアクティビティの洗い出し

ドメインの作成

ワークフローとアクティビティタイプの設定

アクティビティの 実装

デサイダーの実装

ワーカー/ ワークフロー

スターターの実装

ワークフロースターターの実行

ワークフロー エグゼキューショ

デサイダーやアクティビティワーカーのデプロイと

実行

設計 実行

Page 18: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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の機能

Page 19: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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)

デサイダー/アクティビティへのタスクルーティング(基本機能) • デサイダー/アクティビティ間のデータ受け渡し

ワークフローエグゼキューションの実行履歴保持 • どのようなアクティビティが呼ばれたかの履歴を保持

• 渡された値や戻り値の情報も保持

タイムアウト・リトライの設定 • ワークフローエグゼキューション全体のタイムアウト時間

• アクティビティワーカーごとのタイムアウト時間

• リトライ回数/リトライポリシー

マニュアル実行のアクティビティワーカーのサポート • メールでの承認作業や、発送処理などの手作業が入る場合

Page 20: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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つまで)

タイマー(ワークフロークロック)

• 指定時間後にデサイダーを再コール

実行中のワークフローのキャンセル

Page 21: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

プログラミングモデル

Page 22: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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」を提供

Page 23: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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を使用したテストのサポート機能

Page 24: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

MyWorkflowClientExternalImpl

MyWorkflowClient ExternalFactory

MyWorkflowClient ExternalFactory

Impl

APT APT

MyActivityWorker

MyWorkflowWorker

MyWorkflow

Starter

ActivityWorker

AmazonSimple

Workflow

MyWorkflowImpl

MyWorkflow

WorkflowWorker

認証情報

実装する部分

生成される部分

SDK提供部分

利用

アクティビティ の実装

デサイダーの実装 ワーカー/ワークフロー

スターターの実装

Page 25: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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";

}

}

Page 26: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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();

}

}

Page 27: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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();

}

}

Page 28: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

Page 29: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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 Chart processStart

fileTransfer

dataProcessingA

combinResult

showReport

dataProcessingB dataProcessingC

dataProcessingD

Page 30: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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 デサイダー実装例

Page 31: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

Page 32: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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

利用料金

Page 33: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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回は無料金

Page 34: [AWSマイスターシリーズ]Amazon Simple Workflow Service (SWF)

© 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