2016-02-09 09:17:31 +01:00
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System ;
using CoCoL ;
using System.Threading.Tasks ;
using Duplicati.Library.Main.Database ;
using Duplicati.Library.Main.Volumes ;
using Duplicati.Library.Main.Operation.Common ;
using System.IO ;
using System.Linq ;
2018-01-30 08:34:57 +01:00
using System.Collections.Generic ;
2016-02-09 09:17:31 +01:00
namespace Duplicati.Library.Main.Operation.Backup
{
/// <summary>
2016-02-25 19:15:51 +01:00
/// This class receives data blocks, registers then in the database.
/// New blocks are added to a compressed archive and sent
/// to the uploader
2016-02-09 09:17:31 +01:00
/// </summary>
internal static class DataBlockProcessor
{
2018-01-31 22:03:37 +01:00
/// <summary>
/// The number of bytes to reserve for the file header
/// in the compressed volume
/// </summary>
public const int BlockCompressionOverhead = 1024 ;
/// <summary>
/// A multiplier for protecting against block data
/// expanding during compression
/// </summary>
public const float NonCompressibleExpansionFactor = 1.02f ;
2016-02-27 02:15:42 +01:00
public static Task Run ( BackupDatabase database , Options options , ITaskReader taskreader )
2016-02-09 09:17:31 +01:00
{
return AutomationExtensions . RunTask (
2016-02-24 23:48:42 +01:00
new
{
LogChannel = Common . Channels . LogChannel . ForWrite ,
Input = Channels . OutputBlocks . ForRead ,
Output = Channels . BackendRequest . ForWrite ,
SpillPickup = Channels . SpillPickup . ForWrite ,
},
async self =>
{
BlockVolumeWriter blockvolume = null ;
2018-01-30 08:34:57 +01:00
var useindex = options . IndexfilePolicy == Options . IndexFileStrategy . Full ;
var indexdata = useindex ? new Library . Utility . FileBackedStringList () : null ;
2016-02-24 23:48:42 +01:00
2018-01-31 22:03:37 +01:00
// The limit for a single volume size
var maxvolumesize = options . VolumeSize - BlockCompressionOverhead ;
2016-02-24 23:48:42 +01:00
try
2016-02-09 09:17:31 +01:00
{
2016-02-24 23:48:42 +01:00
while ( true )
2016-02-09 09:17:31 +01:00
{
2016-02-24 23:48:42 +01:00
var b = await self . Input . ReadAsync ();
2016-02-09 09:17:31 +01:00
2016-02-24 23:48:42 +01:00
// Lazy-start a new block volume
if ( blockvolume == null )
{
// Before we start a new volume, probe to see if it exists
// This will delay creation of volumes for differential backups
// There can be a race, such that two workers determine that
// the block is missing, but this will be solved by the AddBlock call
// which runs atomically
if ( await database . FindBlockIDAsync ( b . HashKey , b . Size ) >= 0 )
2016-02-09 09:17:31 +01:00
{
2016-02-24 23:48:42 +01:00
b . TaskCompletion . TrySetResult ( false );
continue ;
2016-02-20 15:06:29 +01:00
}
2016-02-24 23:48:42 +01:00
blockvolume = new BlockVolumeWriter ( options );
blockvolume . VolumeID = await database . RegisterRemoteVolumeAsync ( blockvolume . RemoteFilename , RemoteVolumeType . Blocks , RemoteVolumeState . Temporary );
}
var newBlock = await database . AddBlockAsync ( b . HashKey , b . Size , blockvolume . VolumeID );
b . TaskCompletion . TrySetResult ( newBlock );
2016-02-20 15:06:29 +01:00
2016-02-24 23:48:42 +01:00
if ( newBlock )
{
2018-01-28 12:29:02 +01:00
// At this point we have registered the block as belonging to the current
// volume, but it is possible that there is not enough space to put it in
2018-01-31 22:03:37 +01:00
if ( blockvolume . Filesize + ( b . Size * NonCompressibleExpansionFactor ) > maxvolumesize )
2016-02-24 23:48:42 +01:00
{
2018-01-28 12:29:02 +01:00
BlockVolumeWriter tmpvolume = null ;
try
{
// Start a new volume
tmpvolume = new BlockVolumeWriter ( options );
tmpvolume . VolumeID = await database . RegisterRemoteVolumeAsync ( tmpvolume . RemoteFilename , RemoteVolumeType . Blocks , RemoteVolumeState . Temporary );
// Move the current block to the new volume
await database . MoveBlockToVolumeAsync ( b . HashKey , b . Size , blockvolume . VolumeID , tmpvolume . VolumeID );
// Close this volume, and send it to upload
blockvolume . Close ();
await database . CommitTransactionAsync ( "CommitAddBlockToOutputFlush" );
2018-01-30 08:34:57 +01:00
await self . Output . WriteAsync ( new VolumeUploadRequest ( blockvolume , true , indexdata ));
2018-01-28 12:29:02 +01:00
// Continue with the freshly created volume
blockvolume = tmpvolume ;
2018-01-30 08:34:57 +01:00
if ( useindex )
indexdata = new Library . Utility . FileBackedStringList ();
2018-01-28 12:29:02 +01:00
}
catch
{
// If something goes wrong, we need to clear the temp volume
if ( tmpvolume != null && tmpvolume != blockvolume )
try { tmpvolume . Dispose (); }
catch { } // Ignore this and report the original error
throw ;
}
2016-02-20 15:06:29 +01:00
}
2016-02-24 23:48:42 +01:00
2018-01-31 22:03:37 +01:00
#if DEBUG
var presize = blockvolume . Filesize ;
#endif
2018-01-28 12:29:02 +01:00
// Now add the block to the current volume, as we know there is space for it
blockvolume . AddBlock ( b . HashKey , b . Data , b . Offset , ( int ) b . Size , b . Hint );
2018-01-30 08:34:57 +01:00
if ( b . IsBlocklistHashes && useindex )
indexdata . Add ( VolumeUploadRequest . EncodeBlockListEntry ( b . HashKey , b . Size , b . Data ));
2018-01-31 22:03:37 +01:00
#if DEBUG
var volumesizeincrease = blockvolume . Filesize - presize ;
var expectedincrease = ( b . Size * NonCompressibleExpansionFactor ) + BlockCompressionOverhead ;
if ( volumesizeincrease > expectedincrease )
Logging . Log . WriteMessage ( string . Format ( "Size increased {0} bytes more than expected when adding {1} to volume" , volumesizeincrease - expectedincrease , b . HashKey ), Logging . LogMessageType . Warning );
#endif
2016-02-09 09:17:31 +01:00
}
2016-02-27 02:15:42 +01:00
// We ignore the stop signal, but not the pause and terminate
await taskreader . ProgressAsync ;
2016-02-09 09:17:31 +01:00
}
2016-02-24 23:48:42 +01:00
}
catch ( Exception ex )
{
if ( ex . IsRetiredException ())
2016-02-09 09:17:31 +01:00
{
2016-02-24 23:48:42 +01:00
// If we have collected data, merge all pending volumes into a single volume
if ( blockvolume != null && blockvolume . SourceSize > 0 )
2017-08-13 11:58:39 +01:00
{
2018-01-30 08:34:57 +01:00
await self . SpillPickup . WriteAsync ( new VolumeUploadRequest ( blockvolume , true , indexdata ));
2017-08-13 11:58:39 +01:00
}
2016-02-09 09:17:31 +01:00
}
2016-02-24 23:48:42 +01:00
throw ;
2016-02-09 09:17:31 +01:00
}
2016-02-24 23:48:42 +01:00
});
2016-02-09 09:17:31 +01:00
}
}
}