DESCRIPTION

Run this here, that there, etc.

Net::OpenSSH::Parallel is an scheduler that can run commands in parallel in a set of hosts through SSH. It tries to find a compromise between being simple to use, efficient and covering a good part of the problem space of parallel process execution via SSH.

register the hosts where you want to run commands with the "add_host" method

queue the actions you want to run (commands, file copy operations, etc.) using the "push" method.

call the "run" method and let the parallel scheduler take care of everything!

Labelling hosts

Every host is identified by an unique label that is given when the host is registered into the parallel scheduler. Usually, the host name is used also as the label, but this is not required by the module.

The rationale behind using labels is that a hostname does not necessarily identify unique "remote processors" (for instance, sometimes your logical "remote processors" may be user accounts distributed over a set of hosts: foo1@bar1, foo2@bar1, foo3@bar2, ...; a set of hosts that are accesible behind an unique IP, listening in different ports; etc.)

Selecting hosts

Several of the methods of this module (well, currently, just push) accept a selector string to determine which of the registered hosts should be affected by the operation.

For instance, in...

$pssh->push('*', command => 'ls')

the first argument is the selector. The one used here, *, selects all the registered hosts.

Note: I am still considering how the selector mini-language should be, don't hesitate to send your suggestions!

Local resource usage

When the number of hosts managed by the scheduler is too high, the local node can become overloaded.

Roughly, every SSH connection requires two local ssh processes (one to run the SSH connection and another one to launch the remote command) that results in around 5MB of RAM usage per host.

CPU usage varies greatly depending on the tasks carried out. The most expensive are short remote tasks (because of the local process creation and destruction overhead) and tasks that transfer big ammounts of data through SSH (because of the encryption going on).

In practice, CPU usage doesn't matter too much (mostly because the OS would be able to manage it but also because there is not too many things we can do to reduce it) and usually it is RAM about what we should be more concerned.

The module accepts two parameters to limit resource usage:

workers

is the maximun number of remote commands that can be running concurrently.

connections

is the maximum number of SSH connections that can be active concurrently.

In practice, limiting the maximum number of connections indirectly limits RAM usage and limiting the the maximum number of workers indirectly limits CPU usage.

The module requires the maximum number of connections to be at least equal or bigger than the maximun number of workers, and it is recomended that maximum_connections >= 2 * maximum_workers (otherwise the scheduler will not be able to reuse connections efficiently).

You will have to experiment to find out which combinations give the best results in your particular scenarios.

Also, for small sets of hosts you can just let these parameters unlimited.

Variable expansion

This module activates Net::OpenSSHvariable expansion by default. That way, it is possible to easily customize the actions executed on every host in base to some of its properties.

For instance:

$pssh->push('*', scp_get => "/var/log/messages", "messages.%HOST%");

copies the log files appending the name of the remote hosts to the local file names.

The variables HOST, USER, PORT and LABEL are predefined.

Error handling

When something goes wrong (for instance, some host is unreachable, some connection dies, some command fails, etc.) the module can handle the error in several predefined ways as follows:

Ignores the error and continues executing tasks in the host queue as it had never happened.

OSSH_ON_ERROR_ABORT

Aborts the processing on the corresponding host. The error will be propagated to other hosts joining it at any later point once the join is reached.

In other words, this police aborts the queued jobs for this host and any other that has a dependency on it.

OSSH_ON_ERROR_DONE

Similar to OSSH_ON_ERROR_ABORT but will not propagate errors to other hosts via joins.

OSSH_ON_ERROR_ABORT_ALL

Causes all the host queues to be aborted as soon as possible (and that usually means after currently running actions end).

OSSH_ON_ERROR_REPEAT

The module will try to perform the current task again and again until it succeeds. This police can lead to an infinite loop and so its direct usage is discouraged (but see the following point about setting the policy dynamically).

The default policy is OSSH_ON_ERROR_ABORT.

Setting the policy dynamically

When a subroutine reference is used as the policy instead of the any of the constants previously described, the given subroutine will be called on error conditions as follows:

$on_error->($pssh, $label, $error, $task)

$pssh is a reference to the Net::OpenSSH::Parallel object, $label is the label associated to the host where the error happened. $error is the error type as defined in Net::OpenSSH::Parallel::Constants and $task is a reference to the task that was being carried out.

The return value of the subroutine must be one of the described constants and the corresponding policy will be applied.

Retrying connection errors

If the module fails when trying to stablish a new SSH connection or when an existing connection dies unexpectedly, the option reconnections can be used to instruct the module to retry the connection until it succeds or the given maximun is reached.

The join makes server_A to wait for the scp_get operation queued in server_B to finish before proceeding with the scp_put.

In general the join will make the selected servers wait for any task queued on the servers matched by $selector to finish before proceeding with the next queued tasks.

One common usage is to synchronize all servers at some point:

$ssh->push('*', join => '*');

By default, errors are propagated at joins. For instance, in the example above, if the scp_get operation queued on server_B failed, it would abort any further operation queued on server_B and any further operation queued after the join in server_A. See also "Error handling".

here => $tag

Push a tag in the stack that can be used as a target for goto operations.

goto => $target

Jumps forward until the given here tag is reached.

Joins to other hosts queues will be ignored, and joins from other queues to this one will be succesfully fulfilled. For instance:

TODO

when connecting fails, do not try to reconnect inmediately but after some predefined period

rationalize debugging

currently it is a mess

add loggin support

log the operations performed in a given file

stdio redirection

add support for better handling of the Net::OpenSSH stdio redirection facilities

configurable valid return codes

Non zero exit code is not always an error.

BUGS AND SUPPORT

This module should be considered beta quality, everything seems to work but it may yet contain critical bugs.

If you find any, report it via http://rt.cpan.org or by email (to sfandino@yahoo.com), please.

Feedback and comments are also welcome!

The 'sub' and 'parsub' features should be considered experimental and its API or behaviour could be changed in future versions of the module.

Reporting bugs

In order to report a bug, write a minimal program that triggers it and place the following line at the beggining:

$Net::OpenSSH::Parallel::debug = -1;

Then, send me (via rt or email) the debugging output you get when you run it. Include also the source code of the script, a description of what is going wrong and the details of your OS and the versions of Perl, Net::OpenSSH and Net::OpenSSH::Parallel you are using.

Development version

Commercial support

Commercial support, professional services and custom software development around this module are available through my current company. Drop me an email with a rough description of your requirements and we will get back to you ASAP.

COPYRIGHT AND LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.