SlideShare ist ein Scribd-Unternehmen logo
1 von 72
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 : ,[object Object]
Failover
Round-Robin, least loaded ,[object Object],[object Object]
Query Analysis
Query Auditing ,[object Object],[object Object]
Throttling
Sharding
What are mysqlnd plugins? ,[object Object],[object Object],[object Object],[object Object]
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: ,[object Object]
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! ,[object Object],[object Object]
Cure applications without touching .php
No extra software, no MySQL Proxy!
Extend, add driver functionality ,[object Object],[object Object]

  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 ! ,[object Object]
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 ,[object Object]
Creative potential: endless
Ease of use: absolutely
Fun factor: tremendous ,[object Object],[object Object]
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! ,[object Object]
mysqlnd_uh_set_statement_proxy()
Class MysqlndUhConnection
Class MysqlndUhStatement

Weitere Àhnliche Inhalte

Was ist angesagt?

Secure Ftp Java Style Rev004
Secure Ftp  Java Style Rev004Secure Ftp  Java Style Rev004
Secure Ftp Java Style Rev004Rich Helton
 
Programming
ProgrammingProgramming
Programmingmafffffe19
 
C#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 FinalC#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 FinalRich Helton
 
Php Presentation
Php PresentationPhp Presentation
Php PresentationManish Bothra
 
Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015iScripts
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 OverviewNicola Pedot
 
Php Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerPhp Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerJackson F. de A. Mafra
 
Google closure compiler
Google closure compilerGoogle closure compiler
Google closure compilerPrasad Kancharla
 
Getting started with Catalyst and extjs
Getting started with Catalyst and extjsGetting started with Catalyst and extjs
Getting started with Catalyst and extjsPeter Edwards
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 

Was ist angesagt? (20)

Java 9 Features
Java 9 FeaturesJava 9 Features
Java 9 Features
 
Secure Ftp Java Style Rev004
Secure Ftp  Java Style Rev004Secure Ftp  Java Style Rev004
Secure Ftp Java Style Rev004
 
Programming
ProgrammingProgramming
Programming
 
C#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 FinalC#Web Sec Oct27 2010 Final
C#Web Sec Oct27 2010 Final
 
php
phpphp
php
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015Advanced PHP Web Development Tools in 2015
Advanced PHP Web Development Tools in 2015
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
 
Php Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant KillerPhp Conference Brazil - Phalcon Giant Killer
Php Conference Brazil - Phalcon Giant Killer
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Google closure compiler
Google closure compilerGoogle closure compiler
Google closure compiler
 
Getting started with Catalyst and extjs
Getting started with Catalyst and extjsGetting started with Catalyst and extjs
Getting started with Catalyst and extjs
 
PHP .ppt
PHP .pptPHP .ppt
PHP .ppt
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Java notes
Java notesJava notes
Java notes
 
Php intro
Php introPhp intro
Php intro
 
Java 3 rd sem. 2012 aug.ASSIGNMENT
Java 3 rd sem. 2012 aug.ASSIGNMENTJava 3 rd sem. 2012 aug.ASSIGNMENT
Java 3 rd sem. 2012 aug.ASSIGNMENT
 
Phpbasics
PhpbasicsPhpbasics
Phpbasics
 
Php ppt
Php pptPhp ppt
Php ppt
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 

Ähnlich wie mysqlnd Plugins - Scale PHP MySQL Apps

The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyThe PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyUlf Wendel
 
The mysqlnd replication and load balancing plugin
The mysqlnd replication and load balancing pluginThe mysqlnd replication and load balancing plugin
The mysqlnd replication and load balancing pluginUlf Wendel
 
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011Ulf Wendel
 
HTTP Plugin for MySQL!
HTTP Plugin for MySQL!HTTP Plugin for MySQL!
HTTP Plugin for MySQL!Ulf Wendel
 
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQLHTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQLUlf Wendel
 
