2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
2017-06-09 23:51:41 +02:00
|
|
|
# Protocol Master Base
|
2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
2017-06-09 23:51:41 +02:00
|
|
|
package pgBackRest::Protocol::Base::Master;
|
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
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
2015-12-24 17:32:25 +02:00
|
|
|
use Time::HiRes qw(gettimeofday);
|
2017-06-09 23:51:41 +02:00
|
|
|
use JSON::PP;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Ini;
|
|
|
|
use pgBackRest::Common::Log;
|
2016-12-04 00:34:51 +02:00
|
|
|
use pgBackRest::Version;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Operation constants
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant OP_NOOP => 'noop';
|
|
|
|
push @EXPORT, qw(OP_NOOP);
|
|
|
|
use constant OP_EXIT => 'exit';
|
|
|
|
push @EXPORT, qw(OP_EXIT);
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONSTRUCTOR
|
|
|
|
####################################################################################################################################
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift; # Class name
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Create the class hash
|
|
|
|
my $self = {};
|
|
|
|
bless $self, $class;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
(
|
2017-06-09 23:51:41 +02:00
|
|
|
my $strOperation,
|
|
|
|
$self->{strName},
|
|
|
|
$self->{strId},
|
|
|
|
$self->{oIo},
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->new', \@_,
|
2017-01-27 17:06:16 +02:00
|
|
|
{name => 'strName', trace => true},
|
|
|
|
{name => 'strId', trace => true},
|
2017-06-09 23:51:41 +02:00
|
|
|
{name => 'oIo', trace => true},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Create JSON object
|
|
|
|
$self->{oJSON} = JSON::PP->new()->allow_nonref();
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-12-24 17:32:25 +02:00
|
|
|
# Setup the keepalive timer
|
2017-06-09 23:51:41 +02:00
|
|
|
$self->{fKeepAliveTimeout} = $self->io()->timeout() / 2 > 120 ? 120 : $self->io()->timeout() / 2;
|
2015-12-24 17:32:25 +02:00
|
|
|
$self->{fKeepAliveTime} = gettimeofday();
|
|
|
|
|
2016-07-30 00:27:35 +02:00
|
|
|
# Set the error prefix used when raising error messages
|
|
|
|
$self->{strErrorPrefix} = 'raised on ' . $self->{strId} . ' host';
|
|
|
|
|
2017-10-21 20:11:33 +02:00
|
|
|
# Check greeting to be sure the protocol matches
|
|
|
|
$self->greetingRead();
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2017-01-27 17:06:16 +02:00
|
|
|
{name => 'self', value => $self, trace => true}
|
2016-08-24 18:39:27 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# DESTROY
|
|
|
|
####################################################################################################################################
|
|
|
|
sub DESTROY
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->close();
|
|
|
|
}
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# greetingRead
|
|
|
|
#
|
|
|
|
# Read the greeting and make sure it is as expected.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub greetingRead
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# Get the first line of output from the remote if possible
|
2017-06-09 23:51:41 +02:00
|
|
|
my $strGreeting = $self->io()->readLine(true);
|
|
|
|
|
2017-06-15 21:12:28 +02:00
|
|
|
# Parse the greeting and make sure it is valid
|
|
|
|
my $hGreeting;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2017-06-15 21:12:28 +02:00
|
|
|
eval
|
|
|
|
{
|
|
|
|
$hGreeting = $self->{oJSON}->decode($strGreeting);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
# Report any error that stopped parsing
|
|
|
|
or do
|
|
|
|
{
|
|
|
|
$self->io()->error(ERROR_PROTOCOL, 'invalid protocol greeting', $strGreeting);
|
|
|
|
};
|
2017-06-09 23:51:41 +02:00
|
|
|
|
2017-06-15 21:12:28 +02:00
|
|
|
# Error if greeting parameters do not match
|
2016-12-04 00:34:51 +02:00
|
|
|
for my $hParam ({strName => 'name', strExpected => BACKREST_NAME},
|
|
|
|
{strName => 'version', strExpected => BACKREST_VERSION},
|
|
|
|
{strName => 'service', strExpected => $self->{strName}})
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
if (!defined($hGreeting->{$hParam->{strName}}) || $hGreeting->{$hParam->{strName}} ne $hParam->{strExpected})
|
|
|
|
{
|
|
|
|
confess &log(ERROR,
|
|
|
|
'found name \'' . (defined($hGreeting->{$hParam->{strName}}) ? $hGreeting->{$hParam->{strName}} : '[undef]') .
|
2017-01-24 21:58:55 +02:00
|
|
|
"' in protocol greeting instead of expected '$hParam->{strExpected}'", ERROR_HOST_CONNECT);
|
2016-12-04 00:34:51 +02:00
|
|
|
}
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
2016-07-30 00:27:35 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
# Perform noop to catch errors early
|
|
|
|
$self->noOp();
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# outputRead
|
|
|
|
#
|
|
|
|
# Read output from the remote process.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub outputRead
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$bOutputRequired,
|
2016-08-24 18:39:27 +02:00
|
|
|
$bSuppressLog,
|
|
|
|
$bWarnOnError,
|
2016-12-04 00:34:51 +02:00
|
|
|
$bRef,
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->outputRead', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'bOutputRequired', default => false, trace => true},
|
2016-08-24 18:39:27 +02:00
|
|
|
{name => 'bSuppressLog', required => false, trace => true},
|
|
|
|
{name => 'bWarnOnError', default => false, trace => true},
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'bRef', default => false, trace => true},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
my $strProtocolResult = $self->io()->readLine();
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
logDebugMisc
|
|
|
|
(
|
|
|
|
$strOperation, undef,
|
|
|
|
{name => 'strProtocolResult', value => $strProtocolResult, trace => true}
|
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
my $hResult = $self->{oJSON}->decode($strProtocolResult);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
# Raise any errors
|
2016-12-04 00:34:51 +02:00
|
|
|
if (defined($hResult->{err}))
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
my $strError = $self->{strErrorPrefix} . (defined($hResult->{out}) ? ": $hResult->{out}" : '');
|
2016-08-24 18:39:27 +02:00
|
|
|
|
|
|
|
# Raise the error if a warning is not requested
|
|
|
|
if (!$bWarnOnError)
|
|
|
|
{
|
2016-12-04 00:34:51 +02:00
|
|
|
confess &log(ERROR, $strError, $hResult->{err}, $bSuppressLog);
|
2016-08-24 18:39:27 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
&log(WARN, $strError, $hResult->{err});
|
|
|
|
undef($hResult->{out});
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
2015-12-24 17:32:25 +02:00
|
|
|
# Reset the keep alive time
|
|
|
|
$self->{fKeepAliveTime} = gettimeofday();
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
# If output is required and there is no output, raise exception
|
2016-12-04 00:34:51 +02:00
|
|
|
if ($bOutputRequired && !defined($hResult->{out}))
|
2015-08-05 14:43:41 +02:00
|
|
|
{
|
2016-08-24 18:39:27 +02:00
|
|
|
confess &log(ERROR, "$self->{strErrorPrefix}: output is not defined", ERROR_PROTOCOL_OUTPUT_REQUIRED);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'hOutput', value => $hResult->{out}, ref => $bRef, trace => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# cmdWrite
|
|
|
|
#
|
|
|
|
# Send command to remote process.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub cmdWrite
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$strCommand,
|
2016-12-04 00:34:51 +02:00
|
|
|
$hParam,
|
2015-08-29 20:20:46 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '->cmdWrite', \@_,
|
2015-08-29 20:20:46 +02:00
|
|
|
{name => 'strCommand', trace => true},
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'hParam', required => false, trace => true},
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2016-12-04 00:34:51 +02:00
|
|
|
my $strProtocolCommand = $self->{oJSON}->encode({cmd => $strCommand, param => $hParam});
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-08-29 20:20:46 +02:00
|
|
|
logDebugMisc
|
|
|
|
(
|
|
|
|
$strOperation, undef,
|
2016-12-04 00:34:51 +02:00
|
|
|
{name => 'strProtocolCommand', value => $strProtocolCommand, trace => true}
|
2015-08-29 20:20:46 +02:00
|
|
|
);
|
2015-08-05 14:43:41 +02:00
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Write out the command
|
2017-06-09 23:51:41 +02:00
|
|
|
$self->io()->writeLine($strProtocolCommand);
|
2015-10-08 17:43:56 +02:00
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
# Reset the keep alive time
|
|
|
|
$self->{fKeepAliveTime} = gettimeofday();
|
|
|
|
|
2015-10-08 17:43:56 +02:00
|
|
|
# Return from function and log return values if any
|
|
|
|
logDebugReturn($strOperation);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# cmdExecute
|
|
|
|
#
|
|
|
|
# Send command to remote process and wait for output.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub cmdExecute
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $strCommand = shift;
|
|
|
|
my $oParamRef = shift;
|
|
|
|
my $bOutputRequired = shift;
|
2016-08-24 18:39:27 +02:00
|
|
|
my $bWarnOnError = shift;
|
2015-08-05 14:43:41 +02:00
|
|
|
|
|
|
|
$self->cmdWrite($strCommand, $oParamRef);
|
|
|
|
|
2016-08-24 18:39:27 +02:00
|
|
|
return $self->outputRead($bOutputRequired, undef, $bWarnOnError);
|
2015-08-05 14:43:41 +02:00
|
|
|
}
|
|
|
|
|
2015-12-24 17:32:25 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# keepAlive
|
|
|
|
#
|
|
|
|
# Send periodic noops so the remote does not time out.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub keepAlive
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if (gettimeofday() - $self->{fKeepAliveTimeout} > $self->{fKeepAliveTime})
|
|
|
|
{
|
2016-07-30 00:27:35 +02:00
|
|
|
$self->noOp();
|
2015-12-24 17:32:25 +02:00
|
|
|
|
|
|
|
# Keep alive test point
|
|
|
|
&log(TEST, TEST_KEEP_ALIVE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 00:27:35 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# noOp
|
|
|
|
#
|
|
|
|
# Send noop to test connection or keep it alive.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub noOp
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->cmdExecute(OP_NOOP, undef, false);
|
2016-09-06 15:35:02 +02:00
|
|
|
$self->{fKeepAliveTime} = gettimeofday();
|
2016-07-30 00:27:35 +02:00
|
|
|
}
|
|
|
|
|
2017-06-09 23:51:41 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Getters
|
|
|
|
####################################################################################################################################
|
|
|
|
sub io {shift->{oIo}}
|
|
|
|
sub master {true}
|
|
|
|
|
2015-08-05 14:43:41 +02:00
|
|
|
1;
|