… | |
… | |
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. Not doing so |
32 | database connections, one for each thread that runs queries. Not doing so |
… | |
… | |
33 | |
34 | |
34 | 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 |
35 | can freely share the database handles between threads, of course. |
36 | can freely share the database handles between threads, of course. |
36 | |
37 | |
37 | Also, this module uses a number of "unclean" techniques (patching an |
38 | Also, this module uses a number of "unclean" techniques (patching an |
38 | internal libmysql structure for one thing) and was hacked within a few |
39 | internal libmysql structure for one thing) and was initially hacked within |
39 | hours on a long flight to Malaysia. |
40 | a few hours on a long flight to Malaysia. |
40 | |
41 | |
41 | It does, however, check whether it indeed got the structure layout |
42 | It does, however, check whether it indeed got the structure layout |
42 | 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 |
43 | to data corruption when something goes wrong during patching. |
44 | to data corruption when something goes wrong during patching. |
44 | |
45 | |
45 | =head2 SPEED |
46 | =head2 SPEED |
46 | |
47 | |
47 | 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 |
48 | enough, it adds no overhead to the standard libmysql communication |
49 | enough, it adds no overhead to the standard libmysql communication |
49 | 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 often |
|
|
52 | decreases the actual time to run many queries considerably. |
50 | |
53 | |
51 | 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 |
52 | (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 |
53 | 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. |
54 | |
58 | |
55 | 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 |
56 | 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 |
57 | does its stuff. |
61 | does its stuff. |
58 | |
62 | |
59 | =head2 LIMITATIONS |
63 | =head2 LIMITATIONS |
60 | |
64 | |
61 | This module only supports "standard" mysql connection handles - this |
65 | This module only supports "standard" mysql connection handles - this |
62 | means unix domain or TCP sockets, and excludes SSL/TLS connections, named |
66 | means unix domain or TCP sockets, and excludes SSL/TLS connections, named |
63 | pipes (windows) and shared memory (also windows). No support for these |
67 | pipes (windows) and shared memory (also windows). No support for these |
64 | connection types is planned, either. |
68 | connection types is planned, either. |
65 | |
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 | |
66 | =head1 FUNCTIONS |
77 | =head1 FUNCTIONS |
67 | |
78 | |
68 | Coro::Mysql offers a single user-accessible function: |
79 | Coro::Mysql offers a single user-accessible function: |
69 | |
80 | |
70 | =over 4 |
81 | =over 4 |
… | |
… | |
78 | |
89 | |
79 | use Scalar::Util (); |
90 | use Scalar::Util (); |
80 | use Carp qw(croak); |
91 | use Carp qw(croak); |
81 | |
92 | |
82 | use Guard; |
93 | use Guard; |
|
|
94 | use AnyEvent (); |
83 | use Coro::Handle (); |
95 | use Coro (); |
|
|
96 | use Coro::AnyEvent (); # not necessary with newer Coro versions |
84 | |
97 | |
85 | # we need this extra indirection, as Coro doesn't support |
98 | # we need this extra indirection, as Coro doesn't support |
86 | # calling SLF-like functions via call_sv. |
99 | # calling SLF-like functions via call_sv. |
87 | |
100 | |
88 | sub readable { &Coro::Handle::FH::readable } |
101 | sub readable { &Coro::Handle::FH::readable } |
… | |
… | |
106 | It is safe to call this function on any database handle (or just about any |
119 | It is safe to call this function on any database handle (or just about any |
107 | value), but it will only do anything to L<DBD::mysql> handles, others are |
120 | value), but it will only do anything to L<DBD::mysql> handles, others are |
108 | returned unchanged. That means it is harmless when applied to database |
121 | returned unchanged. That means it is harmless when applied to database |
109 | handles of other databases. |
122 | handles of other databases. |
110 | |
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; |
|
|
128 | |
111 | =cut |
129 | =cut |
112 | |
130 | |
113 | sub unblock { |
131 | sub unblock { |
114 | my ($DBH) = @_; |
132 | my ($DBH) = @_; |
115 | |
133 | |
116 | if ($DBH->{Driver}{Name} eq "mysql") { |
134 | if ($DBH && $DBH->{Driver}{Name} eq "mysql") { |
117 | my $sock = $DBH->{sock}; |
135 | my $sock = $DBH->{sock}; |
118 | |
136 | |
119 | open my $fh, "+>&" . $DBH->{sockfd} |
137 | open my $fh, "+>&" . $DBH->{sockfd} |
120 | or croak "Coro::Mysql unable to clone mysql fd"; |
138 | or croak "Coro::Mysql unable to clone mysql fd"; |
121 | |
139 | |
|
|
140 | if (AnyEvent::detect ne "AnyEvent::Impl::EV" || !_use_ev) { |
|
|
141 | require Coro::Handle; |
122 | $fh = Coro::Handle::unblock $fh; |
142 | $fh = Coro::Handle::unblock ($fh); |
|
|
143 | } |
123 | |
144 | |
124 | _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; |
145 | _patch $sock, $DBH->{sockfd}, $fh, tied ${$fh}; |
125 | } |
146 | } |
126 | |
147 | |
127 | $DBH |
148 | $DBH |
… | |
… | |
144 | use PApp::SQL; |
165 | use PApp::SQL; |
145 | |
166 | |
146 | sub with_db($$$&) { |
167 | sub with_db($$$&) { |
147 | my ($database, $user, $pass, $cb) = @_; |
168 | my ($database, $user, $pass, $cb) = @_; |
148 | |
169 | |
149 | my $dbh = Coro::Mysql::unblock DBI->connect ($database, $user, $pass) |
170 | my $dbh = DBI->connect ($database, $user, $pass)->Coro::Mysql::unblock |
150 | or die $DBI::errstr; |
171 | or die $DBI::errstr; |
151 | |
172 | |
152 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
173 | Coro::on_enter { $PApp::SQL::DBH = $dbh }; |
153 | |
174 | |
154 | $cb->(); |
175 | $cb->(); |