treasure dataを支える技術 - messagepack編

42
Copyright ©2014 Treasure Data. All Rights Reserved. Treasure Dataを支える技術 - MessagePackTaro L. Saito, Treasure Data [email protected] December 11, 2014 ビッグデータ基盤勉強会@NTT武蔵野開発センター

Upload: taro-l-saito

Post on 12-Jul-2015

5.623 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Treasure Dataを支える技術 - MessagePack編

Taro L. Saito, Treasure Data [email protected] December 11, 2014 ビッグデータ基盤勉強会@NTT武蔵野開発センター

Page 2: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

自己紹介 @taroleo •  2002 東京大学 理学部 情報科学科卒 •  2007 Ph.D.

–  XMLデータベース(SIGMOD’08)、トランザクション処理の研究

•  ~ 2014 東京大学 情報生命科学専攻 助教 –  ゲノムサイエンス研究

•  大規模データ処理、並列・分散コンピューティング

•  2014.3月~ Treasure Data –  ソフトウェアエンジニア, MPP Team Leader

2

Page 3: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

「データベース」に関連するオープンソース活動

•  sqlite-jdbc –  SQLite DBMS for Java –  1ファイル=1DB

•  snappy-java –  高速圧縮ライブラリ –  月に10万回以上ダウンロード –  Sparkでも採用

•  msgpack-java •  UT Genome Browser (UTGB)

–  大規模ゲノム情報の可視化

3

Page 4: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Treasure Dataとは? •  米シリコンバレー発日本人創業のビッグデータ関連企業

–  2011年12月、米Mountain Viewにて創業 –  2012年11月、東京丸の内に日本支社設立

•  クラウド型データマネージメントサービス「Treasure Data Service」を提供

4

芳川裕誠 – CEO Open source business veteran

太田一樹 – CTO Founder of world’s largest Hadoop Group

主要投資家

Bill Tai Charles River Ventures, Twitterなどに投資 まつもとゆきひろ Ruby言語開発者

Sierra Ventures – (Tim Guleri) 企業向けソフト・データベース領域での有力VC

創業者

Jerry Yang Yahoo! Inc. 創業者

古橋貞之 – Software Engineer MessagePack, Fluentd開発者

Page 5: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Treasure Data Service ビッグデータのための「クラウド + マネジメント」一体型サービス

データ収集~保存~分析までワンストップでサポート

5

•  毎日数百億規模のレコードが取り込まれている –  2014年5月に5兆(trillion)レコードに到達

•  SQLベース(Hive, Presto, Pigなど)による検索サービスを提供

Page 6: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Impressive Momentum

0

1000

2000

3000

4000

5000

6000

7000

Da

ta G

row

th in

Bill

ions

Data (records) Imported

Service Launched

3 Trillion

4 Trillion

1 Trillion

5 Trillion

2 Trillion

Series A Funding

100 Customers

Gartner Cool Vendor Report

6

Page 7: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved. 7

Page 8: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Over 100 customers, including:

8

Page 9: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Claremont Report on Database Research •  データベースの研究者、ユーザー、識者が今後のDBMSについて議論 –  CACM, Vol. 52 No. 6, 2009

•  Cloud Data Serviceの発展を予測 –  SQLの重要な役割

•  機能を制限できる •  サービスとして提供するには好都合

–  難しい例:Spark  •  任意のコードを実行させるためのセキュアなコンテナ開発が別途必要

9

Page 10: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Beckman Report on Database Research •  2013年版

–  http://beckman.cs.wisc.edu/beckman-report2013.pdf –  ほぼ全面Big Dataについての話題

•  End-to-end –  データの収集から、knowledgeまで生み出す

•  Cloud Serviceの普及 –  IaaS, PaaS, SaaS –  DBMSの全てをクラウドで、という理想にはまだ到達していない

•  DB教育にも言及 –  70, 80年代のシステムの紹介から脱するべき –  DBの技術は、分散システム、Hadoopなど他分野にも価値があることを理解すべき

10

Page 11: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Results Push

Results Push

SQL

Big Data Simplified: The Treasure Data Approach A

pp S

erve

rs

Multi-structured Events!•  register!•  login!•  start_event!•  purchase!•  etc!

SQL-based Ad-hoc Queries

SQL-based Dashboards

DBs & Data Marts

Other Apps

Familiar & Table-oriented

Infinite & Economical Cloud Data Store

ü  App log data!ü  Mobile event data!ü  Sensor data!ü  Telemetry!

Mobile SDKs

Web SDK

Multi-structured Events

Multi-structured Events

Treasure Agent

Treasure Agent

Treasure Agent

Treasure Agent Treasure Agent

Treasure Agent

Treasure Agent

