###############################################################################
#                                                                             #
#  TakTuk, a middleware for adaptive large scale parallel remote executions   #
#  deployment. Perl implementation, copyright(C) 2006 Guillaume Huard.        #
#                                                                             #
#  This program is free software; you can redistribute it and/or modify       #
#  it under the terms of the GNU General Public License as published by       #
#  the Free Software Foundation; either version 2 of the License, or          #
#  (at your option) any later version.                                        #
#                                                                             #
#  This program is distributed in the hope that it will be useful,            #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of             #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
#  GNU General Public License for more details.                               #
#                                                                             #
#  You should have received a copy of the GNU General Public License          #
#  along with this program; if not, write to the Free Software                #
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA #
#                                                                             #
#  Contact: Guillaume.Huard@imag.fr                                           #
#           ENSIMAG - Laboratoire ID                                          #
#           51 avenue Jean Kuntzmann                                          #
#           38330 Montbonnot Saint Martin                                     #
#                                                                             #
###############################################################################

=pod TakTuk documentation

=begin html

<center><h1>USER MANUAL</h1></center>

=end html

=head1 NAME

B<TakTuk> - a tool for large scale remote execution deployment

=head1 SYNOPSIS

B<taktuk> [B<-hinsvP>] [B<-c> connector] [B<-d> limit] [B<-f> filename]
[B<-g> duration] [B<-l> login] [B<-m> hostname [B<-[> args ... B<-]>]]
[B<-o> stream=template] [B<-t> timeout] [B<-w> size] [B<-C> separators]
[B<-E> character] [B<-F> filename] [B<-L> hostname] [B<-O> separators]
[B<-R> stream=descriptor] [B<-S> files] [B<-T> command] [B<-W> scheme]
[commands ... ]

=head1 DESCRIPTION

B<TakTuk> is a tool for broadcasting the remote execution of one ore more
commands to a set of one or more distant machines. B<TakTuk> combines local
parallelization (using concurrent deployment processes) and work distribution
(using an adaptive work-stealing algorithm) to achieve both
B<scalability> and B<efficiency>.

B<TakTuk> is especially suited to interactive tasks involving several distant 
machines and parallel remote executions. This is the case of clusters 
administration and parallel program debugging.

B<TakTuk> also provides a basic communication layer to programs it executes.
This communication layer uses the communication infrastructure set up by
B<TakTuk> during its deployment. It is available both for the Perl and the C
langages and is described in C<taktukcomm(3)>.

=head1 OPTIONS

Caution, in B<TakTuk> options are parsed in the order given on the comand line.
This means that B<TakTuk> is not POSIX compliant regarding options order.  This
is important as some of the options change the behavior of following ones (and
only these ones - e.g. B<-l> applies to following B<-m> options). The defaults
settings of B<TakTuk> can be obtained by using the C<--print-defaults> option.
The following options are given by category in alphabetical order.

=head1 DEPLOYMENT OPTIONS

=over

=item B<-c> connector

=item B<--connector> connector

defines the connector command used to contact the following remote machines.

=item B<-d> limit

=item B<--dynamic> limit

turns dynamic mode (work stealing) on or off for the deployment of all the
following remote machines specifications. If C<limit> is positive or null,
B<TakTuk> uses the dynamic mode with its value as a maximal arity (0 =
no maximal arity).
A negative value for C<limit> turns dynamic mode off.
B<Warning>,
currently it is a bad idea to use several -d options on the same command line.

=item B<-f> filename

=item B<--machines-file> filename

C<filename> is the name of a file that contains remote machines names
(equivalent to several C<-m> opions). In each line of this file, the first word
is considered as a machine name and the remaining as comments.

=item B<-l> login

=item B<--login> login

sets the login name for the following hosts. This assumes that the connector
command accepts C<-l> option (this is the case for ssh, the default).

=item B<-m> hostname

=item B<--machine> hostname

C<hostname> is the name of a remote machine on which B<TakTuk> has to be
deployed. C<hostname> might contain list of values specified inside brackets
and exclusion lists (see L<HOSTNAMES SPECIFICATION>). In such case, the
depolyment will be performed on all the matching hosts.
This option can be followed by any number of arguments enclosed
between C<-[> and C<-]> which will be transmitted as arguments of the remote
B<TakTuk> that will execute on all the target hosts.

=item B<-s>

=item B<--self-propagate>

propagates the B<TakTuk> executable through the following connectors and thus
eliminates the need for a B<TakTuk> installation on remote machines.

=item B<-z>

=item B<--dont-self-propagate>

cancels a preceding B<-s> option.

=item B<-F> filename

=item B<--args-file> filename

C<filename> is the name of a file that contains additional options for B<TakTuk>. The parsing of the file is done when encountering this option.

=item B<-L> hostname

=item B<--localhost> hostname

changes the name of localhost as viewed by B<TakTuk>.

=item B<-S> files

=item B<--send-files> files

