my sql proxy

27
 MySQL Proxy Making MySQL more flexible Jan Kneschke [email protected]

Upload: liu-lizhi

Post on 10-May-2015

1.752 views

Category:

Education


0 download

DESCRIPTION

MySQL-Proxy

TRANSCRIPT

Page 1: My Sql Proxy

   

MySQL Proxy

Making MySQL more flexibleJan Kneschke

[email protected]

Page 2: My Sql Proxy

   

MySQL Proxy● proxy­servers forward requests to backends 

and can– transform, handle or block them

● released under the GPL– see http://forge.mysql.com/wiki/MySQL_Proxy

● developed as part of the Enterprise Tools since February 2007

Page 3: My Sql Proxy

   

Design Decisions● goal is to be transparent to the application layer● supports all platforms and languages● designed to handle thousands of parallel 

connections (c10k)● uses a embedded scripting language for 

customizations

Page 4: My Sql Proxy

   

Transparency● SHOW WARNINGS can be worked around with 

Query Injection● SELECT USER() shows the connected user 

(the proxy, not the client) which can be corrected with result­set rewriting

● host auth against the MySQL server

Page 5: My Sql Proxy

   

Latency● early tests via localhost● same script run directly and through the proxy● latency per mysql­packet: 0.4ms● ping RTT on 1Gbit: 0.1ms

Page 6: My Sql Proxy

   

Load Balancing● load balancing distributes the load across 

several slaves● Shortest Queue First is default

– send new connections to the server with the least number of open connections

Page 7: My Sql Proxy

   

Fail Over● dead host are detected● taking out of load balancing for 2min● uses custom load balancers to decide how to 

handle a dead host– hot + standby– uses load balancing

Page 8: My Sql Proxy

   

Removing SPoF● one Proxy == Single Point of Failure● use external Heartbeat (linuxha.org) or● 2 LB proxies + 1 Host Selecting Proxy per 

application server

Page 9: My Sql Proxy

   

Failsafe Load Balancing

Page 10: My Sql Proxy

   

Flexibility● proxy embeds LUA ● allows analyzing and manipulating packets 

– Inspection– Rewriting– Blocking– Injection

Page 11: My Sql Proxy

   

LUA● PiL http://lua.org/manual/5.1/● embedded, simple, efficient● can do OO­like programming● has scalars, tables, metatables and anonymous 

functions

Page 12: My Sql Proxy

   

Query Rewriting● Macro Packages (ls, cd, who, ...)● tagging queries with SQL_CACHE● migrating table­names and SQL dialects● turn EXPLAIN UPDATE|DELETE  into 

equivalent EXPLAIN SELECT

Page 13: My Sql Proxy

   

Query Profiling● SHOW SESSION STATUS around a QueryExec_time: 6749 us

.. Handler_read_rnd_next = 252

.. Handler_write = 252

.. Select_scan = 1

Page 14: My Sql Proxy

   

Query Statistics● Normalize Queries to track query usage● Count Table and Index usage● Optimize Query Cache Usage by injecting 

SQL_CACHE in cachable queries● see lib/analyze­queries.lua

Page 15: My Sql Proxy

   

Auditing● Diagnostic Auditing● track which user+ip run which query or 

accessed which objects when● assign query­costs● log gathered information in a central place● see lib/auditing.lua

Page 16: My Sql Proxy

   

Global Transaction ID● Inject a counter in all transactions● Answers questions like

– which slave is most current– can I read from this slave, or do I have to read from 

master– you name it

Page 17: My Sql Proxy

   

Connection Pooling● reusing open connections between proxy and 

server● reduces concurrency on the MySQL Server● external connection pool for PHP

Page 18: My Sql Proxy

   

Statement Routing● split the query stream into reading and writing

– READs go to the slaves– WRITEs and transactions to the master

● automatic scale­out● sharding

Page 19: My Sql Proxy

   

Tokenizer● turns a SQL query into a token stream● not a full parser, just a tokenizer for speed 

reasons● understands KEYWORDS, /*comments*/, “strings”, 123 and `literals`

● later we'll add support for SQL modes

Page 20: My Sql Proxy

   

normalizing Queries

1:  { TK_SQL_SELECT, select }2:  { TK_STAR, * }3:  { TK_SQL_FROM, from }4:  { TK_LITERAL, t1 }5:  { TK_SQL_WHERE, where }6:  { TK_LITERAL, id }7:  { TK_EQ, = }8:  { TK_INTEGER, 1 }normalized query: SELECT * FROM `t1` WHERE `id` = ? 

Page 21: My Sql Proxy

   

Libraries● auto­config

– SET GLOBAL ... ● balance

– load balancers● commands

– parse MySQL Command Packets

● parser– extract tablenames

● tokenizer– normalize()– cleanup queries

Page 22: My Sql Proxy

   

Internals – LUA scripting● proxy.* is the namespace● proxy.connection.* is the current 

connection● proxy.backends[...] are the backends● proxy.global.* is the global table● proxy.global.config.* is used for the 

config

Page 23: My Sql Proxy

   

Internals ­ Scope● Each connection has its own script scope● proxy.global.* to share data between 

connections● use local to make variables local to the 

function● use package.seeall() to export functions 

from modules

Page 24: My Sql Proxy

   

Internals ­ Threading● the global scope and threading don't play nice 

by default● http://www.cs.princeton.edu/~diego/professional/luathread/● patches lua to apply mutexes around variable 

access

Page 25: My Sql Proxy

   

Internals – Script Cache● 0.6.0 we reload the script on each connection 

start● adding a script cache with mtime check● lua_pushvalue(L, ­1) does the trick

Page 26: My Sql Proxy

   

Roadmap● to be released 0.6.0

– tokenizer– read­write splitting– Query Statistics

● later– parallel Queries– proxy initiates connections

Page 27: My Sql Proxy

   

LUA ­ Gotchas● only false and nil are !true, 0 is true● to say “not equal” you use ~=● there are no shortcuts

– no a++, no a *= 4, ...– no a > b ? a : b (there is “(a > b) and a or b)