Treasure Agent

Embedded SDKs

Server-side Agents

11

Page 12: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Treasure Dataの基盤技術

•  データを「正しく集める」 –  Fluentd

•  ログコレクター •  タグ -> JSON or MessagePack形式のログ(データ)

–  PlazmaDB •  Columnar DB •  MessagePack + Compression

•  データを「手軽に解析する」 –  クラウド上での分散クエリエンジン

•  Hive, Presto, Pigベース

12

Page 13: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Fluentd: データを正しく集めるための入り口

•  基本:JSON・MessagePack形式のデータを受け取る –  種々のinput/output plugin

•  メッセージのルーティング –  メッセージに付けられたタグ毎に送り先を切り替えられる

13

fluentd.org

Page 14: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Multi-structured Events

Multi-structured Events

Agent

Server-side Agents!

Javascript SDK!

Collectors

Mobile / Embedded SDKs!

Web File Uploader!

Bulk Loader

Bulk Importers!

Small Files

http://docs.treasuredata.com

For the latest list

14

Page 15: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

TDのバックエンドストレージ

•  Real-Time Storage –  直近のデータ (MessagePack format)

•  Archive Storage –  過去のデータ, MapReduceでcolumnarに分解、圧縮 (MessagePack + gzipなど) –  もともとのサイズ(json形式)の10分の1程度になる

15

Real-time Storage

Archive Storage

23c82b0ba3405d4c15aa85d2190e 6d7b1482412ab14f0332b8aee119 8a7bc848b2791b8fd603c719e54f 0e3d402b17638477c9a7977e7dab ...

2012-10-08 00:23:00 912ec80 2012-10-09 00:01:00 277a259 ...

1時間毎にmerge

import SQL Query

Page 16: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Object Storage + Index

•  Storage –  Amazon S3を利用。S3互換のRiak CS (Basho)も利用できる –  HTTP経由のデータ通信

•  Object Index: PostgreSQLを使用 –  S3ファイルのリスト列挙操作は遅いため –  ユーザー毎、時系列毎の範囲で検索するためにGiST indexを利用 16

Real-time Storage

Archive Storage

23c82b0ba3405d4c15aa85d2190e 6d7b1482412ab14f0332b8aee119 8a7bc848b2791b8fd603c719e54f 0e3d402b17638477c9a7977e7dab ...

2012-10-08 00:23:00 912ec80 2012-10-09 00:01:00 277a259 ...

1時間毎にmerge

import SQL Query

Page 17: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

PlazmaDB

17

Page 18: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

time ip : string code : int action : string

2014-09-21 10:14:33 135.52.211.23 15 upload

2014-09-21 10:14:33 45.25.38.156 -1 temp

2014-09-21 10:14:34 97.12.76.55 99 null

2014-09-21 10:14:36 76.121.9.35 21 event

ü  Familiar Tables

Agent

Collectors PlazmaDB

18

Page 19: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

time ip : string code : int action : string

2014-09-21 10:14:33 135.52.211.23 15 upload

2014-09-21 10:14:33 45.25.38.156 -1 temp

2014-09-21 10:14:34 97.12.76.55 99 null

2014-09-21 10:14:36 76.121.9.35 21 event

2014-09-21 11:27:41 97.12.76.55 99 null

2014-09-21 11:27:42 45.25.38.156 -1 temp

2014-09-21 11:27:42 135.52.211.23 15 upload

2014-09-21 12:02:18 97.12.76.55 99 null

2014-09-21 12:02:18 45.25.38.156 -1 temp

2014-09-21 12:02:19 135.52.211.23 15 upload

ü  Familiar Tablesü  Time-based Partitioning

Agent

Collectors

QueryPartitionPruning

PlazmaDB

19

Page 20: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

time ip : string code : int action : string status : string

2014-09-21 10:14:33 135.52.211.23 15 upload null

2014-09-21 10:14:33 45.25.38.156 -1 temp null

2014-09-21 10:14:34 97.12.76.55 99 null null

2014-09-21 10:14:36 76.121.9.35 21 event null

2014-09-21 11:27:41 97.12.76.55 99 null null

2014-09-21 11:27:42 45.25.38.156 -1 temp null

2014-09-21 11:27:42 135.52.211.23 15 upload null

2014-09-21 12:02:18 97.12.76.55 99 null null

2014-09-21 12:02:18 45.25.38.156 -1 temp null

2014-09-21 12:02:19 135.52.211.23 15 upload null

2014-09-21 12:03:24 76.34.123.54 13 status ok

2014-09-21 12:03:25 92.67.7.113 13 status error

2014-09-21 12:04:51 135.52.211.23 15 upload null

ü  Familiar Tablesü  Time-based Partitioningü  Schema-Flexible