gives to B<TakTuk> a list of files to be sent upon successfull remote
connection. Use with caution as it will slow down deployment (the files are
transfered before the end of the deployment). The intended use is NOT to
transfer application data files but rather files that are necessary to perform
further connections from the remote node.  C<files> is a comma separated list
of pairs C<local_file:remote_file>. It behaves as the C<put> command.

=item B<-T> command

=item B<--taktuk-command> command

C<command> is the name of the B<TakTuk> command (default is $0, the name used to
launch B<TakTuk>). This is the command used once connected to initiate the
remote B<TakTuk> engine. Note that
the effect of this switch is void when using the C<-s> option.

=back

=head1 COMMAND LINE PARSING OPTIONS

=over

=item B<-C> separators set

=item B<--command-separator> separators set

replaces the default set of characters that separates successive B<TakTuk>
commands.

=item B<-E> character

=item B<--escape-character> character

defines an escape character that can be used to protect any following character
from B<TakTuk> interpretation. This protection applies nearly to the whole
B<TakTuk> command line.

=item B<-O> separators set

=item B<--option-separator> separators set

replaces the default set of characters that separates successive words when
reading options and commands from a file (using the C<-F> switch).

=back

=head1 I/O OPTIONS

=over

=item B<-o> stream=template

=item B<--output-template> stream=template

sets an output template specification for one of output streams designed by the
given name. When giving only a stream name (without template) this disables the
stream. Available streams include :

=over

=item C<connector> (errors from the connector command)

this stream contains in C<$line> informations outputed to stderr by a connector
command. Relevant fields include C<$host>, C<$peer> and others listed below.

=item C<error>, C<output> (error and output of remotely executed commands)

these streams contain in C<$line> informations outputed by executed command to
stdout or stderr (respectively). Relevant fields include C<$host>,
C<$command>, C<$pid> and others listed below.

=item C<info> (general informations such as help summary or version)

this stream contains in C<$line> informations outputed by taktuk in response to
commands such as C<help> or C<version>.

=item C<state> (internal state of TakTuk instance)

this stream contains in C<$line> the state change code matching some B<TakTuk>
events. Codes are the following:

  0 - TakTuk is ready
  1 - TakTuk is numbered
  2 - TakTuk terminated
  3 - connection failed
  4 - connection initialized
  5 - connection lost
  6 - command started
  7 - command failed
  8 - command terminated
  9 - numbering update failed
 10 - pipe input started
 11 - pipe input failed
 12 - pipe input terminated
 13 - file reception started
 14 - file reception failed
 15 - file reception terminated

The function C<event_msg($)> can be used in the template to translate this code
into a string that describes the event.  Relevant fields include C<$host>,
C<$position>, C<$rank>, C<$count> and others listed below.

=item C<status> (commands exit status)

this stream contains in C<$line> the return code of executed commands. Relevant
fields include C<$host>, C<$pid> and others listed below.

=item C<taktuk> (internal messages, warnings and errors)

this stream contains in C<$line> error, warning and debug messages outputed by
B<TakTuk> itself. Relevant fields include C<$line_number>, C<$package> and
others listed below.

=back

a template is a Perl expression that should evaluate to the string
eventually displayed. Within a
specification, some variables might be used depending on the concerned stream
(notice that C<$father_position> and C<$depth> that appeared in 3.2.4 are not
supported anymore and that C<$position> evolved since 3.2.5):

=over

=item C<$command> (not for C<taktuk> nor C<info>)

the command line execution that generated output.

=item C<$count>

the total number of B<TakTuk> instances.

=item C<$eof>

an empty string or a newline depending on what character ended command output.

=item C<$filename> (C<taktuk> only)

the file in which a B<TakTuk> diagnostic function has been called.

=item C<$host>

the name of the host executing the current B<TakTuk> instance

=item C<$init_date> (C<connector> and C<state> only)

the time in seconds since Jan. 1, 1970 at which the remote B<TakTuk> instance
completed its initialization (including ssh connection time and self
propagation if any).

=item C<$level> (C<taktuk> only)

the level of the diagnostic function called.

=item C<$level_name> (C<taktuk> only)

the name associated with the level of the diagnostic function called.

=item C<$line>

the raw data outputed by a command or passed to a diagnostic function, any
trailing newline removed (see $eof).

=item C<$line_number> (C<taktuk> only)

the line number at which a B<TakTuk> diagnostic function has been called.

=item C<$package> (C<taktuk> only)

the package in which a B<TakTuk> diagnostic function has been called.

=item C<$peer> (C<connector> and C<state> only)

the remote machine for which a connector command outputed information.

=item C<$peer_position> (C<connector> and C<state> only)

position on the command line of the remote host. This position is expressed as
a path in the hierarchy of hosts expressed on the command line (dot separated
position number at each level of imbrication).

=item C<$pid> (not for C<taktuk> nor C<info>)

the pid of the command line executed (that produced output).

=item C<$position>

