Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
libAttachSQL 
The Next-Generation C Connector for MySQL 
by Andrew Hutchings (LinuxJedi), HP Advanced Technology Group
About LinuxJedi 
• Worked for Sun/Oracle, MySQL Support Engineer 
• Worked at Rackspace, Drizzle Software Engineer 
• Work...
About HP's Advanced Techonology Group 
• An Open Source think-tank 
• Fosters collaboration throughout HP 
and Open Source...
Other MySQL C Connectors 
• libmysqlclient (GPLv2/proprietary) 
• libdrizzle (3-clause BSD) 
• Maria DB Client (LGPLv2.1 (...
None of these have an official fully non-blocking API!
Introducing libAttachSQL
libAttachSQL 1.0 Features 
• Non-blocking API 
• Apache 2.0 license 
• Very lightweight (1.0.0 shared binary is 501KiB) 
•...
Compatible With 
• Ubuntu 12.04 and up 
• RedHat/CentOS 6.x and up 
• Windows (via. MinGW cross-compile) 
• GCC and CLang
Why not libdrizzle? 
• libdrizzle 5.1 has a blocking API 
• Internal state loop is ugly to deal with 
• Networking code wa...
Good things about libdrizzle 
• Lightweight 
• Nice, simple API 
• Well tested (Jenkins CI) 
• Easy to contribute to 
• Li...
Why libAttachSQL? 
• Modern techniques 
• All of the good points of libdrizzle, none of the legacy 
• Designed from the gr...
Event loops are web scale!
Non-blocking Internals 
• Using libuv - a library born out of Node.js 
• Uses epoll/poll/kqueue in *nix 
• Uses I/O Comple...
Very Open Development 
• GitHub (with issues & pull requests) 
• GitHub Pages + Pelican 
• Travis CI 
• Waffle.io 
• Read ...
Travis Tests 
• GCC 32bit & 64bit 
• CLang 3.0 32bit & 64bit 
• CLang 3.4 64bit 
• No OpenSSL & zlib 
• Valgrind 
• Cppche...
Software Using libAttachSQL 
• Sysbench 
• AttachBench
Basic Query Example
con= attachsql_connect_create("localhost", 3306, "test", "test", "testdb", NULL); 
attachsql_query(con, strlen(query), que...
Things to Note 
• We haven't actually even connected yet! 
• No query/result object
while ((ret != ATTACHSQL_RETURN_EOF) && (error == NULL)) 
{ 
ret= attachsql_connect_poll(con, &error); 
if (ret != ATTACHS...
row= attachsql_query_row_get(con, &error); 
columns= attachsql_query_column_count(con); 
... 
attachsql_query_row_next(con...
Escaped Queries
const char *query= "SELECT * FROM t1 WHERE name = ? AND age > ?"; 
attachsql_query_parameter_st param[2]; 
const char *nam...
Prepared Statements
const char *query= "SELECT * FROM t1 WHERE name = ? AND age > ?"; 
attachsql_statement_prepare(con, strlen(query), query, ...
const char *name= "fred"; 
uint32_t age= 30; 
attachsql_statement_set_string(con, 0, strlen(name), name, NULL); 
attachsql...
ret= attachsql_connect_poll(con, &error); 
if (ret != ATTACHSQL_RETURN_ROW_READY) 
{ 
continue; 
} 
attachsql_statement_ro...
Connection Groups 
• Group many connections into a single event loop 
• Many queries can run simultaneously on a single th...
Connection Groups Example
group= attachsql_group_create(NULL); 
con[0]= attachsql_connect_create("localhost", 3306, "test", "test", "testdb", NULL);...
attachsql_query(con[0], strlen(query1), query1, 0, NULL, &error);
while(done_count < 3) 
{ 
attachsql_group_run(group); 
} 
attachsql_group_destroy(group);
void callbk(attachsql_connect_t *current_con, attachsql_events_t events, 
void *context, attachsql_error_t *error) 
{ 
......
case ATTACHSQL_EVENT_CONNECTED: 
printf("Connected event on con %dn", *con_no); 
break;
case ATTACHSQL_EVENT_ERROR: 
printf("Error exists on con %d: %dn", *con_no, attachsql_error_code(error)); 
attachsql_error...
case ATTACHSQL_EVENT_EOF: 
printf("Connection %d finishedn", *con_no); 
done_count++; 
attachsql_query_close(current_con);...
case ATTACHSQL_EVENT_ROW_READY: 
row= attachsql_query_row_get(current_con, &error); 
... 
attachsql_query_row_next(current...
Error Handling
attachsql_error_t *error= NULL; 
ret= attachsql_connect_poll(con, &error); 
if (error != NULL) 
{ 
printf("Error occurred:...
Project Status 
• 4 months in development (coding started 4th July) 
• 3 alpha releases, 2 beta releases, 1 RC release 
• ...
Future 
• Python wrapper 
• Other language wrappers 
• Server API? 
• Tools built around libAttachSQL
Questions?
Thank you 
libAttachSQL - http://libattachsql.org/ 
Freenode IRC - #libAttachSQL 
GitHub - http://github.com/libattachsql/...
libAttachSQL, The Next-Generation C Connector For MySQL
libAttachSQL, The Next-Generation C Connector For MySQL
libAttachSQL, The Next-Generation C Connector For MySQL
libAttachSQL, The Next-Generation C Connector For MySQL
Upcoming SlideShare
Loading in …5
×

libAttachSQL, The Next-Generation C Connector For MySQL

1,201 views

Published on

Slides for my libAttachSQL talk for Percona Live London 2014

Published in: Software
  • .DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... .DOWNLOAD PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... .DOWNLOAD doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

libAttachSQL, The Next-Generation C Connector For MySQL

  1. 1. libAttachSQL The Next-Generation C Connector for MySQL by Andrew Hutchings (LinuxJedi), HP Advanced Technology Group
  2. 2. About LinuxJedi • Worked for Sun/Oracle, MySQL Support Engineer • Worked at Rackspace, Drizzle Software Engineer • Worked at SkySQL, Senior Sustaining Engineer • Managed two teams, several projects at HP Cloud • Co-Author of MySQL 5.1 Plugins Development
  3. 3. About HP's Advanced Techonology Group • An Open Source think-tank • Fosters collaboration throughout HP and Open Source communities
  4. 4. Other MySQL C Connectors • libmysqlclient (GPLv2/proprietary) • libdrizzle (3-clause BSD) • Maria DB Client (LGPLv2.1 (sort-of))
  5. 5. None of these have an official fully non-blocking API!
  6. 6. Introducing libAttachSQL
  7. 7. libAttachSQL 1.0 Features • Non-blocking API • Apache 2.0 license • Very lightweight (1.0.0 shared binary is 501KiB) • Can use OpenSSL and zlib for encryption and compression • Server side and (sort-of) local prepared statements • Good documentation from the start (http://docs.libattatchsql.org/)
  8. 8. Compatible With • Ubuntu 12.04 and up • RedHat/CentOS 6.x and up • Windows (via. MinGW cross-compile) • GCC and CLang
  9. 9. Why not libdrizzle? • libdrizzle 5.1 has a blocking API • Internal state loop is ugly to deal with • Networking code was hard to work with
  10. 10. Good things about libdrizzle • Lightweight • Nice, simple API • Well tested (Jenkins CI) • Easy to contribute to • Liberal license
  11. 11. Why libAttachSQL? • Modern techniques • All of the good points of libdrizzle, none of the legacy • Designed from the ground-up for use with MySQL
  12. 12. Event loops are web scale!
  13. 13. Non-blocking Internals • Using libuv - a library born out of Node.js • Uses epoll/poll/kqueue in *nix • Uses I/O Completion Ports in Windows
  14. 14. Very Open Development • GitHub (with issues & pull requests) • GitHub Pages + Pelican • Travis CI • Waffle.io • Read The Docs • Coverity Scan • IRC
  15. 15. Travis Tests • GCC 32bit & 64bit • CLang 3.0 32bit & 64bit • CLang 3.4 64bit • No OpenSSL & zlib • Valgrind • Cppcheck • CLang Scan-Build • Documentation (Sphinx nit-pick mode)
  16. 16. Software Using libAttachSQL • Sysbench • AttachBench
  17. 17. Basic Query Example
  18. 18. con= attachsql_connect_create("localhost", 3306, "test", "test", "testdb", NULL); attachsql_query(con, strlen(query), query, 0, NULL, &error);
  19. 19. Things to Note • We haven't actually even connected yet! • No query/result object
  20. 20. while ((ret != ATTACHSQL_RETURN_EOF) && (error == NULL)) { ret= attachsql_connect_poll(con, &error); if (ret != ATTACHSQL_RETURN_ROW_READY) { continue; }
  21. 21. row= attachsql_query_row_get(con, &error); columns= attachsql_query_column_count(con); ... attachsql_query_row_next(con); }
  22. 22. Escaped Queries
  23. 23. const char *query= "SELECT * FROM t1 WHERE name = ? AND age > ?"; attachsql_query_parameter_st param[2]; const char *name= "fred"; uint32_t age= 30; param[0].type= ATTACHSQL_ESCAPE_TYPE_CHAR; param[0].data= (char*)name; param[0].length= strlen(name); param[1].type= ATTACHSQL_ESCAPE_TYPE_INT; param[1].data= &age; param[1].is_unsigned= true; attachsql_query(con, strlen(query), query, 2, param, &error);
  24. 24. Prepared Statements
  25. 25. const char *query= "SELECT * FROM t1 WHERE name = ? AND age > ?"; attachsql_statement_prepare(con, strlen(query), query, &error); while((ret != ATTACHSQL_RETURN_EOF) && (error == NULL)) { ret= attachsql_connect_poll(con, &error); }
  26. 26. const char *name= "fred"; uint32_t age= 30; attachsql_statement_set_string(con, 0, strlen(name), name, NULL); attachsql_statement_set_int(con, 1, age, NULL); attachsql_statement_execute(con, &error); ret= ATTACHSQL_RETURN_NONE; while ((ret != ATTACHSQL_RETURN_EOF) && (error == NULL)) {
  27. 27. ret= attachsql_connect_poll(con, &error); if (ret != ATTACHSQL_RETURN_ROW_READY) { continue; } attachsql_statement_row_get(con, &error); printf("ID: %d, ", attachsql_statement_get_int(con, 0, &error)); size_t len; char *name_data= attachsql_statement_get_char(con, 1, &len, &error); printf("Name: %.*s, ", (int)len, name_data); printf("Age: %dn", attachsql_statement_get_int(con, 2, &error)); attachsql_statement_row_next(con); }
  28. 28. Connection Groups • Group many connections into a single event loop • Many queries can run simultaneously on a single thread • Uses a callback API when there events to process • Can't be used for Prepared Statements (yet)
  29. 29. Connection Groups Example
  30. 30. group= attachsql_group_create(NULL); con[0]= attachsql_connect_create("localhost", 3306, "test", "test", "testdb", NULL); attachsql_group_add_connection(group, con[0], &error); attachsql_connect_set_callback(con[0], callbk, &con_no[0]);
  31. 31. attachsql_query(con[0], strlen(query1), query1, 0, NULL, &error);
  32. 32. while(done_count < 3) { attachsql_group_run(group); } attachsql_group_destroy(group);
  33. 33. void callbk(attachsql_connect_t *current_con, attachsql_events_t events, void *context, attachsql_error_t *error) { ... switch(events) {
  34. 34. case ATTACHSQL_EVENT_CONNECTED: printf("Connected event on con %dn", *con_no); break;
  35. 35. case ATTACHSQL_EVENT_ERROR: printf("Error exists on con %d: %dn", *con_no, attachsql_error_code(error)); attachsql_error_free(error); break;
  36. 36. case ATTACHSQL_EVENT_EOF: printf("Connection %d finishedn", *con_no); done_count++; attachsql_query_close(current_con); break;
  37. 37. case ATTACHSQL_EVENT_ROW_READY: row= attachsql_query_row_get(current_con, &error); ... attachsql_query_row_next(current_con); break;
  38. 38. Error Handling
  39. 39. attachsql_error_t *error= NULL; ret= attachsql_connect_poll(con, &error); if (error != NULL) { printf("Error occurred: %s", attachsql_error_message(error)); attachsql_error_free(error); }
  40. 40. Project Status • 4 months in development (coding started 4th July) • 3 alpha releases, 2 beta releases, 1 RC release • First GA (1.0.0) is out now! • Packages for Ubuntu and RHEL/CentOS
  41. 41. Future • Python wrapper • Other language wrappers • Server API? • Tools built around libAttachSQL
  42. 42. Questions?
  43. 43. Thank you libAttachSQL - http://libattachsql.org/ Freenode IRC - #libAttachSQL GitHub - http://github.com/libattachsql/libattachsql Email andrew@linuxjedi.co.uk Twitter @LinuxJedi or @libAttachSQL

×