Agent

Collectors PlazmaDB

20

Page 21: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessagePack

21

•  レコードはMessagePack形式 •  入力時のデータ型はそのまま保存

–  intやstring型のデータが列中に混在した状況でも使える

PlazmaDBがスキーマに合わせて自動型変換を行う

Page 22: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessagePack Format Types •  0x00 ~ 0x7f int

–  1 byte •  0xe0 ~ 0xff neg int

–  1 byte

•  Support fixed/variable length data

22

Page 23: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Dynamic Data Type Conversion

23

{“user”:54, “name”:”test”, “value”:”120”, “host”:”local”}

(Explicit) Schema user:int name:string value:int

SELECT 54 (int)

データ(JSON)

“test” (string) 120 (int)

host:int

NULL

Page 24: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Implicit / Explicit Schema •  Implicit Schema

–  スキーマは自動的に付与される –  入力されたデータ列から自動的に決定 –  ユーザーはスキーマ定義なしにデータを放り込める

•  Explicit Schema –  明示的にカラムに型を設定できる

•  変換可能でない場合はNULLとして出力される –  スキーマ変更への柔軟さ

24

Page 25: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Non-1NF Type (NF^2) Support •  Array types

–  [1, 2, … ] •  Map types

–  {id:1, name:leo, address:[xxxx, yyyy]} •  In SQL

–  JSON-based interface

25

Page 26: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

msgpack-java

26

Page 27: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessagePacker

27

Page 28: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessageUnpacker

28

Page 29: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

msgpack-java: Implementation Details

29

Page 30: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved. •  https://github.com/msgpack/msgpack-java/ 30

Page 31: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessagePackの高速化

•  なぜ高速化するのか?

•  例:2割速くなる => マシン台数を2割減らせる –  大幅なコストダウン

•  クエリ処理も速くなる –  全体のスループット向上

31

Page 32: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessageBuffer •  内部用buffer •  Javaのheapメモリに直接アクセス •  sun.misc.Unsafe

–  byte[]中の任意の位置のデータを特定のprimitive typeにcastできる

–  JavaでDBを作りたいなら必須の機能

32

Page 33: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

UnsafeでCに匹敵するメモリアクセス性能を出せる

•  http://frsyuki.hatenablog.com/entry/2014/03/12/155231 33

Page 34: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

なぜByteBufferは遅いのか? •  良いプログラミング作法

–  interfaceを定義して、派生クラスによる実装を用意する –  例: ByteBufferにはHeapByteBuffer, DirectByteBufferなどの実装がクラスパスに存在

•  現実:TypeProfile –  どの派生クラスのコードが呼び出されるか実行時に判定 –  JVMでTypeProfileが生成され、table lookupのコストが余計に必要 –  遅くなる!

•  MessageBufferのlittle endian版はTypeProfileを生成しないので高速

34

Page 35: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Format Typeの確認 •  Unpackerで必要

–  prefixを1 byte 読んでデータタイプを判定

•  自然な実装 –  switch-case

•  ANTLRが生成するlexer/parserはこのパターンのコード

35

Page 36: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Format Typeの確認 •  lookup tableを作成 - cacheの活用 – 20000x faster!

36

Page 37: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

2x performance improvement in v07

37

Page 38: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessagePack v07 •  Value interface

–  Integer, Float, Array, Map, ExtendedValue, etc.

–  Query Result -> Value -> ResultOutput •  TDではクエリ結果(Valueで表現)をCSV, JSON, S3, RDBMS, Tableau Server用フォーマットなどに変換して出力している

•  msgpack-jackson –  オブジェクトデータをMessagePack形式でserialize/deserialize –  v06にあったコード生成テンプレート機能

•  廃止へ •  維持が大変、javassistで生成されるコードをクラスローダーに読ませる部分が難しいなど、様々な課題

38

Page 39: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

MessagePack Hackathon at Silicon Valley •  SV

–  @frsyuki –  @oza_x86 –  @hkmurakami –  @taroleo

•  JP –  @komamitsu

39

Page 40: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved. 40

Page 41: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved. 41

Page 42: Treasure Dataを支える技術 - MessagePack編

Copyright ©2014 Treasure Data. All Rights Reserved.

Treasure Dataが目指すもの

•  「世の中の誰もが手軽にデータベースを使える」ようにする

•  まずは誰もが「データを手軽に正しく集められる」ように –  Fluentd+msgpackで柔軟にデータを取り込む

•  そして誰もが「データを手軽に解析」できる ように –  分散SQLエンジン

•  Hive, Presto –  plazmadbによるcolumnar storage, MessagePack

•  A lot more …

42

WE ARE HIRING!