… | |
… | |
20 | This can be used to make parallel sql requests using Coro, or to do other |
20 | This can be used to make parallel sql requests using Coro, or to do other |
21 | stuff while mysql is rumbling in the background. |
21 | stuff while mysql is rumbling in the background. |
22 | |
22 | |
23 | =head2 CAVEAT |
23 | =head2 CAVEAT |
24 | |
24 | |
25 | Note that this module must be linked against exactly the same |
25 | Note that this module must be linked against exactly the same (shared, |
26 | F<libmysqlclient> library as DBD::mysql, otherwise it will not work. |
26 | possibly not working with all OSes) F<libmysqlclient> library as |
|
|
27 | DBD::mysql, otherwise it will not work. |
27 | |
28 | |
28 | Also, while this module makes database handles non-blocking, you still |
29 | Also, while this module makes database handles non-blocking, you still |
29 | cannot run multiple requests in parallel on the same database handle. If |
30 | cannot run multiple requests in parallel on the same database handle. If |
30 | you want to run multiple queries in parallel, you have to create multiple |
31 | you want to run multiple queries in parallel, you have to create multiple |
31 | database connections, one for each thread that runs queries. |
32 | database connections, one for each thread that runs queries. Not doing so |
|
|
33 | can corrupt your data - use a Coro::Semaphore when in doubt. |
32 | |
34 | |
33 | If you make sure that you never run two or more requests in parallel, you |
35 | If you make sure that you never run two or more requests in parallel, you |
34 | cna freely share the database handles between threads, of course. |
36 | can freely share the database handles between threads, of course. |
35 | |
37 | |
36 | Also, this module uses a number of "unclean" techniques (patching an |
38 | Also, this module uses a number of "unclean" techniques (patching an |
37 | internal libmysql structure for one thing) and was hacked within a few |
39 | internal libmysql structure for one thing) and was initially hacked within |
38 | hours on a long flight to Malaysia. |
40 | a few hours on a long flight to Malaysia. |
39 | |
41 | |
40 | It does, however, check whether it indeed got the structure layout |
42 | It does, however, check whether it indeed got the structure layout |
41 | correct, so you should expect perl exceptions or early crashes as opposed |
43 | correct, so you should expect perl exceptions or early crashes as opposed |
42 | to data corruption when something goes wrong. |
44 | to data corruption when something goes wrong during patching. |
43 | |
45 | |
44 | =head2 SPEED |
46 | =head2 SPEED |
45 | |
47 | |
46 | 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 |
47 | enough, it adds no overhead to the standard libmysql communication |
49 | enough, it adds no overhead to the standard libmysql communication |
48 | routines (which are very badly written). |
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 often |
|
|
52 | decreases the actual time to run many queries considerably. |
49 | |
53 | |
50 | For very fast queries ("select 0"), this module can add noticable overhead |
54 | For very fast queries ("select 0"), this module can add noticable overhead |
51 | (around 15%) as it tries to switch to other coroutines when mysqld doesn't |
55 | (around 15%, 7% when EV can be used) as it tries to switch to other |
52 | deliver the data instantly. |
56 | coroutines when mysqld doesn't deliver the data immediately, although, |
|
|
57 | again, when running queries in parallel, they will usually execute faster. |
53 | |
58 | |
54 | For most types of queries, there will be no overhead, especially on |
59 | For most types of queries, there will be no extra latency, especially on |
55 | multicore systems where your perl process can do other things while mysqld |
60 | multicore systems where your perl process can do other things while mysqld |
56 | does its stuff. |
61 | does its stuff. |
57 | |
62 | |
|
|
63 | =head2 LIMITATIONS |
|
|
64 | |
|
|
65 | This module only supports "standard" mysql connection handles - this |
|
|
66 | means unix domain or TCP sockets, and excludes SSL/TLS connections, named |
|
|
67 | pipes (windows) and shared memory (also windows). No support for these |
|
|
68 | connection types is planned, either. |
|
|
69 | |
|
|
70 | =head1 CANCELLATION |
|
|
71 | |
|
|
72 | Cancelling a thread that is within a mysql query will likely make the |
|
|
73 | handle unusable. As far as Coro::Mysql is concerned, the handle can be |
|
|
74 | safely destroyed, but it's not clear how mysql itself will react to a |
|
|
75 | cancellation. |
|
|
76 | |
|
|
77 | =head1 FUNCTIONS |
|
|
78 | |
|
|
79 | Coro::Mysql offers a single user-accessible function: |
|
|
80 | |
58 | =over 4 |
81 | =over 4 |
59 | |
82 | |
60 | =cut |
83 | =cut |
61 | |
84 | |
62 | package Coro::Mysql; |
85 | package Coro::Mysql; |
… | |
… | |
66 | |
89 | |
67 | use Scalar::Util (); |
90 | use Scalar::Util (); |
68 | use Carp qw(croak); |
91 | use Carp qw(croak); |
69 | |
92 | |
70 | use Guard; |
93 | use Guard; |
|
|
94 | use AnyEvent (); |
71 | use Coro::Handle (); |
95 | use Coro (); |
|
|
96 | use Coro::AnyEvent (); # not necessary with newer Coro versions |
72 | |
97 | |
73 | # we need this extra indirection, as Coro doesn't support |
98 | # we need this extra indirection, as Coro doesn't support |
74 | # calling SLF-like functions via call_sv. |
99 | # calling SLF-like functions via call_sv. |
75 | |
100 | |
76 | sub readable { &Coro::Handle::FH::readable } |
101 | sub readable { &Coro::Handle::FH::readable } |
77 | sub writable { &Coro::Handle::FH::writable } |
102 | sub writable { &Coro::Handle::FH::writable } |
78 | |
103 | |
79 | BEGIN { |
104 | BEGIN { |
80 | our $VERSION = '0.2'; |
105 | our $VERSION = '1.2'; |
81 | |
106 | |
82 | require XSLoader; |
107 | require XSLoader; |
83 | XSLoader::load Coro::Mysql::, $VERSION; |
108 | XSLoader::load Coro::Mysql::, $VERSION; |
84 | } |
109 | } |
85 | |
110 | |
… | |
… | |
89 | so it becomes compatible to Coro threads. |
114 | so it becomes compatible to Coro threads. |
90 | |
115 | |
91 | After that, it returns the patched handle - you should always use the |
116 | After that, it returns the patched handle - you should always use the |
92 | newly returned database handle. |
117 | newly returned database handle. |
93 | |
118 | |
94 | It is safe to call this function on any database handle, but it will only |
119 | It is safe to call this function on any database handle (or just about any |
95 | do anything to L<DBD::mysql> handles, others are returned unchanged. |
120 | value), but it will only do anything to L<DBD::mysql> handles, others are |
|
|
121 | returned unchanged. That means it is harmless when applied to database |
|
|
122 | handles of other databases. |
|
|
123 | |
|
|
124 | It is also safe to pass C<undef>, so code like this is works as expected: |
|
|
125 | |
|
|
126 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
|
|
127 | or die $DBI::errstr; |
96 | |
128 | |
97 | =cut |
129 | =cut |
98 | |
130 | |
99 | sub unblock { |
131 | sub unblock { |
100 | my ($DBH) = @_; |
132 | my ($DBH) = @_; |
101 | |
133 | |
102 | if ($DBH->{Driver}{Name} eq "mysql") { |
134 | if ($DBH && $DBH->{Driver}{Name} eq "mysql") { |
103 | my $sock = $DBH->{sock}; |
135 | my $sock = $DBH->{sock}; |
104 | |
136 | |
105 | open my $fh, "+>&" . $DBH->{sockfd} |
137 | open my $fh, "+>&" . $DBH->{sockfd} |
106 | or croak "Coro::Mysql unable to clone mysql fd"; |
138 | or croak "Coro::Mysql unable to clone mysql fd"; |
107 | |
139 | |
|
|
140 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
|
|
141 | require Coro::Handle; |
108 | $fh = Coro::Handle::unblock $fh; |
142 | $fh = Coro::Handle::unblock ($fh); |
109 | |
|
|
110 | _patch $sock, $DBH->{sockfd}, tied ${$fh}; |
|
|
111 | $DBH->{private_Coro_Mysql} = guard { |
|
|
112 | _unpatch $sock; |
|
|
113 | undef $fh; |
|
|
114 | }; |
143 | } |
|
|
144 | |
|
|
145 | _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied ${$fh}; |
115 | } |
146 | } |
116 | |
147 | |
117 | $DBH |
148 | $DBH |
118 | } |
149 | } |
119 | |
150 | |
120 | 1; |
151 | 1; |
121 | |
152 | |
122 | =back |
153 | =back |
123 | |
154 | |
|
|
155 | =head1 USAGE EXAMPLE |
|
|
156 | |
|
|
157 | This example uses L<PApp::SQL> and L<Coro::on_enter> to implement a |
|
|
158 | function C<with_db>, that connects to a database, uses C<unblock> on the |
|
|
159 | resulting handle and then makes sure that C<$PApp::SQL::DBH> is set to the |
|
|
160 | (per-thread) database handle when the given thread is running (it does not |
|
|
161 | restore any previous value of $PApp::SQL::DBH, however): |
|
|
162 | |
|
|
163 | use Coro; |
|
|
164 | use Coro::Mysql; |
|
|
165 | use PApp::SQL; |
|
|
166 | |
|
|
167 | sub with_db($$$&) { |
|
|
168 | my ($database, $user, $pass, $cb) = @_; |
|
|
169 | |
|
|
170 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
|
|
171 | or die $DBI::errstr; |
|
|
172 | |
|
|
173 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
|
|
174 | |
|
|
175 | $cb->(); |
|
|
176 | } |
|
|
177 | |
|
|
178 | This function makes it possible to easily use L<PApp::SQL> with |
|
|
179 | L<Coro::Mysql>, without worrying about database handles. |
|
|
180 | |
|
|
181 | # now start 10 threads doing stuff |
|
|
182 | async { |
|
|
183 | |
|
|
184 | with_db "DBI:mysql:test", "", "", sub { |
|
|
185 | sql_exec "update table set col = 5 where id = 7"; |
|
|
186 | |
|
|
187 | my $st = sql_exec \my ($id, $name), |
|
|
188 | "select id, name from table where name like ?", |
|
|
189 | "a%"; |
|
|
190 | |
|
|
191 | while ($st->fetch) { |
|
|
192 | ... |
|
|
193 | } |
|
|
194 | |
|
|
195 | my $id = sql_insertid sql_exec "insert into table values (1,2,3)"; |
|
|
196 | # etc. |
|
|
197 | }; |
|
|
198 | |
|
|
199 | } for 1..10; |
|
|
200 | |
|
|
201 | =head1 SEE ALSO |
|
|
202 | |
|
|
203 | L<Coro>, L<PApp::SQL> (a user friendly but efficient wrapper around DBI). |
|
|
204 | |
124 | =head1 AUTHOR |
205 | =head1 AUTHOR |
125 | |
206 | |
126 | Marc Lehmann <schmorp@schmorp.de> |
207 | Marc Lehmann <schmorp@schmorp.de> |
127 | http://home.schmorp.de/ |
208 | http://home.schmorp.de/ |
128 | |
209 | |