高可用性とデータ・シャーディングを実現できるMySQL Fabricとは?
-
Upload
yoyamasaki -
Category
Technology
-
view
4.243 -
download
7
description
Transcript of 高可用性とデータ・シャーディングを実現できるMySQL Fabricとは?
高可用性とデータ・シャーディングを 実現できるMySQL Fabricとは?
日本オラクル株式会社
山崎 由章 / MySQL Senior Sales Consultant,
Asia Pacific and Japan
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 2
以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはできません。以下の事項は、マテリアルやコード、機能を提供することをコミットメント(確約)するものではないため、購買決定を行う際の判断材料になさらないで下さい。オラクル製品に関して記載されている機能の開発、リリースおよび時期については、弊社の裁量により決定されます。
OracleとJavaは、Oracle Corporation 及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 3
MySQL Fabricとは?
MySQLサーバー群を管理する
統合型のフレームワーク
高可用性(HA)とデータ・シャーディング
による拡張性を実現できる
MySQL Utilitiesの一部として
提供されている(1.4.3以降)
– GTIDモードによるレプリケーション
機能を活用している
(MySQL 5.6以降で使用可能)
MySQL Fabric
Connector
Application
Read-slaves
SQL
Master group
Read-slaves
Master group
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 4
レプリケーション
• MySQLの標準機能
– シンプルな設定
– マスタ→スレーブ
– 多数Webでの実績
• 非同期型 or 準同期
• 特徴
– 参照性能を向上させる構成
(スレーブサーバーで参照処理が実行可能)
– 高可用性、バックアップ用途での利用も
– 基本は一方向でのデータコピーだが、
双方向や循環型での利用も可能
(データの更新には注意が必要)
– 更新ログ(bin-log:バイナリログ)を利用
Webアプリケーションでは参照が95%、 更新が5%というケースも (Digg.com)
> シンプルなスケールアウト構成によって 簡単に20倍以上の性能向上が図れる
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 5
レプリケーション: スケールアウト構成による性能向上 参照性能の向上
M S S
S
S
M
更新処理 参照処理
参照処理
更新処理
参照処理
の増加→
スレーブ
追加
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 6
C
B
A
マスターの障害時に、スレーブをマスターに昇格
レプリケーション:高可用性構成
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 7
C
B
A 障害発生
レプリケーション:高可用性構成
マスターの障害時に、スレーブをマスターに昇格
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 8
C
B
A
B が新しい
マスターに
レプリケーション:高可用性構成
マスターの障害時に、スレーブをマスターに昇格
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 11
Slave Master
“Lossless”準同期レプリケーション
マスターは指定のスレーブがトランザクションを受信するまで以下の処理を待つ
– ストレージエンジンへのコミット
– 他のクライアントから変更へのアクセス
– アプリケーションへのコミットの応答
スレーブが変更点を反映させるまでは
待たない
– 遅延を最小化
スレーブに安全にコピーされるまで他のトランザクションが新しいデータを変更できないように
App commits
Written to Binary log
Send to slave(s)
Commit to SE & externalized
Ack Commit
Write to relay log
Apply txn
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 12
Slave Master
準同期レプリケーション MySQL 5.6 MySQL5.7“Lossless”
App commits
Written to Binary log
Send to slave(s)
Commit to SE & externalized
Ack Commit
Write to relay log
Apply txn
Slave Master
App commits
Written to Binary log
Send to slave(s)
Commit to SE & externalized
Ack Commit
Write to relay log
Apply txn
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 13
Slave Master
準同期レプリケーション MySQL 5.6 MySQL5.7“Lossless”
App commits
Written to Binary log
Send to slave(s)
Commit to SE & externalized
Ack Commit
Write to relay log
Apply txn
Slave Master
App commits
Written to Binary log
Send to slave(s)
Commit to SE & externalized
Ack Commit
Write to relay log
Apply txn
障害発生
ここで参照できたデータが消失
する可能性あり
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 14
マスタ > スレーブ
マルチマスタ > スレーブ (マルチソース)
マスタ < > マスタ (マルチマスタ)
マスタ > マルチスレーブ
循環型 (マルチマスタ)
マスタ > スレーブ > マルチスレーブ
MySQL レプリケーションの構成パターン
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 15
マルチソース レプリケーション
複数のマスターでの変更点を1台のスレーブに集約
– 全ての「シャード」のデータを単一のビューで分析
– バックアップ用にデータを集約
準同期レプリケーションおよびマルチスレッド スレーブに対応
マスターごとにフィルタと制御可能となる予定
アプリケーションはマスタごとに個別にアクセス
Binlog
Master 1
Binlog
Master 2
…
…
Binlog
Master N
IO 1
Relay 1
Coordinator
W1 W2 … WX
IO 2
Relay 2
Coordinator
W1 W2 … WX
…
…
Coordinator
W1 W2 … WX
IO N
Relay N
Coordinator
W1 W2 … WX
Slave
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 16
MySQLの高可用性構成
MySQL
Cluster
MySQL
Cluster
アプリケーション/ APサーバ
負荷分散
双方向 同期複製
• MySQL Cluster シェアードナッシング型高性能クラスタ
MySQL
Server
• MySQL+DRBD Linux用のノード間データコピー
アプリケーション/ APサーバ
フェールオーバー
同期複製 MySQL
Server
アプリケーション/ APサーバ
共有ディスク
• 3rdベンダ製HAソフト利用 共有ディスクにデータを格納
フェールオーバー
MySQL
Server
MySQL
Server
アプリケーション/ APサーバ
負荷分散
非同期複製
• レプリケーション(標準機能) 非同期&準同期データレプリケーション
MySQL
Server
MySQL
Server
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 17
MySQLの高可用性ソリューション
MySQL 5.6
レプリケーション
Oracle VM
Template
Solaris
Cluster
Windows
Cluster DRBD
Oracle
Clusterware 12c
MySQL
Cluster
自動フェールオーバー ✔ ✔ ✔ ✔ ✔ ✔ ✔
データロス無し Semi-Sync ✔ ✔ ✔ ✔ ✔ ✔
サポートOS All Linux Solaris Windows Linux Linux All
クラスタモード Master +
Slaves
Active/
Passive
Active/
Passive
Active/
Passive
Active/
Passive
Active/
Passive
Multi-
Master
共有ディスク 不要 必要 必要 必要 不要 必要 不要
可用性デザイン 99.9% 99.99% 99.99% 99.95% 99.99% 99.99% 99.999%
単一ベンダサポート ✔ ✔ ✔ ✖ ✔ ✔ ✔
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 18
GTID (GLOBAL TRANSACTION IDENTIFIERS)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 19
MySQL 5.6から追加された機能
複数台のレプリケーション環境でも容易にトランザクションの
追跡/比較が可能
– トランザクションを一意に識別できる識別子をバイナリログに記録
フェイルオーバーのために、
最も最新のスレーブを自動認識
多段構成のレプリケーションが容易に
グローバルトランザクションID(GTID)
Master
GTID=123456
GTID=123456
GTID=123456 GTID=123456
Slave
Slave Slave
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 20
コミット時にGTIDが生成される
– server_uuid:number
a61678ba-4889-4279-9e58-45ba840af334:1
– server_uuid によって、サーバを一意に識別できる
– number は、各サーバ単位でトランザクション毎に1ずつ増加する
GTIDはバイナリログに記録される(バイナリログのフォーマットが従来と変わる)
GTIDによって、スレーブによるトランザクションの再実行を防ぐ
グローバルトランザクションID(GTID)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 21
フェイルオーバー、トポロジ変更時の動作(例:マスターを変更) – スレーブは新しいマスターに実行が完了しているGTID を伝える
– マスターはスレーブが実行していないトランザクションだけを
スレーブに送る
@@GTID_EXECUTED で実行が完了したGTID を確認できる – 確認例) SELECT @@global.gtid_EXECUTED;
マスターの変更時、ポジションは自動的に認識される – 管理の手間削減
– CHANGE MASTER TO MASTER_AUTO_POSITION = 1
グローバルトランザクションID(GTID)
※従来のやり方(GTIDを使用しないレプリケーション)では、
マスター変更時にバイナリログファイルのファイル名とポジションを指定する
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 22
参考情報
• MySQL 5.6 レプリケーション : 概要 (ホワイトペーパー) http://www-jp.mysql.com/why-mysql/white-papers/mysql-replication-ja/
⇒ MySQL 5.6での機能強化点についてもまとまっています。
• MySQL 5.6 レプリケーション : チュートリアル (ホワイトペーパー) http://www-jp.mysql.com/why-mysql/white-papers/mysql-replication-tutorial-ja/
⇒ レプリケーションの設定手順等の解説も掲載されています。
• Think IT での紹介記事 「 MySQL 5.6での機能強化点(その3)- 人気のレプリケーションが更に機能強化」 http://thinkit.co.jp/story/2014/01/23/4717
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 24
MySQL Utilities 運用管理に関するPythonスクリプト
データベース管理
データベース運用
レプリケーション管理
設定管理
MySQL Utilities
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 25
データベース管理
• mysqldbcompare – データや定義を比較
• mysqldbcopy – 別のサーバにデータベースをコピー
• mysqldbexport – データとメタデータをエクスポート
• mysqldbimport – データとメタデータをインスポート
• mysqldiff – サーバ間のテーブルなどオブジェクトの定義を比較
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 26
データベース運用
• mysqldiskusage – デーブルおよびデータファイルのサイズを表示
• mysqlfrm – .frmファイルを読取り、CREATE TABLE文を作成
• mysqlindexcheck – インデックスの重複をチェック
• mysqlmetagrep – テーブル定義のメタデータをgrep (正規表現利用可)
• mysqlprocgrep – プロセス情報をgrep (正規表現利用可)
• mysqluserclone – 別のサーバにユーザアカウントををコピー
• mysqluc – コマンドライン環境
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 27
レプリケーション管理
• mysqlfailover – レプリケーションの自動フェールオーバー
• mysqlreplicate – レプリケーションを設定
• mysqlrplms – ラウンドロビンによるマルチソースレプリケーション
• mysqlrpladmin – レプリケーションの各種管理
• mysqlrplcheck – レプリケーションが正しく設定されているかの確認
• mysqlrplshow – レプリケーショントポロジ(親子関係)を図示
• mysqlrplsync – マスタ-スレーブ間、スレーブ-スレーブ間での
データ同期状況を確認
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 28
設定管理
• mysqlserverclone – 既存のMySQLサーバのコピーを作成
• mysqlserverinfo – サーバの稼働状況を表示
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 29
Specialized Operations
• mysqlauditadmin – 監査ログのメンテナンス
• mysqlauditgrep – 監査ログの検索
※監査ログ取得は、MySQL Enterprise Edition(商用版)のみの機能です。
MySQL Enterprise Audit
http://www-jp.mysql.com/products/enterprise/audit.html
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 30
参考情報
• MySQL Utilitiesのダウンロード
http://dev.mysql.com/downloads/utilities/
• MySQL Utilitiesのドキュメント
http://dev.mysql.com/doc/mysql-utilities/1.4/en/index.html
• Think IT での紹介記事
「Pythonで作られた便利なコマンドラインツール MySQL Utilities」 http://thinkit.co.jp/story/2014/02/10/4814
• http://http://dev.mysql.com/doc/workbench/en/mysql-
utilities.html
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 32
MySQL Fabricとは?
MySQLサーバー群を管理する
統合型のフレームワーク
高可用性(HA)とデータ・シャーディング
による拡張性を実現できる
MySQL Utilitiesの一部として
提供されている(1.4.3以降)
– GTIDモードによるレプリケーション
機能を活用している
(MySQL 5.6以降で使用可能)
MySQL Fabric
Connector
Application
Read-slaves
SQL
Master group
Read-slaves
Master group
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 33
MySQL Utilities - Fabric
「シャーディング」による拡張性
レプリケーションの自動フェイル
オーバーによる高可用性
コネクタ
– Python
– Java
– PHP
アプリケーションでの分割キー
– Range または Hash
– シャード再構成も可能
– シャード全体の更新も可能
MySQL Fabric
Connector
Application
Read-slaves
SQL
Master group
Read-slaves
Master group
GA!!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 34 Copyright © 2014, Oracle and/or its affiliates. All rights reserved.| Percona Live | April 3, 2014 |9
High-Level Components
● Fabric-aware Connectors● Python, PHP, and Java● Enhanced Connector API
● MySQL Fabric Node● Manage information about farm● Provide status information● Execute procedures
● MySQL Servers● Organized in High-Availability Groups● Handling application data
High AvailabilityGroup
ApplicationConnector
Connector
Connector
MySQLFabricNode
High-Level Components
Fabric-aware Connectors
– Python, PHP, and Java
– Enhanced Connector API
MySQL Fabric Node
– Manage information about farm
– Provide status information
Execute procedures MySQL Servers
– Organized in High-Availability Groups
– Handling application data
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 35
MySQL Fabric Configuration
Backing Store
– MySQL server
– Persistent storage for state
– Storage engine-agnostic
Protocol
– Address where node will be
– Currently only XML-RPC
Logging
– Chatty: INFO (default)
– Moderate: WARNING
– URL for rotating log
[storage]
address = localhost:3306
user = fabric
password =
database = fabric
[servers]
user = fabric
password =
[protocol.xmlrpc]
address = localhost:32274
threads = 5
disable_authentication = yes
[logging]
level = INFO
url = file:///var/log/fabric.log
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 36
MySQL Fabric: Basic Commands and Help
● Command Structure ● mysqlfabric group command ...
● Getting help mysqlfabric help
mysqlfabric help commands
mysqlfabric help manage
mysqlfabric help manage setup
● MySQL Utilities Documentation: ● http://dev.mysql.com/doc/mysql-utilities/1.4/en/index.html
● MySQL Fabric Documentation: ● http://dev.mysql.com/doc/mysql-utilities/1.4/en/fabric.html
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 37
Setting up and Tearing down MySQL Fabric
● Create and populate the necessary tables in backing store
mysqlfabric manage setup
● Remove the tables from backing store
mysqlfabric manage teardown
● Connectstothedatabaseserverin“storage”section ● Ensure that you have the necessary users and privileges
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 38
Starting and Stopping MySQL Fabric
● Start MySQL Fabric node in foreground – print log to terminal
mysqlfabric manage start
● Start MySQL Fabric node in background – print log to file
mysqlfabric manage start --daemonize
● Stop MySQL Fabric node
mysqlfabric manage stop
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 39
Create Groups and add Servers
● Define a group
mysqlfabric group create my_group
● Add servers to group
mysqlfabric group add my_group server1.example.com
mysqlfabric group add my_group server2.example.com
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 40
Activate High-Availability Group
● Promote one server to be primary
mysqlfabric group promote my_group
● Tell built-in failure detector to monitor group
mysqlfabric group activate my_group
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 41
Distributed Failure Detector
Connectors report errors
– Report that an error was noticed
– Failover based on statistics
– report_error(server, source, error)
Report failure
– A server is known to have failed
– Failover occurs immediately
– report_fault(server, source, error)
New in MySQL Fabric 1.4.2
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.| Percona Live | April 3, 2014 |22
Distributed Failure DetectorNew in MySQL Fabric 1.4.2
● Connectors report errors● Report that an error was noticed● Failover based on statistics● report_error(server, source, error)
● Report failure● A server is known to have failed● Failover occurs immediately● report_fault(server, source, error)
Connector
r epor t _ er r or
r epor t _ f aul t
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 42
● Fabric-aware Connectors ● Connector/J
● Connector/Python
● Connector/PHP
● Fabric-aware Frameworks ● Doctrine
● Hibernate
● In this presentation: ● Connector/Python
Fabric-aware Connector
● Connector API Extensions ● Support Transactions
● Support full SQL
● Decision logic in connector ● Reducing network load
● Load Balancing ● Read-Write Split
● Distribute transactions
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 43
Fabric-aware Connector API
import mysql.connector
conn = mysql.connector.connect(
fabric={"host": "fabric.example.com"},
user='mats', password='xyzzy', database="employees"
)
● Establisha“virtual”connection ● Real server connection established lazily
● Provide connection information for the Fabric node ● Connector will fetch information about servers
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 44
Enable Connector/Python Error Reporting
Connectors can report errors to Fabric node
– Enable using report_error
– Defaults to False
– Require MySQL Fabric 1.4.2
New in Connector/Python 1.2.1
import mysql.connector
conn = mysql.connector.connect(
fabric={"host": "fabric.example.com"},
user='mats', password='xyzzy', database="employees",
report_error=True,
)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 45
Connector API: Executing a Transaction
● Provide group name ● Property: group
● Fabric will compute candidate
servers
conn.set_property(group='my_group', mode=MODE_READWRITE)
cur = conn.cursor()
cur.execute("INSERT INTO employees VALUES (%s,%s,%s)",
(emp_no, first_name, last_name))
cur.execute("INSERT INTO titles(emp_no,title,from_date)"
“ VALUES (%s,%s,CURDATE())",
(emp_no, 'Intern'));
conn.commit()
● Provide transaction mode ● Property: mode
● Fabric will pick server in right
mode
Same as
before
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 46
MySQL Fabric: Set up Shard Mapping
● Define shard mapping mysqlfabric sharding ¥
create_definition hash my_global
● Add tables that should be sharded mysqlfabric sharding add_table 1 ¥
employees.employees emp_no
mysqlfabric sharding add_table 1 ¥
employees.salaries emp_no
● Tables not added are considered global
Shard map identifier
Will return a
shard map identifier
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 47
MySQL Fabric: Add Shards
● Add shards to shard mapping
mysqlfabric sharding add_shard 1 ¥
“my_group.1,...,my_group.N” --state=ENABLED
Shard map identifier
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 48
MySQL Fabric: Moving and Splitting Shards
● Moving a shard from one group to another
mysqlfabric sharding move 5 my_group.5
● Splitting a shard into two pieces (hash)
mysqlfabric sharding split 5 my_group.6
Shard ID
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 49
Connector API: Shard Specific Query
● Provide tables in query ● Property: tables
● Fabric will compute map
conn.set_property(tables=['employees.employees', 'employees.titles'],
key=emp_no)
cur = conn.cursor()
cur.execute("INSERT INTO employees VALUES (%s,%s,%s)",
(emp_no, first_name, last_name))
cur.execute("INSERT INTO titles(emp_no, title, from_date)"
" VALUES (%s, %s, CURDATE())",
(emp_no, 'Intern'));
conn.commit()
● Provide sharding key ● Property: key
● Fabric will compute shard
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 50
Connector API: Global Update
● Provide tables in query ● Property: tables
● Fabric will compute map
● (Not necessary)
conn.set_property(tables=['employees.titles'], scope='GLOBAL')
cur = conn.cursor()
cur.execute("ALTER TABLE employees.titles ADD nickname VARCHAR(64)")
● Set global scope ● Property: scope
● Query goes to global group
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 51
What do we have now?
MySQL Farm Management
– High-Availability
– Sharding
High-Availability
– Group Concept
– Slave promotion
Sharding
– Range and hash sharding
– Shard move and shard split
Connector APIs
– Transaction properties
– “Virtual”connections
Enhanced Connectors
– Connector/Python
– Connector/PHP
– Connector/J
Command-line Interface
XML-RPC Interfaces
Distributed failure detector
– Connectors report failures
– Custom failure detectors
Credentials
– RFC 2617
– SSL support
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 52
Thoughts for the Future
Connector multi-cast
– Scatter-gather
– UNION of result sets
– More complex operations?
Extension interfaces
– Improve extension support
– Improve procedures support
Command-line interface
– Improving usability
– Focus on ease-of-use
More protocols
– MySQL-RPC Protocol?
More frameworks?
More connectors?
– C/C++?
– Fabric-unaware connectors?
More HA group types
– DRBD
– MySQL Cluster
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 53
Thoughts for the Future
“Transparent”Sharding
– Single-query transactions?
– Speculative execution?
– Cross-shard join?
Multiple shard mappings
– Independent tables
Multi-way shard split
– Efficient initial sharding
– Better use of resources
High-availability executor
– Node failure stop execution
– Replicated State Machine
Paxos?
Raft?
– Continue execution on other Fabric
node
Session Consistency
– We have a distributed database
– It should look like a single database
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 54
是非MySQL Fabricを試して、 フィードバックを下さい!!
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 55
フィードバックはこちらから MySQL Bugs
http://bugs.mysql.com/
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 56
フィードバックはこちらから
“Report a bug” をクリック
・重要度を選択
・機能追加要望の場合は、
“Feature request” をクリック
MySQL Bugs
http://bugs.mysql.com/
“Category” は ”MySQL Fabric”を選択
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 57
既に報告されているバグ、機能追加要望の場合は、”Affects me”ボタンをクリック