| 1 | package Object::Remote; | ||
| 2 | |||
| 3 | 0 | ▶ | use Object::Remote::MiniLoop; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use Object::Remote::Loop; | ||
| 5 | use Object::Remote::Handle; | ||
| 6 | use Object::Remote::Logging qw( :log ); | ||
| 7 | use Module::Runtime qw(use_module); | ||
| 8 | |||
| 9 | our $VERSION = '0.005004'; # v0.5.4 | ||
| 10 | |||
| 11 | sub new::on { | ||
| 12 | my ($class, $on, @args) = @_; | ||
| 13 | my $conn = __PACKAGE__->connect($on); | ||
| 14 | log_trace { sprintf("constructing instance of $class on connection for child pid of %i", $conn->child_pid) }; | ||
| 15 | return $conn->remote_object(class => $class, args => \@args); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub can::on { | ||
| 19 | my ($class, $on, $name) = @_; | ||
| 20 | my $conn = __PACKAGE__->connect($on); | ||
| 21 | log_trace { "Invoking remote \$class->can('$name')" }; | ||
| 22 | return $conn->remote_sub(join('::', $class, $name)); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | shift; | ||
| 27 | Object::Remote::Handle->new(@_)->proxy; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub connect { | ||
| 31 | my ($class, $to, @args) = @_; | ||
| 32 | use_module('Object::Remote::Connection')->maybe::start::new_from_spec($to, @args); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub current_loop { | ||
| 36 | our $Current_Loop ||= Object::Remote::Loop->new; | ||
| 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 | my $conn = Object::Remote->connect('/run/object-remote.sock'); # connection over Unix socket | ||
| 117 | |||
| 118 | =head2 new::on | ||
| 119 | |||
| 120 | my $eval = Eval::WithLexicals->new::on($conn); | ||
| 121 | |||
| 122 | my $eval = Eval::WithLexicals->new::on('myserver'); # implicit connect | ||
| 123 | |||
| 124 | my $obj = Some::Class->new::on($conn, %args); # with constructor arguments | ||
| 125 | |||
| 126 | =head2 can::on | ||
| 127 | |||
| 128 | my $hostname = Sys::Hostname->can::on($conn, 'hostname'); | ||
| 129 | |||
| 130 | my $hostname = Sys::Hostname->can::on('myserver', 'hostname'); | ||
| 131 | |||
| 132 | =head1 ENVIRONMENT | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item OBJECT_REMOTE_PERL_BIN | ||
| 137 | |||
| 138 | When starting a new Perl interpreter the contents of this environment | ||
| 139 | variable will be used as the path to the executable. If the variable | ||
| 140 | is not set the path is 'perl' | ||
| 141 | |||
| 142 | =item OBJECT_REMOTE_LOOP | ||
| 143 | |||
| 144 | Forces the loop implementation to the named module. Defaults to | ||
| 145 | C< Object::Remote::MiniLoop >; setting this value to | ||
| 146 | C< IO::Async::Loop > integrates Object::Remote with IO::Async. | ||
| 147 | |||
| 148 | =item OBJECT_REMOTE_LOG_LEVEL | ||
| 149 | |||
| 150 | Setting this environment variable will enable logging and send all log messages | ||
| 151 | at the specfied level or higher to STDERR. Valid level names are: trace debug | ||
| 152 | verbose info warn error fatal | ||
| 153 | |||
| 154 | =item OBJECT_REMOTE_LOG_FORMAT | ||
| 155 | |||
| 156 | The format of the logging output is configurable. By setting this environment variable | ||
| 157 | the format can be controlled via printf style position variables. See | ||
| 158 | L<Object::Remote::Logging::Logger>. | ||
| 159 | |||
| 160 | =item OBJECT_REMOTE_LOG_FORWARDING | ||
| 161 | |||
| 162 | Forward log events from remote connections to the local Perl interpreter. Set to 1 to enable | ||
| 163 | this feature which is disabled by default. See L<Object::Remote::Logging>. | ||
| 164 | |||
| 165 | =item OBJECT_REMOTE_LOG_SELECTIONS | ||
| 166 | |||
| 167 | Space separated list of class names to display logs for if logging output is enabled. Default | ||
| 168 | value is "Object::Remote::Logging" which selects all logs generated by Object::Remote. | ||
| 169 | See L<Object::Remote::Logging>. | ||
| 170 | |||
| 171 | =back | ||
| 172 | |||
| 173 | =head1 KNOWN ISSUES | ||
| 174 | |||
| 175 | =over 4 | ||
| 176 | |||
| 177 | =item Windows support | ||
| 178 | |||
| 179 | Windows is not supported: it lacks support for non-blocking I/O for pipes. | ||
| 180 | |||
| 181 | =item Large data structures | ||
| 182 | |||
| 183 | Object::Remote communication is encapsalated with JSON and values passed to remote objects | ||
| 184 | will be serialized with it. When sending large data structures or data structures with a lot | ||
| 185 | of deep complexity (hashes in arrays in hashes in arrays) the processor time and memory requirements | ||
| 186 | for serialization and deserialization can be either painful or unworkable. During times of | ||
| 187 | serialization the local or remote nodes will be blocked potentially causing all remote | ||
| 188 | interpreters to block as well under worse case conditions. | ||
| 189 | |||
| 190 | To help deal with this issue it is possible to configure resource ulimits for a Perl interpreter | ||
| 191 | that is executed by Object::Remote. See C<Object::Remote::Role::Connector::PerlInterpreter> | ||
| 192 | for details on the perl_command attribute. | ||
| 193 | |||
| 194 | =item User can starve run loop of execution opportunities | ||
| 195 | |||
| 196 | The Object::Remote run loop is responsible for performing I/O and managing timers in a cooperative | ||
| 197 | multitasing way but it can only do these tasks when the user has given control to Object::Remote. | ||
| 198 | There are times when Object::Remote must wait for the user to return control to the run loop and | ||
| 199 | during these times no I/O can be performed and no timers can be executed. | ||
| 200 | |||
| 201 | As an end user of Object::Remote if you depend on connection timeouts, the watch dog or timely | ||
| 202 | results from remote objects then be sure to hand control back to Object::Remote as soon as you | ||
| 203 | can. | ||
| 204 | |||
| 205 | =item Run loop favors certain filehandles/connections | ||
| 206 | |||
| 207 | =item High levels of load can starve timers of execution opportunities | ||
| 208 | |||
| 209 | These are issues that only become a problem at large scales. The end result of these two | ||
| 210 | issues is quite similar: some remote objects may block while the local run loop is either busy | ||
| 211 | servicing a different connection or is not executing because control has not yet been returned to | ||
| 212 | it. For the same reasons timers may not get an opportunity to execute in a timely way. | ||
| 213 | |||
| 214 | Internally Object::Remote uses timers managed by the run loop for control tasks. Under | ||
| 215 | high load the timers can be preempted by servicing I/O on the filehandles and execution | ||
| 216 | can be severely delayed. This can lead to connection watchdogs not being updated or connection | ||
| 217 | timeouts taking longer than configured. | ||
| 218 | |||
| 219 | =item Deadlocks | ||
| 220 | |||
| 221 | Deadlocks can happen quite easily because of flaws in programs that use Object::Remote or | ||
| 222 | Object::Remote itself so the C<Object::Remote::WatchDog> is available. When used the run | ||
| 223 | loop will periodically update the watch dog object on the remote Perl interpreter. If the | ||
| 224 | watch dog goes longer than the configured interval with out being updated then it will | ||
| 225 | terminate the Perl process. The watch dog will terminate the process even if a deadlock | ||
| 226 | condition has occurred. | ||
| 227 | |||
| 228 | =item Log forwarding at scale can starve timers of execution opportunities | ||
| 229 | |||
| 230 | Currently log forwarding can be problematic at large scales. When there is a large | ||
| 231 | amount of log events the load produced by log forwarding can be high enough that it starves | ||
| 232 | the timers and the remote object watch dogs (if in use) don't get updated in timely way | ||
| 233 | causing them to erroneously terminate the Perl process. If the watch dog is not in use | ||
| 234 | then connection timeouts can be delayed but will execute when load settles down enough. | ||
| 235 | |||
| 236 | Because of the load related issues Object::Remote disables log forwarding by default. | ||
| 237 | See C<Object::Remote::Logging> for information on log forwarding. | ||
| 238 | |||
| 239 | =item IO::Async::Loop fails on nested connections | ||
| 240 | |||
| 241 | The tests C< t/bridged.t > and C< t/chained.t > fail when using the IO::Async loop, | ||
| 242 | meaning that using remote objects from the remote fails under IO::Async. (Fixes welcome!) | ||
| 243 | |||
| 244 | =back | ||
| 245 | |||
| 246 | =head1 SUPPORT | ||
| 247 | |||
| 248 | IRC: #web-simple on irc.perl.org | ||
| 249 | |||
| 250 | =head1 AUTHOR | ||
| 251 | |||
| 252 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> | ||
| 253 | |||
| 254 | =head1 CONTRIBUTORS | ||
| 255 | |||
| 256 | bfwg - Colin Newell (cpan:NEWELLC) <colin.newell@gmail.com> | ||
| 257 | |||
| 258 | phaylon - Robert Sedlacek (cpan:PHAYLON) <r.sedlacek@shadowcat.co.uk> | ||
| 259 | |||
| 260 | triddle - Tyler Riddle (cpan:TRIDDLE) <t.riddle@shadowcat.co.uk> | ||
| 261 | |||
| 262 | =head1 SPONSORS | ||
| 263 | |||
| 264 | Parts of this code were paid for by | ||
| 265 | |||
| 266 | Socialflow L<http://www.socialflow.com> | ||
| 267 | |||
| 268 | Shadowcat Systems L<http://www.shadow.cat> | ||
| 269 | |||
| 270 | =head1 COPYRIGHT | ||
| 271 | |||
| 272 | Copyright (c) 2012 the Object::Remote L</AUTHOR>, L</CONTRIBUTORS> and | ||
| 273 | L</SPONSORS> as listed above. | ||
| 274 | |||
| 275 | =head1 LICENSE | ||
| 276 | |||
| 277 | This library is free software and may be distributed under the same terms | ||
| 278 | as perl itself. | ||
| 279 | |||
| 280 | =cut | ||