use Moo;
our $DEBUG = !!$ENV{OBJECT_REMOTE_DEBUG};
+#numbering each connection allows it to be
+#tracked along with file handles in
+#the logs
+BEGIN { our $NEXT_CONNECTION_ID = 0 }
+has _id => ( is => 'ro', required => 1, default => sub { our $NEXT_CONNECTION_ID++ } );
has send_to_fh => (
is => 'ro', required => 1,
is => 'ro', required => 1,
trigger => sub {
my ($self, $ch) = @_;
+ Dlog_trace { "trigger for read_channel has been invoked for connection $_" } $self->_id;
weaken($self);
$ch->on_line_call(sub { $self->_receive(@_) });
$ch->on_close_call(sub { $self->on_close->done(@_) });
has on_close => (
is => 'ro', default => sub { CPS::Future->new },
- trigger => sub {
+ trigger => sub {
my ($self, $f) = @_;
+ Dlog_trace { "trigger for on_close has been invoked for connection $_" } $self->_id;
weaken($self);
$f->on_done(sub {
$self->_fail_outstanding("Connection lost: ".($f->get)[0]);
sub _fail_outstanding {
my ($self, $error) = @_;
+ Dlog_debug { "Failing outstanding futures with '$error' for connection $_" } $self->_id;
my $outstanding = $self->outstanding_futures;
$_->fail($error) for values %$outstanding;
%$outstanding = ();
sub _id_to_remote_object {
my ($self, $id) = @_;
+ Dlog_trace { "fetching proxy for remote object with id '$id' for connection $_" } $self->_id;
return bless({}, 'Object::Remote::Null') if $id eq 'NULL';
(
$self->remote_objects_by_id->{$id}
sub new_from_spec {
my ($class, $spec) = @_;
return $spec if blessed $spec;
+ Dlog_debug { "creating a new connection from spec" };
foreach my $poss (do { our @Guess }) {
if (my $conn = $poss->($spec)) {
+ #Dlog_debug { my $id = $conn->_id; "created connection $id for spec $_" } $spec;
return $conn->maybe::start::connect;
}
}
sub connect {
my ($self, $to) = @_;
- Dlog_debug { "Creating connection to remote node $_" } $to;
+ Dlog_debug { "Creating connection to remote node '$to' for connection $_" } $self->_id;
return await_future(
$self->send_class_call(0, 'Object::Remote', connect => $to)
);
sub remote_sub {
my ($self, $sub) = @_;
my ($pkg, $name) = $sub =~ m/^(.*)::([^:]+)$/;
- log_debug { "Invoking remote sub '$sub'" };
+ Dlog_debug { "Invoking remote sub '$sub' for connection $_" } $self->_id;
return await_future($self->send_class_call(0, $pkg, can => $name));
}
sub send_class_call {
my ($self, $ctx, @call) = @_;
- log_trace { "Sending a non-blocking class call" };
+ Dlog_trace { "Sending a class call for connection $_" } $self->_id;
$self->send(call => class_call_handler => $ctx => call => @call);
}
sub register_remote {
my ($self, $remote) = @_;
- log_trace { my $i = $remote->id; "Registered a remote object with id of '$i'" };
+ Dlog_trace { my $i = $remote->id; "Registered a remote object with id of '$i' for connection $_" } $self->_id;
weaken($self->remote_objects_by_id->{$remote->id} = $remote);
return $remote;
}
sub send_free {
my ($self, $id) = @_;
- log_debug { "sending request to free object '$id'" };
+ Dlog_debug { "sending request to free object '$id' for connection $_" } $self->_id;
delete $self->remote_objects_by_id->{$id};
$self->_send([ free => $id ]);
}
sub _send {
my ($self, $to_send) = @_;
my $fh = $self->send_to_fh;
+ Dlog_trace { "Starting to serialize data in argument to _send for connection $_" } $self->_id;
my $serialized = $self->_serialize($to_send)."\n";
- Dlog_debug { my $l = length($serialized); "Sending '$l' characters of serialized data to $_" } $fh;
+ Dlog_debug { my $l = length($serialized); "serialization is completed; sending '$l' characters of serialized data to $_" } $fh;
#TODO this is very risky for deadlocks unless it's set to non-blocking and then with out extra
#logic it could easily do short-writes to the remote side
my $ret = print $fh $serialized;
sub _serialize {
my ($self, $data) = @_;
local our @New_Ids = (-1);
+ Dlog_debug { "starting to serialize data for connection $_" } $self->_id;
return eval {
my $flat = $self->_encode($self->_deobjectify($data));
warn "$$ >>> ${flat}\n" if $DEBUG;
sub _receive {
my ($self, $flat) = @_;
warn "$$ <<< $flat\n" if $DEBUG;
+ Dlog_trace { my $l = length($flat); "Starting to deserialize $l characters of data for connection $_" } $self->_id;
my ($type, @rest) = eval { @{$self->_deserialize($flat)} }
or do { warn "Deserialize failed for ${flat}: $@"; return };
+ Dlog_trace { "deserialization complete for connection $_" } $self->_id;
eval { $self->${\"receive_${type}"}(@rest); 1 }
or do { warn "Receive failed for ${flat}: $@"; return };
return;
sub receive_free {
my ($self, $id) = @_;
+ Dlog_trace { "got a receive_free for object '$id' for connection $_" } $self->_id;
delete $self->local_objects_by_id->{$id}
or warn "Free: no such object $id";
return;
sub receive_call {
my ($self, $future_id, $id, @rest) = @_;
+ Dlog_trace { "got a receive_call for object '$id' for connection $_" } $self->_id;
my $future = $self->_id_to_remote_object($future_id);
$future->{method} = 'call_discard_free';
my $local = $self->local_objects_by_id->{$id}
sub receive_call_free {
my ($self, $future, $id, @rest) = @_;
+ Dlog_trace { "got a receive_call_free for object '$id' for connection $_" } $self->_id;
$self->receive_call($future, $id, undef, @rest);
$self->receive_free($id);
}
sub _invoke {
my ($self, $future, $local, $ctx, $method, @args) = @_;
+ Dlog_trace { "got _invoke for a method named '$method' for connection $_" } $self->_id;
if ($method =~ /^start::/) {
my $f = $local->$method(@args);
$f->on_done(sub { undef($f); $future->done(@_) });
use Scalar::Util qw(blessed weaken);
use Module::Runtime qw(use_module);
use Object::Remote;
+use Object::Remote::Logging qw( :log :dlog );
use IO::Socket::UNIX;
use POSIX ();
use Moo;
},
trigger => sub {
my ($self, $fh) = @_;
+ log_debug { "adding connection server to run loop because the trigger has executed" };
weaken($self);
Object::Remote->current_loop
->watch_io(
);
sub BUILD {
+ log_debug { "A connection server has been built; calling want_run on run loop" };
Object::Remote->current_loop->want_run;
}
sub run {
+ log_debug { "Connection server is calling run_while_wanted on the run loop" };
Object::Remote->current_loop->run_while_wanted;
}
sub _listen_ready {
my ($self, $fh) = @_;
+ log_debug { "Got a connection, calling accept on the file handle" };
my $new = $fh->accept or die "Couldn't accept: $!";
+ log_trace { "Setting file handle non-blocking" };
$new->blocking(0);
my $f = CPS::Future->new;
+ log_trace { "Creating a new connection with the remote node" };
my $c = use_module('Object::Remote::Connection')->new(
receive_from_fh => $new,
send_to_fh => $new,
@{$self->connection_args}
)->${\$self->connection_callback};
$f->on_ready(sub { undef($c) });
+ log_trace { "marking the future as done" };
$c->ready_future->done;
#TODO see if this runs on the controller or the remote node
#if this runs on the controller a poorly behaved remote node
#could cause the print() to block but it's a very low probability
+ Dlog_trace { "Sending 'Shere' to socket $_" } $new;
print $new "Shere\n" or die "Couldn't send to new socket: $!";
+ log_debug { "Connection has been fully handled" };
return $c;
}
sub DEMOLISH {
my ($self, $gd) = @_;
+ log_debug { "A connection server is being destroyed; global destruction: '$gd'" };
return if $gd;
+ log_trace { "Removing the connection server IO watcher from run loop" };
Object::Remote->current_loop
->unwatch_io(
handle => $self->listen_on,
on_read_ready => 1
);
if ($self->listen_on->can('hostpath')) {
+ log_debug { my $p = $self->listen_on->hostpath; "Removing '$p' from the filesystem" };
unlink($self->listen_on->hostpath);
}
+ log_trace { "calling want_stop on the run loop" };
Object::Remote->current_loop->want_stop;
}
no warnings 'once';
+BEGIN { }
+
push @Object::Remote::Connection::Guess, sub {
if (($_[0]||'') eq '-') { __PACKAGE__->new }
};
#TODO is there a specific reason sysread() and syswrite() aren't
#a part of ::MiniLoop? It's one spot to handle errors and other
#logic involving filehandles
- log_debug { "LocalSudo: Preparing to read data" };
- if (sysread($sudo_stderr, my $buf, 1024) > 0) {
+ Dlog_debug { "LocalSudo: Preparing to read data from $_" } $sudo_stderr;
+ if (sysread($sudo_stderr, my $buf, 32768) > 0) {
log_trace { "LocalSudo: successfully read data, printing it to STDERR" };
print STDERR $buf;
log_trace { "LocalSudo: print() to STDERR is done" };
use strictures 1;
use Object::Remote;
+use Object::Remote::Logging qw ( :log );
our %Modules;
our %Not_Loaded_Yet;
sub import {
my $target = caller;
+ log_trace { "import has been invoked by '$target' on " . __PACKAGE__ };
return if $Seen{$target};
+ log_debug { "'$target' has not yet loaded " . __PACKAGE__ };
$Seen{$target} = $Not_Loaded_Yet{$target} = 1;
}
sub flush_loaded {
+ log_debug { "flushing the loaded classes" };
foreach my $key (keys %Not_Loaded_Yet) {
+ log_trace { "flushing '$key'" };
my $data_fh = do { no strict 'refs'; *{"${key}::DATA"} };
my $data = do { local $/; <$data_fh> };
my %modules = reverse(
@Modules{keys %modules} = values %modules;
delete $Not_Loaded_Yet{$key};
}
+ log_trace { "done flushing loaded classes" };
}
sub find_module {
use warnings;
use base qw(Exporter);
-use Object::Remote::Logging qw( :log );
+use Object::Remote::Logging qw( :log Dlog_trace );
use CPS::Future;
use Object::Remote::Proxy;
use Scalar::Util qw(weaken blessed);
+use Object::Remote::Logging qw ( :log );
use Object::Remote::Future;
#must find way to exclude certain log events
#from being forwarded - log events generated in
sub BUILD {
my ($self, $args) = @_;
-# log_debug { "constructing instance of " . ref($self) };
+ log_debug { "constructing remote handle" };
if ($self->id) {
+ log_trace { "disaming free for this hanle" };
$self->disarm_free;
} else {
die "No id supplied and no class either" unless $args->{class};
ref($_) eq 'HASH' and $_ = [ %$_ ] for $args->{args};
+ log_trace { "fetching id for handle and disarming free on remote side" };
$self->_set_id(
await_future(
$self->connection->send_class_call(
)->{remote}->disarm_free->id
);
}
-# log_trace { "finished constructing " . ref($self) };
+ log_trace { "finished constructing remote handle; registering it" . ref($self) };
$self->connection->register_remote($self);
}
sub call {
my ($self, $method, @args) = @_;
my $w = wantarray;
+ log_debug { my $def = defined $w; "call() has been invoked on a remote handle; wantarray: '$def'" };
$method = "start::${method}" if (caller(0)||'') eq 'start';
future {
$self->connection->send(call => $self->id, $w, $method, @args)
sub call_discard {
my ($self, $method, @args) = @_;
+ log_trace { "invoking send_discard() with 'call' for method '$method' on connection for remote handle" };
$self->connection->send_discard(call => $self->id, $method, @args);
}
sub call_discard_free {
my ($self, $method, @args) = @_;
$self->disarm_free;
+ log_trace { "invoking send_discard() with 'call_free' for method '$method' on connection for remote handle" };
$self->connection->send_discard(call_free => $self->id, $method, @args);
}
sub DEMOLISH {
my ($self, $gd) = @_;
+ log_trace { "Demolishing remote handle" };
return if $gd or $self->disarmed_free;
$self->connection->send_free($self->id);
}
#this info to the selector is a good feature
local($_) = { level => $level, package => $caller };
if ($selector->(@values)) {
- #TODO resolve caller_level issues with routing
+ #SOLVED resolve caller_level issues with routing
#idea: the caller level will differ in distance from the
#start of the call stack but it's a constant distance from
#the end of the call stack - can that be exploited to calculate
#it can be invoked in that location and the caller level
#problem doesn't exist anymore
$logger = $logger->($caller, { caller_level => -1 });
-
+
+ #TODO there is a known issue with the interaction of this
+ #routed logging scheme and objects proxied with Object::Remote.
+ #Specifically the loggers must be invoked with a calling
+ #depth of 0 which isn't possible using a logger that has
+ #been proxied which is what happens with routed logging
+ #if the logger is created in one Perl interpreter and the
+ #logging happens in another
$logger->$level($log_meth->(@values))
if $logger->${\"is_$level"};
}
use strictures 1;
use Object::Remote::Connector::STDIO;
-use Object::Remote::Logging qw(:log);
+use Object::Remote::Logging qw(:log :dlog);
use Object::Remote;
use CPS::Future;
sub run {
- log_trace { "run() has been invoked on remote node; creating STDIO connector" };
+ log_trace { "run() has been invoked on remote node" };
my $c = Object::Remote::Connector::STDIO->new->connect;
$c->register_class_call_handler;
$loop->want_stop
});
+ Dlog_trace { "Node is sending 'Shere' to $_" } $c->send_to_fh;
print { $c->send_to_fh } "Shere\n";
log_debug { "Node is going to start the run loop" };
use CPS::Future;
use Scalar::Util qw(weaken);
-use Object::Remote::Logging qw(:log);
+use Object::Remote::Logging qw(:log :dlog);
use Moo;
has fh => (
#there is no actual data to read from the socket
sub _receive_data_from {
my ($self, $fh) = @_;
- log_trace { "Preparing to read data" };
+ Dlog_trace { "Preparing to read data from $_" } $fh;
#use Carp qw(cluck); cluck();
my $rb = $self->_receive_data_buffer;
#TODO is there a specific reason sysread() and syswrite() aren't
#a part of ::MiniLoop? It's one spot to handle errors and other
#logic involving filehandles
- #TODO why are the buffers so small? BUFSIZ is usually 32768
- my $len = sysread($fh, $$rb, 1024, length($$rb));
+ my $len = sysread($fh, $$rb, 32768, length($$rb));
my $err = defined($len) ? '' : ": $!";
if (defined($len) and $len > 0) {
log_trace { "Read $len bytes of data" };
requires '_open2_for';
+#TODO return to 10 seconds after debugging
+#has timeout => (is => 'ro', default => sub { { after => 10 } });
has timeout => (is => 'ro', default => sub { { after => 10 } });
sub connect {
Dlog_trace { "Connection timeout timer has fired for child pid '$child_pid'; is_ready: $_" } $f->is_ready;
unless($f->is_ready) {
log_warn { "Connection with child pid '$child_pid' has timed out" };
- $f->fail("Connection timed out") unless $f->is_ready;
+ $f->fail("Connection timed out") unless $f->is_ready;
}
- #TODO hrm was this supposed to be conditional on the is_ready ?
- #a connection is only good for timeout seconds?
undef($channel);
+
}
);
log_trace { "connection for child pid '$child_pid' has been initialized" };
use Object::Remote::ModuleSender;
use Object::Remote::Handle;
use Object::Remote::Future;
-use Object::Remote::Logging qw( :log );
+use Object::Remote::Logging qw( :log :dlog );
use Scalar::Util qw(blessed);
use Moo::Role;
has perl_command => (is => 'lazy');
-sub _build_perl_command { [ 'perl', '-' ] }
+#TODO convert nice value into optional feature enabled by
+#setting value of attribute
+#ulimit of ~500 megs of v-ram
+#TODO only works with ssh with quotes but only works locally
+#with out quotes
+sub _build_perl_command { [ 'sh', '-c', '"ulimit -v 500000; nice -n 15 perl -"' ] }
+#sub _build_perl_command { [ 'perl', '-' ] }
around connect => sub {
my ($orig, $self) = (shift, shift);
sub _start_perl {
my $self = shift;
+ Dlog_debug { "invoking connection to perl interpreter using command line: $_" } @{$self->final_perl_command};
my $pid = open2(
my $foreign_stdout,
my $foreign_stdin,
@{$self->final_perl_command},
) or die "Failed to run perl at '$_[0]': $!";
+ Dlog_trace { "Connection to remote side successful; remote stdin and stdout: $_" } [ $foreign_stdin, $foreign_stdout ];
return ($foreign_stdin, $foreign_stdout, $pid);
}
->watch_io(
handle => $foreign_stdin,
on_write_ready => sub {
- my $len = syswrite($foreign_stdin, $to_send, 4096);
+ my $len = syswrite($foreign_stdin, $to_send, 32768);
if (defined $len) {
substr($to_send, 0, $len) = '';
}