position on the command line of the current host. This position is expressed as
a path in the hierarchy of hosts expressed on the command line (dot separated
position number at each level of imbrication).

=item C<$rank>

the logical number of B<TakTuk> instance in which output is collected.

=item C<$reply_date> (C<connector> and C<state> only)

the time in seconds since Jan. 1, 1970 at which the connector command replied
to the local B<TakTuk> instance (note that this does not include the
initialization exange occuring before the remote B<TakTuk> is considered as
functional).

=item C<$start_date> (C<output>, C<error>, C<connector> and C<status>)

the time in seconds since Jan. 1, 1970 at which the command started (taken just
after the call to fork()).

=item C<$stop_date> (C<connector> and C<status> only)

the time in seconds since Jan. 1, 1970 at which the command terminated (after
the closing of its stdout).

=item C<$type>

the name of the output stream (connector, error, output, ...).

=item C<$user_scalar>

a scalar, global to the package, initially initialized to undef, that the user
is free to use for any purpose.

=back

at the end of the day, the specification is evaluated for each line of the
concerned stream and the result printed on the root node.
Note that the newline has to be added explicitely as C<\n> in the template if
needed.

WARNING: take care of your specification, if the Perl syntax is not correct
lots of awfull compilation error messages will be displayed and B<TakTuk>
execution will fail.

=item B<-R> stream=descriptor

=item B<--output-redirect> stream=descriptor

redirect a given stream output to a given file descriptor number (located at
the root node). See C<-o> option for more details about streams output.

=back

=head1 PERFORMANCE TUNING OPTIONS

=over

=item B<-B> parameter=expression

=item B<--worksteal-behavior> parameter=expression

caution: this feature should be considered as experimental and could be removed
from future releases.
Tunes finely the behavior of the work-stealing algorithm by setting some its
parameters to Perl expressions. These Perl expressions can use the variables
C<$last_given> and C<$available> which are respectively equal to the number of
tasks given to a stealer last time it stole and to the total number of
remaining deployment tasks.
Available parameters are :

=over 

=item initial

the number of tasks given to a remote B<TakTuk> instance the first time it send
a steal request to the local host.

=item growth

the number of tasks given to a remote B<TakTuk> instance each time it performs
a steal to the local host after the first time.

=item limit

an upper bound to the value resulting from the evaluation of C<growth>.

=back

=item B<-g> duration

=item B<--time-granularity> duration

sets to C<duration> (in seconds) the maximal interval between timeouts checks
(usually checks are made more often: at each message received a timeouts check
is made).

=item B<-n>

=item B<--no-numbering>

disables B<TakTuk> logical numbering and the variables C<TAKTUK_COUNT> and
C<TAKTUK_RANK> are not defined in executed commands. This has the advantage of
removing the global synchronization occuring at the end of the deployment and
making the deployment more efficient.

WARNING: use this option only before any remote node specification (C<-m> or
C<-f>) otherwise you might get serious synchronization issues in B<TakTuk>.
Using B<TakTuk> point-to-point communication along with this option will
fail and produce B<TakTuk> warnings.

=item B<-t> timeout

=item B<--timeout> timeout

sets the timeout (in seconds) for following connectors (0 = no timeout). Notice
that this option override the timeout of the connector command (C<ssh> for
instance).  When the delay expires, the connector command is simply killed.

=item B<-w> size

=item B<--window> size

sets initial window to the given size (= pipeline width).

=item B<-W> scheme

=item B<--window-adaptation> scheme

sets the windows adaptation scheme to number (default is 0, 0: no adaptation,
1: implementation in progress).

=back

=head1 MISCELLANEOUS OPTIONS

=over

=item B<-M>

=item B<--my>

makes the next option local (not inherited). This means that the next option is
set only for the local host and not propagated to children B<TakTuk> instances.
Has no effect if the next option is already a local one.

=item B<-h>

=item B<--help>

prints a short description of B<TakTuk> on the screen.

=item B<-i>

=item B<--interactive>

forces B<TakTuk> interactive mode even after some batch commands given on the
command line.

=item B<-v>

=item B<--version>

prints the B<TakTuk> version.

=item B<-P>

=item B<--print-defaults>

prints the defaults settings used by B<TakTuk> (environment variables taken
into account).

=back

=head1 INTERNAL OPTIONS

These option are not useful for most users. They are used either internally by
B<TakTuk> itself or for development purposes.

=over

=item B<-p> packagename

=item B<--print-package> packagename 

prints the content of package C<packagename> extracted from the taktuk code in
execution.

=item B<-r>

=item B<--not-root>

current B<TakTuk> instance is not the root node. This is an internal switch
used by spawned B<TakTuk> instances. This is usually not useful for most users.

=item B<-D> scope=level

=item B<--debug> scope=level

sets debugging level (1..4) for C<scope>. The C<scope> might be a B<TakTuk> package name or C<default> and the lowest the level the more verbose the output.

=back

=head1 COMMANDS