Libmysqld Introduction
Libmysqld IntroductionLibmysqld Introduction
Libmysqld Introductionpapablues
 
Intro to drupal_7_architecture
Intro to drupal_7_architectureIntro to drupal_7_architecture
Intro to drupal_7_architectureHai Vo Hoang
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionjulien pauli
 
PHP Basics
PHP BasicsPHP Basics
PHP BasicsRoohul Amin
 
Lamp Zend Security
Lamp Zend SecurityLamp Zend Security
Lamp Zend SecurityRam Srivastava
 
Vipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentationVipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentationVipul Divyanshu
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it securityCESAR A. RUIZ C
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01Getachew Ganfur
 

Ähnlich wie mysqlnd Plugins - Scale PHP MySQL Apps (20)

The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL ProxyThe PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy
 
The mysqlnd replication and load balancing plugin
The mysqlnd replication and load balancing pluginThe mysqlnd replication and load balancing plugin
The mysqlnd replication and load balancing plugin
 
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
 
Php mysql-tutorial-en
Php mysql-tutorial-enPhp mysql-tutorial-en
Php mysql-tutorial-en
 
Php simple
Php simplePhp simple
Php simple
 
HTTP Plugin for MySQL!
HTTP Plugin for MySQL!HTTP Plugin for MySQL!
HTTP Plugin for MySQL!
 
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQLHTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
HTTP, JSON, JavaScript, Map&Reduce built-in to MySQL
 
Libmysqld Introduction
Libmysqld IntroductionLibmysqld Introduction
Libmysqld Introduction
 
Intro to drupal_7_architecture
Intro to drupal_7_architectureIntro to drupal_7_architecture
Intro to drupal_7_architecture
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Software Development with PHP & Laravel
Software Development  with PHP & LaravelSoftware Development  with PHP & Laravel
Software Development with PHP & Laravel
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Lamp Zend Security
Lamp Zend SecurityLamp Zend Security
Lamp Zend Security
 
Vipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentationVipul divyanshu mahout_documentation
Vipul divyanshu mahout_documentation
 
Lovely
LovelyLovely
Lovely
 
C plus plus for hackers it security
C plus plus for hackers it securityC plus plus for hackers it security
C plus plus for hackers it security
 
C++ for hackers
C++ for hackersC++ for hackers
C++ for hackers
 
His162013 140529214456-phpapp01
His162013 140529214456-phpapp01His162013 140529214456-phpapp01
His162013 140529214456-phpapp01
 
Bettercap
BettercapBettercap
Bettercap
 

Mehr von Ulf Wendel

MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group ReplicationUlf Wendel
 
Data massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesData massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesUlf Wendel
 
MySQL 5.7 clustering: The developer perspective
MySQL 5.7 clustering: The developer perspectiveMySQL 5.7 clustering: The developer perspective
MySQL 5.7 clustering: The developer perspectiveUlf Wendel
 
MySQL 5.7 Fabric: Introduction to High Availability and Sharding
MySQL 5.7 Fabric: Introduction to High Availability and Sharding MySQL 5.7 Fabric: Introduction to High Availability and Sharding
MySQL 5.7 Fabric: Introduction to High Availability and Sharding Ulf Wendel
 
PoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HAPoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HAUlf Wendel
 
DIY: A distributed database cluster, or: MySQL Cluster
DIY: A distributed database cluster, or: MySQL ClusterDIY: A distributed database cluster, or: MySQL Cluster
DIY: A distributed database cluster, or: MySQL ClusterUlf Wendel
 
NoSQL in MySQL
NoSQL in MySQLNoSQL in MySQL
NoSQL in MySQLUlf Wendel
 
Vote NO for MySQL
Vote NO for MySQLVote NO for MySQL
Vote NO for MySQLUlf Wendel
 
