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