After the options parsing, B<TakTuk> expects some commands either on the
remaining of the command line (batch mode) or on the standard input
(interactive mode). These commands are actions to be performed by B<TakTuk>
using the logical network infrastructure set up during the deployment. By
default, commands might be separated by ; or newlines. For all the commands,
any non ambiguous prefix can be used instead of their full name.
In interactive mode, B<TakTuk> has support for C<readline> (history, command
line editing) if installed on your system.

When B<TakTuk> commands accept arguments, they should be enclosed into matching
delimiters (indicated by * below). In other words, * might be replaced either
by any non alphanumeric character or by a pair of matching braces, brackets or
parenthesis.
These delimiters must be separated from their content (using the options
separator). If the argument contains a closing delimiter preceded by a
separator, then it is probably a good idea to escape it (see B<-E> option) or
to protect the whole arguments string if given on the command line.

Taktuk understands the following commands:

=over

=item B<[ set specification ]> command

Sends the execution of command to all the peers belonging to the given set. For
more details about the set specification, see section L<"SET SPECIFICATION">.

=item B<broadcast> command

Broadcasts the execution of command on all the remote peers (not including the
node initiating the broadcast).

=item B<downcast> command

Spreads the execution of command on all the children of the node initiating
the downcast (not including itself).

=item B<exec> parameters * command line *

Executes the C<command line> on the local machine but has no effect on the root
node. Inputs/outputs of the execution are multiplexed and fowarded to the
B<TakTuk> root.

This command accepts optional parameters that enable the attachment of
callbacks triggered by timeouts to the execution of the command. See section
L<"EXEC PARAMETERS"> for more details.

=item B<get> * src * * dest *

Copies (a) remote sourceZ<>(s) (present on the remote nodeZ<>(s)) to a 
destination on the node executing this command.
The type of source and destination is quite similar to those accepted by the
C<cp -r> command (that is file or directories). This command also performs some
kind of magic with its parameters: shell environment variables present in the
source or destination names are replaced by their respective value on the
remote and local nodes.
In addition, the variables C<$host>, C<$rank> and C<$position> of the remote
node (which are the same as in B<-o> option) can also be used in both names.

=item B<help>

Prints a very concise B<TakTuk> help.

=item B<input> * data *

=item B<input data> * data *

Sends the parameters as input to each command in execution on the
local machine.

=item B<input close>

Closes the inputs descriptor of all executing commands on the local machine.

=item B<input file> * filename *

Sends the content of a file (which must be local to the node executing the
input file) as input to each command in execution on the local machine.

WARNING: this command is not atomic. If you manage somehow to initiate a
input file command from two different B<TakTuk> instances, data will probably
be interleaved. In this case you should synchronize the two instances. This is
not required when spreading files only from the root node.

=item B<input line> * data *

Sends the parameters with an additional newline as input to each
command in execution on the local machine.

=item B<input pipe> * data *

Treat the parameter as a filename (which must be local to the node executing
the input pipe) which behavior is the same as a pipe: data
can arrive continuously. B<TakTuk> add it as one of its input channels and send
data as input to commands when available.

=item B<kill> <signal>

Sends the given signal to local commands processes groups. If no signal is
given, sends a TERM signal (signal 15).

=item B<network>

=item B<network state>

Prints the current B<TakTuk> deployment tree. Numbers in parenthesis match the
peer rank in the logical B<TakTuk> numbering and the peer ready state. If the
deployment is not complete, the printed tree will display C<connecting ...>
leaves.

=item B<network cancel>

Cancels all ongoing connections. Consequently, this causes immediate starting
of the possible numbering of B<TakTuk> instances and the execution of B<TakTuk>
commands.

=item B<network renumber>

Completely recomputes the logical numbering of B<TakTuk> instances. This is
especially useful when adding new nodes to the deployed network after initial
numbering.

=item B<network update>

Updates the logical numbering of B<TakTuk> instances without changing already
existing numbers. Does not always succeed as B<TakTuk> uses a depth first
scheme for numbering. Sends an event 9 in the stream 'state' for each node of
the tree that cannot be updated while keeping B<TakTuk> numbering scheme.
This is especially useful when adding new nodes to the deployed network after
initial numbering.

=item B<option> name * value *

=item B<option> * line *

Either changes a single option which name is given (first form) or parses an
additional options line (second form) on the nodeZ<>(s) executing this command.
See B<TakTuk> options for more details, name can be either short or long in the
first form.

WARNING: new nodes added to B<TakTuk> network using this command are not
numbered. Further use of network renumbering or update is necessary to get
B<TakTuk> logical numbering.

=item B<put> * src * * dest *

Copies a local source (present on the node executing this command) to a
destination on all the remote nodes given as prefix of the command.
The type of source and destination is quite similar to those accepted by the
C<cp -r> command (that is file or directories). This command also performs some
kind of magic with its parameters: shell environment variables present in the
source or destination names are replaced by their respective value on the
local and remote nodes.
In addition, the variables C<$host>, C<$rank> and C<$position> of the local
node (which are the same as in B<-o> option) can also be used in both names.