PHP mysqlnd connection multiplexing plugin
PHP mysqlnd connection multiplexing pluginPHP mysqlnd connection multiplexing plugin
PHP mysqlnd connection multiplexing pluginUlf Wendel
 
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4Ulf Wendel
 
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
MySQL 5.6 Global Transaction IDs - Use case: (session) consistencyMySQL 5.6 Global Transaction IDs - Use case: (session) consistency
MySQL 5.6 Global Transaction IDs - Use case: (session) consistencyUlf Wendel
 
MySQL 5.6 Global Transaction Identifier - Use case: Failover
MySQL 5.6 Global Transaction Identifier - Use case: FailoverMySQL 5.6 Global Transaction Identifier - Use case: Failover
MySQL 5.6 Global Transaction Identifier - Use case: FailoverUlf Wendel
 
PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011Ulf Wendel
 
Award-winning technology: Oxid loves the query cache
Award-winning technology: Oxid loves the query cacheAward-winning technology: Oxid loves the query cache
Award-winning technology: Oxid loves the query cacheUlf Wendel
 
Mysqlnd query cache plugin benchmark report
Mysqlnd query cache plugin benchmark reportMysqlnd query cache plugin benchmark report
Mysqlnd query cache plugin benchmark reportUlf Wendel
 
mysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlermysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlerUlf Wendel
 
Mysqlnd query cache plugin statistics and tuning
Mysqlnd query cache plugin statistics and tuningMysqlnd query cache plugin statistics and tuning
Mysqlnd query cache plugin statistics and tuningUlf Wendel
 
Built-in query caching for all PHP MySQL extensions/APIs
Built-in query caching for all PHP MySQL extensions/APIsBuilt-in query caching for all PHP MySQL extensions/APIs
Built-in query caching for all PHP MySQL extensions/APIsUlf Wendel
 
Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008Ulf Wendel
 

Mehr von Ulf Wendel (19)

MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
 
Data massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesData massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodes
 
MySQL 5.7 clustering: The developer perspective
MySQL 5.7 clustering: The developer perspectiveMySQL 5.7 clustering: The developer perspective
MySQL 5.7 clustering: The developer perspective
 
MySQL 5.7 Fabric: Introduction to High Availability and Sharding
MySQL 5.7 Fabric: Introduction to High Availability and Sharding MySQL 5.7 Fabric: Introduction to High Availability and Sharding
MySQL 5.7 Fabric: Introduction to High Availability and Sharding
 
PoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HAPoC: Using a Group Communication System to improve MySQL Replication HA
PoC: Using a Group Communication System to improve MySQL Replication HA
 
DIY: A distributed database cluster, or: MySQL Cluster
DIY: A distributed database cluster, or: MySQL ClusterDIY: A distributed database cluster, or: MySQL Cluster
DIY: A distributed database cluster, or: MySQL Cluster
 
NoSQL in MySQL
NoSQL in MySQLNoSQL in MySQL
NoSQL in MySQL
 
Vote NO for MySQL
Vote NO for MySQLVote NO for MySQL
Vote NO for MySQL
 
PHP mysqlnd connection multiplexing plugin
PHP mysqlnd connection multiplexing pluginPHP mysqlnd connection multiplexing plugin
PHP mysqlnd connection multiplexing plugin
 
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
MySQL? Load? Clustering! Balancing! PECL/mysqlnd_ms 1.4
 
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
MySQL 5.6 Global Transaction IDs - Use case: (session) consistencyMySQL 5.6 Global Transaction IDs - Use case: (session) consistency
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
 
MySQL 5.6 Global Transaction Identifier - Use case: Failover
MySQL 5.6 Global Transaction Identifier - Use case: FailoverMySQL 5.6 Global Transaction Identifier - Use case: Failover
MySQL 5.6 Global Transaction Identifier - Use case: Failover
 
PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011
 
Award-winning technology: Oxid loves the query cache
Award-winning technology: Oxid loves the query cacheAward-winning technology: Oxid loves the query cache
Award-winning technology: Oxid loves the query cache
 
