| 1 | package Myriad::Service::Implementation; | ||
| 2 | |||
| 3 | 8 | ▶ | use Myriad::Class extends => 'IO::Async::Notifier'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.001'; # VERSION | ||
| 6 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 7 | |||
| 8 | =encoding utf8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Myriad::Service::Implementation - microservice coördination | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | 0 | ▶ | use Myriad::Storage::Implementation::Redis; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 21 | use Myriad::Subscription; | ||
| 22 | |||
| 23 | use Myriad::Service::Attributes; | ||
| 24 | |||
| 25 | # Only defer up to this many seconds between batch iterations | ||
| 26 | use constant MAX_EXPONENTIAL_BACKOFF => 2; | ||
| 27 | |||
| 28 | sub MODIFY_CODE_ATTRIBUTES { | ||
| 29 | my ($class, $code, @attrs) = @_; | ||
| 30 | Myriad::Service::Attributes->apply_attributes( | ||
| 31 | class => $class, | ||
| 32 | code => $code, | ||
| 33 | attributes => \@attrs | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | |||
| 37 | has $ryu; | ||
| 38 | has $storage; | ||
| 39 | has $myriad; | ||
| 40 | has $service_name; | ||
| 41 | has %active_batch; | ||
| 42 | |||
| 43 | =head1 ATTRIBUTES | ||
| 44 | |||
| 45 | These methods return instance variables. | ||
| 46 | |||
| 47 | =head2 ryu | ||
| 48 | |||
| 49 | Provides a common L<Ryu::Async> instance. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | method ryu () { $ryu } | ||
| 54 | |||
| 55 | =head2 myriad | ||
| 56 | |||
| 57 | The L<Myriad> instance which owns this service. Stored internally as a weak reference. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | method myriad () { $myriad } | ||
| 62 | |||
| 63 | =head2 rpc | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | method rpc () { $myriad->rpc } | ||
| 68 | |||
| 69 | =head2 subscription | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | method subscription () { $myriad->subscription } | ||
| 74 | |||
| 75 | =head2 service_name | ||
| 76 | |||
| 77 | The name of the service, defaults to the package name. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | method service_name () { $service_name } | ||
| 82 | |||
| 83 | |||
| 84 | =head1 METRICS | ||
| 85 | |||
| 86 | General metrics that any service is assumed to have | ||
| 87 | |||
| 88 | =head2 myriad.service.rpc | ||
| 89 | |||
| 90 | Timing information about RPC calls tagged by service, status and method name | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | |||
| 95 | $metrics->make_timer( rpc_timing => | ||
| 96 | name => [ qw(myriad service rpc) ], | ||
| 97 | description => "Time taken to process the RPC request", | ||
| 98 | labels => [qw(method status service)] | ||
| 99 | ); | ||
| 100 | |||
| 101 | =head2 myriad.service.batch | ||
| 102 | |||
| 103 | Timing information about the batch subscriptions tagged by service, status and method name | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | $metrics->make_timer( batch_timing => | ||
| 108 | name => [ qw(myriad service batch) ], | ||
| 109 | description => "Time taken to process the RPC request", | ||
| 110 | labels => [qw(method status service)] | ||
| 111 | ); | ||
| 112 | |||
| 113 | =head2 myriad.service.receiver | ||
| 114 | |||
| 115 | Timing information about events receivers tagged by service, status and method name | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | $metrics->make_timer( receiver_timing => | ||
| 120 | name => [ qw(myriad service receiver) ], | ||
| 121 | description => "Time taken to process the received events", | ||
| 122 | labels => [qw(method status service)] | ||
| 123 | ); | ||
| 124 | |||
| 125 | =head2 myriad.service.emitter | ||
| 126 | |||
| 127 | A counter for the events emitted by emitters tagged by service and method name | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | $metrics->make_counter( emitters_count => | ||
| 132 | name => [qw(myriad service emitter)], | ||
| 133 | description => "Counter for the events emitted by the emitters", | ||
| 134 | labels => [qw(method service)], | ||
| 135 | ); | ||
| 136 | |||
| 137 | =head1 METHODS | ||
| 138 | |||
| 139 | =head2 configure | ||
| 140 | |||
| 141 | Populate internal configuration. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | method configure (%args) { | ||
| 146 | $service_name //= (delete $args{name} || die 'need a service name'); | ||
| 147 | weaken($myriad = delete $args{myriad}) if exists $args{myriad}; | ||
| 148 | $self->next::method(%args); | ||
| 149 | } | ||
| 150 | |||
| 151 | =head2 _add_to_loop | ||
| 152 | |||
| 153 | Apply this service to the current event loop. | ||
| 154 | |||
| 155 | This will trigger a number of actions: | ||
| 156 | |||
| 157 | =over 4 | ||
| 158 | |||
| 159 | =item * initial startup | ||
| 160 | |||
| 161 | =item * first diagnostics check | ||
| 162 | |||
| 163 | =item * if successful, batch and subscription registration will occur | ||
| 164 | |||
| 165 | =back | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | method _add_to_loop($loop) { | ||
| 170 | $log->tracef('Adding %s to loop', ref $self); | ||
| 171 | $self->add_child( | ||
| 172 | $ryu = Ryu::Async->new | ||
| 173 | ); | ||
| 174 | |||
| 175 | $self->next::method($loop); | ||
| 176 | } | ||
| 177 | |||
| 178 | =head1 ASYNC METHODS | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | async method process_batch($k, $code, $src) { | ||
| 183 | my $backoff; | ||
| 184 | while (1) { | ||
| 185 | await $src->unblocked; | ||
| 186 | my $data = []; | ||
| 187 | try { | ||
| 188 | $data = await $self->$code->on_ready(sub { | ||
| 189 | my $f = shift; | ||
| 190 | $metrics->report_timer( | ||
| 191 | batch_timing => $f->elapsed // 0, { | ||
| 192 | method => $k, | ||
| 193 | status => $f->state, | ||
| 194 | service => $service_name | ||
| 195 | } | ||
| 196 | ); | ||
| 197 | }); | ||
| 198 | } catch ($e) { | ||
| 199 | $log->warnf('Batch iteration for %s failed - %s', $k, $e); | ||
| 200 | } | ||
| 201 | |||
| 202 | die 'Batch should return an arrayref' unless ref $data eq 'ARRAY'; | ||
| 203 | |||
| 204 | if ($data->@*) { | ||
| 205 | $backoff = 0; | ||
| 206 | $src->emit($_) for $data->@*; | ||
| 207 | # Defer next processing, give other events a chance | ||
| 208 | await $self->loop->delay_future(after => 0); | ||
| 209 | } else { | ||
| 210 | $backoff = min(MAX_EXPONENTIAL_BACKOFF, ($backoff || 0.02) * 2); | ||
| 211 | $log->tracef('Batch for %s returned no results, delaying for %dms before retry', $k, $backoff * 1000.0); | ||
| 212 | await $self->loop->delay_future( | ||
| 213 | after => $backoff | ||
| 214 | ); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | =head2 load | ||
| 220 | |||
| 221 | To wire the service with Myriad's component | ||
| 222 | before it actually starts work | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | async method load () { | ||
| 227 | my $registry = $Myriad::REGISTRY; | ||
| 228 | |||
| 229 | if(my $emitters = $registry->emitters_for(ref($self))) { | ||
| 230 | for my $method (sort keys $emitters->%*) { | ||
| 231 | $log->tracef('Adding Emitter %s as %s for %s', $method, $emitters->{$method}, $service_name); | ||
| 232 | my $spec = $emitters->{$method}; | ||
| 233 | my $chan = $spec->{args}{channel} // die 'expected a channel, but there was none to be found'; | ||
| 234 | my $sink = $spec->{sink} = $ryu->sink( | ||
| 235 | label => "emitter:$chan", | ||
| 236 | ); | ||
| 237 | |||
| 238 | $spec->{src} = $sink->source->map(sub { | ||
| 239 | $metrics->inc_counter( | ||
| 240 | 'emitters_count', { | ||
| 241 | method => $method, | ||
| 242 | service => $service_name | ||
| 243 | } | ||
| 244 | ); | ||
| 245 | return $_; | ||
| 246 | }); | ||
| 247 | |||
| 248 | await $self->subscription->create_from_source( | ||
| 249 | source => $spec->{src}->pause, | ||
| 250 | channel => $chan, | ||
| 251 | service => $service_name, | ||
| 252 | ); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | if(my $receivers = $registry->receivers_for(ref($self))) { | ||
| 257 | for my $method (sort keys $receivers->%*) { | ||
| 258 | try { | ||
| 259 | $log->tracef('Adding Receiver %s as %s for %s', $method, $receivers->{$method}, $service_name); | ||
| 260 | my $spec = $receivers->{$method}; | ||
| 261 | my $chan = $spec->{args}{channel} // die 'expected a channel, but there was none to be found'; | ||
| 262 | my $sink = $spec->{sink} = $ryu->sink( | ||
| 263 | label => "receiver:$chan", | ||
| 264 | ); | ||
| 265 | $sink->pause; | ||
| 266 | await $self->subscription->create_from_sink( | ||
| 267 | sink => $sink, | ||
| 268 | channel => $chan, | ||
| 269 | from => $spec->{args}{service}, | ||
| 270 | service => $service_name, | ||
| 271 | ); | ||
| 272 | } catch ($e) { | ||
| 273 | $log->errorf('Failed while setting up receiver: %s', $e); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | if (my $batches = $registry->batches_for(ref($self))) { | ||
| 279 | for my $method (sort keys $batches->%*) { | ||
| 280 | $log->tracef('Adding Batch %s for %s', $method, $service_name); | ||
| 281 | my $sink = $batches->{$method}{sink} = $ryu->sink(label => 'batch:' . $method); | ||
| 282 | $sink->pause; | ||
| 283 | await $self->subscription->create_from_source( | ||
| 284 | source => $sink->source, | ||
| 285 | channel => $method, | ||
| 286 | service => $service_name, | ||
| 287 | ); | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | if (my $rpc_calls = $registry->rpc_for(ref($self))) { | ||
| 292 | for my $method (sort keys $rpc_calls->%*) { | ||
| 293 | $log->tracef('Adding RPC %s for %s', $method, $service_name); | ||
| 294 | my $spec = $rpc_calls->{$method}; | ||
| 295 | my $sink = $spec->{sink} = $ryu->sink(label => "rpc:$service_name:$method"); | ||
| 296 | $sink->pause; | ||
| 297 | $self->rpc->create_from_sink( | ||
| 298 | service => $service_name, | ||
| 299 | method => $method, | ||
| 300 | sink => $sink | ||
| 301 | ); | ||
| 302 | |||
| 303 | my $code = $spec->{code}; | ||
| 304 | $spec->{current} = $sink->source->map($self->$curry::weak(async method ($message) { | ||
| 305 | try { | ||
| 306 | my $response = await $self->$code( | ||
| 307 | $message->args->%* | ||
| 308 | )->on_ready(sub { | ||
| 309 | my $f = shift; | ||
| 310 | $metrics->report_timer( | ||
| 311 | rpc_timing => $f->elapsed // 0, { | ||
| 312 | method => $method, | ||
| 313 | status => $f->state, | ||
| 314 | service => $service_name | ||
| 315 | } | ||
| 316 | ); | ||
| 317 | }); | ||
| 318 | await $self->rpc->reply_success($service_name, $message, $response); | ||
| 319 | } catch ($e) { | ||
| 320 | $e = Myriad::Exception::InternalError->new(reason => $e) unless (blessed $e and $e->does('Myriad::Exception')); | ||
| 321 | await $self->rpc->reply_error($service_name, $message, $e); | ||
| 322 | } | ||
| 323 | }))->resolve->completed; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | =head2 start | ||
| 329 | |||
| 330 | Perform the diagnostics check and start the service | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | async method start { | ||
| 335 | await $self->startup; | ||
| 336 | |||
| 337 | my $diag = await Future->wait_any( | ||
| 338 | $self->loop->timeout_future(after => 30), | ||
| 339 | $self->diagnostics(1), | ||
| 340 | ); | ||
| 341 | |||
| 342 | # Since everything is ready now we let the service commence work | ||
| 343 | my $registry = $Myriad::REGISTRY; | ||
| 344 | if(my $emitters = $registry->emitters_for(ref($self))) { | ||
| 345 | for my $method (sort keys $emitters->%*) { | ||
| 346 | $log->tracef('Starting Emitter %s as %s for %s', $method, $emitters->{$method}{args}{channel}, $service_name); | ||
| 347 | my $spec = $emitters->{$method}; | ||
| 348 | my $code = delete $spec->{code}; | ||
| 349 | $spec->{current} = $self->$code( | ||
| 350 | $spec->{sink}, | ||
| 351 | )->on_fail(sub { | ||
| 352 | $log->fatalf('Emitter for %s failed - %s', $method, shift); | ||
| 353 | })->retain; | ||
| 354 | $spec->{src}->resume; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | if(my $receivers = $registry->receivers_for(ref($self))) { | ||
| 359 | for my $method (sort keys $receivers->%*) { | ||
| 360 | try { | ||
| 361 | $log->tracef('Starting Receiver %s as %s for %s', $method, $receivers->{$method}{args}{channel}, $service_name); | ||
| 362 | my $spec = $receivers->{$method}; | ||
| 363 | my $code = delete $spec->{code}; | ||
| 364 | my $current = await $self->$code( | ||
| 365 | $spec->{sink}->source | ||
| 366 | ); | ||
| 367 | |||
| 368 | die "Receivers method: $method should return a Ryu::Source" | ||
| 369 | unless blessed $current && $current->isa('Ryu::Source'); | ||
| 370 | |||
| 371 | Scalar::Util::weaken(my $sink_copy = $spec->{sink}); | ||
| 372 | $spec->{current} = $current->map(sub { | ||
| 373 | my $f = Future->wrap(shift); | ||
| 374 | $metrics->report_timer( | ||
| 375 | receiver_timing => ($f->elapsed // 0), { | ||
| 376 | method => $method, | ||
| 377 | status => $f->state, | ||
| 378 | service => $service_name | ||
| 379 | } | ||
| 380 | ); | ||
| 381 | return $f; | ||
| 382 | })->resolve->completed->on_fail(sub { | ||
| 383 | my $error = shift; | ||
| 384 | $log->errorf('Receiver %s failed while processing messages - %s', $method, $error); | ||
| 385 | my $sink = $sink_copy or return; | ||
| 386 | my $src = $sink->source; | ||
| 387 | $src->fail($error) unless $src->completed->is_ready; | ||
| 388 | })->retain; | ||
| 389 | $spec->{sink}->resume; | ||
| 390 | } catch ($e) { | ||
| 391 | $log->errorf('Failed while starting up receiver: %s', $e); | ||
| 392 | } | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | if (my $batches = $registry->batches_for(ref($self))) { | ||
| 397 | for my $method (sort keys $batches->%*) { | ||
| 398 | $log->tracef('Starting Batch %s for %s', $method, $service_name); | ||
| 399 | my $code = delete $batches->{$method}{code}; | ||
| 400 | |||
| 401 | $active_batch{$method} = [ | ||
| 402 | $batches->{$method}{sink}, | ||
| 403 | $self->process_batch( | ||
| 404 | $method, | ||
| 405 | $code, | ||
| 406 | $batches->{$method}{sink} | ||
| 407 | ) | ||
| 408 | ]; | ||
| 409 | |||
| 410 | $batches->{$method}{sink}->resume; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | if (my $rpc_calls = $registry->rpc_for(ref($self))) { | ||
| 415 | for my $method ( sort keys $rpc_calls->%* ) { | ||
| 416 | $log->tracef('Starting RPC %s for %s', $method, $service_name); | ||
| 417 | $rpc_calls->{$method}->{sink}->resume; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | $log->infof('Service [%s] started', $service_name); | ||
| 422 | |||
| 423 | }; | ||
| 424 | |||
| 425 | =head2 startup | ||
| 426 | |||
| 427 | Initialize the service internal status it will be called when the service is added to the L<IO::Async::Loop>. | ||
| 428 | |||
| 429 | The method here is just a placeholder it should be reimplemented by the service code. | ||
| 430 | |||
| 431 | =cut | ||
| 432 | |||
| 433 | async method startup { | ||
| 434 | return; | ||
| 435 | } | ||
| 436 | |||
| 437 | =head2 diagnostics | ||
| 438 | |||
| 439 | Runs any internal diagnostics. | ||
| 440 | |||
| 441 | The method here is just a placeholder it should be reimplemented by the service code. | ||
| 442 | |||
| 443 | =cut | ||
| 444 | |||
| 445 | async method diagnostics($level) { | ||
| 446 | return 'ok'; | ||
| 447 | } | ||
| 448 | |||
| 449 | =head2 shutdown | ||
| 450 | |||
| 451 | Gracefully shut down the service. At the moment, this means we: | ||
| 452 | |||
| 453 | =over 4 | ||
| 454 | |||
| 455 | =item * stop accepting more requests | ||
| 456 | |||
| 457 | =item * finish the pending requests | ||
| 458 | |||
| 459 | =back | ||
| 460 | |||
| 461 | =cut | ||
| 462 | |||
| 463 | async method shutdown { | ||
| 464 | return; | ||
| 465 | } | ||
| 466 | |||
| 467 | 1; | ||
| 468 | |||
| 469 | =head1 AUTHOR | ||
| 470 | |||
| 471 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 472 | |||
| 473 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 474 | |||
| 475 | =head1 LICENSE | ||
| 476 | |||
| 477 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||
| 478 | |||