=item B<synchronize>

Forces the given command to wait for the completion of deployment, nodes
numbering and previous commands before executing. This is usefull for commands
like C<broadcast> which does not wait for nodes numbering.

=item B<taktuk_perl> * arguments *

Forks a perl interpreter on the local node just as if the command C<exec perl
arguments> have been used. The difference is that this interpreter is
previously fetched with the C<taktuk> package that contains point-to-point
communication routines (C<taktuk::send> and C<taktuk::recv>, see
C<taktukcomm(3)>).
WARNING: due to the limitations of the parser that analyses the arguments of
this command, you have to give arguments (even if empty) and to use '--' if you
give any option to the perl interpreter (and even if you give it only options).

=item B<version>

prints B<TakTuk> version.

=item B<quit>

Quit the B<TakTuk> engine and shut down the logical communication network
established during the deployment.

=back

=head1 EXEC PARAMETERS

The B<TakTuk> command C<exec> accepts optional parameters. These parameters are
used to attach actions triggered by timeouts to commands execution. An C<exec>
command accepts any number of parameters. These parameters are
interpreted from left to right using the following syntax:

=over

=item B<timeout> value

Creates a new timeout specification. At the end of the duration expressed by
the given value, it will trigger its attached callbacks. If it has no attached
callback, it will send a TERM signal to the command. If the command
execution terminates before the end of the timeout duration, the timeout is
canceled.

=item B<kill> value

Attach a callback to the last defined timeout. This callback send a signal,
which number is the given value, to the timeouted command.

=item B<action> command

Attach a callback to the last defined timeout. This callback executes the given
B<TakTuk> command. If it executes some other command, it can use the
environment variable C<TAKTUK_PID> that contains the pid of the timeouted
command. The given B<TakTuk> command can be any valid B<TakTuk> command
(without command separator).

=back

Notice that each timeout can have any number of attached callbacks. They will
be processed in the order they are given as parameters.

=head1 ENVIRONMENT

=over

=item B<Variables that change TakTuk default behavior>

Some of B<TakTuk> defaults settings can be changed on some host using
environment variables. These settings are propagated as are other options.
They are overridden by propagated settings and command line options.

To change some default setting use the variable B<TAKTUK_NAME> where B<NAME> is
the name of the according long option in upper case and with dashes replaced by
underscores. For option taking complex value (such as C<--debug>) just add an
underscore and the field you want to change in upper case at the end of the
name.
Using C<taktuk --print-defaults> will give you examples of names used to change
default settings.
Note that defining in the environment a default setting not used by B<TakTuk>
has no effect.

You can also change some B<TakTuk> default settings locally without propagating
the change in the deployment tree. To do this, use the variable
B<TAKTUK_MY_NAME> where name is defined as above. As before, these local
settings are overridden by propagated settings and command line options.

=item B<Variables set by TakTuk in remotely executed commands>

Taktuk sets the following environment variables for all the commands it
executes :

=over

=item TAKTUK_CONTROL_READ, TAKTUK_CONTROL_WRITE

File descriptors, used internally by B<TakTuk>.

=item TAKTUK_COUNT

The total number of successfully deployed B<TakTuk> instances

=item TAKTUK_HOSTNAME

Local node hostname as given to B<TakTuk> (on the command line)

=item TAKTUK_PIDS

List of pids (separated by spaces) of commands executed by the local B<TakTuk>
instance.

=item TAKTUK_POSITION

Host position on the command line.

=item TAKTUK_RANK

The logical rank of the local instance

=back

=back

=head1 HOSTNAMES SPECIFICATION

Hostnames given to B<TakTuk> might be simple machine name or complex hosts
lists specifications. In its general form, an hostname is made of an host set
and an optional exclusion set separated by a slash.  Each of those sets is a
comma separated list of host templates.  Each of these templates is made of
constant part (characters outside brackets) and optional range parts
(characters inside brackets). Each range part is a comma separated list of
intervals or single values. Each interval is made of two single values
separated by a dash. This is true for all hostnames given to B<TakTuk> (both
with B<-m> or B<-f> options).

In other words, the following expressions are valid host specifications:
    node1
    node[19]
    node[1-3]
    node[1-3],otherhost/node2
    node[1-3,5]part[a-b]/node[3-5]parta,node1partb

they respectively expand to:
    node1
    node19
    node1 node2 node3
    node1 node3 otherhost
    node1parta node2parta node2partb node3partb node5partb

Notice that these list of values are not regular expressions (C<node[19]> is
C<node19> and not C<node1, node2, ...., node9>). Intervals are implemented
using the perl magical auto increment feature, thus you can use alphanumeric
values as interval bounds (see perl documentation, operator ++ for limitations
of this auto increment).

=head1 SET SPECIFICATION

The B<TakTuk> command line and the C<taktuk::send> routine accept a set
specification as destination hostZ<>(s). A set specification is made of interval
specifications separated by slashes. An interval specification is made of a
single number or two numbers separated by a dash. Of course the two numbers
specifying an interval must be given in increasing order.

