スティルハウスの書庫の書庫

はてなダイアリーで書いてた「スティルハウスの書庫」を移転してきました。

Channel API+Matcher APIでつくるイベント駆動Webアプリ

これはappengine ja night #12のプレゼン資料です。

Channel APIとは

Channel APIは、App Engineで動作するプッシュ通信サービスです。詳しくはこちら:

Matcher APIとは

Matcher APIとは、「スケーラブルなリアルタイムマッチングサービス」です。

  • マッチングの条件を独自のクエリ構文で記述して登録
  • エンティティやdictの内容が条件に合致すると、タスクで通知
  • 数千、数万件の条件を数秒でチェックし通知できます

Matcher APIとは

簡単に言うと。。

DB/Datastoreのクエリは「過去に対するクエリ」
Matcherのクエリは「未来に対するクエリ」

例えばこんなdb.Modelを想定

class Person(db.Model):
name = db.StringProperty()
birth = db.IntegerProperty()
height_meters = db.FloatProperty()

条件を登録

matcher.subscribe(Person, “name: smith OR name:schmidt”, “smith”)
matcher.subscribe(Person, “birth < 1900”, “old”)
matcher.subscribe(Person, “height_meters > 2.0”, “tall”)
matcher.subscribe(Person, “birth > 1945 AND birth < 1965”, “boomer”)

  • 個々の登録にIDを振る(sub_id)

personが更新されたら、match()

person = MatcherDocument()
person.name = “smith”
person.height_meters = 1.5
matcher.match(person)

タスクで通知される

  • /_ah/matcher が呼ばれる
  • 合致したsub_idのリストが渡される
  • あまり大きなデータは受け取れない
    • Memcacheなどを併用

dictでもOK

person = {
'name': 'Andrew Smith',
'citizenship': 'Brazil',
'birth': 1975,
'height_meters': 1.54,
}
  • Datastoreには非依存

dictの場合、事前にスキーマを定義

PersonSchema = {
str: ['name', 'citizenship'],
int: ['birth']
float: [height_meters']
}

Matcherのクエリ言語

テキストの部分一致

field_text_a:horse
field_text_a:"horse riding"

数値

> >=, =, <= and <

論理演算

AND, OR, NOT

Matcherのクエリ例

  • カッコで括ってネストもできる

field_int32_a > 20 AND field_int32_b = 10

(field_int32_a > 20 AND field_int32_b = 10) OR field_text_a:fox

MatcherのAPI「subscribe」

Matcherに条件を登録します。

matcher.subscribe(document_class,
query,
sub_id,
schema=None,
topic=None,
lease_duration_sec=DEFAULT_LEASE_DURATION_SEC)

MatcherのAPI「unsubscribe」

Matcherの条件を削除します。

matcher.unsubscribe(document_class,
sub_id,
topic=None)

MatcherのAPI「match」

引数のドキュメントとすべての条件と比較します。

matcher.match(document,
topic=None,
result_key=None,
result_relative_url='/_ah/matcher',
result_task_queue='default',
result_batch_size=DEFAULT_RESULT_BATCH_SIZE,
result_return_document=True)

MatcherのAPI「match」

  • result_key: ドキュメントに割り当てるキー
  • result_batch_size: 個々のタスクに渡すsub_idの最大数
  • result_return_document: タスクにドキュメントを渡すかどうか

MatcherのAPI「list_subscriptions」

サブスクリプションの一覧を返します。

list_subscriptions(document_class,
sub_id_start="",
topic=None,
max_results=1000,
expires_before=None)

OK -- アクティブ
PENDING -- まだアクティブではない状態
ERROR -- エラー

Matcherのタスク

タスク/_ah/matcherでは、マッチ結果を処理します。
次のパラメータが渡されます。

Matcherの使い道

Twitterのキーワード検索のストリーミング
Google Instant/Percolator

  • (ストアドプロシージャ)

商品・株価等のアラート
pub/subメッセージング、CEP(後述)
*使い道が想像つかないのが面白い

jsonengineでのCQL対応

CQL=Continuous Query Language

/_je/myDoc?name.eq.kaz

Channel API経由で結果が返る
クエリ条件に合致したdoc更新を継続的に通知
→ストリーム検索とか簡単に実装可能

イベント、ストリーム、CEP

ストリーム/イベント駆動なクラウド技術

CEPって何だ

Complex Event Processing

リクエスト:これから起きてほしいことの要求
イベント:すでに起きたことの通知

  • (メッセージとはちょっと違うみたい)

イベント処理エージェントの集合体でアプリを構築

ストリーム/イベント駆動な書籍

  • Event processing in action
  • Distributed event-based systems

Channel API+Matcher APIサンプルコード