Mysqlnd query cache plugin benchmark report
Mysqlnd query cache plugin benchmark reportMysqlnd query cache plugin benchmark report
Mysqlnd query cache plugin benchmark report
 
mysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlermysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handler
 
Mysqlnd query cache plugin statistics and tuning
Mysqlnd query cache plugin statistics and tuningMysqlnd query cache plugin statistics and tuning
Mysqlnd query cache plugin statistics and tuning
 
Built-in query caching for all PHP MySQL extensions/APIs
Built-in query caching for all PHP MySQL extensions/APIsBuilt-in query caching for all PHP MySQL extensions/APIs
Built-in query caching for all PHP MySQL extensions/APIs
 
Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008
 

KĂŒrzlich hochgeladen

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

KĂŒrzlich hochgeladen (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

mysqlnd Plugins - Scale PHP MySQL Apps

  • 1. The power of mysqlnd plugins! Get in! First come, first serve – limited food!
  • 2. Ulf Wendel, MySQL/Sun/Oracle/TellMeWhatIsNext Gardening mysqlnd: growing and harvesting The mysqlnd green house
  • 3. 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.
  • 4. The pub, the idea, the demo
  • 6. 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!
  • 7. 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 ...
  • 8. 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.
  • 9. Replacement for libmysql $mysqli = new mysqli(...); $mysql = mysql_connect(...); $pdo = new PDO(...);
  • 10. 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.
  • 12. 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.
  • 14.
  • 16.
  • 18.
  • 21.
  • 23. 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.
  • 24. Proxy: Single Point of Failure!
  • 25.
  • 26. Horizontal scale out, scale by client
  • 28. Use existing deployment infrastructure
  • 29. Plugin: C API and wire protocol
  • 30. 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.
  • 31. Follow me or leave the room!
  • 32.
  • 33. Cure applications without touching .php
  • 34. No extra software, no MySQL Proxy!
  • 35.
  • 36. 
 plug, hack, pray (*.php)
  • 37. 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.
  • 39. 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.
  • 40. 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; };
  • 41. 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.
  • 42. 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...
  • 43. 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.
  • 44. 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); }
  • 45. 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!
  • 46. 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); } }
  • 47. 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
  • 48.
  • 56. 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.
  • 57. On PHP, the borg and ladies
  • 58. 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!
  • 59.
  • 61. Ease of use: absolutely
  • 62.
  • 63. The C API has not been designed to be exposed to the userspace
  • 64. Continued on page PHP_INT_MAX.
  • 65. 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.
  • 66. The David Sorria Para way... auto_prepend.php : class ConnProxy extends MysqlndUhConnection { public function query($conn, $query) { } } mysqlnd_uh_set_connection_proxy(new ConnProxy());
  • 67. 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.
  • 68. 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); } }
  • 69.
  • 74.
  • 76.
  • 77. Recommended to be cooperative
  • 78. 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!
  • 79. 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); } }
  • 80. 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, ...
  • 81. 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; } }
  • 82. 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...
  • 83. Sugar, sugar Baby! $pdo = new PDO(...); $proxy = new MysqlndUhConnection(); $proxy->getThreadId(mysqlnd_plugin_pdo_to_conn($pdo));
  • 84. 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.
  • 86. 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).
  • 87.
  • 88.
  • 89.
  • 91. 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.
  • 92.
  • 93. 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();
  • 94. 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).
  • 95.
  • 96.
  • 97.
  • 99. 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.
  • 100.
  • 101.
  • 102. 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.
  • 103. Vs. MySQL Server Query Cache
  • 104.
  • 105. Scale out hoizontally by adding more clients
  • 106. Short distance to cache: lower latency
  • 108.
  • 111.
  • 112. 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.
  • 113. S.O.S – cache entry expires
  • 114. 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.
  • 115. Slam defense: serve stale! Refresh
  • 116. 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.
  • 117.
  • 118. THE END Credits: Andrey Hristov, David Sorria Para Contact: ulf.wendel@oracle.com