SlideShare ist ein Scribd-Unternehmen logo
1 von 75
Downloaden Sie, um offline zu lesen
PHP mysqlnd plugins as an alternative to MySQL Proxy Get in, hurry up!  The mysqlnd plugin talk starts!
The mysqlnd green house Gardening mysqlnd – Concepts and Internals Ulf Wendel, MySQL/Sun/Oracle/WhatIsNext
Use the URL not the slides! The slides are a quick copy of the real stuff at: http://blog.ulf-wendel.de/mysqlnd_plugin_ipc2010.html
Target audience PHP developers with  C programming knowledge.  Familarity with PHP  extension writing is benefitial. „ Das MySQL-Treibhaus erweitern ... Level: Experte“, Session description
The speaker says... Relax, it won't get that complicated.  I am trying to sieve out potential mysqlnd plugin implementors from the audience.
Quick poll: are you a man? Raise your hand, if you call yourself a man.  Not a lamer, not a sissy - a real man.  Cowboys, who of you calls himself an  Open Source developer? Please state your name!
The pub, the idea, the demo
The speaker says... No offline demo for Slideshare.  Maybe next time we meet in person! Cu!
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.
Plugin vs. MySQL Proxy (I)
The speaker says... Hardware topology:  MySQL Proxy can either be installed on the PHP application server or be run on a dedicated machine. A mysqlnd plugin always runs on the application server. Running  a proxy on the application machines has two advantages: ,[object Object]
easy to scale out (horizontal scale out, scale by client)
The speaker says... Hardware topology:  MySQL Proxy can either be installed on the PHP application server or be run on a dedicated machine. A mysqlnd plugin always runs on the application server. Running  a proxy on the application machines has two advantages: ,[object Object]
easy to scale out (horizontal scale out, scale by client)
Choose: C API or 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. If the wire protocol changes, which happens very rarely, MySQL Proxy scripts need to be changed as well. 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. There is  no need to go down to the level of the wire protocol. However, you can, if you want.
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]
The speaker says... mysqlnd plugins can be written in C and PHP - as we will see. We need to look at C first. C is the "natural" choice. However, we will use it to carry the mysqlnd plugin functionality into the userspace.
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. This always reminds me of PHP 4 but any comparison would only distract you.
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.
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... 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. Once you have an understanding of the basics, we can talk about edge-cases. In some cases, for example in case of Conn::stmt_init(), it is vital to call the parent method prior to any other activity in the derived method. Details will be given below.
Extending:  properties (concept) OO concept mysqlnd C struct member comment Methods struct object_methods * methods Function table Properties c_type member Parent properties Extended properties void ** plugin_data List of void*. One void* per registered plugin
The speaker says... Basic idea: allow plugins to associate arbitrary data pointer with objects. See below for technical details.
Extending: properties (API) void  minit _register_hooks(TSRMLS_D) { /* obtain unique plugin ID */ my_plugin_id = mysqlnd_plugin_register(); /* snip - see Extending Connection: methods */ } static PROPS** get_conn_properties(const MYSQLND *conn TSRMLS_DC) { PROPS** props; props = (PROPS**)mysqlnd_plugin_get_plugin_connection_data (conn, my_plugin_id); if (!props || !(*props)) { *props =  mnd_pecalloc (1, sizeof(MY_CONN_PROPERTIES), conn->persistent); (*props)->query_counter = 0; }  return props; }
The speaker says... Arbitrary data (properties) can be added to a mysqlnd objects using an appropriate function of the mysqlnd_plugin_get_plugin_<object>_data() family.  When allocating an object mysqlnd reserves space at the end of the object to hold void* to arbitrary data. mysqlnd reserves space for one void* per plugin.  The management of plugin data memory is your task (allocation, resizing, freeing)!   See the below notes on constructors and destructors for hints.  Andrey recommends to use the mysqlnd allocator for plugin data (mnd_*loc()).  This is not a must.
Daddy, it is a 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.
Risks: a (silly) man's world ,[object Object]
Limitations: use your leadfoot
Chaining: alphabetical = random order ,[object Object]
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!  As we saw, plugins can associate data pointer with objects (&quot;properties&quot;). The pointer is not protected from other plugins in any meaningful way. The storage place is not secure. Simple offset arithmetic can be used to read other plugins data.
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.  Nothing stops you from writing a mysqlnd plugin which exposes the mysqlnd plugin API to PHP users - for use in *.php files!
What borg technology can do! class proxy extends mysqlnd_plugin_connection { public function connect($host, ...) { /* security */ $host = '127.0.0.1'; return parent::connect($host); } public function query($query, ...) { error_log($query); return parent::query($query); } } mysqlnd_plugin_set_conn_proxy(new proxy()); (auto_prepend.inc.php) any_php_mysql_app_main(); ( index.php)
The speaker says... Guess what will happen when running the fictive code! The application will be restricted to connect to '127.0.0.1'. Assuming the proposed ext/mysqlnd_plugin is the only active myqlnd plugin,  there is no way for the application to work around this restriction.  Same about the query logging. It works with all PHP applications. And, it does not require any application changes.
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

