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

Comparing Coro-Mysql/Mysql.pm (file contents):
Revision 1.6 by root, Wed Sep 1 16:38:07 2010 UTC vs.
Revision 1.16 by root, Sat Aug 3 02:11:27 2013 UTC

12 12
13(Note that in this manual, "thread" refers to real threads as implemented 13(Note that in this manual, "thread" refers to real threads as implemented
14by the Coro module, not to the built-in windows process emulation which 14by the Coro module, not to the built-in windows process emulation which
15unfortunately is also called "threads") 15unfortunately is also called "threads")
16 16
17This module "patches" DBD::mysql database handles so that they do not 17This module replaces the I/O handlers for a database connection, with the
18block the whole process, but only the thread that they are used in. 18effect that "patched" database handles no longer block the all threads of
19a process, but only the thread that does the request.
19 20
20This can be used to make parallel sql requests using Coro, or to do other 21This can be used to make parallel sql requests using Coro, or to do other
21stuff while mysql is rumbling in the background. 22stuff while mysql is rumbling in the background.
22 23
23=head2 CAVEAT 24=head2 CAVEAT
24 25
25Note that this module must be linked against exactly the same 26Note that this module must be linked against exactly the same (shared,
26F<libmysqlclient> library as DBD::mysql, otherwise it will not work. 27possibly not working with all OSes) F<libmysqlclient> library as
28DBD::mysql, otherwise it will not work.
29
30Also, this module requires a header file that apparently isn't installed
31everywhere (F<violite.h>), and therefore comes with it's own copy, which
32might or might not be compatible to the F<violite.h> of your library -
33when in doubt, make sure all the libmysqlclient header files are installed
34and delete the F<violite.h> header that comes with this module.
35
36On the good side, this module does a multitude of checks to ensure that
37the libray versions match on the binary level, so on incompatibilities you
38should expect an exception when trying to unblock a handle, rather than
39data corruption.
27 40
28Also, while this module makes database handles non-blocking, you still 41Also, while this module makes database handles non-blocking, you still
29cannot run multiple requests in parallel on the same database handle. If 42cannot run multiple requests in parallel on the same database handle. If
30you want to run multiple queries in parallel, you have to create multiple 43you want to run multiple queries in parallel, you have to create multiple
31database connections, one for each thread that runs queries. Not doing so 44database connections, one for each thread that runs queries. Not doing
32can corrupt your data - use a Coro::Semaphore when in doubt. 45so can corrupt your data - use a Coro::Semaphore to protetc access to a
46shared database handle when in doubt.
33 47
34If you make sure that you never run two or more requests in parallel, you 48If you make sure that you never run two or more requests in parallel, you
35can freely share the database handles between threads, of course. 49can freely share the database handles between threads, of course.
36 50
37Also, this module uses a number of "unclean" techniques (patching an
38internal libmysql structure for one thing) and was hacked within a few
39hours on a long flight to Malaysia.
40
41It does, however, check whether it indeed got the structure layout
42correct, so you should expect perl exceptions or early crashes as opposed
43to data corruption when something goes wrong during patching.
44
45=head2 SPEED 51=head2 SPEED
46 52
47This module is implemented in XS, and as long as mysqld replies quickly 53This module is implemented in XS, and as long as mysqld replies quickly
48enough, it adds no overhead to the standard libmysql communication 54enough, it adds no overhead to the standard libmysql communication
49routines (which are very badly written, btw.). 55routines (which are very badly written, btw.). In fact, since it has a
56more efficient buffering and allows requests to run in parallel, it often
57decreases the actual time to run many queries considerably.
50 58
51For very fast queries ("select 0"), this module can add noticable overhead 59For very fast queries ("select 0"), this module can add noticable overhead
52(around 15%) as it tries to switch to other coroutines when mysqld doesn't 60(around 15%, 7% when EV can be used) as it tries to switch to other
53deliver the data instantly. 61coroutines when mysqld doesn't deliver the data immediately, although,
62again, when running queries in parallel, they will usually execute faster.
54 63
55For most types of queries, there will be no overhead, especially on 64For most types of queries, there will be no extra latency, especially on
56multicore systems where your perl process can do other things while mysqld 65multicore systems where your perl process can do other things while mysqld
57does its stuff. 66does its stuff.
58 67
59=head2 LIMITATIONS 68=head2 LIMITATIONS
60 69
61This module only supports "standard" mysql connection handles - this 70This module only supports "standard" mysql connection handles - this
62means unix domain or TCP sockets, and excludes SSL/TLS connections, named 71means unix domain or TCP sockets, and excludes SSL/TLS connections, named
63pipes (windows) and shared memory (also windows). No support for these 72pipes (windows) and shared memory (also windows). No support for these
64connection types is planned, either. 73connection types is planned, either.
65 74
75=head1 CANCELLATION
76
77Cancelling a thread that is within a mysql query will likely make the
78handle unusable. As far as Coro::Mysql is concerned, the handle can be
79safely destroyed, but it's not clear how mysql itself will react to a
80cancellation.
81
66=head1 FUNCTIONS 82=head1 FUNCTIONS
67 83
68Coro::Mysql offers a single user-accessible function: 84Coro::Mysql offers a single user-accessible function:
69 85
70=over 4 86=over 4
78 94
79use Scalar::Util (); 95use Scalar::Util ();
80use Carp qw(croak); 96use Carp qw(croak);
81 97
82use Guard; 98use Guard;
99use AnyEvent ();
83use Coro::Handle (); 100use Coro ();
101use Coro::AnyEvent (); # not necessary with newer Coro versions
84 102
85# we need this extra indirection, as Coro doesn't support 103# we need this extra indirection, as Coro doesn't support
86# calling SLF-like functions via call_sv. 104# calling SLF-like functions via call_sv.
87 105
88sub readable { &Coro::Handle::FH::readable } 106sub readable { &Coro::Handle::FH::readable }
89sub writable { &Coro::Handle::FH::writable } 107sub writable { &Coro::Handle::FH::writable }
90 108
91BEGIN { 109BEGIN {
92 our $VERSION = '1.02'; 110 our $VERSION = '1.22';
93 111
94 require XSLoader; 112 require XSLoader;
95 XSLoader::load Coro::Mysql::, $VERSION; 113 XSLoader::load Coro::Mysql::, $VERSION;
96} 114}
97 115
106It is safe to call this function on any database handle (or just about any 124It is safe to call this function on any database handle (or just about any
107value), but it will only do anything to L<DBD::mysql> handles, others are 125value), but it will only do anything to L<DBD::mysql> handles, others are
108returned unchanged. That means it is harmless when applied to database 126returned unchanged. That means it is harmless when applied to database
109handles of other databases. 127handles of other databases.
110 128
129It is also safe to pass C<undef>, so code like this is works as expected:
130
131 my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
132 or die $DBI::errstr;
133
111=cut 134=cut
112 135
113sub unblock { 136sub unblock {
114 my ($DBH) = @_; 137 my ($DBH) = @_;
115 138
116 if ($DBH->{Driver}{Name} eq "mysql") { 139 if ($DBH && $DBH->{Driver}{Name} eq "mysql") {
117 my $sock = $DBH->{sock}; 140 my $sock = $DBH->{sock};
118 141
119 open my $fh, "+>&" . $DBH->{sockfd} 142 open my $fh, "+>&" . $DBH->{sockfd}
120 or croak "Coro::Mysql unable to clone mysql fd"; 143 or croak "Coro::Mysql unable to clone mysql fd";
121 144
145 if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) {
146 require Coro::Handle;
122 $fh = Coro::Handle::unblock $fh; 147 $fh = Coro::Handle::unblock ($fh);
148 }
123 149
124 _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; 150 _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied *$$fh;
125 } 151 }
126 152
127 $DBH 153 $DBH
128} 154}
129 155
144 use PApp::SQL; 170 use PApp::SQL;
145 171
146 sub with_db($$$&) { 172 sub with_db($$$&) {
147 my ($database, $user, $pass, $cb) = @_; 173 my ($database, $user, $pass, $cb) = @_;
148 174
149 my $dbh = Coro::Mysql::unblock DBI->connect ($database, $user, $pass) 175 my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock
150 or die $DBI::errstr; 176 or die $DBI::errstr;
151 177
152 Coro::on_enter { $PApp::SQL::DBH = $dbh }; 178 Coro::on_enter { $PApp::SQL::DBH = $dbh };
153 179
154 $cb->(); 180 $cb->();
179 205
180=head1 SEE ALSO 206=head1 SEE ALSO
181 207
182L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI). 208L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI).
183 209
210=head1 HISTORY
211
212This module was initially hacked together within a few hours on a long
213flight to Malaysia, and seems to have worked ever since, with minor
214adjustments for newer libmysqlclient libraries.
215
184=head1 AUTHOR 216=head1 AUTHOR
185 217
186 Marc Lehmann <schmorp@schmorp.de> 218 Marc Lehmann <schmorp@schmorp.de>
187 http://home.schmorp.de/ 219 http://home.schmorp.de/
188 220

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines