231
AP4R 加藤 & 篠原 俊一 2008. 2. 13 1

AP4R on Developers Summit 2008

Embed Size (px)

Citation preview

Page 1: AP4R on Developers Summit 2008

AP4R加藤 究 & 篠原 俊一

2008. 2. 13

1

Page 2: AP4R on Developers Summit 2008

はじめに

2

Page 3: AP4R on Developers Summit 2008

•むかし、土木専攻•少し前、メッセージングミドルウェアの保守 / 拡張

kiwamu

3

Page 4: AP4R on Developers Summit 2008

•Ruby は 2006 年から

•いま、AP4R の開発

•http://d.hatena.ne.jp/kiwamu/

kiwamu

4

Page 5: AP4R on Developers Summit 2008

shino•ぷろぐらまー、

コーディング大好き !

•今年の言語は...C

5

Page 6: AP4R on Developers Summit 2008

•オープンソースの世界へ•http://d.hatena.ne.jp/ita-wasa 

•日曜物理学者、超ひも理論

shino

6

Page 7: AP4R on Developers Summit 2008

会社紹介• フューチャーアーキテクト株式会社• http://www.future.co.jp/index.html

• 本社: 大崎オフィス

• 設立: 1989.11.28

• 社員数: 連結 1,007名、単体 663名 (2007.12.31 時点)

• 売上: 連結 250億18百万円、単体 160億46百万円7

Page 8: AP4R on Developers Summit 2008

8

Page 9: AP4R on Developers Summit 2008

9

Page 10: AP4R on Developers Summit 2008

10

Page 11: AP4R on Developers Summit 2008

発表の流れ1. 概説

2. 「軽量」について

3. デモ

4. 「堅牢」について

5. その他

6. まとめ11

Page 12: AP4R on Developers Summit 2008

1. 概説

12

Page 13: AP4R on Developers Summit 2008

大事なことは先に

13

Page 14: AP4R on Developers Summit 2008

1-1. AP4R とは何か ?

1-2. ユースケース

1-3. AP4R の強みは ?

1-4 事例14

Page 15: AP4R on Developers Summit 2008

AP4R とは何か?

15

Page 16: AP4R on Developers Summit 2008

AP4R とは何か ?

•Ruby のメッセージングライブラリ

•Asynchronous Processing for Ruby 

•非同期処理による疎結合化

16

Page 17: AP4R on Developers Summit 2008

•社内開発してきたメッセージングミドルウェア

•“RtFA”

AP4R の開発背景

17

Page 18: AP4R on Developers Summit 2008

•Pure Java 

•独自のプロトコルと API (≠ JMS)

•さまざまなシステム上での実績

RtFA

18

Page 19: AP4R on Developers Summit 2008

http://www.future.co.jp/company/news/061227.html

“RtFA” の最近の事例

19

Page 20: AP4R on Developers Summit 2008

•1日1億件以上の貨物データ

•並列実行•負荷分散

プロジェクト概要

20

Page 21: AP4R on Developers Summit 2008

•約 100 台のサーバ群

•AP サーバに “RtFA” 採用

プロジェクト概要

21

Page 22: AP4R on Developers Summit 2008

http://www.oreilly.co.jp/books/9784873113203/

From Java to Ruby

22

Page 23: AP4R on Developers Summit 2008

From RtFA to AP4R

•Java での経験をもとに、よい部分を継承しつつ、進化

•とくに使いやすさ (API, 設定 etc)

23

Page 24: AP4R on Developers Summit 2008

オープンソース•2006 年 OSS 化 (MIT)

•コミュニティとともに成長•エンタープライズ分野への適用

24

Page 25: AP4R on Developers Summit 2008

AP4R のユースケース

•ユーザーへの素早い応答•負荷分散

25

Page 26: AP4R on Developers Summit 2008

ユーザーへの素早い応答

26

Page 27: AP4R on Developers Summit 2008

すぐに応答の必要がない処理は、

あとで実行

27

Page 28: AP4R on Developers Summit 2008

Synchronously

Client Server

Sequential tasks

28

Page 29: AP4R on Developers Summit 2008

Synchronously

Client Server

A B C D E

Sequential tasks