The remote peers included in a set specification are all the peer which logical
number belong to at least one interval of the set. Here are some exemples of
set specifications :

    1
the peer numbered 1

    2-7
the peers numbered 2,3,4,5,6 and 7

    2-4/1/10
the peers numbered 1,2,3,4 an 10

=head1 EXAMPLES

The following examples illustrate the basic use of B<TakTuk> on a few machines and the use of developper options. Notice that B<TakTuk> is designed to scale to much more peers than the number involved in these examples.

=head2 Basic usage

=over

=item B<simple deployment>

the simplest way to use B<TakTuk> is to make it selfpropagate with option
C<-s>. In this case, the basic remote execution of C<hostname> on the host
C<toto.nowhere.com> can be written :

    taktuk -s -m toto.nowhere.com broadcast exec [ hostname ]

In this example, C<-s> asks B<TakTuk> to propagate its own code on remote
hosts. It can be removed by installing the C<taktuk> executable on
C<toto.nowhere.com>. By the following we will assume that B<TakTuk> is
installed on all the remote hosts.

The C<-m toto.nowhere.com> describe the set of remote hosts to be contacted by
B<TakTuk> and C<broadcast exec [hostname]> is a command that will be executed
by the B<TakTuk> interpreter.

This example can be written in many other ways. In interactive mode, the
same execution might become:

    taktuk -m toto.nowhere.com

here B<TakTuk> is blocked waiting for commands from stdin. Thus, we just have
to type:

    broadcast exec { hostname }
    Ctrl-D

here you can notice that parameters to the C<exec> B<TakTuk> command (as all
commands parameters) can be enclosed in any reasonable pair of delimiters.
We might also write the list of hosts involved in the command in
a file C<machine> that contains:

    toto.nowhere.com

and the B<TakTuk> command becomes:

    taktuk -f machine broadcast exec - hostname -

We could also use another file C<options> that contains:

    -f machine 

and use it as the options line given to B<TakTuk>:

    taktuk -F options broadcast exec \( hostname \)

Finally, everything could be stored in a last file C<command_line> that
contains:

    -f machine broadcast exec = hostname =

and the following command achieve the same result:

    taktuk -F command_line

All of these variants have the same effect: they execute C<hostname> on C<toto.nowhere.com> and the output of the program is forwarded to the localhost. In this case:

    toto.nowhere.com: hostname: somepid: output > toto.nowhere.com

=item B<parameters braces>

notice that braces for command parameters must be separated from their content.
Thus, they can contain other braces as long as no single closing brace is part
of the content:

    taktuk -m localhost broadcast exec [ 'if [ $RANDOM -gt 10000 ];then echo greater;else echo lower;fi' ]

In this example, quotes are necessary to prevent the shell from interpreting
the C<$> and C<;> characters and to prevent the closing brace for C<if> toe be
considered as closing the C<exec> command.
In this case the variable will be interpolated
only on remote hosts. This same example can also be expressed using shortcuts
and intercative mode:

    taktuk -m localhost -E%

then type:

    b e [ if [ $RANDOM -gt 10000 %];then echo greater;else echo lower;fi ]
    Ctrl-D

Notice the closing bracket used in the test that should not be
interpreted as the closing bracket for C<exec> arguments. In such case, a
simpler solution is probably to use another kind of braces

    taktuk -m localhost

and then:
    b e { if [ $RANDOM -gt 10000 ];then echo greater;else echo lower;fi }
    Ctrl-D

Usually, if you want to be safe, you can quote all commands parameters.
Nevertheless, notice that parameters should not be quoted in interactive mode
as input lines are not interpreted by the shell.

=item B<exec parameters>

commands executed by B<TakTuk> can be timeouted using C<exec> parameters.
For instance if you want to execute some command and send it a TERM signal
after two seconds, just type:

    taktuk -m localhost broadcast exec timeout 2 [ sleep 10 ]

the callback executed when a timeout occurs can also be something else than a
TERM signal. This can be another signal (KILL for instance):

    taktuk -m localhost broadcast exec timeout 2 kill 9 [ sleep 10 ]

or any valid B<TakTuk> command:

    taktuk -m localhost broadcast exec timeout 2 action broadcast exec [ echo hello ] [ sleep 10 ]

or even several timeouts and several callbacks:

    taktuk -m localhost b e t 2 a e [ echo hello ] k 30 t 10 k 9 [ sleep 5 ]

in this last example, the command C<sleep 5> is executed by B<TakTuk>. After 2
seconds, the first timeout will be triggered, it will execute the command
C<echo hello> and send a USR1 signal to the first command (C<sleep 5>). The
second timeout is set to 10 seconds. Thus, it will never occur as the
C<sleep 5> command will be terminated before its expiration.

=item B<topology>

Usually, B<TakTuk> deploys itself using a relatively flat tree because its
default window size is quite large (10 simultaneous ongoing connections).
Using a smaller window size will result in a deeper tree although it also
depends on the local load of the deployment nodes.  You can use the
B<TakTuk> C<network state> command to print the tree constructed by B<TakTuk>.

Notice that it is usually a bad idea to use a too large window as it results in
too much local load and bad distribution of work (something like 10 is often sufficient).

You can also force B<TakTuk> to use more specific topologies. For instance, to
execute C<echo $$> using a flat-tree as deployment topology, just disable
work-stealing in B<TakTuk>:

    taktuk -d -1 -m host1 -m host2 -m host3 broadcast exec [ 'echo $$' ]

and to use a chain-like topology, either encode the topology in arguments
structure:

    taktuk -m host1 -[ -m host2 -[ -m host3 -] -] broadcast exec [ 'echo $$' ]

or limits the arity of the dynamic tree to 1:

    taktuk -d 1 -m host1 -m host2 -m host3 broadcast exec [ 'echo $$' ]

Finally, the default will use a dynamicly constructed topology:

    taktuk -d 0 -m host1 -m host2 -m host3 broadcast exec [ 'echo $$' ]

=item B<lightweight grid deployment>

On a lightweight grid, because of locality issues (ldap cache, network
topology, ...) it seems interesting to separate the deployment of each
subcluster. To do this, it is possible to choose one node in each subcluster
that will be deployed first and from which the rest of the cluster will be
deployed:

    taktuk -m node1.cluster1 -[ -m node2.cluster1 -m node3.cluster1 -m node4.cluster1 -] -m node1.cluster2 -[ -m node2.cluster2 -m node3.cluster2 -m node4.cluster2 -] broadcast exec [ hostname ]

This command has the effect of deploying B<TakTuk> on two clusters (cluster 1
and 2) made of four nodes (node 1 to 4) using node1 of each cluster to deploy
the other nodes in the same cluster. Finally, once the deployment is complete,
it executes the command C<hostname> on all these nodes.

=item B<executing a distinct command on each host>

each distinct host can be given its own command using arguments:

    ./taktuk -m host1 -[ exec [ hostname ] -] -m host2 -[ exec [ id ] -] -m host3 -[ exec [ 'echo $TAKTUK_RANK; ls' ] -] quit

but this could also be given using set specification (in this case logical
number are used for hosts):

    ./taktuk -m host1 -m host3 -m host8 1 exec [ hostname ], 2 exec [ id ], 3 exec [ 'echo $TAKTUK_RANK; ls' ]

or in interactive mode:

    ./taktuk -m host1 -m host3 -m host8
    1 exec [ hostname ]
    2 exec [ id ]
    3 exec [ echo $TAKTUK_RANK; ls ]
    Ctrl-D

=item B<script diffusion>

spreading and executing a perl script named C<essai.pl> on three hosts, knowing
that C<taktuk> and C<essai.pl> are only present on the root node:

    taktuk -s -m host1 -m host2 -m host3
    broadcast exec [ perl -- - ]
    broadcast input file [ essai.pl ]
    broadcast input close
    Ctrl-D

=item B<files diffusion and collection>

since version 3.4, files transfer is supported directly by TakTuk. Notice that
this transfer uses the B<TakTuk> network (usually a tree composed of ssh
connections by default). This network is not efficient for the tranfer of large
files (both topologically and from a protocol point of view), so keep this in
mind when using this feature.

copying a file named C<message.txt> to the C</tmp> directory of each remote
host is thus as easy as:

    taktuk -s -m host1 -m host2 -m host3
    broadcast put [ message.txt ] [ /tmp ]
    Ctrl-D

but the older method still works (and does almost the same as the previous
command):

    taktuk -s -m host1 -m host2 -m host3
    broadcast exec [ cat - >/tmp/message.txt ]
    broadcast input file [ message.txt ]
    broadcast input close
    Ctrl-D

although it reuqires to be more careful about shell interpretation when typing
everything directly on the command line:

    taktuk -s -m host1 -m host2 -m host3  broadcast exec [ 'cat - >/tmp/message.txt' ]\;broadcast input file [ message.txt ]

notice in this latter command that the C<input close> is not necessary as
B<TakTuk> closes inputs of all spawned commands when quiting.

the C<get> command also makes possible things that were previously very
difficult in B<TakTuk>, files collecting. The following command gets the file
C</tmp/message.txt> from each remote host and copies it locally to
C<message-number.txt> where C<number> is the logical rank of the source node:

    taktuk -s -m host1 -m host2 -m host3
    broadcast get [ /tmp/message.txt ] [ message-$rank.txt ]
    Ctrl-D

finally, it seems important to mention that C<put/get> commands can copy
directories and keep files permissions unchanged.

=item B<communication>

