2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# PROTOCOL COMMON MINION MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Protocol::CommonMinion;
|
|
|
|
use parent 'pgBackRest::Protocol::Common';
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2016-09-06 15:44:50 +02:00
|
|
|
use English '-no_match_vars';
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
use JSON::PP;
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Ini;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Protocol::Common;
|
|
|
|
use pgBackRest::Protocol::IO;
|
2016-12-04 00:34:51 +02:00
|
|
|
use pgBackRest::Version;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strName, # Name of the protocol
|
2015-10-08 17:43:56 +02:00
|
|
|
$strCommand, # Command the master process is running
|
|
|
|
$iBufferMax, # Maximum buffer size
|
2015-08-29 20:20:46 +02:00
|
|
|
$iCompressLevel, # Set compression level
|
2015-10-08 17:43:56 +02:00
|
|
|
$iCompressLevelNetwork, # Set compression level for network only compression
|
2016-09-06 15:15:22 +02:00
|
|
|
$iProtocolTimeout, # Protocol timeout
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->new', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strName'},
|
2015-10-08 17:43:56 +02:00
|
|
|
{name => 'strCommand'},
|
|
|
|
{name => 'iBufferMax'},
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'iCompressLevel'},
|
2015-10-08 17:43:56 +02:00
|
|
|
{name => 'iCompressLevelNetwork'},
|
2016-09-06 15:15:22 +02:00
|
|
|
{name => 'iProtocolTimeout'},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# Create the class hash
|
2015-10-08 17:43:56 +02:00
|
|
|
my $self = $class->SUPER::new($iBufferMax, $iCompressLevel, $iCompressLevelNetwork, $iProtocolTimeout, $strName);
|
2015-08-05 14:43:41 +02:00
|
|
|
bless $self, $class;
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
$self->{strCommand} = $strCommand;
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
# Create the IO object with std io
|
2016-07-26 22:57:38 +02:00
|
|
|
$self->{io} = new pgBackRest::Protocol::IO(*STDIN, *STDOUT, *STDERR, undef, undef, $iProtocolTimeout, $iBufferMax);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# Write the greeting so master process knows who we are
|
|
|
|
$self->greetingWrite();
|
|
|
|
|
2016-09-06 15:15:22 +02:00
|
|
|
# Initialize module variables
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{hCommandMap} = $self->init();
|
2016-09-06 15:15:22 +02:00
|
|
|
|
2016-08-11 23:32:28 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'self', value => $self}
|
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# greetingWrite
|
|
|
|
#
|
|
|
|
# Send a greeting to the master process.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub greetingWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
# Write the greeting
|
|
|
|
$self->{io}->lineWrite((JSON::PP->new()->canonical()->allow_nonref())->encode(
|
|
|
|
{name => BACKREST_NAME, service => $self->{strName}, version => BACKREST_VERSION}));
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
# Exchange one protocol message to catch errors early
|
|
|
|
$self->outputWrite();
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# binaryXferAbort
|
|
|
|
#
|
|
|
|
# Abort transfer when source file does not exist.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub binaryXferAbort
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Only allow in the backend process
|
|
|
|
$self->{io}->lineWrite('block -1');
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# errorWrite
|
|
|
|
#
|
|
|
|
# Write errors with error codes in protocol format, otherwise write to stderr and exit with error.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub errorWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2016-09-06 15:44:50 +02:00
|
|
|
my $oException = shift;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
# Throw hard error if this is not a standard exception
|
|
|
|
if (!isException($oException))
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2016-09-06 15:44:50 +02:00
|
|
|
confess &log(ERROR, 'unknown error: ' . $oException, ERROR_UNKNOWN);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
# Write error code and message
|
|
|
|
$self->{io}->lineWrite($self->{oJSON}->encode({err => $oException->code(), out => $oException->message()}));
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# outputWrite
|
|
|
|
#
|
|
|
|
# Write output for the master process.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub outputWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{io}->lineWrite($self->{oJSON}->encode({out => \@_}));
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# cmdRead
|
|
|
|
#
|
|
|
|
# Read command sent by the master process.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub cmdRead
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
my $hCommand = $self->{oJSON}->decode($self->{io}->lineRead());
|
2016-09-06 15:15:22 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
return $hCommand->{cmd}, $hCommand->{param};
|
2016-09-06 15:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# process
|
|
|
|
####################################################################################################################################
|
|
|
|
sub process
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2016-12-05 00:51:00 +02:00
|
|
|
# Reset stderr log level so random errors do not get output
|
|
|
|
logLevelSet(undef, undef, OFF);
|
|
|
|
|
2016-09-06 15:15:22 +02:00
|
|
|
# Loop until the exit command is received
|
2016-10-05 15:09:30 +02:00
|
|
|
eval
|
2016-09-06 15:15:22 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
while (true)
|
2016-09-06 15:15:22 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
my ($strCommand, $rParam) = $self->cmdRead();
|
|
|
|
|
|
|
|
last if ($strCommand eq OP_EXIT);
|
2016-10-05 15:09:30 +02:00
|
|
|
|
|
|
|
eval
|
2016-09-06 15:15:22 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
# Check for the command in the map and run it if found
|
|
|
|
if (defined($self->{hCommandMap}{$strCommand}))
|
|
|
|
{
|
|
|
|
$self->outputWrite($self->{hCommandMap}{$strCommand}->($rParam));
|
|
|
|
}
|
|
|
|
# Run the standard NOOP command. This this can be overridden in hCommandMap to implement a custom NOOP.
|
|
|
|
elsif ($strCommand eq OP_NOOP)
|
|
|
|
{
|
|
|
|
$self->outputWrite();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess "invalid command: ${strCommand}";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run the post command if defined
|
|
|
|
if (defined($self->{hCommandMap}{&OP_POST}))
|
2016-09-06 15:15:22 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
$self->{hCommandMap}{&OP_POST}->($strCommand, $rParam);
|
2016-09-06 15:15:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 15:09:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
# Process errors
|
|
|
|
or do
|
|
|
|
{
|
|
|
|
$self->errorWrite($EVAL_ERROR);
|
|
|
|
};
|
2016-09-06 15:44:50 +02:00
|
|
|
}
|
2016-10-05 15:09:30 +02:00
|
|
|
|
|
|
|
return true;
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
2016-10-05 15:09:30 +02:00
|
|
|
or do
|
|
|
|
{
|
|
|
|
my $oException = $EVAL_ERROR;
|
|
|
|
|
|
|
|
# Change log level so error will go to stderr
|
2016-12-05 00:51:00 +02:00
|
|
|
logLevelSet(undef, undef, PROTOCOL);
|
2016-10-05 15:09:30 +02:00
|
|
|
|
|
|
|
# If standard exception
|
|
|
|
if (isException($oException))
|
|
|
|
{
|
|
|
|
confess &log($oException->level(), $oException->message(), $oException->code());
|
|
|
|
}
|
|
|
|
|
|
|
|
# Else unexpected Perl exception
|
|
|
|
confess &log(ERROR, 'unknown error: ' . $oException, ERROR_UNKNOWN);
|
|
|
|
};
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-09-06 15:15:22 +02:00
|
|
|
return 0;
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|