28

Page 30: AP4R on Developers Summit 2008

Client Server Messaging Server

Better solution

29

Page 31: AP4R on Developers Summit 2008

Client Server Messaging Server

Better solution

A B

29

Page 32: AP4R on Developers Summit 2008

Client Server Messaging Server

Asynchronously

A B

30

Page 33: AP4R on Developers Summit 2008

Client Server Messaging Server

Asynchronously

A B C D E

30

Page 34: AP4R on Developers Summit 2008

典型的な用途

•メール送信•重いサマリデータの作成•システム間連携

31

Page 35: AP4R on Developers Summit 2008

負荷分散

32

Page 36: AP4R on Developers Summit 2008

スケールアウトのしやすさ

Client Server Messaging Server

33

Page 37: AP4R on Developers Summit 2008

Client Server Messaging Servers

スケールアウトのしやすさ

34

Page 38: AP4R on Developers Summit 2008

AP4R の強みは ?

35

Page 39: AP4R on Developers Summit 2008

「軽量」 かつ

「堅牢」36

Page 40: AP4R on Developers Summit 2008

軽量って ?

37

Page 41: AP4R on Developers Summit 2008

空気のような存在

38

Page 42: AP4R on Developers Summit 2008

•動的•簡潔•オープンクラス•メタプログラミング ...etc

Ruby の強み

39

Page 43: AP4R on Developers Summit 2008

AP4R で実現•使いやすさ

• シンプルな API と設定

• Rails とのシームレスな連携

•All in one support• ライブラリだけでなく、

• テストや運用のための仕組みも提供40

Page 44: AP4R on Developers Summit 2008

「軽量」 一つめの強みは、

41

Page 45: AP4R on Developers Summit 2008

堅牢って ?

42

Page 46: AP4R on Developers Summit 2008

ポストマンの意地

43

Page 47: AP4R on Developers Summit 2008

メッセージの配送保障

44

Page 48: AP4R on Developers Summit 2008

メッセージの喪失や二重処理の危険性

45

Page 49: AP4R on Developers Summit 2008

•注文したのに商品が届かない...

•代金が二重に引き落とされてる!

46

Page 50: AP4R on Developers Summit 2008

メッセージは

信頼できる方法で配送しないといけない

47

Page 51: AP4R on Developers Summit 2008

異常時こそ真価•データベース障害•N/W 障害

•サーバダウン•ビジー状態、タイムアウト

... etc48

Page 52: AP4R on Developers Summit 2008

「堅牢」 二つめの強みは、

49

Page 53: AP4R on Developers Summit 2008

AP4R の強みは...

50

Page 54: AP4R on Developers Summit 2008

「軽量」 かつ

「堅牢」51

Page 55: AP4R on Developers Summit 2008

堅牢軽量

シンプルな API

テストサポート

SAF

リカバリ

柔軟なシステム構成

52

Page 56: AP4R on Developers Summit 2008

事例

53

Page 57: AP4R on Developers Summit 2008

•オンラインゲーム

•Interactivemediums.com

54

Page 58: AP4R on Developers Summit 2008

オンラインゲーム

55

Page 59: AP4R on Developers Summit 2008

概要

•イギリス•Web ベースの戦略ゲーム

•未公開、現在 β テスト中

56

Page 60: AP4R on Developers Summit 2008

• メール送信• レポート作成• データ分析• データベースアクセスを含む、

CPU インテンシブな処理

AP4R の利用用途

57

Page 61: AP4R on Developers Summit 2008

規模•専用 AP4R サーバ × 2

•Rails プロセス × 4 / AP4R

•1時間に数百メッセージ

•MySQL に永続化58

Page 62: AP4R on Developers Summit 2008

Interactivemediums.com

59

Page 63: AP4R on Developers Summit 2008

概要•アメリカ•モバイル向けサービス、

