Zing.pm untested

filter: Statementstmt 0.0% 0 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 33 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zing;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry 'Zing::Types';
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13  
14 extends 'Zing::Kernel';
15  
16 our $VERSION = '0.26'; # VERSION
17  
18 # ATTRIBUTES
19  
20 has 'scheme' => (
21 is => 'ro',
22 isa => 'Scheme',
23 req => 1,
24 );
25  
26 # METHODS
27  
28 method start() {
29 return $self->execute;
30 }
31  
32 1;
33  
34 =encoding utf8
35  
36 =head1 NAME
37  
38 Zing - Actor-Model Toolkit
39  
40 =cut
41  
42 =head1 ABSTRACT
43  
44 Actor Toolkit and Multi-Process Management System
45  
46 =cut
47  
48 =head1 SYNOPSIS
49  
50 use Zing;
51  
52 my $zing = Zing->new(scheme => ['MyApp', [], 1]);
53  
54 # $zing->execute;
55  
56 =cut
57  
58 =head1 DESCRIPTION
59  
60 This distribution includes an actor-model architecture toolkit and
61 multi-process management system which provides primatives for building
62 resilient, reactive, concurrent, distributed message-driven applications in
63 Perl 5. If you're unfamiliar with this architectural pattern, learn more about
64 L<"the actor model"|https://en.wikipedia.org/wiki/Actor_model>.
65  
66 =cut
67  
68 =head1 INHERITS
69  
70 This package inherits behaviors from:
71  
72 L<Zing::Kernel>
73  
74 =cut
75  
76 =head1 LIBRARIES
77  
78 This package uses type constraints from:
79  
80 L<Zing::Types>
81  
82 =cut
83  
84 =head1 ATTRIBUTES
85  
86 This package has the following attributes:
87  
88 =cut
89  
90 =head2 scheme
91  
92 scheme(Scheme)
93  
94 This attribute is read-only, accepts C<(Scheme)> values, and is required.
95  
96 =cut
97  
98 =head1 METHODS
99  
100 This package implements the following methods:
101  
102 =cut
103  
104 =head2 start
105  
106 start() : Kernel
107  
108 The start method prepares the L<Zing::Kernel> and executes its event-loop.
109  
110 =over 4
111  
112 =item start example #1
113  
114 # given: synopsis
115  
116 $zing->start;
117  
118 =back
119  
120 =cut
121  
122 =head1 PRIMATIVES
123  
124 This distribution provides a collection of actor-model primitives which can be
125 used to create sophisticated and powerful distributed systems, organized within
126 intricate process topologies. The following is a directory of those primitives
127 listed by classification:
128  
129 =head2 messaging
130  
131 These classes facilitate message-passing and communications:
132  
133 =over 4
134  
135 =item *
136  
137 L<Zing::Channel>: Shared Communication
138  
139 =item *
140  
141 L<Zing::Data>: Process Data
142  
143 =item *
144  
145 L<Zing::Domain>: Shared State
146  
147 =item *
148  
149 L<Zing::KeyVal>: Key/Value Store
150  
151 =item *
152  
153 L<Zing::Lookup>: Domain Index
154  
155 =item *
156  
157 L<Zing::Table>: Generic Index
158  
159 =item *
160  
161 L<Zing::Mailbox>: Process Mailbox
162  
163 =item *
164  
165 L<Zing::PubSub>: Pub/Sub Store
166  
167 =item *
168  
169 L<Zing::Queue>: Message Queue
170  
171 =item *
172  
173 L<Zing::Registry>: Process Registry
174  
175 =item *
176  
177 L<Zing::Repo>: Generic Store
178  
179 =item *
180  
181 L<Zing::Store>: Storage Abstraction
182  
183 =back
184  
185 =head2 processes
186  
187 These base classes implement the underlying process (actor) logic:
188  
189 =over 4
190  
191 =item *
192  
193 L<Zing>: Process Wrapper
194  
195 =item *
196  
197 L<Zing::Kernel>: Kernel Process
198  
199 =item *
200  
201 L<Zing::Launcher>: Scheme Launcher
202  
203 =item *
204  
205 L<Zing::Process>: Processing Unit
206  
207 =item *
208  
209 L<Zing::Ring>: Process Ring
210  
211 =item *
212  
213 L<Zing::Scheduler>: Scheme Launcher
214  
215 =item *
216  
217 L<Zing::Simple>: Simple Process
218  
219 =item *
220  
221 L<Zing::Single>: Single Process
222  
223 =item *
224  
225 L<Zing::Spawner>: Scheme Spawner
226  
227 =item *
228  
229 L<Zing::Timer>: Timer Process
230  
231 =item *
232  
233 L<Zing::Watcher>: Watcher Process
234  
235 =item *
236  
237 L<Zing::Worker>: Worker Process
238  
239 =back
240  
241 =head2 stores
242  
243 These classes handle data persistence for all messaging abstractions:
244  
245 =over 4
246  
247 =item *
248  
249 L<Zing::Store::Disk>: File-based Persistence
250  
251 =item *
252  
253 L<Zing::Store::Hash>: In-Memory Persistence
254  
255 =item *
256  
257 L<Zing::Store::Mysql>: MySQL Persistence
258  
259 =item *
260  
261 L<Zing::Store::Pg>: PostgreSQL Persistence
262  
263 =item *
264  
265 L<Zing::Store::Redis>: Redis Persistence
266  
267 =item *
268  
269 L<Zing::Store::Sqlite>: SQLite Persistence
270  
271 =item *
272  
273 L<Zing::Store::Temp>: Temporary File-based Persistence
274  
275 =back
276  
277 =head2 ready-made
278  
279 These classes are ready-made process implementations using callbacks:
280  
281 =over 4
282  
283 =item *
284  
285 L<Zing::Zang>: Process Implementation
286  
287 =item *
288  
289 L<Zing::Zang::Launcher>: Process Launcher
290  
291 =item *
292  
293 L<Zing::Zang::Simple>: Process Performer
294  
295 =item *
296  
297 L<Zing::Zang::Single>: Single-Task Process
298  
299 =item *
300  
301 L<Zing::Zang::Spawner>: Process Spawner
302  
303 =item *
304  
305 L<Zing::Zang::Timer>: Timer Process
306  
307 =item *
308  
309 L<Zing::Zang::Watcher>: Process Watcher
310  
311 =item *
312  
313 L<Zing::Zang::Worker>: Worker Process
314  
315 =back
316  
317 =cut
318  
319 =head1 FEATURES
320  
321 All features are implemented using classes and objects. The following is a list
322 of features currently enabled by this toolkit:
323  
324 =cut
325  
326 =head2 actor-model
327  
328 use Zing::Process;
329  
330 my $p1 = Zing::Process->new;
331 my $p2 = Zing::Process->new;
332  
333 $p1->send($p2, { action => 'greetings' });
334  
335 say $p2->recv->{action}; # got it?
336  
337 This distribution provides a toolkit for creating processes (actors) which can
338 be run in isolation and which communicate with other processes through
339 message-passing.
340  
341 =cut
342  
343 =head2 asynchronous
344  
345 # in process (1)
346 use Zing::Domain;
347 use Zing::Process;
348  
349 my $d1 = Zing::Domain->new(name => 'peers');
350 my $p1 = Zing::Process->new(name => 'p1');
351  
352 $d1->push('mailboxes', $p1->mailbox->term);
353 $p1->execute;
354  
355 # in process (2)
356 use Zing::Domain;
357 use Zing::Process;
358  
359 my $d2 = Zing::Domain->new(name => 'peers');
360 my $p2 = Zing::Process->new(name => 'p2');
361  
362 my $mailboxes = $d2->get('mailboxes');
363  
364 for my $address (@$mailboxes) {
365 # send each registered process a message
366 $p2->send($address, { discovery => $p2->mailbox->term });
367 }
368  
369 $p2->execute;
370  
371 This distribution provides a multi-process management system which allows
372 processes to be deployed and managed separately having the ability to
373 communicate across threads of execution.
374  
375 =cut
376  
377 =head2 atomicity
378  
379 # in process (1)
380 use Zing::KeyVal;
381  
382 my $i = 0;
383 my $kv = Zing::KeyVal->new(name => 'stash');
384  
385 while ($i < 1_000) {
386 $kv->send('random', { value => 1 });
387  
388 # my $data = $kv->recv('random');
389 }
390  
391 # in process (2)
392 use Zing::KeyVal;
393  
394 my $i = 0;
395 my $kv = Zing::KeyVal->new(name => 'stash');
396  
397 while ($i < 1_000) {
398 $kv->send('random', { value => 2 });
399  
400 # my $data = $kv->recv('random');
401 }
402  
403 This distribution provides data storage abstractions which perform atomic reads
404 and write by leveraging L<Redis|https://redis.io> as the default data storage
405 backend.
406  
407 =cut
408  
409 =head2 chainable
410  
411 use Zing::Process;
412 use Zing::Ring;
413  
414 my $p1 = Zing::Process->new(name => 'p1');
415 my $p2 = Zing::Process->new(name => 'p2');
416  
417 my $ring = Zing::Ring->new(processes => [$p1, $p2]);
418  
419 $ring->execute;
420  
421 This distribution provides a mechanism for chaining (i.e. joining) two or more
422 processes together and executing them in a turn-based manner. This ability
423 allows you to design complex hierarchical process topologies.
424  
425 =cut
426  
427 =head2 channels
428  
429 # in process (1)
430 use Zing::Channel;
431  
432 my $chan = Zing::Channel->new(name => 'chat');
433  
434 while (1) {
435 if (my $data = $chan->recv) {
436 # broadcast received
437 warn $data->{text};
438 }
439 }
440  
441 # in process (2)
442 use Zing::Channel;
443  
444 my $chan = Zing::Channel->new(name => 'chat');
445  
446 while (1) {
447 if (my $data = $chan->recv) {
448 # broadcast received
449 warn $data->{text};
450 }
451 }
452  
453 This distribution provides the means for braodcasting and communicating to
454 multiple processes simulaneously through channels which are similar to FIFO
455 queues.
456  
457 =cut
458  
459 =head2 clusterable
460  
461 # in process (1) on cluster (1)
462 use Zing::Queue;
463  
464 my $queue = Zing::Queue->new(name => 'tasks', target => 'global');
465  
466 # pull from global queue
467 $queue->recv;
468  
469 # in process (1) on cluster (2)
470 use Zing::Queue;
471  
472 my $queue = Zing::Queue->new(name => 'tasks', target => 'global');
473  
474 # pull from global queue
475 $queue->recv;
476  
477 This distribution provides support cross-cluster communication and thus
478 operations. Using federated Redis as the data storage backend means you can
479 scale your deployments without changing your implementations.
480  
481 =cut
482  
483 =head2 configuration
484  
485 # configure the namespace
486 ZING_NS=app
487  
488 # enable process debugging tracing
489 ZING_DEBUG=1
490  
491 # configure the namespace, same as ZING_NS (defaults to "main")
492 ZING_HANDLE=app
493  
494 # configure where the command-line tool finds catridges
495 ZING_HOME=/tmp
496  
497 # configure the hostname used in process registration
498 ZING_HOST=0.0.0.0
499 ZING_HOST=68.80.90.100
500  
501 # configure the resource target (e.g. when distributing across multiple hosts)
502 ZING_TARGET='global' # 'us-east', 'us-west', etc
503  
504 # configure the datastore (defaults to 'Zing::Store::Redis')
505 ZING_STORE='Zing::Store::Redis'
506  
507 # configure Redis driver without touching your source code
508 ZING_REDIS='server=127.0.0.1:6379'
509 ZING_REDIS='every=1_000_000,reconnect=60'
510 ZING_REDIS='sentinels=127.0.0.1:12345|127.0.0.1:23456,sentinels_cnx_timeout=0.1'
511 ZING_REDIS='server=192.168.0.1:6379,debug=0'
512  
513 # configure the object encoder (defaults to 'Zing::Encoder::Json')
514 ZING_ENCODER='Zing::Encoder::Json'
515  
516 # configure where the command-line tool finds catridges and PID files
517 ZING_APPDIR=./
518 ZING_PIDDIR=/tmp
519  
520 This distribution provides environment variables that let you customize how
521 Zing operates and behaves without the need to modify source code. These
522 attributes are not required and fallback to sane defaults.
523  
524 =cut
525  
526 =head2 distributed
527  
528 # in process (1..n) on cluster (1)
529 use Zing;
530  
531 my $zing = Zing->new(scheme => ['MyApp', [], 16]);
532  
533 $zing->execute;
534  
535 # in process (1..n) on cluster (2)
536 use Zing;
537  
538 my $zing = Zing->new(scheme => ['MyApp', [], 16]);
539  
540 $zing->execute;
541  
542 # in process (1..n) on cluster (3)
543 use Zing;
544  
545 my $zing = Zing->new(scheme => ['MyApp', [], 16]);
546  
547 $zing->execute;
548  
549 This distribution provides a collection of actor-model primitives which can be
550 used to create sophisticated and powerful distributed systems, organized within
551 intricate process topologies.
552  
553 =cut
554  
555 =head2 event-driven
556  
557 # in process (1)
558 package MyApp::FileUpoad;
559  
560 use parent 'Zing::Process';
561  
562 sub receive {
563 my ($self, $from, $data) = @_;
564  
565 # react to file-upload events
566  
567 return;
568 }
569  
570 my $p1 = MyApp::FileUpoad->new;
571  
572 $p1->execute;
573  
574 # in process (2)
575 package MyApp::TextTranslate;
576  
577 use parent 'Zing::Process';
578  
579 sub receive {
580 my ($self, $from, $data) = @_;
581  
582 # react to text-translate events
583  
584 return;
585 }
586  
587 my $p2 = MyApp::TextTranslate->new;
588  
589 $p2->execute;
590  
591 This distribution provides all the prerequisites needed to develop scalable
592 reactive event-driven applications distributed across one or several servers.
593  
594 =cut
595  
596 =head2 fifo-queues
597  
598 # in process (1)
599 use Zing::Queue;
600  
601 my $queue = Zing::Queue->new(name => 'tasks');
602  
603 # pull from queue
604 $queue->send({ command => { ... } });
605 $queue->send({ command => { ... } });
606 $queue->send({ command => { ... } });
607 $queue->send({ command => { ... } });
608  
609 # in process (2)
610 use Zing::Queue;
611  
612 my $queue = Zing::Queue->new(name => 'tasks');
613  
614 # pull from FIFO queue
615 $queue->recv;
616  
617 This distribution provides high-performance FIFO message queues which enhance
618 messaging across processes when the order of operations and events is critical
619 and where duplicates can't be tolerated.
620  
621 =cut
622  
623 =head2 hot-reloadable
624  
625 use Zing;
626 use Zing::Daemon;
627  
628 my $daemon = Zing::Daemon->new(
629 name => 'myapp-sleep',
630 app => Zing->new(scheme => ['MyApp::Sleep', [], 4])
631 );
632  
633 $daemon->execute; # pid 12345
634  
635 # $ kill -USR2 12345
636  
637 This distribution provides zero-downtime through hot-reloading which is where
638 the process watchers (supervisors) keep the app running and gracefully reload
639 child processes at runtime.
640  
641 =cut
642  
643 =head2 log-shipping
644  
645 use Zing::Zang;
646  
647 my $zang = Zing::Zang->new(on_perform => sub {
648 my ($self) = @_;
649  
650 $self->log->fatal('something went wrong');
651  
652 return;
653 });
654  
655 $zang->execute;
656  
657 # tap the logs using the command-line tool
658  
659 # $ zing logs
660  
661 This distribution provides the ability to ship the logs of individual processes
662 to a specific centralized channel which can be tapped asynchronously,
663 especially by the command-line tool.
664  
665 =cut
666  
667 =head2 mailboxes
668  
669 use Zing::Process;
670  
671 my $p1 = Zing::Process->new(name => 'p1');
672 my $p2 = Zing::Process->new(name => 'p2');
673  
674 $p1->mailbox->send($p2->mailbox->term, { say => 'ehlo' });
675  
676 my $message = $p2->mailbox->recv;
677  
678 $p2->mailbox->reply($message, { say => 'helo' });
679  
680 This distribution provides every process with its own unique mailbox which can
681 receive messages from other processes as a means of cooperating through message
682 passing.
683  
684 =cut
685  
686 =head2 management
687  
688 use Zing::Kernel;
689  
690 my $kernel = Zing::Kernel->new(scheme => ['MyApp::Logger', [], 2]);
691  
692 $kernel->execute;
693  
694 This distribution provides a kernel process that can be used to manage the
695 deployment of child processes and is used to wrap cartridges by the
696 command-line tool when daemonizing process.
697  
698 =cut
699  
700 =head2 multitasking
701  
702 use Zing::Zang;
703  
704 my $zang = Zing::Zang->new(
705 on_perform => sub {
706 # do something
707 },
708 on_receive => sub {
709 # handle something
710 },
711 );
712  
713 $zang->execute;
714  
715 This distribution provides all processes with an event-loop that allows them to
716 perform multiple operations in sequence and supports operating in a
717 non-blocking manner.
718  
719 =cut
720  
721 =head2 non-blocking
722  
723 use Zing::Zang;
724  
725 my $zang = Zing::Zang->new(
726 on_perform => sub {
727 my ($self) = @_;
728  
729 $self->defer({ command => {...} }) and return;
730  
731 return;
732 },
733 on_receive => sub {
734 my ($self, $from, $data) = @_;
735  
736 return unless $self->term eq $from; # from myself
737  
738 # do something
739  
740 return;
741 }
742 );
743  
744 $zang->exercise;
745  
746 This distribution provides features that allow processes to operate in a
747 non-blocking manner, yielding and deferring operations, and chunking workloads.
748  
749 =cut
750  
751 =head2 parallelism
752  
753 use Zing::Process;
754  
755 my $p1 = Zing::Process->new;
756  
757 my $f1 = $p1->spawn(['MyApp', [id => 12345] 1]);
758 my $f2 = $p1->spawn(['MyApp', [id => 12346] 1]);
759  
760 This distribution provides multiple ways of executing operations in parallel,
761 including spawning processes via forking with the guarantee of not creating
762 zombie processes.
763  
764 =cut
765  
766 =head2 supervisors
767  
768 use Zing::Zang::Watcher;
769  
770 my $zang = Zing::Zang::Watcher->new(
771 scheme => ['MyApp', [] 8]
772 );
773  
774 $zang->exercise;
775  
776 This distribution provides watcher processes which to supervise child processes
777 but also are capable of multitasking and performing other operations while
778 monitoring supervised processes.
779  
780 =head2 virtual-actors
781  
782 use Zing::Zang::Spawner;
783  
784 my $zang = Zing::Zang::Spawner->new(
785 queues => ['schemes']
786 );
787  
788 $zang->exercise;
789  
790 This distribution provides the ability to use virtual actors, which are
791 processes (actors) created on-demand as a result of some system event. This
792 feature is enabled by the L<Zing::Launcher> and L<Zing::Spawner> superclasses.
793  
794 =cut
795  
796 =head1 COMMANDS
797  
798 Given the following process (actor):
799  
800 # in lib/MyApp.pm
801  
802 package MyApp;
803  
804 use parent 'Zing::Single';
805  
806 sub perform {
807 # do something (once)
808 }
809  
810 1;
811  
812 With an application cartridge specifying 4 forks:
813  
814 # in app/myapp
815  
816 ['MyApp', [], 4]
817  
818 The L<zing> command-line application lets you manage Zing applications from the
819 command-line using these commands:
820  
821 =cut
822  
823 =head2 start
824  
825 $ zing start app/myapp
826  
827 The C<start> command loads an application I<cartridge> which returns a
828 L<"scheme"|Zing::Types/scheme> and runs it as a daemon.
829  
830 =cut
831  
832 =head2 stop
833  
834 $ zing stop app/myapp
835  
836 The C<stop> command finds a running application by its PID (process ID) and
837 terminates the process.
838  
839 =cut
840  
841 =head2 logs
842  
843 $ zing logs --level fatal
844  
845 The C<logs> command taps the centralized log source and outputs new events to
846 STDOUT (standard output).
847  
848 =cut
849  
850 =head1 SEE ALSO
851  
852 L<The Actor Model|https://en.wikipedia.org/wiki/Actor_model>
853  
854 L<Concurrent Computation|https://www.amazon.com/Actors-Concurrent-Computation-Distributed-Systems/dp/026251141X>
855  
856 L<Concurrency in Go/Erlang|https://www.youtube.com/watch?v=2yiKUIDFc2I>
857  
858 L<The Akka Project|https://github.com/akka/akka>
859  
860 L<The Actorkit Project|https://github.com/influx6/actorkit>
861  
862 L<The Orleans Project|http://dotnet.github.io/orleans>
863  
864 L<The Pyakka Project|https://github.com/jodal/pykka>
865  
866 L<The Reactive Manifesto|http://www.reactivemanifesto.org>
867  
868 =head1 DISCLOSURES
869  
870 The following is a list of all the known ways Zing is not like a traditional
871 actor-model system:
872  
873 =over 4
874  
875 =item *
876  
877 In Zing, actors act independently and aren't beholden to a system manager.
878  
879 =item *
880  
881 In Zing, actors are always active (each runs its own infinite event-loop).
882  
883 =item *
884  
885 In Zing, actors can communicate unrestricted (no approved communicators).
886  
887 =item *
888  
889 In Zing, actors can block using C<poll> but do not block by default.
890  
891 =item *
892  
893 In Zing, the system responsible for persistence and atomicity is pluggable and
894 as such is subject to the guarantees and limitations of that underlying system.
895 Data serialization, e.g. L<JSON|https://json.org>, is also pluggable.
896  
897 =back
898  
899 =cut
900  
901 =head1 AUTHOR
902  
903 Al Newkirk, C<awncorp@cpan.org>
904  
905 =head1 LICENSE
906  
907 Copyright (C) 2011-2019, Al Newkirk, et al.
908  
909 This is free software; you can redistribute it and/or modify it under the terms
910 of the The Apache License, Version 2.0, as elucidated in the L<"license
911 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
912  
913 =head1 PROJECT
914  
915 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
916  
917 L<Project|https://github.com/iamalnewkirk/zing>
918  
919 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
920  
921 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
922  
923 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
924  
925 L<Issues|https://github.com/iamalnewkirk/zing/issues>
926  
927 =cut