Remote.pm

filter: Statementstmt 82.3% 28 / 34 Branchbran n/a Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.9% 10 / 13 Podpod 33.3% 1 / 3 totaltotal 74.5% 41 / 55 CC 3 SCAR 2.3
CC
3
Cov
81%
current_loop
2.1
current_loop
2.1
__ANON__
1.3
CRAP 1.3
« Remote/GlobProxy.pm ↑ Summary Remote/Tied.pm »
1 package Object::Remote;
2  
3 26 ▶use Object::Remote::MiniLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Object::Remote::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Object::Remote::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Object::Remote::Logging qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 our $VERSION = '0.005003'; # v0.5.3
10  
11 20 ▶sub new::on {
Subroutine on: called CC=1 SCAR=1.6
12 0 ▶ my ($class, $on, @args) = @_;
Subroutine on: not called CC=1 SCAR=1.6
13 0 my $conn = __PACKAGE__->connect($on);
14 0 ▶ log_trace { sprintf("constructing instance of $class on connection for child pid of %i", $conn->child_pid) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
15 0 return $conn->remote_object(class => $class, args => \@args);
16 }
17  
18 sub can::on {
19 2 ▶ my ($class, $on, $name) = @_;
Subroutine on: called CC=1 SCAR=0.1
20 2 my $conn = __PACKAGE__->connect($on);
21 2 ▶ log_trace { "Invoking remote \$class->can('$name')" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
22 2 return $conn->remote_sub(join('::', $class, $name));
23 }
24  
25 sub new {
26 1 ▶ shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
27 1 Object::Remote::Handle->new(@_)->proxy;
28 }
29  
30 sub connect {
31 28 ▶ my ($class, $to, @args) = @_;
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
32 28 use_module('Object::Remote::Connection')->maybe::start::new_from_spec($to, @args);
33 }
34  
35 sub current_loop {
36 729 ▶ our $Current_Loop ||= Object::Remote::Loop->new;
Subroutine current_loop: called CC=2 SCAR=7.2
Pod undocumented
Condition66%2/3
our $Current_Loop ||= "Object::Remote::Loop"->new
l!l&&r!l&&!r
716130
MC/DC0%0/2
our $Current_Loop ||= "Object::Remote::Loop"->new
our $Current_Loop "Object::Remote::Loop"->new
Truth table66%2/3
A ||= B
A= our $Current_LoopB= "Object::Remote::Loop"->new
ABresult
1X1
011
000
37 }
38  
39 1;
40  
41 =head1 NAME
42  
43 Object::Remote - Call methods on objects in other processes or on other hosts
44  
45 =head1 SYNOPSIS
46  
47 Creating a connection:
48  
49 use Object::Remote;
50  
51 my $conn = Object::Remote->connect('myserver'); # invokes ssh
52  
53 Calling a subroutine:
54  
55 my $capture = IPC::System::Simple->can::on($conn, 'capture');
56  
57 warn $capture->('uptime');
58  
59 Using an object:
60  
61 my $eval = Eval::WithLexicals->new::on($conn);
62  
63 $eval->eval(q{my $x = `uptime`});
64  
65 warn $eval->eval(q{$x});
66  
67 Importantly: 'myserver' only requires perl 5.8+ - no non-core modules need to
68 be installed on the far side, Object::Remote takes care of it for you!
69  
70 =head1 DESCRIPTION
71  
72 Object::Remote allows you to create an object in another process - usually
73 one running on another machine you can connect to via ssh, although there
74 are other connection mechanisms available.
75  
76 The idea here is that in many cases one wants to be able to run a piece of
77 code on another machine, or perhaps many other machines - but without having
78 to install anything on the far side.
79  
80 =head1 COMPONENTS
81  
82 =head2 Object::Remote
83  
84 The "main" API, which provides the L</connect> method to create a connection
85 to a remote process/host, L</new::on> to create an object on a connection,
86 and L</can::on> to retrieve a subref over a connection.
87  
88 =head2 Object::Remote::Connection
89  
90 The object representing a connection, which provides the
91 L<Object::Remote::Connection/remote_object> and
92 L<Object::Remote::Connection/remote_sub> methods that are used by
93 L</new::on> and L</can::on> to return proxies for objects and subroutines
94 on the far side.
95  
96 =head2 Object::Remote::Future
97  
98 Code for dealing with asynchronous operations, which provides the
99 L<Object::Remote::Future/start::method> syntax for calling a possibly
100 asynchronous method without blocking, and
101 L<Object::Remote::Future/await_future> and L<Object::Remote::Future/await_all>
102 to block until an asynchronous call completes or fails.
103  
104 =head1 METHODS
105  
106 =head2 connect
107  
108 my $conn = Object::Remote->connect('-'); # fork()ed connection
109  
110 my $conn = Object::Remote->connect('myserver'); # connection over ssh
111  
112 my $conn = Object::Remote->connect('user@myserver'); # connection over ssh
113  
114 my $conn = Object::Remote->connect('root@'); # connection over sudo
115  
116 =head2 new::on
117  
118 my $eval = Eval::WithLexicals->new::on($conn);
119  
120 my $eval = Eval::WithLexicals->new::on('myserver'); # implicit connect
121  
122 my $obj = Some::Class->new::on($conn, %args); # with constructor arguments
123  
124 =head2 can::on
125  
126 my $hostname = Sys::Hostname->can::on($conn, 'hostname');
127  
128 my $hostname = Sys::Hostname->can::on('myserver', 'hostname');
129  
130 =head1 ENVIRONMENT
131  
132 =over 4
133  
134 =item OBJECT_REMOTE_PERL_BIN
135  
136 When starting a new Perl interpreter the contents of this environment
137 variable will be used as the path to the executable. If the variable
138 is not set the path is 'perl'
139  
140 =item OBJECT_REMOTE_LOOP
141  
142 Forces the loop implementation to the named module. Defaults to
143 C< Object::Remote::MiniLoop >; setting this value to
144 C< IO::Async::Loop > integrates Object::Remote with IO::Async.
145  
146 =item OBJECT_REMOTE_LOG_LEVEL
147  
148 Setting this environment variable will enable logging and send all log messages
149 at the specfied level or higher to STDERR. Valid level names are: trace debug
150 verbose info warn error fatal
151  
152 =item OBJECT_REMOTE_LOG_FORMAT
153  
154 The format of the logging output is configurable. By setting this environment variable
155 the format can be controlled via printf style position variables. See
156 L<Object::Remote::Logging::Logger>.
157  
158 =item OBJECT_REMOTE_LOG_FORWARDING
159  
160 Forward log events from remote connections to the local Perl interpreter. Set to 1 to enable
161 this feature which is disabled by default. See L<Object::Remote::Logging>.
162  
163 =item OBJECT_REMOTE_LOG_SELECTIONS
164  
165 Space separated list of class names to display logs for if logging output is enabled. Default
166 value is "Object::Remote::Logging" which selects all logs generated by Object::Remote.
167 See L<Object::Remote::Logging>.
168  
169 =back
170  
171 =head1 KNOWN ISSUES
172  
173 =over 4
174  
175 =item Windows support
176  
177 Windows is not supported: it lacks support for non-blocking I/O for pipes.
178  
179 =item Large data structures
180  
181 Object::Remote communication is encapsalated with JSON and values passed to remote objects
182 will be serialized with it. When sending large data structures or data structures with a lot
183 of deep complexity (hashes in arrays in hashes in arrays) the processor time and memory requirements
184 for serialization and deserialization can be either painful or unworkable. During times of
185 serialization the local or remote nodes will be blocked potentially causing all remote
186 interpreters to block as well under worse case conditions.
187  
188 To help deal with this issue it is possible to configure resource ulimits for a Perl interpreter
189 that is executed by Object::Remote. See C<Object::Remote::Role::Connector::PerlInterpreter>
190 for details on the perl_command attribute.
191  
192 =item User can starve run loop of execution opportunities
193  
194 The Object::Remote run loop is responsible for performing I/O and managing timers in a cooperative
195 multitasing way but it can only do these tasks when the user has given control to Object::Remote.
196 There are times when Object::Remote must wait for the user to return control to the run loop and
197 during these times no I/O can be performed and no timers can be executed.
198  
199 As an end user of Object::Remote if you depend on connection timeouts, the watch dog or timely
200 results from remote objects then be sure to hand control back to Object::Remote as soon as you
201 can.
202  
203 =item Run loop favors certain filehandles/connections
204  
205 =item High levels of load can starve timers of execution opportunities
206  
207 These are issues that only become a problem at large scales. The end result of these two
208 issues is quite similar: some remote objects may block while the local run loop is either busy
209 servicing a different connection or is not executing because control has not yet been returned to
210 it. For the same reasons timers may not get an opportunity to execute in a timely way.
211  
212 Internally Object::Remote uses timers managed by the run loop for control tasks. Under
213 high load the timers can be preempted by servicing I/O on the filehandles and execution
214 can be severely delayed. This can lead to connection watchdogs not being updated or connection
215 timeouts taking longer than configured.
216  
217 =item Deadlocks
218  
219 Deadlocks can happen quite easily because of flaws in programs that use Object::Remote or
220 Object::Remote itself so the C<Object::Remote::WatchDog> is available. When used the run
221 loop will periodically update the watch dog object on the remote Perl interpreter. If the
222 watch dog goes longer than the configured interval with out being updated then it will
223 terminate the Perl process. The watch dog will terminate the process even if a deadlock
224 condition has occurred.
225  
226 =item Log forwarding at scale can starve timers of execution opportunities
227  
228 Currently log forwarding can be problematic at large scales. When there is a large
229 amount of log events the load produced by log forwarding can be high enough that it starves
230 the timers and the remote object watch dogs (if in use) don't get updated in timely way
231 causing them to erroneously terminate the Perl process. If the watch dog is not in use
232 then connection timeouts can be delayed but will execute when load settles down enough.
233  
234 Because of the load related issues Object::Remote disables log forwarding by default.
235 See C<Object::Remote::Logging> for information on log forwarding.
236  
237 =item IO::Async::Loop fails on nested connections
238  
239 The tests C< t/bridged.t > and C< t/chained.t > fail when using the IO::Async loop,
240 meaning that using remote objects from the remote fails under IO::Async. (Fixes welcome!)
241  
242 =back
243  
244 =head1 SUPPORT
245  
246 IRC: #web-simple on irc.perl.org
247  
248 =head1 AUTHOR
249  
250 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
251  
252 =head1 CONTRIBUTORS
253  
254 bfwg - Colin Newell (cpan:NEWELLC) <colin.newell@gmail.com>
255  
256 phaylon - Robert Sedlacek (cpan:PHAYLON) <r.sedlacek@shadowcat.co.uk>
257  
258 triddle - Tyler Riddle (cpan:TRIDDLE) <t.riddle@shadowcat.co.uk>
259  
260 =head1 SPONSORS
261  
262 Parts of this code were paid for by
263  
264 Socialflow L<http://www.socialflow.com>
265  
266 Shadowcat Systems L<http://www.shadow.cat>
267  
268 =head1 COPYRIGHT
269  
270 Copyright (c) 2012 the Object::Remote L</AUTHOR>, L</CONTRIBUTORS> and
271 L</SPONSORS> as listed above.
272  
273 =head1 LICENSE
274  
275 This library is free software and may be distributed under the same terms
276 as perl itself.
277  
278 =cut
« Remote/GlobProxy.pm ↑ Summary Remote/Tied.pm »