| 1 | ## Copyright (C) Graham Barr | ||
| 2 | ## vim: ts=8:sw=2:expandtab:shiftround | ||
| 3 | |||
| 4 | ## WARNING!!!! | ||
| 5 | ## WARNING!!!! This code is still very much a work in progress, | ||
| 6 | ## WARNING!!!! do not depend on anything not changing | ||
| 7 | ## WARNING!!!! | ||
| 8 | |||
| 9 | package AnyEvent::MongoDB::Connection; | ||
| 10 | |||
| 11 | use Moose; | ||
| 12 | use AnyEvent::Handle; | ||
| 13 | use AnyEvent::Socket qw(tcp_connect); | ||
| 14 | use AnyEvent::Util qw(guard); | ||
| 15 | use Scalar::Util qw(weaken); | ||
| 16 | use MongoDB; | ||
| 17 | use MongoDB::Cursor; | ||
| 18 | use MongoDB::OID; | ||
| 19 | use DateTime; | ||
| 20 | use Digest::MD5 qw(md5_hex); | ||
| 21 | |||
| 22 | use namespace::autoclean; | ||
| 23 | |||
| 24 | has mongo => ( | ||
| 25 | weak_ref => 1, | ||
| 26 | required => 1, | ||
| 27 | handles => [qw(timeout)], | ||
| 28 | ); | ||
| 29 | |||
| 30 | has host => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Str', | ||
| 33 | ); | ||
| 34 | |||
| 35 | has port => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'Int', | ||
| 38 | ); | ||
| 39 | |||
| 40 | has handle => ( | ||
| 41 | is => 'rw', | ||
| 42 | predicate => 'connected', | ||
| 43 | clearer => 'disconnect', | ||
| 44 | ); | ||
| 45 | |||
| 46 | has timer => ( | ||
| 47 | is => 'rw', | ||
| 48 | clearer => 'clear_timer', | ||
| 49 | ); | ||
| 50 | |||
| 51 | has current_request => ( | ||
| 52 | is => 'rw', | ||
| 53 | clearer => 'clear_current_request', | ||
| 54 | predicate => 'waiting', | ||
| 55 | ); | ||
| 56 | |||
| 57 | has [qw/ on_connect on_connect_error on_error on_eof /] => ( | ||
| 58 | is => 'rw', | ||
| 59 | isa => 'CodeRef' | ||
| 60 | ); | ||
| 61 | |||
| 62 | has pending => ( | ||
| 63 | traits => ['Array'], | ||
| 64 | default => sub { [] }, | ||
| 65 | handles => { | ||
| 66 | pending => 'elements', | ||
| 67 | clear_pending => 'clear', | ||
| 68 | push_pending => 'push', | ||
| 69 | shift_pending => 'shift', | ||
| 70 | has_pending => 'count', | ||
| 71 | }, | ||
| 72 | ); | ||
| 73 | |||
| 74 | after disconnect => sub { | ||
| 75 | my $self = shift; | ||
| 76 | $self->clear_timer; | ||
| 77 | $self->clear_current_request; | ||
| 78 | }; | ||
| 79 | |||
| 80 | |||
| 81 | sub connect { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | $self->disconnect; | ||
| 85 | |||
| 86 | tcp_connect( | ||
| 87 | $self->host, | ||
| 88 | $self->port, | ||
| 89 | sub { | ||
| 90 | if (@_) { | ||
| 91 | $self->_connect(@_); | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | my $cb = $self->on_connect_error; | ||
| 95 | $cb->() if $cb; | ||
| 96 | } | ||
| 97 | }, | ||
| 98 | sub { | ||
| 99 | $self->timeout / 1000; | ||
| 100 | } | ||
| 101 | ); | ||
| 102 | |||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _connect { | ||
| 107 | my ($self, $fh) = @_; | ||
| 108 | |||
| 109 | my $handle = AnyEvent::Handle->new( | ||
| 110 | fh => $fh, | ||
| 111 | on_error => sub { warn "ERR"; $self->on_error }, | ||
| 112 | on_read => sub { | ||
| 113 | shift->unshift_read( | ||
| 114 | chunk => 36, | ||
| 115 | sub { | ||
| 116 | return unless $self; | ||
| 117 | |||
| 118 | # header arrived, decode | ||
| 119 | # We extract cursor with Z8 so that it will work correctly on 32-bit machines | ||
| 120 | # using Z means that when cursor_id is all zero it is extracted as '' | ||
| 121 | # so length($cursor_id) can be used to determine if we got a cursor id | ||
| 122 | my ($len, $request_id, $response_to, $op_code, $flags, $cursor_id, $from, $doc_count) = | ||
| 123 | unpack "V V V V V Z8 V V", $_[1]; | ||
| 124 | |||
| 125 | my $blen = $len - 36; | ||
| 126 | if ($blen < 0) { | ||
| 127 | die; # XXX FIXME | ||
| 128 | } | ||
| 129 | |||
| 130 | my $req = $self->current_request; | ||
| 131 | if ($req) { | ||
| 132 | if ($req->request_id == $response_to) { | ||
| 133 | $self->clear_current_request; | ||
| 134 | $self->clear_timer; | ||
| 135 | $self->flush_pending; | ||
| 136 | } | ||
| 137 | else { | ||
| 138 | undef $req; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | my $cb = $req && $req->cb; | ||
| 142 | |||
| 143 | if ($blen) { | ||
| 144 | |||
| 145 | # We have documents to read | ||
| 146 | shift->unshift_read( | ||
| 147 | chunk => $blen, | ||
| 148 | sub { | ||
| 149 | if (!$req or $req->cancelled) { | ||
| 150 | |||
| 151 | # The cursor was cancelled after we sent a prefetch OP_GET_MORE | ||
| 152 | # So we need to just ignore this OP_RESULT and cancel if needed | ||
| 153 | |||
| 154 | $self->op_kill_cursors($request_id, $cursor_id) | ||
| 155 | if length($cursor_id); | ||
| 156 | |||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | $req->received($doc_count); | ||
| 161 | my $at = $req->at; | ||
| 162 | my $max = $req->max; | ||
| 163 | my $done = $max && $at >= $max; | ||
| 164 | my $do_prefetch = !$done && length($cursor_id) && $req->prefetch; | ||
| 165 | |||
| 166 | my $page = do { | ||
| 167 | my $todo = $max && $max - $at; | ||
| 168 | my $p = $req->limit; | ||
| 169 | ($todo && $todo < $p) ? $todo : $p; | ||
| 170 | }; | ||
| 171 | |||
| 172 | if ($do_prefetch) { | ||
| 173 | |||
| 174 | # User has requested prefetch, so lets send the OP_GET_MORE | ||
| 175 | # request before we start processing this one | ||
| 176 | $self->push_request($req->op_get_more($request_id, $cursor_id, $page)); | ||
| 177 | } | ||
| 178 | |||
| 179 | my $want_more = $cb && $cb->(MongoDB::read_documents($_[1])); | ||
| 180 | |||
| 181 | if ($want_more) { | ||
| 182 | if (length($cursor_id) and !$done) { | ||
| 183 | $self->push_request($req->op_get_more($request_id, $cursor_id, $page)) | ||
| 184 | unless $do_prefetch; # already sent | ||
| 185 | } | ||
| 186 | elsif ($page >= 0) { | ||
| 187 | $cb->() if $cb; # signal finished | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | if (length($cursor_id) and ($done or !$want_more)) { | ||
| 192 | if ($do_prefetch) { | ||
| 193 | |||
| 194 | # We have already requested the next page of results, | ||
| 195 | # so will need to cancel when we get that | ||
| 196 | $req->cancel; | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | $self->op_kill_cursors($request_id, $cursor_id); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | elsif ($cb) { | ||
| 206 | |||
| 207 | # No documents in OP_REPLY, just call callback to signal end of cursor | ||
| 208 | $cb->(); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | ); | ||
| 212 | }, | ||
| 213 | on_eof => sub { | ||
| 214 | $self->disconnect; | ||
| 215 | if (my $on_eof = $self->on_eof) { | ||
| 216 | $on_eof->(); | ||
| 217 | } | ||
| 218 | }, | ||
| 219 | ); | ||
| 220 | |||
| 221 | $self->handle($handle); | ||
| 222 | if (my $on_connect = $self->on_connect) { | ||
| 223 | $on_connect->($self); | ||
| 224 | } | ||
| 225 | $self->flush_pending; | ||
| 226 | |||
| 227 | weaken($self); # avoid loops | ||
| 228 | |||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | sub flush_pending { | ||
| 234 | my $self = shift; | ||
| 235 | |||
| 236 | while (!$self->waiting and my $req = $self->shift_pending) { | ||
| 237 | $self->send_request($req); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | sub send_request { | ||
| 242 | my ($self, $req) = @_; | ||
| 243 | |||
| 244 | return if $req->cancelled; | ||
| 245 | if (my $cb = $req->cb) { | ||
| 246 | $self->current_request($req); | ||
| 247 | if (my $timeout = $req->timeout) { | ||
| 248 | $self->timer( | ||
| 249 | AE::timer( | ||
| 250 | $timeout / 1000, | ||
| 251 | 0, | ||
| 252 | sub { | ||
| 253 | my $current = $self->current_request; | ||
| 254 | if ($current and $current == $req) { | ||
| 255 | $req->cancel; | ||
| 256 | $cb->({'$err' => 'query timeout'}); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | ) | ||
| 260 | ); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | $self->handle->push_write(${ $req->message }); | ||
| 265 | |||
| 266 | weaken($self); | ||
| 267 | |||
| 268 | return; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub push_request { | ||
| 272 | my $self = shift; | ||
| 273 | my $req = shift; | ||
| 274 | |||
| 275 | if ($self->has_pending or $self->waiting) { | ||
| 276 | $self->push_pending($req); | ||
| 277 | } | ||
| 278 | else { | ||
| 279 | $self->send_request($req); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | sub op_kill_cursors { | ||
| 285 | my ($self, $request_id, $cursor_id) = @_; | ||
| 286 | my $next_request_id = ++$MongoDB::Cursor::_request_id; | ||
| 287 | my $message = pack( | ||
| 288 | "V V V V V V a8", | ||
| 289 | 0, $next_request_id, $request_id, 2007, # OP_KILL_CURSORS | ||
| 290 | 0, 1, $cursor_id | ||
| 291 | ); | ||
| 292 | substr($message, 0, 4) = pack("V", length($message)); | ||
| 293 | $self->handle->push_write($message); | ||
| 294 | } | ||
| 295 | |||
| 296 | __PACKAGE__->meta->make_immutable; |