ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro-Mysql/README
(Generate patch)

Comparing Coro-Mysql/README (file contents):
Revision 1.3 by root, Sat Jul 18 05:58:27 2009 UTC vs.
Revision 1.5 by root, Thu Oct 11 03:21:49 2012 UTC

9DESCRIPTION 9DESCRIPTION
10 (Note that in this manual, "thread" refers to real threads as 10 (Note that in this manual, "thread" refers to real threads as
11 implemented by the Coro module, not to the built-in windows process 11 implemented by the Coro module, not to the built-in windows process
12 emulation which unfortunately is also called "threads") 12 emulation which unfortunately is also called "threads")
13 13
14 This module "patches" DBD::mysql database handles so that they do not 14 This module replaces the I/O handlers for a database connection, with
15 block the whole process, but only the thread that they are used in. 15 the effect that "patched" database handles no longer block the all
16 threads of a process, but only the thread that does the request.
16 17
17 This can be used to make parallel sql requests using Coro, or to do 18 This can be used to make parallel sql requests using Coro, or to do
18 other stuff while mysql is rumbling in the background. 19 other stuff while mysql is rumbling in the background.
19 20
20 CAVEAT 21 CAVEAT
21 Note that this module must be linked against exactly the same 22 Note that this module must be linked against exactly the same (shared,
23 possibly not working with all OSes) libmysqlclient library as
22 libmysqlclient library as DBD::mysql, otherwise it will not work. 24 DBD::mysql, otherwise it will not work.
25
26 Also, this module requires a header file that apparently isn't installed
27 everywhere (violite.h), and therefore comes with it's own copy, which
28 might or might not be compatible to the violite.h of your library - when
29 in doubt, make sure all the libmysqlclient header files are installed
30 and delete the violite.h header that comes with this module.
31
32 On the good side, this module does a multitude of checks to ensure that
33 the libray versions match on the binary level, so on incompatibilities
34 you should expect an exception when trying to unblock a handle, rather
35 than data corruption.
23 36
24 Also, while this module makes database handles non-blocking, you still 37 Also, while this module makes database handles non-blocking, you still
25 cannot run multiple requests in parallel on the same database handle. If 38 cannot run multiple requests in parallel on the same database handle. If
26 you want to run multiple queries in parallel, you have to create 39 you want to run multiple queries in parallel, you have to create
27 multiple database connections, one for each thread that runs queries. 40 multiple database connections, one for each thread that runs queries.
28 Not doing so can corrupt your data - use a Coro::Semaphore when in 41 Not doing so can corrupt your data - use a Coro::Semaphore to protetc
29 doubt. 42 access to a shared database handle when in doubt.
30 43
31 If you make sure that you never run two or more requests in parallel, 44 If you make sure that you never run two or more requests in parallel,
32 you can freely share the database handles between threads, of course. 45 you can freely share the database handles between threads, of course.
33 46
34 Also, this module uses a number of "unclean" techniques (patching an
35 internal libmysql structure for one thing) and was hacked within a few
36 hours on a long flight to Malaysia.
37
38 It does, however, check whether it indeed got the structure layout
39 correct, so you should expect perl exceptions or early crashes as
40 opposed to data corruption when something goes wrong during patching.
41
42 SPEED 47 SPEED
43 This module is implemented in XS, and as long as mysqld replies quickly 48 This module is implemented in XS, and as long as mysqld replies quickly
44 enough, it adds no overhead to the standard libmysql communication 49 enough, it adds no overhead to the standard libmysql communication
45 routines (which are very badly written, btw.). 50 routines (which are very badly written, btw.). In fact, since it has a
51 more efficient buffering and allows requests to run in parallel, it
52 often decreases the actual time to run many queries considerably.
46 53
47 For very fast queries ("select 0"), this module can add noticable 54 For very fast queries ("select 0"), this module can add noticable
48 overhead (around 15%) as it tries to switch to other coroutines when 55 overhead (around 15%, 7% when EV can be used) as it tries to switch to
49 mysqld doesn't deliver the data instantly. 56 other coroutines when mysqld doesn't deliver the data immediately,
57 although, again, when running queries in parallel, they will usually
58 execute faster.
50 59
51 For most types of queries, there will be no overhead, especially on 60 For most types of queries, there will be no extra latency, especially on
52 multicore systems where your perl process can do other things while 61 multicore systems where your perl process can do other things while
53 mysqld does its stuff. 62 mysqld does its stuff.
54 63
55 LIMITATIONS 64 LIMITATIONS
56 This module only supports "standard" mysql connection handles - this 65 This module only supports "standard" mysql connection handles - this
57 means unix domain or TCP sockets, and excludes SSL/TLS connections, 66 means unix domain or TCP sockets, and excludes SSL/TLS connections,
58 named pipes (windows) and shared memory (also windows). No support for 67 named pipes (windows) and shared memory (also windows). No support for
59 these connection types is planned, either. 68 these connection types is planned, either.
69
70CANCELLATION
71 Cancelling a thread that is within a mysql query will likely make the
72 handle unusable. As far as Coro::Mysql is concerned, the handle can be
73 safely destroyed, but it's not clear how mysql itself will react to a
74 cancellation.
60 75
61FUNCTIONS 76FUNCTIONS
62 Coro::Mysql offers a single user-accessible function: 77 Coro::Mysql offers a single user-accessible function:
63 78
64 $DBH = Coro::Mysql::unblock $DBH 79 $DBH = Coro::Mysql::unblock $DBH
71 It is safe to call this function on any database handle (or just 86 It is safe to call this function on any database handle (or just
72 about any value), but it will only do anything to DBD::mysql 87 about any value), but it will only do anything to DBD::mysql
73 handles, others are returned unchanged. That means it is harmless 88 handles, others are returned unchanged. That means it is harmless
74 when applied to database handles of other databases. 89 when applied to database handles of other databases.
75 90
91 It is also safe to pass "undef", so code like this is works as
92 expected:
93
94 my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
95 or die $DBI::errstr;
96
76USAGE EXAMPLE 97USAGE EXAMPLE
77 This example uses PApp::SQL and Coro::on_enter to implement a function 98 This example uses PApp::SQL and Coro::on_enter to implement a function
78 "with_db", that connects to a database, uses "unblock" on the resulting 99 "with_db", that connects to a database, uses "unblock" on the resulting
79 handle and then makes sure that $PApp::SQL::DBH is set to the 100 handle and then makes sure that $PApp::SQL::DBH is set to the
80 (per-thread) database handle when the given thread is running (it does 101 (per-thread) database handle when the given thread is running (it does
85 use PApp::SQL; 106 use PApp::SQL;
86 107
87 sub with_db($$$&) { 108 sub with_db($$$&) {
88 my ($database, $user, $pass, $cb) = @_; 109 my ($database, $user, $pass, $cb) = @_;
89 110
90 my $dbh = Coro::Mysql::unblock DBI->connect ($database, $user, $pass) 111 my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
91 or die $DBI::errstr; 112 or die $DBI::errstr;
92 113
93 Coro::on_enter { $PApp::SQL::DBH = $dbh }; 114 Coro::on_enter { $PApp::SQL::DBH = $dbh };
94 115
95 $cb->(); 116 $cb->();
119 } for 1..10; 140 } for 1..10;
120 141
121SEE ALSO 142SEE ALSO
122 Coro, PApp::SQL (a user friendly but efficient wrapper around DBI). 143 Coro, PApp::SQL (a user friendly but efficient wrapper around DBI).
123 144
145HISTORY
146 This module was initially hacked together within a few hours on a long
147 flight to Malaysia, and seems to have worked ever since, with minor
148 adjustments for newer libmysqlclient libraries.
149
124AUTHOR 150AUTHOR
125 Marc Lehmann <schmorp@schmorp.de> 151 Marc Lehmann <schmorp@schmorp.de>
126 http://home.schmorp.de/ 152 http://home.schmorp.de/
127 153

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines