2013-03-27 16:06:45 +01:00
// Copyright (C) 2013, The Duplicati Team
// http://www.duplicati.com, opensource@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 System.Linq ;
using System.Collections.Generic ;
2013-05-08 20:17:07 +02:00
using Duplicati.Library.Main.Database ;
using Duplicati.Library.Main.Volumes ;
2013-03-27 16:06:45 +01:00
using System.Text ;
2018-01-28 12:29:02 +01:00
using System.Threading.Tasks ;
2013-03-27 16:06:45 +01:00
2013-05-08 20:17:07 +02:00
namespace Duplicati.Library.Main.Operation
2013-03-27 16:06:45 +01:00
{
2016-09-15 11:39:27 +02:00
internal class CompactHandler
{
2013-03-27 16:06:45 +01:00
protected string m_backendurl ;
2013-05-06 09:58:19 +02:00
protected Options m_options ;
2013-05-25 16:40:15 +02:00
protected CompactResults m_result ;
2013-03-27 16:06:45 +01:00
2016-09-15 11:39:27 +02:00
public CompactHandler ( string backend , Options options , CompactResults result )
{
2013-03-27 16:06:45 +01:00
m_backendurl = backend ;
m_options = options ;
2013-05-25 16:40:15 +02:00
m_result = result ;
2016-09-15 11:39:27 +02:00
}
public virtual void Run ()
2013-06-09 16:26:08 +02:00
{
if (! System . IO . File . Exists ( m_options . Dbpath ))
throw new Exception ( string . Format ( "Database file does not exist: {0}" , m_options . Dbpath ));
2016-09-15 11:39:27 +02:00
2016-12-29 22:41:13 +01:00
using ( var db = new LocalDeleteDatabase ( m_options . Dbpath , "Compact" ))
2018-01-20 19:12:20 +01:00
using ( var backend = new BackendManager ( m_backendurl , m_options , m_result . BackendWriter , db , m_result ))
2013-06-09 16:26:08 +02:00
{
2018-01-21 07:35:07 +01:00
m_result . SetDatabase ( db );
Utility . UpdateOptionsFromDb ( db , m_options );
Utility . VerifyParameters ( db , m_options );
2018-01-31 22:06:06 +01:00
var changed = DoCompact ( db , false , backend );
2018-01-21 07:35:07 +01:00
if ( changed && m_options . UploadVerificationFile )
FilelistProcessor . UploadVerificationFile ( backend , m_options , m_result . BackendWriter , db );
if (! m_options . Dryrun )
2013-06-09 16:26:08 +02:00
{
2018-01-21 07:35:07 +01:00
if ( changed )
2014-07-16 00:57:57 +02:00
{
2018-01-21 07:35:07 +01:00
db . WriteResults ();
db . CommitTransaction ( "CommitCompact" , false );
if ( m_options . AutoVacuum )
2015-11-17 12:37:33 +01:00
{
2018-01-21 07:35:07 +01:00
db . Vacuum ();
2015-11-17 12:37:33 +01:00
}
2014-07-16 00:57:57 +02:00
}
2016-09-15 11:39:27 +02:00
else
2018-01-21 07:35:07 +01:00
db . CommitTransaction ( "CommitCompact" , false );
2013-06-09 16:26:08 +02:00
}
2018-01-21 07:35:07 +01:00
else
db . RollbackTransaction ( false );
2013-08-23 22:15:07 +02:00
}
2016-09-15 11:39:27 +02:00
}
2018-01-31 22:06:06 +01:00
internal bool DoCompact ( LocalDeleteDatabase db , bool hasVerifiedBackend , BackendManager backend )
2014-05-15 12:47:16 +02:00
{
2018-01-21 07:35:07 +01:00
var report = db . GetCompactReport ( m_options . VolumeSize , m_options . Threshold , m_options . SmallFileSize , m_options . SmallFileMaxCount );
2014-05-15 12:47:16 +02:00
report . ReportCompactData ( m_result );
2016-09-15 11:39:27 +02:00
2014-05-15 12:47:16 +02:00
if ( report . ShouldReclaim || report . ShouldCompact )
2017-09-18 11:54:48 +02:00
{
2018-01-31 22:06:06 +01:00
if (! hasVerifiedBackend && ! m_options . NoBackendverification )
FilelistProcessor . VerifyRemoteList ( backend , m_options , db , m_result . BackendWriter );
2016-09-15 11:39:27 +02:00
2018-01-31 22:06:06 +01:00
BlockVolumeWriter newvol = new BlockVolumeWriter ( m_options );
newvol . VolumeID = db . RegisterRemoteVolume ( newvol . RemoteFilename , RemoteVolumeType . Blocks , RemoteVolumeState . Temporary );
long blocksInVolume = 0 ;
long discardedBlocks = 0 ;
long discardedSize = 0 ;
byte [] buffer = new byte [ m_options . Blocksize ];
var remoteList = db . GetRemoteVolumes (). Where ( n => n . State == RemoteVolumeState . Uploaded || n . State == RemoteVolumeState . Verified ). ToArray ();
//These are for bookkeeping
var uploadedVolumes = new List < KeyValuePair < string , long >>();
var uploadedIndexVolumes = new Dictionary < string , KeyValuePair < string , long >>();
var deletedVolumes = new List < KeyValuePair < string , long >>();
var downloadedVolumes = new List < KeyValuePair < string , long >>();
//We start by deleting unused volumes to save space before uploading new stuff
var fullyDeleteable = ( from v in remoteList
where report . DeleteableVolumes . Contains ( v . Name )
select ( IRemoteVolume ) v ). ToList ();
deletedVolumes . AddRange ( DoDelete ( db , backend , fullyDeleteable ));
2013-04-08 22:24:54 +02:00
2018-01-31 22:06:06 +01:00
// This list is used to pick up unused volumes,
// so they can be deleted once the upload of the
// required fragments is complete
var deleteableVolumes = new List < IRemoteVolume >();
2013-04-08 22:24:54 +02:00
2018-01-31 22:06:06 +01:00
if ( report . ShouldCompact )
{
Func < string , Task < IndexVolumeWriter >> indexCreator = null ;
if ( m_options . IndexfilePolicy != Options . IndexFileStrategy . None )
2014-05-15 12:47:16 +02:00
{
2018-01-31 22:06:06 +01:00
indexCreator = async ( name ) => {
var tmp = await Common . IndexVolumeCreator . CreateIndexVolume ( name , m_options , backend . Database , null );
// We assign to a dictionary, to prevent counting multiple items if the upload fails
// and the index file is recreated
uploadedIndexVolumes [ name ] = new KeyValuePair < string , long >( tmp . RemoteFilename , new System . IO . FileInfo ( tmp . LocalFilename ). Length );
return tmp ;
};
}
2018-01-28 12:29:02 +01:00
2018-01-31 22:06:06 +01:00
var volumesToDownload = ( from v in remoteList
where report . CompactableVolumes . Contains ( v . Name )
select ( IRemoteVolume ) v ). ToList ();
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
var dl = new AsyncDownloader ( volumesToDownload , backend );
using ( var q = db . CreateBlockQueryHelper ( m_options ))
{
while ( dl . MoveNext ())
2014-05-15 12:47:16 +02:00
{
2018-01-31 22:06:06 +01:00
var entry = dl . Current ;
using ( var tmpfile = entry . TempFile )
2014-05-15 12:47:16 +02:00
{
2018-01-31 22:06:06 +01:00
if ( m_result . TaskControlRendevouz () == TaskControlState . Stop )
2016-09-15 11:39:27 +02:00
{
2018-01-31 22:06:06 +01:00
backend . WaitForComplete ();
return false ;
}
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
downloadedVolumes . Add ( new KeyValuePair < string , long >( entry . Name , entry . Size ));
var inst = VolumeBase . ParseFilename ( entry . Name );
using ( var f = new BlockVolumeReader ( inst . CompressionModule , tmpfile , m_options ))
{
foreach ( var e in f . Blocks )
2016-09-15 11:39:27 +02:00
{
2018-01-31 22:06:06 +01:00
if ( q . UseBlock ( e . Key , e . Value ))
2016-09-15 11:39:27 +02:00
{
2018-01-31 22:06:06 +01:00
//TODO: How do we get the compression hint? Reverse query for filename in db?
var s = f . ReadBlock ( e . Key , buffer );
if ( s != e . Value )
throw new Exception ( string . Format ( "Size mismatch problem for block {0}, {1} vs {2}" , e . Key , s , e . Value ));
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
newvol . AddBlock ( e . Key , buffer , 0 , s , Duplicati . Library . Interface . CompressionHint . Compressible );
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
db . MoveBlockToNewVolume ( e . Key , e . Value , newvol . VolumeID );
blocksInVolume ++;
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
if ( newvol . Filesize > m_options . VolumeSize )
{
uploadedVolumes . Add ( new KeyValuePair < string , long >( newvol . RemoteFilename , newvol . Filesize ));
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
newvol . Close ();
if (! m_options . Dryrun )
backend . Put ( newvol , indexCreator );
else
m_result . AddDryrunMessage ( string . Format ( "Would upload generated blockset of size {0}" , Library . Utility . Utility . FormatSizeString ( newvol . Filesize )));
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
newvol = new BlockVolumeWriter ( m_options );
newvol . VolumeID = db . RegisterRemoteVolume ( newvol . RemoteFilename , RemoteVolumeType . Blocks , RemoteVolumeState . Temporary );
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
blocksInVolume = 0 ;
2018-01-20 19:12:20 +01:00
2018-01-31 22:06:06 +01:00
//After we upload this volume, we can delete all previous encountered volumes
deletedVolumes . AddRange ( DoDelete ( db , backend , deleteableVolumes ));
deleteableVolumes = new List < IRemoteVolume >();
2018-01-20 19:12:20 +01:00
}
2016-09-15 11:39:27 +02:00
}
2018-01-31 22:06:06 +01:00
else
{
discardedBlocks ++;
discardedSize += e . Value ;
}
2016-09-15 11:39:27 +02:00
}
}
2018-01-31 22:06:06 +01:00
deleteableVolumes . Add ( entry );
2016-09-15 11:39:27 +02:00
}
2018-01-31 22:06:06 +01:00
}
if ( blocksInVolume > 0 )
{
uploadedVolumes . Add ( new KeyValuePair < string , long >( newvol . RemoteFilename , newvol . Filesize ));
newvol . Close ();
if (! m_options . Dryrun )
backend . Put ( newvol , indexCreator );
2016-09-15 11:39:27 +02:00
else
2018-01-31 22:06:06 +01:00
m_result . AddDryrunMessage ( string . Format ( "Would upload generated blockset of size {0}" , Library . Utility . Utility . FormatSizeString ( newvol . Filesize )));
2016-09-15 11:39:27 +02:00
}
2013-08-24 22:31:16 +02:00
else
2018-01-31 22:06:06 +01:00
{
db . RemoveRemoteVolume ( newvol . RemoteFilename );
}
2016-09-15 11:39:27 +02:00
}
}
2018-01-31 22:06:06 +01:00
deletedVolumes . AddRange ( DoDelete ( db , backend , deleteableVolumes ));
backend . WaitForEmpty ();
2016-12-01 23:59:54 +01:00
2018-01-31 22:06:06 +01:00
uploadedVolumes . AddRange ( uploadedIndexVolumes . Values );
var downloadSize = downloadedVolumes . Where ( x => x . Value >= 0 ). Aggregate ( 0L , ( a , x ) => a + x . Value );
var deletedSize = deletedVolumes . Where ( x => x . Value >= 0 ). Aggregate ( 0L , ( a , x ) => a + x . Value );
var uploadSize = uploadedVolumes . Where ( x => x . Value >= 0 ). Aggregate ( 0L , ( a , x ) => a + x . Value );
m_result . DeletedFileCount = deletedVolumes . Count ;
m_result . DownloadedFileCount = downloadedVolumes . Count ;
m_result . UploadedFileCount = uploadedVolumes . Count ;
m_result . DeletedFileSize = deletedSize ;
m_result . DownloadedFileSize = downloadSize ;
m_result . UploadedFileSize = uploadSize ;
m_result . Dryrun = m_options . Dryrun ;
if ( m_result . Dryrun )
{
if ( downloadedVolumes . Count == 0 )
m_result . AddDryrunMessage ( string . Format ( "Would delete {0} files, which would reduce storage by {1}" , m_result . DeletedFileCount , Library . Utility . Utility . FormatSizeString ( m_result . DeletedFileSize )));
else
m_result . AddDryrunMessage ( string . Format ( "Would download {0} file(s) with a total size of {1}, delete {2} file(s) with a total size of {3}, and compact to {4} file(s) with a size of {5}, which would reduce storage by {6} file(s) and {7}" ,
m_result . DownloadedFileCount ,
Library . Utility . Utility . FormatSizeString ( m_result . DownloadedFileSize ),
m_result . DeletedFileCount ,
Library . Utility . Utility . FormatSizeString ( m_result . DeletedFileSize ), m_result . UploadedFileCount ,
Library . Utility . Utility . FormatSizeString ( m_result . UploadedFileSize ),
m_result . DeletedFileCount - m_result . UploadedFileCount ,
Library . Utility . Utility . FormatSizeString ( m_result . DeletedFileSize - m_result . UploadedFileSize )));
}
else
{
if ( m_result . DownloadedFileCount == 0 )
m_result . AddMessage ( string . Format ( "Deleted {0} files, which reduced storage by {1}" , m_result . DeletedFileCount , Library . Utility . Utility . FormatSizeString ( m_result . DeletedFileSize )));
else
m_result . AddMessage ( string . Format ( "Downloaded {0} file(s) with a total size of {1}, deleted {2} file(s) with a total size of {3}, and compacted to {4} file(s) with a size of {5}, which reduced storage by {6} file(s) and {7}" ,
m_result . DownloadedFileCount ,
Library . Utility . Utility . FormatSizeString ( downloadSize ),
m_result . DeletedFileCount ,
Library . Utility . Utility . FormatSizeString ( m_result . DeletedFileSize ),
m_result . UploadedFileCount ,
Library . Utility . Utility . FormatSizeString ( m_result . UploadedFileSize ),
m_result . DeletedFileCount - m_result . UploadedFileCount ,
Library . Utility . Utility . FormatSizeString ( m_result . DeletedFileSize - m_result . UploadedFileSize )));
}
backend . WaitForEmpty ();
2016-12-01 23:59:54 +01:00
m_result . EndTime = DateTime . UtcNow ;
2013-07-01 14:40:45 +02:00
return ( m_result . DeletedFileCount + m_result . UploadedFileCount ) > 0 ;
2016-09-15 11:39:27 +02:00
}
else
{
2016-12-01 23:59:54 +01:00
m_result . EndTime = DateTime . UtcNow ;
2013-07-01 14:40:45 +02:00
return false ;
2016-09-15 11:39:27 +02:00
}
}
2015-11-17 12:37:33 +01:00
2018-01-21 07:35:07 +01:00
private IEnumerable < KeyValuePair < string , long >> DoDelete ( LocalDeleteDatabase db , BackendManager backend , IEnumerable < IRemoteVolume > deleteableVolumes )
2015-11-17 12:37:33 +01:00
{
// Mark all volumes as disposable
foreach ( var f in deleteableVolumes )
2018-01-21 07:35:07 +01:00
db . UpdateRemoteVolume ( f . Name , RemoteVolumeState . Deleting , f . Size , f . Hash );
2015-11-17 12:37:33 +01:00
// Before we commit the current state, make sure the backend has caught up
2018-01-21 07:35:07 +01:00
backend . WaitForEmpty ();
2015-11-17 12:37:33 +01:00
if (! m_options . Dryrun )
2018-01-21 07:35:07 +01:00
db . CommitTransaction ( "AfterDeleteRemotes" );
2015-11-17 12:37:33 +01:00
2018-01-21 07:35:07 +01:00
return PerformDelete ( backend , db . GetDeletableVolumes ( deleteableVolumes ));
2015-11-17 12:37:33 +01:00
}
2016-09-15 11:39:27 +02:00
2018-01-21 07:35:07 +01:00
private IEnumerable < KeyValuePair < string , long >> PerformDelete ( BackendManager backend , IEnumerable < IRemoteVolume > list )
2016-09-15 11:39:27 +02:00
{
2018-01-20 19:12:20 +01:00
var res = new List < KeyValuePair < string , long >>();
2015-11-17 12:37:33 +01:00
foreach ( var f in list )
2016-09-15 11:39:27 +02:00
{
if (! m_options . Dryrun )
2018-01-21 07:35:07 +01:00
backend . Delete ( f . Name , f . Size );
2016-09-15 11:39:27 +02:00
else
m_result . AddDryrunMessage ( string . Format ( "Would delete remote file: {0}, size: {1}" , f . Name , Library . Utility . Utility . FormatSizeString ( f . Size )));
2013-04-08 22:24:54 +02:00
2018-01-20 19:12:20 +01:00
res . Add ( new KeyValuePair < string , long >( f . Name , f . Size ));
}
return res ;
2016-09-15 11:39:27 +02:00
}
}
2013-03-27 16:06:45 +01:00
}