TextMe for Business ( http://textmeforbusiness.com )

•2007年7月より稼動中60

Page 64: AP4R on Developers Summit 2008

http://textmeforbusiness.com/

61

Page 65: AP4R on Developers Summit 2008

AP4R queues

Textprocessing

inbound

outbounddelivery

acceptance

通知サービス on SMS

deliveryprocessing

routing

delivery

Third Partyapplication

62

Page 66: AP4R on Developers Summit 2008

なぜ、AP4R を利用したのか?

63

Page 67: AP4R on Developers Summit 2008

• 「複数サーバ上で、負荷分散しやすい」• 「スケールさせやすい」• 「活発な開発」• 「メッセージングの経験ゆたかな

開発チーム」

アンケート結果

64

Page 68: AP4R on Developers Summit 2008

Coffee break

65

Page 69: AP4R on Developers Summit 2008

:Firefox =>

(c) 2007 Mozilla Japan66

Page 70: AP4R on Developers Summit 2008

:Firefox =>

(c) 2007 Mozilla Japan

Foxkeh

67

Page 71: AP4R on Developers Summit 2008

:Java =>

https://duke.dev.java.net/

Duke

68

Page 72: AP4R on Developers Summit 2008

マスコット重要

69

Page 73: AP4R on Developers Summit 2008

:AP4R => ...

70

Page 75: AP4R on Developers Summit 2008

起源>“AP4R”.sub(/4/, ”A”)

>“APAR”.downcase

>“apar”

>ミツオビアルマジロ

Dictionary:http://www.alc.co.jp/Photo:http://www.flickr.com/photos/jeffclow/29738818/

72

Page 78: AP4R on Developers Summit 2008

Maro75

Page 79: AP4R on Developers Summit 2008

閑話休題

76

Page 80: AP4R on Developers Summit 2008

発表の流れ1. 概説

2. 「軽量」について

3. デモ

4. 「堅牢」について

5. その他

6. まとめ77

Page 81: AP4R on Developers Summit 2008

2. 「軽量」について

78

Page 82: AP4R on Developers Summit 2008

堅牢軽量

シンプルな API

テストサポート

SAF

リカバリ

柔軟なシステム構成

79

Page 83: AP4R on Developers Summit 2008

シンプルな API

80

Page 84: AP4R on Developers Summit 2008

メッセージの PUT

81

Page 85: AP4R on Developers Summit 2008

ap4r.async_to({destination}, {data} [,{options}])

ブロック形式でも記述可能c.f. RDoc: http://ap4r.rubyforge.org/doc/

82

Page 86: AP4R on Developers Summit 2008

Class AsyncShopController < ApplicationController

def order # synchronous part o = Order.new(:name => params[:name]) o.save! ap4r.async_to({:action => ‘payment’}, {:order_id => o.id})

redirect_to ... end

def payment # asynchronous part Payment.new(:order_id => params[:order_id]).save! ... render :text => ‘true’ end

end83

Page 87: AP4R on Developers Summit 2008

User Apache Rails AP4R

84

Page 88: AP4R on Developers Summit 2008

User Apache Rails AP4R

order

message put

85

Page 89: AP4R on Developers Summit 2008

Class AsyncShopController < ApplicationController

def order # synchronous part o = Order.new(:name => params[:name]) o.save! ap4r.async_to({:action => ‘payment’}, {:order_id => o.id})

redirect_to ... end

def payment # asynchronous part Payment.new(:order_id => params[:order_id]).save! ... render :text => ‘true’ end

end86

Page 90: AP4R on Developers Summit 2008

User Apache Rails AP4R

order

message put

87

Page 91: AP4R on Developers Summit 2008

User Apache Rails AP4R

dispatch88

Page 92: AP4R on Developers Summit 2008

User Apache Rails AP4R

payment

89

Page 93: AP4R on Developers Summit 2008

Class AsyncShopController < ApplicationController

def order # synchronous part o = Order.new(:name => params[:name]) o.save! ap4r.async_to({:action => ‘payment’}, {:order_id => o.id})

redirect_to ... end

def payment # asynchronous part Payment.new(:order_id => params[:order_id]).save! ... render :text => ‘true’ end

end90

Page 94: AP4R on Developers Summit 2008

柔軟なシステム構成

91

Page 95: AP4R on Developers Summit 2008

典型的な構成

92

Page 96: AP4R on Developers Summit 2008

User Apache Rails AP4R

93

Page 97: AP4R on Developers Summit 2008

メッセージング層の負荷分散

94

Page 98: AP4R on Developers Summit 2008

User Apache Rails AP4R

95

Page 99: AP4R on Developers Summit 2008

同期と非同期処理でサーバを分離

96

Page 100: AP4R on Developers Summit 2008

User Apache Rails AP4R

URL rewrite

97

Page 101: AP4R on Developers Summit 2008

User Apache Rails AP4R

URL rewriteHTTP

97

Page 102: AP4R on Developers Summit 2008

User Apache Rails AP4R

URL rewriteHTTP

98

Page 103: AP4R on Developers Summit 2008

プロトコル•デフォルト: HTTP POST

•対応済み:

• SOAP

• XML-RPC

• dRuby99

Page 104: AP4R on Developers Summit 2008

流量制御

100

Page 105: AP4R on Developers Summit 2008

処理数 処理時間

Task “busy” 多い 短い

Task “heavy” 少い 長い

2 種類の処理

101

Page 106: AP4R on Developers Summit 2008

URL rewrite

heavy

busy

102

Page 107: AP4R on Developers Summit 2008

設定ファイル dispatchers: - targets: queue.very_busy.* threads: 10 modify_rules: url: proc {|url| url.host = "busy.async.host"} - targets: queue.very_heavy.* threads: 1 modify_rules: url: proc {|url| url.host = "heavy.async.host"}

103

Page 108: AP4R on Developers Summit 2008

堅牢軽量

シンプルな API

テストサポート

SAF

リカバリ

柔軟なシステム構成

104

Page 109: AP4R on Developers Summit 2008

3. デモ

105

Page 110: AP4R on Developers Summit 2008

•買い物アプリ•注文処理と決済処理

概要

106

Page 111: AP4R on Developers Summit 2008

最初Client Server

A

order

B

payment

107

Page 112: AP4R on Developers Summit 2008

AP4R 導入Client Rails AP4R Rails × 3

108

Page 113: AP4R on Developers Summit 2008

注文処理は同期でClient Rails AP4R Rails × 3

A

order

109

Page 114: AP4R on Developers Summit 2008

決済処理は非同期でClient Rails AP4R Rails × 3

A B

order payment

110

Page 115: AP4R on Developers Summit 2008

滞留メッセージの監視Client Rails AP4R Rails × 3

0255075

100

111

Page 116: AP4R on Developers Summit 2008

•アプリを簡単に非同期化•負荷をかけても応答はすぐに•着々と処理されるメッセージ

デモの流れ

112

Page 117: AP4R on Developers Summit 2008

113

Page 118: AP4R on Developers Summit 2008

114

Page 119: AP4R on Developers Summit 2008

発表の流れ1. 概説

2. 「軽量」について

3. デモ

4. 「堅牢」について

5. その他

6. まとめ115

Page 120: AP4R on Developers Summit 2008

4. 「堅牢」について

116

Page 121: AP4R on Developers Summit 2008

堅牢軽量

シンプルな API

テストサポート

SAF

リカバリ

柔軟なシステム構成

117

Page 122: AP4R on Developers Summit 2008

Stored And Forward

118

Page 123: AP4R on Developers Summit 2008

業務処理のトランザクションと

メッセージ生成の

一貫性を保障119

Page 124: AP4R on Developers Summit 2008

CRUD

commit

message put

app-DB AP4RRails msg-DB

insert /commit

SAF create

SAF delete

Store

Forward

✓120

Page 125: AP4R on Developers Summit 2008

Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions by Gregor Hohpe,Bobby Woolf

Guaranteed Delivery [122]

121

Page 126: AP4R on Developers Summit 2008

SAF 詳細

122

Page 127: AP4R on Developers Summit 2008

非同期送信メッセージが生成されるまでの流れ

123

Page 128: AP4R on Developers Summit 2008

start

end

app-DB AP4RRails

124

Page 129: AP4R on Developers Summit 2008

CRUDstart

end

app-DB AP4RRails

124

Page 130: AP4R on Developers Summit 2008

CRUD

commit

start

end

app-DB AP4RRails

124

Page 131: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end

app-DB AP4RRails

124

Page 132: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data

app-DB AP4RRails

124

Page 133: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

app-DB AP4RRails

124

Page 134: AP4R on Developers Summit 2008

業務処理のトランザクションと

メッセージ生成の

一貫性を保障したい

125

Page 135: AP4R on Developers Summit 2008

•エラーが起きた場合は、•メッセージは生成されない

データベース更新で...

126

Page 136: AP4R on Developers Summit 2008

•正常に終了した場合は、•メッセージは必ずキューに送られる

データベース更新で...

127

Page 137: AP4R on Developers Summit 2008

安心☺

128

Page 138: AP4R on Developers Summit 2008

「軽量」 かつ

「堅牢」129

Page 139: AP4R on Developers Summit 2008

Ap4r::Client#transaction Class ShopController < ApplicationController def order # synchronous side ap4r.transaction do o = Order.new(:name => params[:order_id]) o.save!

ap4r.async_to({:action => ‘payment’}, {:order_id => o.id})

end redirect_to ... end end

130

Page 140: AP4R on Developers Summit 2008

キューイングの一連の処理が変ります

131

Page 141: AP4R on Developers Summit 2008

ap4r.transaction do ... # CRUD on database

ap4r.async_to(...)

end

ap4r.transaction の裏側

132

Page 142: AP4R on Developers Summit 2008

ap4r.transaction do ... # CRUD on database

ap4r.async_to(...)

end

ap4r.transaction の裏側

Insert into SAF table

132

Page 143: AP4R on Developers Summit 2008

ap4r.transaction do ... # CRUD on database

ap4r.async_to(...)

end

ap4r.transaction の裏側

Insert into SAF table

Commit database

132

Page 144: AP4R on Developers Summit 2008

ap4r.transaction do ... # CRUD on database

ap4r.async_to(...)

end

ap4r.transaction の裏側

Insert into SAF table

Commit databaseQueue messages

132

Page 145: AP4R on Developers Summit 2008

ap4r.transaction do ... # CRUD on database

ap4r.async_to(...)

end

ap4r.transaction の裏側

Insert into SAF table

Commit databaseQueue messages

Store

132

Page 146: AP4R on Developers Summit 2008

ap4r.transaction do ... # CRUD on database

ap4r.async_to(...)

end

ap4r.transaction の裏側

Insert into SAF table

Commit databaseQueue messages

Store

Forward

132

Page 147: AP4R on Developers Summit 2008

SAF がない場合

133

Page 148: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

134

Page 149: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

134

Page 150: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

135

Page 151: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

135

Page 152: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

135

Page 153: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

136

Page 154: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

136

Page 155: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

136

Page 156: AP4R on Developers Summit 2008

CRUDstart

end data messages

counterchangecommit

message put / commit

137

Page 157: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

counterchange

138

Page 158: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

139

Page 159: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

139

Page 160: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

140

Page 161: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

140

Page 162: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

140

Page 163: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

141

Page 164: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

141

Page 165: AP4R on Developers Summit 2008

CRUD

commit

message put / commit

start

end data messages

141

Page 166: AP4R on Developers Summit 2008

障害時こそ真価•データベース障害•N/W 障害

•サーバダウン•ビジー状態、タイムアウト

... etc142

Page 167: AP4R on Developers Summit 2008

異常系重要☺

143

Page 168: AP4R on Developers Summit 2008

SAF がある場合

144

Page 169: AP4R on Developers Summit 2008

CRUD

commitmessage put / commit

start

end data messages

SAF create

SAF update

145

Page 170: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

146

Page 171: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

146

Page 172: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

147

Page 173: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

147

Page 174: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

147

Page 175: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

☺147

Page 176: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

147

Page 177: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

148

Page 178: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

148

Page 179: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

149

Page 180: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

149

Page 181: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

149

Page 182: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

☺149

Page 183: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

150

Page 184: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

150

Page 185: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

151

Page 186: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

151

Page 187: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

messages

151

Page 188: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

messages

Recoverable

151

Page 189: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

☺messages

Recoverable

151

Page 190: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

152

Page 191: AP4R on Developers Summit 2008

commitmessage put / commit

SAF update

CRUDstart

end data messages

SAF create

152

Page 192: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

153

Page 193: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

messages

153

Page 194: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

☺messages

153

Page 195: AP4R on Developers Summit 2008

message put / commitcommit

SAF update

CRUDstart

end data messages

SAF create

☺messages

Duplicated

153

Page 196: AP4R on Developers Summit 2008

業務処理のトランザクションと

メッセージ生成の

一貫性を保障154

Page 197: AP4R on Developers Summit 2008

プロデューサとチャネル間で

最低一回

at-least-once155

Page 198: AP4R on Developers Summit 2008

堅牢軽量

シンプルな API

テストサポート

SAF

リカバリ

柔軟なシステム構成

156

Page 199: AP4R on Developers Summit 2008

5. その他

157

Page 200: AP4R on Developers Summit 2008

•障害時のメッセージのリカバリ•Capistrano (2.x) との連携

•テストサポート

158

Page 201: AP4R on Developers Summit 2008

•Functional テスト• 通信なし

• 速い、けど制限あり

•Async テスト• 通信あり

• 遅い、けど現実に近い

2 種類のテストを提供

159

Page 202: AP4R on Developers Summit 2008

gihyo.jp 特集

• AP4R、Ruby で非同期メッセージング

•全 4 回 (2007.9.6 - 9.27)

• http://gihyo.jp/dev/feature/01/ap4r

160

Page 203: AP4R on Developers Summit 2008

テストサポート詳細

161

Page 204: AP4R on Developers Summit 2008

TDD / BDD

162

Page 205: AP4R on Developers Summit 2008

安心できるアプリ開発

163

Page 206: AP4R on Developers Summit 2008

非同期アプリでは

•テストは同じく重要、でも...

•N/W をまたいだ複数のプロセス

• 時間がかかる• セットアップが面倒

164

Page 207: AP4R on Developers Summit 2008

•Functional テスト• 通信なし

• 速い、けど制限あり

•Async テスト• 通信あり

• 遅い、けど現実に近い

2 種類のテストを提供

165

Page 208: AP4R on Developers Summit 2008

Functional テスト

•キューのスタブを提供•メモリ上で実行、速い

166

Page 209: AP4R on Developers Summit 2008

コード例このテストコードは架空のオンラインショッピングアプリのものです。同期で実行される注文処理と、非同期で実行される決済処理とがあります。

167

Page 210: AP4R on Developers Summit 2008

def test_order post :order, :order => {:item => "introduction to AP4R"} assert_response :redirect assert_redirected_to :action => 'index'

messages = @controller.ap4r.queued_messages # ... (1) assert_equal 1, messages.keys.size, "should have messages in just ONE queue" assert messages.key?("queue.async_shop.payment"), "queue name is INCORRECT" # ... (2) assert_equal 1, messages["queue.async_shop.payment"].size, "should have just ONE message for payment" assert_match /order_id=\d+/, messages["queue.async_shop.payment"].first[:body], "parameter order_id should be included with a numeric value" # ... (3) end

1. キューのスタブからメッセージを取得2. キューの名前を assert

3. メッセージの中身を assert168

Page 211: AP4R on Developers Summit 2008

CRUD

message put

functional test

Stub

assert

assert

rake ..

169

Page 212: AP4R on Developers Summit 2008

Async テスト

•現実に近い状態で•すべてをテスト• e.g. シリアライゼーション、メッセージ連携

170

Page 213: AP4R on Developers Summit 2008

コード例このテストコードは架空のオンラインショッピングアプリのものです。同期で実行される注文処理と、非同期で実行される決済処理とがあります。

171

Page 214: AP4R on Developers Summit 2008

ENV["RAILS_ENV"] = "test" require File.expand_path(File.dirname(__FILE__) + "/../../config/environment") require "ap4r/service_handler" ap4r_test_helper = Ap4r::ServiceHandler.new require 'test_help' class Test::Unit::TestCase self.use_transactional_fixtures = false self.use_instantiated_fixtures = false # Add more helper methods to be used by all tests here... cattr_accessor :ap4r_helper def ap4r_helper @@ap4r_helper end def with_services(&block) ap4r_helper.with_services(&block) end end

Test::Unit::TestCase.ap4r_helper = ap4r_test_helper

[RAILS_ROOT]/test/async/ap4r_test_helper.rb

172

Page 215: AP4R on Developers Summit 2008

require "#{File.dirname(__FILE__)}/ap4r_test_helper"require 'net/http'

class AsyncShopTest < Test::Unit::TestCase

def test_http_dispatch ap4r_helper.stop_dispatchers # ... (1)

assert_rows_added(Order, 1) { # ... (3) do_order # ... (2) } assert_rows_added(Payment, 1) { # ... (6) ap4r_helper.start_dispatchers # ... (4) ap4r_helper.wait_all_done # ... (5) } end

private # Requests to <tt>async_shop/order</tt>. def do_order(item_name = "test item") Net::HTTP.start("localhost", 3000, nil, nil) do |http| http.request_post("/async_shop/order", "order[item]=#{item_name}") do |res| #nop end end end

def assert_rows_added(model, rows) rows_before = model.count yield rows_after = model.count assert_equal rows, rows_after - rows_before, "table '#{model.table_name}' should count up by #{rows}" endend

[RAILS_ROOT]/test/async/async_shop_test.rb

173

Page 216: AP4R on Developers Summit 2008

1. dispatcher スレッドを停止2. HTTP POST で Rails にリクエスト3. orders テーブルに一行追加されたのを assert

4. dispatcher スレッドを開始5. 非同期のアクションが完了するのを待つ6. payment テーブルに一行追加されたのをassert

174

Page 217: AP4R on Developers Summit 2008

CRUD

message put

async test

HTTP

rake ..

assert

service control

assertCRUD

HTTP

175

Page 218: AP4R on Developers Summit 2008

•補完的な2つのテストで

• Functional テスト

• Async テスト

•安心して非同期アプリ開発

テストサポート

176

Page 219: AP4R on Developers Summit 2008

6. まとめ

177

Page 220: AP4R on Developers Summit 2008

AP4R は ..

178

Page 221: AP4R on Developers Summit 2008

「軽量」 かつ

「堅牢」179

Page 222: AP4R on Developers Summit 2008

堅牢軽量

シンプルな API

テストサポート

SAF

リカバリ

柔軟なシステム構成

180

Page 223: AP4R on Developers Summit 2008

•シンプルな API と設定で使えるメッセージング

•Rails とのシームレスな連携

•柔軟なシステム構成が可能181

Page 224: AP4R on Developers Summit 2008

•dispatcher による非同期処理の自動実行

•SAF によるメッセージ配送保障

•テストサポート

182

Page 225: AP4R on Developers Summit 2008

今後の展望

183

Page 226: AP4R on Developers Summit 2008

円滑な運用 ver.0.3.x

☑デーモン化

☑ URL 書き替えフィルタ

☑ DLQ / SAF リカバリ

□プロトコル拡充: Stomp, HTTP

184

Page 227: AP4R on Developers Summit 2008

監視機能強化 ver.0.4.x

□ OSS 監視ツール Cacti,

ZABBIX などとの連携

□複数 AP4R プロセスの管理

□対話型管理ツール185

Page 228: AP4R on Developers Summit 2008

大規模対応へステップ ver.0.5.x

□ 動的設定□ 自動リカバリ□ 連続無停止稼動

186

Page 229: AP4R on Developers Summit 2008

その他□ セキュリティ☑テストサポート

□ ブロッキングキュー□ Ruby off Rails

187

Page 230: AP4R on Developers Summit 2008

Your patches are welcome!Thank you.

AP4R@RubyForge

•wiki: http://ap4r.rubyforge.org/wiki/wiki.pl?HomePage

• source: svn co http://ap4r.rubyforge.org/svn/trunk/ap4r

•ML: http://rubyforge.org/mailman/listinfo/ap4r-user

by Kanican188

Page 231: AP4R on Developers Summit 2008

• Future Architect logo belongs to Future Architect, Inc. Japan. All rights reserved.

• Rails logo is trademarks of David Heinemeier Hansson. All rights reserved.

• The Ruby logo is copyrighted (c) 2006, Yukihiro Matsumoto. It is released under the terms of the Creative Commons Attribution-ShareAlike 2.5 License.

189