1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Coro::Mysql - let other threads run while doing mysql requests |
3 | Coro::Mysql - let other threads run while doing mysql/mariadb requests |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Coro::Mysql; |
7 | use Coro::Mysql; |
8 | |
8 | |
9 | my $DBH = Coro::Mysql::unblock DBI->connect (...); |
9 | my $DBH = Coro::Mysql::unblock DBI->connect (...); |
10 | |
10 | |
11 | =head1 DESCRIPTION |
11 | =head1 DESCRIPTION |
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 |
14 | by the Coro module, not to the built-in windows process emulation which |
14 | by the L<Coro> module, not to the built-in windows process emulation which |
15 | unfortunately is also called "threads") |
15 | unfortunately is also called "threads"). |
16 | |
16 | |
17 | This module replaces the I/O handlers for a database connection, with the |
17 | This module replaces the I/O handlers for a database connection, with the |
18 | effect that "patched" database handles no longer block the all threads of |
18 | effect that "patched" database handles no longer block all threads of a |
19 | a process, but only the thread that does the request. |
19 | process, but only the thread that does the request. It should work for |
|
|
20 | both L<DBD::mysql> and L<DBD::MariaDB> connections and a wide range of |
|
|
21 | mariadb/mysql client libraries. |
20 | |
22 | |
21 | This can be used to make parallel sql requests using Coro, or to do other |
23 | This can be used to make parallel sql requests using Coro, or to do other |
22 | stuff while mysql is rumbling in the background. |
24 | stuff while mariadb is rumbling in the background. |
23 | |
25 | |
24 | =head2 CAVEAT |
26 | =head2 CAVEAT |
25 | |
27 | |
26 | Note that this module must be linked against exactly the same (shared, |
28 | Note that this module must be linked against exactly the same (shared, |
27 | possibly not working with all OSes) F<libmysqlclient> library as |
29 | possibly not working with all OSes) F<libmariadb>/F<libmysqlclient> |
28 | DBD::mysql, otherwise it will not work. |
30 | library as L<DBD::MariaDB>/L<DBD::mysql>, otherwise it will not work. |
29 | |
|
|
30 | Also, this module requires a header file that apparently isn't installed |
|
|
31 | everywhere (F<violite.h>), and therefore comes with it's own copy, which |
|
|
32 | might or might not be compatible to the F<violite.h> of your library - |
|
|
33 | when in doubt, make sure all the libmysqlclient header files are installed |
|
|
34 | and delete the F<violite.h> header that comes with this module. |
|
|
35 | |
|
|
36 | On the good side, this module does a multitude of checks to ensure that |
|
|
37 | the libray versions match on the binary level, so on incompatibilities you |
|
|
38 | should expect an exception when trying to unblock a handle, rather than |
|
|
39 | data corruption. |
|
|
40 | |
31 | |
41 | Also, while this module makes database handles non-blocking, you still |
32 | Also, while this module makes database handles non-blocking, you still |
42 | cannot run multiple requests in parallel on the same database handle. If |
33 | cannot run multiple requests in parallel on the same database handle. If |
43 | you want to run multiple queries in parallel, you have to create multiple |
34 | you want to run multiple queries in parallel, you have to create multiple |
44 | database connections, one for each thread that runs queries. Not doing |
35 | database connections, one for each thread that runs queries. Not doing |
… | |
… | |
79 | safely destroyed, but it's not clear how mysql itself will react to a |
70 | safely destroyed, but it's not clear how mysql itself will react to a |
80 | cancellation. |
71 | cancellation. |
81 | |
72 | |
82 | =head1 FUNCTIONS |
73 | =head1 FUNCTIONS |
83 | |
74 | |
84 | Coro::Mysql offers a single user-accessible function: |
75 | Coro::Mysql offers these functions, the only one that oyu usually need is C<unblock>: |
85 | |
76 | |
86 | =over 4 |
77 | =over 4 |
87 | |
78 | |
88 | =cut |
79 | =cut |
89 | |
80 | |
… | |
… | |
105 | |
96 | |
106 | sub readable { &Coro::Handle::FH::readable } |
97 | sub readable { &Coro::Handle::FH::readable } |
107 | sub writable { &Coro::Handle::FH::writable } |
98 | sub writable { &Coro::Handle::FH::writable } |
108 | |
99 | |
109 | BEGIN { |
100 | BEGIN { |
110 | our $VERSION = '1.2'; |
101 | our $VERSION = '2.1'; |
111 | |
102 | |
112 | require XSLoader; |
103 | require XSLoader; |
113 | XSLoader::load Coro::Mysql::, $VERSION; |
104 | XSLoader::load Coro::Mysql::, $VERSION; |
114 | } |
105 | } |
115 | |
106 | |
… | |
… | |
134 | =cut |
125 | =cut |
135 | |
126 | |
136 | sub unblock { |
127 | sub unblock { |
137 | my ($DBH) = @_; |
128 | my ($DBH) = @_; |
138 | |
129 | |
|
|
130 | if ($DBH) { |
|
|
131 | my $mariadb = $DBH->{Driver}{Name} eq "MariaDB"; |
139 | if ($DBH && $DBH->{Driver}{Name} eq "mysql") { |
132 | if ($mariadb or $DBH->{Driver}{Name} eq "mysql") { |
140 | my $sock = $DBH->{sock}; |
133 | my $sock = $mariadb ? $DBH->{mariadb_sock} : $DBH->{sock}; |
|
|
134 | my $sockfd = $mariadb ? $DBH->mariadb_sockfd : $DBH->{sockfd}; |
|
|
135 | my $cvers = $mariadb ? $DBH->{mariadb_clientversion} : $DBH->{mysql_clientversion}; |
141 | |
136 | |
142 | open my $fh, "+>&" . $DBH->{sockfd} |
137 | open my $fh, "+>&$sockfd" |
143 | or croak "Coro::Mysql unable to clone mysql fd"; |
138 | or croak "Coro::Mysql unable to dup mariadb/mysql fd"; |
144 | |
139 | |
145 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
140 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
146 | require Coro::Handle; |
141 | require Coro::Handle; |
147 | $fh = Coro::Handle::unblock ($fh); |
142 | $fh = Coro::Handle::unblock ($fh); |
|
|
143 | } |
|
|
144 | |
|
|
145 | _patch $sock, $sockfd, $cvers, $fh, tied *$$fh; |
148 | } |
146 | } |
149 | |
|
|
150 | _patch $sock, $DBH->{sockfd}, $DBH->{mysql_clientversion}, $fh, tied ${$fh}; |
|
|
151 | } |
147 | } |
152 | |
148 | |
153 | $DBH |
149 | $DBH |
154 | } |
150 | } |
|
|
151 | |
|
|
152 | =item $bool = Coro::Mysql::is_unblocked $DBH |
|
|
153 | |
|
|
154 | Returns true iff the database handle was successfully patched for |
|
|
155 | non-blocking operations. |
|
|
156 | |
|
|
157 | =cut |
|
|
158 | |
|
|
159 | sub is_unblocked { |
|
|
160 | my ($DBH) = @_; |
|
|
161 | |
|
|
162 | if ($DBH) { |
|
|
163 | my $mariadb = $DBH->{Driver}{Name} eq "MariaDB"; |
|
|
164 | if ($mariadb or $DBH->{Driver}{Name} eq "mysql") { |
|
|
165 | my $sock = $mariadb ? $DBH->{mariadb_sock} : $DBH->{sock}; |
|
|
166 | return _is_patched $sock |
|
|
167 | } |
|
|
168 | } |
|
|
169 | |
|
|
170 | 0 |
|
|
171 | } |
|
|
172 | |
|
|
173 | =item $bool = Coro::Mysql::have_ev |
|
|
174 | |
|
|
175 | Returns true if this Coro::Mysql installation is compiled with special |
|
|
176 | support for L<EV> or not. |
|
|
177 | |
|
|
178 | Even if compiled in, it will only be used if L<EV> is actually the |
|
|
179 | AnyEvent event backend. |
|
|
180 | |
|
|
181 | =cut |
155 | |
182 | |
156 | 1; |
183 | 1; |
157 | |
184 | |
158 | =back |
185 | =back |
159 | |
186 | |
… | |
… | |
176 | or die $DBI::errstr; |
203 | or die $DBI::errstr; |
177 | |
204 | |
178 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
205 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
179 | |
206 | |
180 | $cb->(); |
207 | $cb->(); |
181 | } |
208 | } |
182 | |
209 | |
183 | This function makes it possible to easily use L<PApp::SQL> with |
210 | This function makes it possible to easily use L<PApp::SQL> with |
184 | L<Coro::Mysql>, without worrying about database handles. |
211 | L<Coro::Mysql>, without worrying about database handles. |
185 | |
212 | |
186 | # now start 10 threads doing stuff |
213 | # now start 10 threads doing stuff |
… | |
… | |
211 | |
238 | |
212 | This module was initially hacked together within a few hours on a long |
239 | This module was initially hacked together within a few hours on a long |
213 | flight to Malaysia, and seems to have worked ever since, with minor |
240 | flight to Malaysia, and seems to have worked ever since, with minor |
214 | adjustments for newer libmysqlclient libraries. |
241 | adjustments for newer libmysqlclient libraries. |
215 | |
242 | |
|
|
243 | Well, at least until mariadb introduced the new Pluggable Virtual IO API |
|
|
244 | in mariadb 10.3, which changed and broke everything. On the positive |
|
|
245 | side, the old system was horrible to use, as many GNU/Linux distributions |
|
|
246 | forgot to include the required heaqder files and there were frequent small |
|
|
247 | changes, while the new PVIO system seems to be "official" and hopefully |
|
|
248 | better supported. |
|
|
249 | |
216 | =head1 AUTHOR |
250 | =head1 AUTHOR |
217 | |
251 | |
218 | Marc Lehmann <schmorp@schmorp.de> |
252 | Marc Lehmann <schmorp@schmorp.de> |
219 | http://home.schmorp.de/ |
253 | http://home.schmorp.de/ |
220 | |
254 | |