Weitere ähnliche Inhalte

Was ist angesagt?

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 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
 
HTTP Plugin for MySQL!
HTTP Plugin for MySQL!HTTP Plugin for MySQL!
HTTP Plugin for MySQL!Ulf 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
 
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
 
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
 
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 Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group ReplicationUlf 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
 
Highly Available MySQL/PHP Applications with mysqlnd
Highly Available MySQL/PHP Applications with mysqlndHighly Available MySQL/PHP Applications with mysqlnd
Highly Available MySQL/PHP Applications with mysqlndJervin Real
 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group ReplicationBogdan Kecman
 
Introduction to Galera
Introduction to GaleraIntroduction to Galera
Introduction to GaleraHenrik Ingo
 
MySQL Multi Master Replication
MySQL Multi Master ReplicationMySQL Multi Master Replication
MySQL Multi Master ReplicationMoshe Kaplan
 
Mysql high availability and scalability
Mysql high availability and scalabilityMysql high availability and scalability
Mysql high availability and scalabilityyin gong
 
Using and Benchmarking Galera in different architectures (PLUK 2012)
Using and Benchmarking Galera in different architectures (PLUK 2012)Using and Benchmarking Galera in different architectures (PLUK 2012)
Using and Benchmarking Galera in different architectures (PLUK 2012)Henrik Ingo
 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationPercona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationKenny Gryp
 
Galera cluster for MySQL - Introduction Slides
Galera cluster for MySQL - Introduction SlidesGalera cluster for MySQL - Introduction Slides
Galera cluster for MySQL - Introduction SlidesSeveralnines
 
Scaling with sync_replication using Galera and EC2
Scaling with sync_replication using Galera and EC2Scaling with sync_replication using Galera and EC2
Scaling with sync_replication using Galera and EC2Marco Tusa
 
High Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
High Availability with Galera Cluster - SkySQL Road Show 2013 in BerlinHigh Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
High Availability with Galera Cluster - SkySQL Road Show 2013 in BerlinMariaDB Corporation
 
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
 MySQL PHP native driver  : Advanced Functions / PHP forum Paris 2013   MySQL PHP native driver  : Advanced Functions / PHP forum Paris 2013
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013 Serge Frezefond
 

Was ist angesagt? (20)

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 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
 
HTTP Plugin for MySQL!
HTTP Plugin for MySQL!HTTP Plugin for MySQL!
HTTP Plugin for MySQL!
 
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
 
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
 
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
 
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 Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
 
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
 
Highly Available MySQL/PHP Applications with mysqlnd
Highly Available MySQL/PHP Applications with mysqlndHighly Available MySQL/PHP Applications with mysqlnd
Highly Available MySQL/PHP Applications with mysqlnd
 
MySQL Group Replication
MySQL Group ReplicationMySQL Group Replication
MySQL Group Replication
 
Introduction to Galera
Introduction to GaleraIntroduction to Galera
Introduction to Galera
 
