q4m - a high-performance message queue for mysql
DESCRIPTION
Explains the design, usage, and user senarios of Q4M.TRANSCRIPT
Q4Ma high-performance message queue for MySQL
Q4Ma high-performance message queue for MySQL
Cybozu Labs, Inc.Kazuho Oku
2008 5 27年 月 日 Q4M 2
What is a Message Queue?from Distributed Systems (Tanenbaum / Van Steen)
2008 5 27年 月 日 Q4M 3
What is a Message Queue?What is a Message Queue?
Middleware for persistent asynchronous communication
持続的な非同期コミュニケーションのためのミドルウェア
A.k.a. Message-Oriented Middleware別名 : MOM
2008 5 27年 月 日 Q4M 4
Message Queue (cont.)Message Queue (cont.)
MQ is an intermediate storageMQ は中間的なストレージ
vs. RDBMS (long-term persistent storage)vs. RDBMS ( 永続的なストレージ )
Senders and/or receivers may go down送受信プロセスが任意に停止可能
2008 5 27年 月 日 Q4M 5
Minimal Configuration of a MQ ( 最小構成 )Minimal Configuration of a MQ ( 最小構成 )
Senders and receivers access a single queue
送信プロセスと受信プロセスが同一のキューにアクセス
Sender Receiver
Queue
2008 5 27年 月 日 Q4M 6
MQ and RelaysMQ and Relays
Separate queue for sender and receiver送信プロセスと受信プロセスが別個にキューをもつ
Messages relayed between queuesメッセージリレーでキュー間を接続
Sender
Queue
Receiver
Queue
Relay
2008 5 27年 月 日 Q4M 7
Merits of Message Relays ( リレーの意義 )Merits of Message Relays ( リレーの意義 )
Destination can be easily changed宛先の変更が容易
Relays may transfer messages to different locations depending on the headerヘッダを見て転送先を変えるとか
Robustness against network failureネットワーク障害の影響を受けない
Logging and Multicasting, etc.ロギングやマルチキャストが可能
2008 5 27年 月 日 Q4M 8
Message BrokersMessage Brokers
Transform (filter) messages within the relay agent
リレーエージェント内でメッセージの変換を行う
Publish / subscribe modelSeparation between logic and data transfer
ロジックとデータ転送の分離
2008 5 27年 月 日 Q4M 9
Q4MQ4M
Q4M (can) support all modelsQ4M は全モデルに対応 ( 可能 )
Message transfer by q4m-forwardメッセージ転送は q4m-forward で
APIs for routers and message brokersルータやメッセージブローカ用の API
2008 5 27年 月 日 Q4M 10
Architecture of Q4M
2008 5 27年 月 日 Q4M 11
File FormatFile Format
Log-based file formatログベースのファイルフォーマット
Each row (queue_row_t) consist of:Row Header (4 bytes) -- row type and body sizeSource Row ID -- optional, for message transferBody -- < 512MB
2008 5 27年 月 日 Q4M 12
Row HeaderRow Header
Row Types: 3bitsNormal rowRemoved rowNormal row with source rowidRemoved row with source rowidChecksum
Used to recovering successful INSERTs 29-bit adler32
Number of Rows Removed Used for recovering rowids
Body Size: 29bits
2008 5 27年 月 日 Q4M 13
Data ConsistencyData Consistency
INSERTs group-commited using writevINSERT は writev を利用してグループコミット
Each group has a checksum prepended各グループの先頭にチェックサムが存在
Crash recovery is an iteration through the logfile until reaching a block with an invalid checksumクラッシュリカバリの際は、チェックサム比較に失敗
するブロックに到達するまでログを舐める
2008 5 27年 月 日 Q4M 14
Data Consistency (cont.)Data Consistency (cont.)
DELETEs are modifs. of row headersDELETE は行ヘッダの型変更
Normal row -> Removed row Normal row w. source rowid -> Removed row w. source rowid
Either by pwrite or mmap(PROT_WRITE) + msync pwrite あるいは mmap(PROT_WRITE) + msync で削除
Multirow DELETEs aren’t atomic 複数行 DELETE は非アトミック
Not a problem; no API to consume multiple rows at once
複数行を一度に消費する API がないので問題ではない
2008 5 27年 月 日 Q4M 15
Row IDsRow IDs
64bit IntStarts from one and incremented per eac
h row1 から始まって1行毎にインクリメント
Used to detect and block duplicates by relays
メッセージリレーが重複を検出・除外するために使用
2008 5 27年 月 日 Q4M 16
Restritions in SQL of Q4MRestritions in SQL of Q4M
No indexesNo UPDATE
Why need UPDATE a queue row?キューのデータを UPDATE する必要なんてない
SELECT and DELETE are supported for queue administration
SELECT DELETE を使ってキューを管理可能
ex. select count(*) from queue;
2008 5 27年 月 日 Q4M 17
Why no Indexes Support?Why no Indexes Support?
There is generally no reason to support indexes with mysql-based queues, since mysql cannot handle a large number of connections requesting data under various conditions
インデックスが必要になるほど多様な条件を指定して同時に購読できるほど、 mysql の同時接続数は大きくない
2008 5 27年 月 日 Q4M 18
Why no Indexes Support?Why no Indexes Support?
Instead use conditional subscription Conditional subscription 機能を使うべき
Or route data after reading from Q4Mあるいは Q4M からデータを読んだ後にルーティング
すべき
2008 5 27年 月 日 Q4M 19
Tutorial
2008 5 27年 月 日 Q4M 20
Sending a MessageSending a Message
INSERT into queue values (1,’Hello World!’);
2008 5 27年 月 日 Q4M 21
Receiving a MessageReceiving a Message
while (1) {SELECT queue_wait(‘queue’);;my @row = SELECT ROW * FROM queue;
or next;…
}SELECT queue_end(‘queue’);;
2008 5 27年 月 日 Q4M 22
OWNER Mode and NON-OWNER ModeOWNER Mode and NON-OWNER Mode
QuickTime˛ Ç∆TIFFÅiîÒà≥èkÅj êLí£ÉvÉçÉOÉâÉÄ
ǙDZÇÃÉsÉNÉ`ÉÉÇ å©ÇÈÇΩÇflÇ…ÇÕïKóvÇ≈Ç∑ÅB
2008 5 27年 月 日 Q4M 23
OWNER Mode and NON-OWNER Mode (cont.)OWNER Mode and NON-OWNER Mode (cont.)
Within OWNER mode, only the owned row are visible
OWNER モードでは、所有中の行のみが見える
Within NON-OWNER mode, rows owned by other connections are invisible
NON-OWNER モードでは、他の接続が所有している行は見えない
2008 5 27年 月 日 Q4M 24
Function for Entering OWNER ModeFunction for Entering OWNER Mode
queue_waitUsed to enter OWNER mode
OWNER モードに (再 )切替するために使用When called within OWNER mode, the owned row i
s deleted
2008 5 27年 月 日 Q4M 25
queue_waitqueue_wait
Two formsqueue_wait(tbl_cond)queue_wait(tbl_cond,[tbl_cond…,timeout])
2008 5 27年 月 日 Q4M 26
queue_wait(tbl_cond)queue_wait(tbl_cond)
Wait max. 60 seconds until any data becomes available on given table under given condition
最大 60秒間、指定された条件に合致する行が指定されたテーブルに登録されるまでブロック
Returns 1 if successful, 0 if no data成功すれば 1 、データがなければ 0
Enters OWNER mode even if no dataデータがない場合でも OWNER モードへ切り替わる
2008 5 27年 月 日 Q4M 27
queue_wait(tbl_cond)queue_wait(tbl_cond)
Table name (and optionally condition) should be specified
テーブル名と (オプションで条件 ) を指定
Only numeric columns may be used within condition
条件節では数値型のカラムのみ使用可能See queue_share_t::init_fixed_fields
tbl_cond ::= table [ “:” cond]
2008 5 27年 月 日 Q4M 28
queue_wait(tbl_cond) (cont.)queue_wait(tbl_cond) (cont.)
SELECT queue_wait(‘table:v<3’);
2008 5 27年 月 日 Q4M 29
queue_wait(tbl_cond,[tbl_cond…,timeout])queue_wait(tbl_cond,[tbl_cond…,timeout])
Accepts multiple tables and timeout複数のテーブルとタイムアウトを指定可能
Data searched from leftmost table to right
データは左端のテーブルから右方向に探索
Returns table index (the leftmost table is 1) of the newly owned row
獲得した行の属するテーブルのインデックス (左端のテーブルが 1) を返す
2008 5 27年 月 日 Q4M 30
Functions for Exiting OWNER ModeFunctions for Exiting OWNER Mode
queue_endDeletes the owned row and exits OWNER mode
所有中の行を削除して OWNER モードを脱出
queue_abortReleases (instead of deleting) the owned row and
exits OWNER mode所有中の行を非所有状態に変更して OWNER モードを脱出
Close of a MySQL connection does the same thingMySQL の接続切断時も同様
2008 5 27年 月 日 Q4M 31
Receiving a Message (revisited)Receiving a Message (revisited)
while (1) {SELECT queue_wait(‘queue’);;my @row = SELECT ROW * FROM queue;
or next;if (consume_row(@row) != SUCCESS) {
exit(1);}
}
2008 5 27年 月 日 Q4M 32
Applications of Q4M
2008 5 27年 月 日 Q4M 33
Connecting Distant ServersConnecting Distant Servers
Pathtraq uses Q4M queues and a relay to communicate with its content analysis service running in a different iDC
Pathtraq では、 Q4M キューとメッセージリレーを使用して、別の iDC にあるコンテンツ分析サービスと接続している
ContentAnalysisService
Queue
PathtraqDB
Queue
MySQL conn.over SSL,gzip
2008 5 27年 月 日 Q4M 34
User NotificationsUser Notifications
For sending notifications from web services
ウェブサービスでユーザ通知送信のために使用可能
DB
Queue(s)
App. Logic SMTP Agent
IM Agent
2008 5 27年 月 日 Q4M 35
Asynchronous UpdatesAsynchronous Updates
Asynchronous updates leads to faster response of web services
非同期更新に利用することでウェブサービスの応答性を向上可能
DB
Queue
App. Logic
DB
2008 5 27年 月 日 Q4M 36
Scheduling Web CrawlersScheduling Web Crawlers
Web crawlers with retry-on-errorリトライ機能つき Web クローラー
URLDB
Request Queue
Spiders
Retry Queue
Re-scheduler
Store Result
Read URL
If failed to fetch, store URL in retry queue
2008 5 27年 月 日 Q4M 37
Current Limitations and the Future of Q4M
2008 5 27年 月 日 Q4M 38
Current LimitationsCurrent Limitations
Table compactions is a blocking operation
テーブルコンパクションが他の処理をブロックするRuns when live data becomes <25% of log file
ライブデータが 25%以下になると実行
Relays are slowNot a problem for Cybozu Labs :-p
2008 5 27年 月 日 Q4M 39
Future of Q4MFuture of Q4M
API for consuming multiple rows at once複数行を一度に受信処理するモード
Necessary for speeding up relaysリレーの高速化に必要
Simple “delete-on-SELECT” mode for novices
初心者向けの「 SELECT 時に削除」モード
Support for transaction w. InnoDB?InnoDB とのトランザクション処理
2008 5 27年 月 日 Q4M 40
Configuration Options of Q4MConfiguration Options of Q4M
--with-sync=no|fsync|fdatasync|fcntlControls synchronization to disk, see --help for det
ail
--enable-mmapMmap’ed reads lead to higher throughput
--enable-mmap-writesDELETEs commited using mmap(PROT_WRITE) and
msync, recommended on linux>=2.6.20 if you need really high performance