2014-12-22 18:24:32 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# THREADGROUP MODULE
|
|
|
|
####################################################################################################################################
|
|
|
|
package BackRest::ThreadGroup;
|
|
|
|
|
|
|
|
use threads;
|
|
|
|
use strict;
|
2015-03-03 07:57:20 +02:00
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-06-14 00:25:49 +02:00
|
|
|
use Exporter qw(import);
|
2014-12-22 18:24:32 +02:00
|
|
|
use File::Basename;
|
2015-01-25 19:20:12 +02:00
|
|
|
|
2014-12-22 18:24:32 +02:00
|
|
|
use lib dirname($0) . '/../lib';
|
2015-04-07 13:34:37 +02:00
|
|
|
use BackRest::Config;
|
|
|
|
use BackRest::BackupFile;
|
2015-06-14 00:25:49 +02:00
|
|
|
use BackRest::RestoreFile;
|
|
|
|
use BackRest::Utility;
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-02-26 16:22:05 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# MODULE EXPORTS
|
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
our @EXPORT = qw(threadGroupCreate threadGroupRun threadGroupComplete threadGroupDestroy);
|
|
|
|
|
|
|
|
my @oyThread;
|
|
|
|
my @oyMessageQueue;
|
|
|
|
my @oyCommandQueue;
|
|
|
|
my @oyResultQueue;
|
|
|
|
my @byThreadRunning;
|
2015-02-26 16:22:05 +02:00
|
|
|
|
2014-12-22 18:24:32 +02:00
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
# threadGroupCreate
|
2014-12-22 18:24:32 +02:00
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
sub threadGroupCreate
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
# If thread-max is not defined then this operation does not use threads
|
|
|
|
if (!optionTest(OPTION_THREAD_MAX))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Get thread-max
|
|
|
|
my $iThreadMax = optionGet(OPTION_THREAD_MAX);
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Only create threads when thread-max > 1
|
|
|
|
if ($iThreadMax > 1)
|
|
|
|
{
|
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < $iThreadMax; $iThreadIdx++)
|
|
|
|
{
|
|
|
|
push @oyCommandQueue, Thread::Queue->new();
|
|
|
|
push @oyMessageQueue, Thread::Queue->new();
|
|
|
|
push @oyResultQueue, Thread::Queue->new();
|
|
|
|
push @oyThread, (threads->create(\&threadGroupThread, $iThreadIdx));
|
|
|
|
push @byThreadRunning, false;
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
# threadGroupThread
|
2014-12-22 18:24:32 +02:00
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
sub threadGroupThread
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
my $iThreadIdx = shift;
|
|
|
|
|
|
|
|
# When a KILL signal is received, immediately abort
|
|
|
|
$SIG{'KILL'} = sub {threads->exit();};
|
|
|
|
|
|
|
|
while (my $oCommand = $oyCommandQueue[$iThreadIdx]->dequeue())
|
|
|
|
{
|
|
|
|
# Exit thread
|
|
|
|
if ($$oCommand{function} eq 'exit')
|
|
|
|
{
|
|
|
|
&log(TRACE, 'thread terminated');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
&log(TRACE, "$$oCommand{function} thread started");
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Create a file object
|
|
|
|
my $oFile = new BackRest::File
|
|
|
|
(
|
|
|
|
optionGet(OPTION_STANZA),
|
|
|
|
optionRemoteTypeTest(BACKUP) ? optionGet(OPTION_REPO_REMOTE_PATH) : optionGet(OPTION_REPO_PATH),
|
|
|
|
optionRemoteType(),
|
2015-06-17 17:26:07 +02:00
|
|
|
protocolGet(undef, false),
|
2015-04-07 13:34:37 +02:00
|
|
|
undef, undef,
|
|
|
|
$iThreadIdx + 1
|
|
|
|
);
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
# Notify parent that init is complete
|
|
|
|
threadMessage($oyResultQueue[$iThreadIdx], 'init');
|
|
|
|
|
|
|
|
my $iDirection = $iThreadIdx % 2 == 0 ? 1 : -1; # Size of files currently copied by this thread
|
|
|
|
|
|
|
|
# Initialize the starting and current queue index based in the total number of threads in relation to this thread
|
|
|
|
my $iQueueStartIdx = int((@{$$oCommand{param}{queue}} / $$oCommand{thread_total}) * $iThreadIdx);
|
|
|
|
my $iQueueIdx = $iQueueStartIdx;
|
|
|
|
|
|
|
|
# Keep track of progress
|
|
|
|
my $lSizeCurrent = 0; # Running total of bytes copied
|
|
|
|
|
|
|
|
# Loop through all the queues (exit when the original queue is reached)
|
|
|
|
do
|
|
|
|
{
|
|
|
|
&log(TRACE, "reading queue ${iQueueIdx}, start queue ${iQueueStartIdx}");
|
|
|
|
|
|
|
|
while (my $oMessage = ${$$oCommand{param}{queue}}[$iQueueIdx]->dequeue_nb())
|
|
|
|
{
|
|
|
|
if ($$oCommand{function} eq 'restore')
|
|
|
|
{
|
2015-05-07 23:56:56 +02:00
|
|
|
restoreFile($oMessage, $$oCommand{param}{copy_time_begin}, $$oCommand{param}{delta}, $$oCommand{param}{force},
|
|
|
|
$$oCommand{param}{backup_path}, $$oCommand{param}{source_compression},
|
|
|
|
$$oCommand{param}{current_user}, $$oCommand{param}{current_group}, $oFile, 0, 0);
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
|
|
|
elsif ($$oCommand{function} eq 'backup')
|
|
|
|
{
|
2015-04-11 21:02:04 +02:00
|
|
|
# Result hash that can be passed back to the master process
|
|
|
|
my $oResult = {};
|
2015-04-07 13:34:37 +02:00
|
|
|
|
|
|
|
# Backup the file
|
2015-04-11 21:02:04 +02:00
|
|
|
($$oResult{copied}, $lSizeCurrent, $$oResult{size}, $$oResult{checksum}) =
|
2015-04-07 13:34:37 +02:00
|
|
|
backupFile($oFile, $$oMessage{db_file}, $$oMessage{backup_file}, $$oCommand{param}{compress},
|
2015-05-07 02:24:34 +02:00
|
|
|
$$oMessage{checksum}, $$oMessage{modification_time},
|
2015-05-07 23:56:56 +02:00
|
|
|
$$oMessage{size}, 0, 0);
|
2015-04-07 13:34:37 +02:00
|
|
|
|
2015-04-11 21:02:04 +02:00
|
|
|
# Send a message to update the manifest
|
|
|
|
$$oResult{file_section} = $$oMessage{file_section};
|
|
|
|
$$oResult{file} = $$oMessage{file};
|
|
|
|
|
|
|
|
$$oCommand{param}{result_queue}->enqueue($oResult);
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "unknown command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Even numbered threads move up when they have finished a queue, odd numbered threads move down
|
|
|
|
$iQueueIdx += $iDirection;
|
|
|
|
|
|
|
|
# Reset the queue index when it goes over or under the number of queues
|
|
|
|
if ($iQueueIdx < 0)
|
|
|
|
{
|
|
|
|
$iQueueIdx = @{$$oCommand{param}{queue}} - 1;
|
|
|
|
}
|
|
|
|
elsif ($iQueueIdx >= @{$$oCommand{param}{queue}})
|
|
|
|
{
|
|
|
|
$iQueueIdx = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ($iQueueIdx != $iQueueStartIdx);
|
|
|
|
|
|
|
|
# Notify parent of shutdown
|
|
|
|
threadMessage($oyResultQueue[$iThreadIdx], 'shutdown');
|
|
|
|
threadMessageExpect($oyMessageQueue[$iThreadIdx], 'continue');
|
|
|
|
|
|
|
|
# Destroy the file object
|
|
|
|
undef($oFile);
|
|
|
|
|
|
|
|
# Notify the parent process of thread exit
|
|
|
|
$oyResultQueue[$iThreadIdx]->enqueue('complete');
|
|
|
|
|
|
|
|
&log(TRACE, "$$oCommand{function} thread exiting");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# threadMessage
|
|
|
|
####################################################################################################################################
|
|
|
|
sub threadMessage
|
|
|
|
{
|
|
|
|
my $oQueue = shift;
|
|
|
|
my $strMessage = shift;
|
|
|
|
my $iThreadIdx = shift;
|
|
|
|
|
|
|
|
# Send the message
|
|
|
|
$oQueue->enqueue($strMessage);
|
|
|
|
|
|
|
|
# Define calling context
|
|
|
|
&log(TRACE, "sent message '${strMessage}' to " . (defined($iThreadIdx) ? 'thread ' . ($iThreadIdx + 1) : 'controller'));
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
# threadMessageExpect
|
|
|
|
####################################################################################################################################
|
|
|
|
sub threadMessageExpect
|
|
|
|
{
|
|
|
|
my $oQueue = shift;
|
|
|
|
my $strExpected = shift;
|
|
|
|
my $iThreadIdx = shift;
|
|
|
|
my $bNoBlock = shift;
|
|
|
|
|
|
|
|
# Set timeout based on the message type
|
|
|
|
my $iTimeout = defined($bNoBlock) ? undef: 600;
|
|
|
|
|
|
|
|
# Define calling context
|
|
|
|
my $strContext = defined($iThreadIdx) ? 'thread ' . ($iThreadIdx + 1) : 'controller';
|
|
|
|
|
|
|
|
# Wait for the message
|
|
|
|
my $strMessage;
|
|
|
|
|
|
|
|
if (defined($iTimeout))
|
|
|
|
{
|
|
|
|
&log(TRACE, "waiting for '${strExpected}' message from ${strContext}");
|
2015-07-10 15:20:28 +02:00
|
|
|
|
|
|
|
my $oWait = waitInit($iTimeout);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
$strMessage = $oQueue->dequeue_nb();
|
|
|
|
}
|
|
|
|
while (!defined($strMessage) && waitMore($oWait));
|
2015-04-07 13:34:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strMessage = $oQueue->dequeue_nb();
|
|
|
|
|
|
|
|
return false if !defined($strMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Throw an exeception when the message was not received
|
|
|
|
if (!defined($strMessage) || $strMessage ne $strExpected)
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "expected message '$strExpected' from ${strContext} but " .
|
|
|
|
(defined($strMessage) ? "got '$strMessage'" : "timed out after ${iTimeout} second(s)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
&log(TRACE, "got '${strExpected}' message from ${strContext}");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# threadGroupRun
|
|
|
|
####################################################################################################################################
|
|
|
|
sub threadGroupRun
|
|
|
|
{
|
|
|
|
my $iThreadIdx = shift;
|
|
|
|
my $strFunction = shift;
|
|
|
|
my $oParam = shift;
|
|
|
|
|
|
|
|
my %oCommand;
|
|
|
|
$oCommand{function} = $strFunction;
|
|
|
|
$oCommand{thread_total} = @oyThread;
|
|
|
|
$oCommand{param} = $oParam;
|
|
|
|
|
|
|
|
$oyCommandQueue[$iThreadIdx]->enqueue(\%oCommand);
|
|
|
|
|
|
|
|
threadMessageExpect($oyResultQueue[$iThreadIdx], 'init', $iThreadIdx);
|
|
|
|
$byThreadRunning[$iThreadIdx] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# threadGroupComplete
|
2014-12-22 18:24:32 +02:00
|
|
|
#
|
|
|
|
# Wait for threads to complete.
|
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
sub threadGroupComplete
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $iTimeout = shift;
|
|
|
|
my $bConfessOnError = shift;
|
|
|
|
|
|
|
|
# Set defaults
|
2015-01-08 22:43:43 +02:00
|
|
|
$bConfessOnError = defined($bConfessOnError) ? $bConfessOnError : true;
|
2014-12-22 18:24:32 +02:00
|
|
|
|
|
|
|
# Wait for all threads to complete and handle errors
|
|
|
|
my $iThreadComplete = 0;
|
|
|
|
my $lTimeBegin = time();
|
2015-04-07 13:34:37 +02:00
|
|
|
my $strFirstError;
|
|
|
|
my $iFirstErrorThreadIdx;
|
|
|
|
|
|
|
|
&log(DEBUG, "waiting for " . @oyThread . " threads to complete");
|
2014-12-22 18:24:32 +02:00
|
|
|
|
|
|
|
# Rejoin the threads
|
2015-05-07 02:24:34 +02:00
|
|
|
# while ($iThreadComplete < @oyThread)
|
|
|
|
# {
|
2015-01-31 01:58:49 +02:00
|
|
|
hsleep(.1);
|
2014-12-22 18:24:32 +02:00
|
|
|
|
|
|
|
# If a timeout has been defined, make sure we have not been running longer than that
|
|
|
|
if (defined($iTimeout))
|
|
|
|
{
|
|
|
|
if (time() - $lTimeBegin >= $iTimeout)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "threads have been running more than ${iTimeout} seconds, exiting...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < @oyThread; $iThreadIdx++)
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
if ($byThreadRunning[$iThreadIdx])
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
my $oError = $oyThread[$iThreadIdx]->error();
|
|
|
|
|
|
|
|
if (defined($oError))
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
my $strError;
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
if ($oError->isa('BackRest::Exception'))
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
$strError = $oError->message();
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
$strError = $oError;
|
|
|
|
&log(ERROR, "thread " . ($iThreadIdx) . ": ${strError}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined($strFirstError))
|
|
|
|
{
|
|
|
|
$strFirstError = $strError;
|
|
|
|
$iFirstErrorThreadIdx = $iThreadIdx;
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
$byThreadRunning[$iThreadIdx] = false;
|
|
|
|
$iThreadComplete++;
|
|
|
|
}
|
|
|
|
elsif (threadMessageExpect($oyResultQueue[$iThreadIdx], 'shutdown', $iThreadIdx, true))
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
threadMessage($oyMessageQueue[$iThreadIdx], 'continue', $iThreadIdx);
|
|
|
|
threadMessageExpect($oyResultQueue[$iThreadIdx], 'complete', $iThreadIdx);
|
|
|
|
$byThreadRunning[$iThreadIdx] = false;
|
2014-12-22 18:24:32 +02:00
|
|
|
$iThreadComplete++;
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 02:24:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$iThreadComplete++;
|
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
2015-05-07 02:24:34 +02:00
|
|
|
# }
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-05-07 02:24:34 +02:00
|
|
|
# If there were errors then confess them
|
2015-04-07 13:34:37 +02:00
|
|
|
if (defined($strFirstError) && $bConfessOnError)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'error in thread' . ($iFirstErrorThreadIdx + 1) . ": $strFirstError");
|
|
|
|
}
|
2015-05-07 02:24:34 +02:00
|
|
|
|
|
|
|
# Return true if all threads have completed
|
|
|
|
if ($iThreadComplete == @oyThread)
|
|
|
|
{
|
|
|
|
&log(DEBUG, 'all threads exited');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
# threadGroupDestroy
|
2014-12-22 18:24:32 +02:00
|
|
|
####################################################################################################################################
|
2015-04-07 13:34:37 +02:00
|
|
|
sub threadGroupDestroy
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
&log(TRACE, "waiting for " . @oyThread . " threads to be destroyed");
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
for (my $iThreadIdx = 0; $iThreadIdx < @oyThread; $iThreadIdx++)
|
2014-12-22 18:24:32 +02:00
|
|
|
{
|
2015-04-07 13:34:37 +02:00
|
|
|
my %oCommand;
|
|
|
|
$oCommand{function} = 'exit';
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
$oyCommandQueue[$iThreadIdx]->enqueue(\%oCommand);
|
|
|
|
hsleep(.1);
|
|
|
|
|
|
|
|
if ($oyThread[$iThreadIdx]->is_running())
|
|
|
|
{
|
|
|
|
$oyThread[$iThreadIdx]->kill('KILL')->join();
|
|
|
|
&log(TRACE, "thread ${iThreadIdx} killed");
|
|
|
|
}
|
|
|
|
elsif ($oyThread[$iThreadIdx]->is_joinable())
|
|
|
|
{
|
|
|
|
$oyThread[$iThreadIdx]->join();
|
|
|
|
&log(TRACE, "thread ${iThreadIdx} joined");
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
2015-04-07 13:34:37 +02:00
|
|
|
|
|
|
|
undef($oyThread[$iThreadIdx]);
|
2014-12-22 18:24:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
&log(TRACE, @oyThread . " threads destroyed");
|
2014-12-22 18:24:32 +02:00
|
|
|
|
2015-04-07 13:34:37 +02:00
|
|
|
return(@oyThread);
|
|
|
|
}
|
2014-12-22 18:24:32 +02:00
|
|
|
|
|
|
|
1;
|