MySQL Multi Master Replication
MySQL Multi Master ReplicationMySQL Multi Master Replication
MySQL Multi Master Replication
 
Mysql high availability and scalability
Mysql high availability and scalabilityMysql high availability and scalability
Mysql high availability and scalability
 
Using and Benchmarking Galera in different architectures (PLUK 2012)
Using and Benchmarking Galera in different architectures (PLUK 2012)Using and Benchmarking Galera in different architectures (PLUK 2012)
Using and Benchmarking Galera in different architectures (PLUK 2012)
 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group ReplicationPercona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
 
Galera cluster for MySQL - Introduction Slides
Galera cluster for MySQL - Introduction SlidesGalera cluster for MySQL - Introduction Slides
Galera cluster for MySQL - Introduction Slides
 
Scaling with sync_replication using Galera and EC2
Scaling with sync_replication using Galera and EC2Scaling with sync_replication using Galera and EC2
Scaling with sync_replication using Galera and EC2
 
High Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
High Availability with Galera Cluster - SkySQL Road Show 2013 in BerlinHigh Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
High Availability with Galera Cluster - SkySQL Road Show 2013 in Berlin
 
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
 MySQL PHP native driver  : Advanced Functions / PHP forum Paris 2013   MySQL PHP native driver  : Advanced Functions / PHP forum Paris 2013
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
 

Ähnlich wie The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy

The power of mysqlnd plugins
The power of mysqlnd pluginsThe power of mysqlnd plugins
The power of mysqlnd pluginsUlf 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
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionjulien pauli
 
Libmysqld Introduction
Libmysqld IntroductionLibmysqld Introduction
Libmysqld Introductionpapablues
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010singingfish
 
Intro to drupal_7_architecture
Intro to drupal_7_architectureIntro to drupal_7_architecture
Intro to drupal_7_architectureHai Vo Hoang
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginnersEnoch Joshua
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Antonio Peric-Mazar
 
Using advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint developmentUsing advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint developmentsadomovalex
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using SwiftDiego Freniche Brito
 
Starting with PHP and Web devepolment
Starting with PHP and Web devepolmentStarting with PHP and Web devepolment
Starting with PHP and Web devepolmentRajib Ahmed
 

Ähnlich wie The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy (20)

The power of mysqlnd plugins
The power of mysqlnd pluginsThe power of mysqlnd plugins
The power of mysqlnd plugins
 
Php mysql-tutorial-en
Php mysql-tutorial-enPhp mysql-tutorial-en
Php mysql-tutorial-en
 
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
 
Php simple
Php simplePhp simple
Php simple
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
 
Libmysqld Introduction
Libmysqld IntroductionLibmysqld Introduction
Libmysqld Introduction
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Bettercap
BettercapBettercap
Bettercap
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
 
Software Development with PHP & Laravel
Software Development  with PHP & LaravelSoftware Development  with PHP & Laravel
Software Development with PHP & Laravel
 
Intro to drupal_7_architecture
Intro to drupal_7_architectureIntro to drupal_7_architecture
Intro to drupal_7_architecture
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Php
PhpPhp
Php
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
 
Using advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint developmentUsing advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint development
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Starting with PHP and Web devepolment
Starting with PHP and Web devepolmentStarting with PHP and Web devepolment
Starting with PHP and Web devepolment
 
Readme
ReadmeReadme
Readme
 

Mehr von Ulf 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
 
PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011Ulf 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
 
Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008Ulf Wendel
 

Mehr von Ulf Wendel (6)

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
 
PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011PHPopstar der PHP Unconference 2011
PHPopstar der PHP Unconference 2011
 
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
 
Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008Mysqlnd Async Ipc2008
Mysqlnd Async Ipc2008
 

Kürzlich hochgeladen

Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
The Kubernetes Gateway API and its role in Cloud Native API Management
The Kubernetes Gateway API and its role in Cloud Native API ManagementThe Kubernetes Gateway API and its role in Cloud Native API Management
The Kubernetes Gateway API and its role in Cloud Native API ManagementNuwan Dias
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 