establishing a point-to-point communication. Assume the file
C<communication.pl> contains the following Perl script:

    my $rank = taktuk::get('rank');
    my $count = taktuk::get('count');
    if ($rank == 1)
      {
        print "I'm process 1\n";
        if ($count > 1)
          {
            taktuk::send(to=>2, body=>"Hello world");
          }
      }
    elsif ($rank == 2)
      {
        print "I'm process 2\n";
        my ($to, $from, $message) = taktuk::recv();
        print "Process $to received $message from $from\n";
      }

then the execution of the following command:

    taktuk -m localhost -m localhost broadcast taktuk_perl [ - ]\;broadcast input file [ communication.pl ]

would produce an output similar to:

    Astaroth.local: taktuk_perl: 3523: output > I'm process 2
    Astaroth.local: taktuk_perl: 3523: output > Process 2 received Hello world from 1
    Astaroth.local: taktuk_perl: 3523: status > 0
    Astaroth.local: taktuk_perl: 3524: output > I'm process 1
    Astaroth.local: taktuk_perl: 3524: status > 0

if the file C<communication.pl> was placed in the login directory of the user,
this could have also been executed by the more simple:

    taktuk -m localhost -m localhost broadcast taktuk_perl [ communication.pl ]

=item B<output templates and redirections>

making each site echo its rank without command status information:

    taktuk -o status -m host1 -m host2 broadcast exec [ 'echo $TAKTUK_RANK' ]

or removing the prompt before each line of output from commands:

    taktuk -o output='"$line\n"' -m host1 -m host2 broadcast exec [ 'echo $TAKTUK_RANK' ]

or even changing the prompt to make it display only the stream type:

    taktuk -o default='"$type > $line\n"' -m host1 -m host2 broadcast exec [ 'echo $TAKTUK_RANK' ]

and it also possible to redirect the status to file descriptor 2 only for the
second host:

    taktuk -m host1 -R status=2 -m host2 broadcast exec [ 'echo $TAKTUK_RANK' ]

and so on...

=back

=head2 Developer usage

=over

=item B<debugging TakTuk>

debugging in B<TakTuk> is made using C<debug>, C<warning> and C<error> functions of the package C<diagnostic>. These routines, depending on the debugging level of the package in which they are called, produce an output that is propagated up to the root node and printed on screen.

By default the debugging level of packages is set to 2 (everything is printed out except C<debug> messages). It might be changed for each package using the B<-D> option. For instance the following code executes C<true> on C<toto.nowhere.com> and prints out every bit of internal messaging:

    taktuk -D default=1 -m toto.nowhere.com broadcast exec [ true ]

but one could have executed the same command keeping only messages from the C<scheduler> package:

    taktuk -D scheduler=1 -m toto.nowhere.com broadcast exec [ true ]

or ensuring an  execution exempted of any warning or error messages:

    taktuk -D default=4 -m toto.nowhere.com broadcast exec [ true ]

=item B<internal messages server>

the internal message server used in B<TakTuk> for the management of logical
network construction, commands execution and I/O forwarding can be exposed
using the B<-r> option:

    taktuk -r

Notice that in this mode the behavior of B<TakTuk> can seem very cryptic. This is not intended for ordinary users.

=back

=head1 BUGS

The development of B<TakTuk> is still in progress, so there are propably
numbers of bugs. For now, the following characteristics (some of them are not
really bugs) have been identified :

=over

=item hanged commands

currently B<TakTuk> do not quit (even if asked to do so) if some local commands
do not terminate upon the closing of their standard input. Interrupting
B<TakTuk> actually terminate the engine but do not kill these commands. We
still have to decide if this is the appropriate behavior.

=item command order

broadcasts and multicast (set specifications) are not synchronized the same way
in B<TakTuk>. Thus, broadcast commands given after multicast commands might be
executed before these last ones. Use the C<synchronize> command to avoid this.

=item slow connections

this is not really a bug : on some clusters, the connections are VERY
slow when doing a large deployment (over 50 nodes) with self propagation.
This result in a total time for the deployment of 50 nodes that can take
several minutes (up to half an hours in some cases).
I don't know what's the exact cause (ldap ? local load ? writes block ?) but it
can be fixed by adding connection timeouts to connectors.

=item fork failures

this is not really a bug : when a machine is highly loaded, fork may fail.
If this happen to a local command, a distributed application may deadlock,
because the taktuk instances numbering is correct but some instance didn't
fork the local command.
The simplest fix is to use timeouts on taktuk::recvZ<>().

=back

You might also want to have a look at:

    http://taktuk.gforge.inria.fr/Bugs.txt

where all the temporary bugs are listed version by version.

=head1 SEE ALSO

C<taktukcomm(3)>

=head1 AUTHOR

The original concept of B<TakTuk> has been proposed by Cyrille Martin in his PhD thesis. People involved in this work include Jacques Briat, Olivier Richard, Thierry Gautier and Guillaume Huard.

The author of the version 3 (perl version) and current maintainer of the package is Guillaume Huard.

=head1 COPYRIGHT

B<TakTuk> is provided under the terms of the GNU General Public License version
2 or later.

=cut