Kürzlich hochgeladen (20)

Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
The Kubernetes Gateway API and its role in Cloud Native API Management
The Kubernetes Gateway API and its role in Cloud Native API ManagementThe Kubernetes Gateway API and its role in Cloud Native API Management
The Kubernetes Gateway API and its role in Cloud Native API Management
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 

The PHP mysqlnd plugin talk - plugins an alternative to MySQL Proxy

  • 1. PHP mysqlnd plugins as an alternative to MySQL Proxy Get in, hurry up! The mysqlnd plugin talk starts!
  • 2. The mysqlnd green house Gardening mysqlnd – Concepts and Internals Ulf Wendel, MySQL/Sun/Oracle/WhatIsNext
  • 3. Use the URL not the slides! The slides are a quick copy of the real stuff at: http://blog.ulf-wendel.de/mysqlnd_plugin_ipc2010.html
  • 4. Target audience PHP developers with C programming knowledge. Familarity with PHP extension writing is benefitial. „ Das MySQL-Treibhaus erweitern ... Level: Experte“, Session description
  • 5. The speaker says... Relax, it won't get that complicated. I am trying to sieve out potential mysqlnd plugin implementors from the audience.
  • 6. Quick poll: are you a man? Raise your hand, if you call yourself a man. Not a lamer, not a sissy - a real man. Cowboys, who of you calls himself an Open Source developer? Please state your name!
  • 7. The pub, the idea, the demo
  • 8. The speaker says... No offline demo for Slideshare. Maybe next time we meet in person! Cu!
  • 9. 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 ...
  • 10. 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.
  • 11. Replacement for libmysql $mysqli = new mysqli(...); $mysql = mysql_connect(...); $pdo = new PDO(...);
  • 12. 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.
  • 14. 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 &quot;plugins&quot;. Plugins can hook and replace all mysqlnd C API calls.
  • 16.
  • 18.
  • 20.
  • 23.
  • 25. The speaker says... A better understanding of the capabilities of the &quot;mysqlnd plugin concept&quot; 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.
  • 26. Plugin vs. MySQL Proxy (I)
  • 27.
  • 28. easy to scale out (horizontal scale out, scale by client)
  • 29.
  • 30. easy to scale out (horizontal scale out, scale by client)
  • 31. Choose: C API or wire protocol
  • 32. 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. If the wire protocol changes, which happens very rarely, MySQL Proxy scripts need to be changed as well. 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. There is no need to go down to the level of the wire protocol. However, you can, if you want.
  • 33. Follow me or leave the room!
  • 34.
  • 35. Cure applications without touching .php
  • 36. No extra software, no MySQL Proxy!
  • 37.
  • 38. The speaker says... mysqlnd plugins can be written in C and PHP - as we will see. We need to look at C first. C is the &quot;natural&quot; choice. However, we will use it to carry the mysqlnd plugin functionality into the userspace.
  • 40. 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.
  • 41. 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; };
  • 42. 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. This always reminds me of PHP 4 but any comparison would only distract you.
  • 43. 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...
  • 44. 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.
  • 45. 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); }
  • 46. 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!
  • 47. Extending: parent methods MYSQLND_METHOD(my_conn_class, query)(MYSQLND *conn, const char *query, unsigned int query_len TSRMLS_DC) { php_printf(&quot;my_conn_class::query(query = %s)&quot;, query); query = &quot;SELECT 'query rewritten' FROM DUAL&quot;; query_len = strlen(query); return org_methods.query(conn, query, query_len); } }
  • 48. The speaker says... 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. Once you have an understanding of the basics, we can talk about edge-cases. In some cases, for example in case of Conn::stmt_init(), it is vital to call the parent method prior to any other activity in the derived method. Details will be given below.
  • 49. Extending: properties (concept) OO concept mysqlnd C struct member comment Methods struct object_methods * methods Function table Properties c_type member Parent properties Extended properties void ** plugin_data List of void*. One void* per registered plugin
  • 50. The speaker says... Basic idea: allow plugins to associate arbitrary data pointer with objects. See below for technical details.
  • 51. Extending: properties (API) void minit _register_hooks(TSRMLS_D) { /* obtain unique plugin ID */ my_plugin_id = mysqlnd_plugin_register(); /* snip - see Extending Connection: methods */ } static PROPS** get_conn_properties(const MYSQLND *conn TSRMLS_DC) { PROPS** props; props = (PROPS**)mysqlnd_plugin_get_plugin_connection_data (conn, my_plugin_id); if (!props || !(*props)) { *props = mnd_pecalloc (1, sizeof(MY_CONN_PROPERTIES), conn->persistent); (*props)->query_counter = 0; } return props; }
  • 52. The speaker says... Arbitrary data (properties) can be added to a mysqlnd objects using an appropriate function of the mysqlnd_plugin_get_plugin_<object>_data() family. When allocating an object mysqlnd reserves space at the end of the object to hold void* to arbitrary data. mysqlnd reserves space for one void* per plugin. The management of plugin data memory is your task (allocation, resizing, freeing)! See the below notes on constructors and destructors for hints. Andrey recommends to use the mysqlnd allocator for plugin data (mnd_*loc()). This is not a must.
  • 53.
  • 61. 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.
  • 62.
  • 64.
  • 65. Recommended to be cooperative
  • 66. 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! As we saw, plugins can associate data pointer with objects (&quot;properties&quot;). The pointer is not protected from other plugins in any meaningful way. The storage place is not secure. Simple offset arithmetic can be used to read other plugins data.
  • 67. On PHP, the borg and ladies
  • 68. 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. Nothing stops you from writing a mysqlnd plugin which exposes the mysqlnd plugin API to PHP users - for use in *.php files!
  • 69. What borg technology can do! class proxy extends mysqlnd_plugin_connection { public function connect($host, ...) { /* security */ $host = '127.0.0.1'; return parent::connect($host); } public function query($query, ...) { error_log($query); return parent::query($query); } } mysqlnd_plugin_set_conn_proxy(new proxy()); (auto_prepend.inc.php) any_php_mysql_app_main(); ( index.php)
  • 70. The speaker says... Guess what will happen when running the fictive code! The application will be restricted to connect to '127.0.0.1'. Assuming the proposed ext/mysqlnd_plugin is the only active myqlnd plugin, there is no way for the application to work around this restriction. Same about the query logging. It works with all PHP applications. And, it does not require any application changes.
  • 71.
  • 73. Ease of use: absolutely
  • 74.
  • 75. The C API has not been designed to be exposed to the userspace
  • 76. Continued on page PHP_INT_MAX.
  • 77. 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.
  • 78.
  • 80. Striping an existing extension
  • 82. The speaker says... BUY BUY – The Über-Lady book (ULB) – BUY BUY Sara Golemon, Extending and Embedding PHP It will teach you all you need to develop PHP extensions. Without that wonderful book mysqlnd development would have taken twice as long as it took!
  • 83. Repetition: MINIT of a plugin static PHP_ MINIT _FUNCTION(mysqlnd_plugin) { /* globals, ini entries, resources, classes */ /* register mysqlnd plugin */ mysqlnd_plugin_id = mysqlnd_plugin_register(); conn_m = mysqlnd_get_conn_methods(); memcpy(org_conn_m, conn_m, sizeof(struct st_mysqlnd_conn_methods)); conn_m->query = MYSQLND_METHOD(mysqlnd_plugin_conn, query); conn_m->connect = MYSQLND_METHOD(mysqlnd_plugin_conn, connect); } ( my_php_mysqlnd_plugin.c) enum_func_status MYSQLND_METHOD(mysqlnd_plugin_conn, query) (/* ... */) { /* ... */ } (my_mysqlnd_plugin.c)
  • 84. The speaker says... Jippie - ext/mysqlnd_plugin is half way done! There is absolutely nothing new here . The purpose of the slide is to recall basics on mysqlnd plugins. We are putting pieces together to outline an extension as a whole.
  • 85.
  • 86. accept and register proxy object through &quot;mysqlnd_plugin_set_conn_proxy()&quot; (->ULB)
  • 87. call userspace proxy methods from C (-> ULB or - optimization - zend_interfaces.h) class proxy extends mysqlnd_plugin_connection { public function connect($host, ...) { .. } } mysqlnd_plugin_set_conn_proxy(new proxy());
  • 88. The speaker says... References to &quot;ULB&quot; aim to point you to the book of the ueber-lady. Sarah Golemons' excellent book &quot;Extending and Embedding PHP&quot; (ULB) will teach you in depth how to write the code for those tasks. Because the book is truly outstanding - although a bit dated - we will not discuss simple tasks marked with &quot;-> ULB&quot; Userspace object methods can either be called using call_user_function() as desribed in the ULB or you go one little step further down the ladder, closer to the Zend Engine and you hack zend_call_method().
  • 89. Calling userspace MYSQLND_METHOD(my_conn_class,connect) ( MYSQLND *conn, const char *host /* ... */ TSRMLS_DC) { enum_func_status ret = FAIL; zval * global_user_conn_proxy = fetch_userspace_proxy(); if ( global_user_conn_proxy ) { /* call userspace proxy */ ret = MY_ZEND_CALL_METHOD_WRAPPER (global_user_conn_proxy, host, /*...*/); } else { /* or original mysqlnd method = do nothing, be transparent */ ret = org_methods.connect(conn, host, user, passwd, passwd_len, db, db_len, port, socket, mysql_flags TSRMLS_CC); } return ret; } (my_mysqlnd_plugin.c)
  • 90.
  • 92. Simple arguments MYSQLND_METHOD(my_conn_class,connect)(/* ... */, const char *host, /* ...*/) { /* ... */ if (global_user_conn_proxy) { /* ... */ zval* zv_host; MAKE_STD_ZVAL(zv_host); ZVAL_STRING(zv_host, host, 1); MY_ZEND_CALL_METHOD_WRAPPER (global_user_conn_proxy, zv_retval, zv_host /*, ...*/); zval_ptr_dtor(&zv_host); /* ... */ } /* ... */ } (my_mysqlnd_plugin.c)
  • 93. The speaker says... A standard task when calling when linking C and userspace are data type convertions. C variables need to be wrapped into PHP variables. PHP variables are represented through zval structs on the C level. Passing all kinds of numeric and string C values to the userspace is quite easy. The ULB has all details.
  • 94. Structs as arguments MYSQLND_METHOD(my_conn_class, connect)( MYSQLND *conn , /* ...*/) { /* ... */ if (global_user_conn_proxy) { /* ... */ zval* zv_conn; ZEND_REGISTER_RESOURCE (zv_conn, (void *)conn, le_mysqlnd_plugin_conn); MY_ZEND_CALL_METHOD_WRAPPER (global_user_conn_proxy, zv_retval, zv_conn,/*, ...*/); zval_ptr_dtor(&zv_conn); /* ... */ } /* ... */ }
  • 95. The speaker says... The first argument of many mysqlnd methods is a C &quot;object&quot;. For example, the first argument of the connect() method is a pointer to MYSQLND. The struct MYSQLND represents a mysqlnd connection object. The mysqlnd connection object pointer can be compared to a standard I/O file handle. Like a standard I/O file handle a mysqlnd connection object shall be linked to the userspace using the PHP resource variable type.
  • 96. Are we done ?! class proxy extends mysqlnd_plugin_connectio n { public function connect($conn, $host, ...) { /* &quot;pre&quot; hook */ printf(&quot;Connecting to host = '%s'&quot;, $host); return parent::connect($conn); } public function query($conn, $query) { /* &quot;post&quot; hook */ $ret = parent::query($conn, $query); printf(&quot;Query = '%s'&quot;, $query); return $ret; } } mysqlnd_plugin_set_conn_proxy(new proxy());
  • 97. The speaker says... We are almost done. One piece is missing, though. PHP users must be able to call the parent implementation of an overwritten method. To be able to call a parent implementation you need one, right? Let's hack it! BTW, thanks to subclassing you may choose to refine only selected methods and you can choose to have &quot;pre&quot; or &quot;post&quot; hooks. It is all up to you.
  • 98. Buildin class PHP_METHOD(&quot;mysqlnd_plugin_connection&quot;, connect) { /* ... simplified! ... */ zval* mysqlnd_rsrc; MYSQLND* conn; char* host; int host_len; if ( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, &quot;rs&quot;, &mysqlnd_rsrc, &host, &host_len) == FAILURE) { RETURN_NULL(); } ZEND_FETCH_RESOURCE(conn, MYSQLND* conn, &mysqlnd_rsrc, -1, &quot;Mysqlnd Connection&quot;, le_mysqlnd_plugin_conn); if (PASS == org_methods.connect(conn, host, /* simplified! */ TSRMLS_CC) ) RETVAL_TRUE; else RETVAL_FALSE; } (my_mysqlnd_plugin_classes.c)
  • 99. The speaker says... The code should bare no surprise. What you see is standard PHP C infrastructure code to call the original mysqlnd method. As usual, the ULB has all the details for you.
  • 100. The End: implementors wanted! Those who have stated their name at the beginning: Would you mind hacking PECL/mysqlnd_plugin for me? See you at the milk bar!
  • 101. The speaker says... From the IPC Spring 2010 (a PHP conference in Berlin): to my own surprise a couple of companies showed interest in hacking mysqlnd plugins. Most of them want to open-source the development. If you have any question or would like to participate in any potential development, feel free to contact me (ulf.wendel@sun.com) . I'll try to get people in touch. And, of course, we'll try to answer all technical questions.
  • 102. Sugar! class global_proxy extends mysqlnd_plugin_connection { public function query($conn, $query) { printf(&quot;%s(%s)&quot;, __METHOD__, $query); return parent::query($conn, $query); } } class specific_proxy extends mysqlnd_plugin_connection { public function query($conn, $query) { printf(&quot;%s(%s)&quot;, __METHOD__, $query); $query = &quot;SELECT 'mysqlnd is cool'&quot;; return parent::query($conn, $query); } } mysqlnd_plugin_set_conn_proxy(new global_proxy()); $conn1 = new mysqli(...); $conn2 = new PDO(...); mysqlnd_plugin_set_conn_proxy(new specific_proxy(), $conn2); (i_love_mysqlnd.php)
  • 103. The speaker says... Wait - every good movie has a trailer! Mysqlnd allows a plugin, such as the fictive ext/mysqlnd_plugin, to associate arbitrary data with a connection. The data storage can be used to hold a pointer to a connection specific userspace proxy object.
  • 104. Sugar, sugar! class proxy extends mysqlnd_plugin_connection { public function connect($host /*... */) { $conn = @parent::connect($host /*... */); if (!$conn) { my_memcache_proxy_set(&quot;failed_host&quot;, $host); $failover_hosts = my_memcache_proxyget(&quot;failover_hosts&quot;); foreach ($failover_hosts as $host) { $conn = @parent::connect($host /* ... */); if ($conn) { my_memcache_proxy_set(&quot;working_host&quot;, $host); break; } else { my_memcache_proxy_set(&quot;failed_host&quot;, $host); } } } return $conn; } } (client_failover_with_config.php)
  • 105. 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...
  • 106. Sugar, sugar Baby! $pdo = new PDO(...); $proxy = new mysqlnd_plugin_connection(); $proxy->getThreadId(mysqlnd_plugin_pdo_to_conn($pdo)); (i_do_not_love_pdo.php)
  • 107. The speaker says... In our discussion we have looked at the userspace proxy and the default proxy class from of our fictive ext/mysqlnd_plugin 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.
  • 108. THE END Credits: Andrey Hristov, Contact: [email_address]