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 CoCoL;
|
|
|
|
|
using Duplicati.Library.Main.Operation.Common;
|
2016-02-15 09:01:05 +01:00
|
|
|
using Duplicati.Library.Main.Volumes;
|
2019-02-22 19:37:35 -06:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using static Duplicati.Library.Main.Operation.Common.BackendHandler;
|
2016-02-09 09:17:31 +01:00
|
|
|
|
|
|
|
|
namespace Duplicati.Library.Main.Operation.Backup
|
|
|
|
|
{
|
2019-02-22 19:37:35 -06:00
|
|
|
internal class SpillVolumeRequest
|
|
|
|
|
{
|
|
|
|
|
public BlockVolumeWriter BlockVolume { get; private set; }
|
|
|
|
|
public TemporaryIndexVolume IndexVolume { get; private set; }
|
|
|
|
|
|
|
|
|
|
public SpillVolumeRequest(BlockVolumeWriter blockvolume, TemporaryIndexVolume indexvolume)
|
|
|
|
|
{
|
|
|
|
|
BlockVolume = blockvolume;
|
|
|
|
|
IndexVolume = indexvolume;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 09:17:31 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// This process just waits until all block processes are terminated
|
|
|
|
|
/// and collects the non-written volumes.
|
|
|
|
|
/// All remaining volumes are re-packed into one or more filled
|
|
|
|
|
/// volumes and uploaded
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static class SpillCollectorProcess
|
|
|
|
|
{
|
2016-02-27 02:15:42 +01:00
|
|
|
public static Task Run(Options options, BackupDatabase database, ITaskReader taskreader)
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
|
|
|
|
return AutomationExtensions.RunTask(
|
2016-02-24 23:48:42 +01:00
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
Input = Channels.SpillPickup.ForRead,
|
|
|
|
|
Output = Channels.BackendRequest.ForWrite,
|
|
|
|
|
},
|
|
|
|
|
|
2019-02-22 19:37:35 -06:00
|
|
|
async self =>
|
2016-02-24 23:48:42 +01:00
|
|
|
{
|
2019-02-22 19:37:35 -06:00
|
|
|
var lst = new List<SpillVolumeRequest>();
|
2016-02-24 23:48:42 +01:00
|
|
|
|
2019-03-07 18:16:39 -06:00
|
|
|
while (!await self.Input.IsRetiredAsync.ConfigureAwait(false))
|
2016-02-24 23:48:42 +01:00
|
|
|
try
|
|
|
|
|
{
|
2019-03-07 18:16:39 -06:00
|
|
|
lst.Add(await self.Input.ReadAsync().ConfigureAwait(false));
|
2016-02-24 23:48:42 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.IsRetiredException())
|
|
|
|
|
break;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2016-02-09 09:17:31 +01:00
|
|
|
|
|
|
|
|
|
2019-02-22 19:37:35 -06:00
|
|
|
while (lst.Count > 1)
|
2016-02-24 23:48:42 +01:00
|
|
|
{
|
2016-02-27 02:15:42 +01:00
|
|
|
// We ignore the stop signal, but not the pause and terminate
|
2019-03-07 18:16:39 -06:00
|
|
|
await taskreader.ProgressAsync.ConfigureAwait(false);
|
2016-02-15 09:01:05 +01:00
|
|
|
|
2019-02-22 19:37:35 -06:00
|
|
|
SpillVolumeRequest target = null;
|
2016-02-24 23:48:42 +01:00
|
|
|
var source = lst[0];
|
2016-02-15 09:01:05 +01:00
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
// Finalize the current work
|
|
|
|
|
source.BlockVolume.Close();
|
2016-02-15 09:01:05 +01:00
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
// Remove it from the list of active operations
|
|
|
|
|
lst.RemoveAt(0);
|
2016-02-15 09:01:05 +01:00
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
var buffer = new byte[options.Blocksize];
|
2016-02-15 09:01:05 +01:00
|
|
|
|
2019-02-22 19:37:35 -06:00
|
|
|
using (var rd = new BlockVolumeReader(options.CompressionModule, source.BlockVolume.LocalFilename, options))
|
2016-02-24 23:48:42 +01:00
|
|
|
{
|
2019-02-22 19:37:35 -06:00
|
|
|
foreach (var file in rd.Blocks)
|
2016-02-15 09:01:05 +01:00
|
|
|
{
|
2016-02-24 23:48:42 +01:00
|
|
|
// Grab a target
|
|
|
|
|
if (target == null)
|
2016-02-15 09:01:05 +01:00
|
|
|
{
|
2016-02-24 23:48:42 +01:00
|
|
|
if (lst.Count == 0)
|
2016-02-15 09:01:05 +01:00
|
|
|
{
|
2016-02-24 23:48:42 +01:00
|
|
|
// No more targets, make one
|
2019-02-22 19:37:35 -06:00
|
|
|
target = new SpillVolumeRequest(new BlockVolumeWriter(options), source.IndexVolume == null ? null : new TemporaryIndexVolume(options));
|
2019-03-07 18:16:39 -06:00
|
|
|
target.BlockVolume.VolumeID = await database.RegisterRemoteVolumeAsync(target.BlockVolume.RemoteFilename, RemoteVolumeType.Blocks, RemoteVolumeState.Temporary).ConfigureAwait(false);
|
2016-02-15 09:01:05 +01:00
|
|
|
}
|
2016-02-24 23:48:42 +01:00
|
|
|
else
|
2016-02-15 09:01:05 +01:00
|
|
|
{
|
2016-02-24 23:48:42 +01:00
|
|
|
// Grab the next target
|
|
|
|
|
target = lst[0];
|
|
|
|
|
lst.RemoveAt(0);
|
2016-02-15 09:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-30 14:04:30 +02:00
|
|
|
// We copy all the blocklisthashes, which may create duplicates
|
|
|
|
|
// but otherwise we need to query all hashes to see if they are blocklisthashes
|
|
|
|
|
if (source.IndexVolume != null)
|
|
|
|
|
source.IndexVolume.CopyTo(target.IndexVolume, true);
|
|
|
|
|
}
|
2016-02-15 09:01:05 +01:00
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
var len = rd.ReadBlock(file.Key, buffer);
|
|
|
|
|
target.BlockVolume.AddBlock(file.Key, buffer, 0, len, Duplicati.Library.Interface.CompressionHint.Default);
|
2019-03-07 18:16:39 -06:00
|
|
|
await database.MoveBlockToVolumeAsync(file.Key, len, source.BlockVolume.VolumeID, target.BlockVolume.VolumeID).ConfigureAwait(false);
|
2016-02-15 09:01:05 +01:00
|
|
|
|
2018-04-30 14:04:30 +02:00
|
|
|
if (target.IndexVolume != null)
|
|
|
|
|
target.IndexVolume.AddBlock(file.Key, len);
|
|
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
if (target.BlockVolume.Filesize > options.VolumeSize - options.Blocksize)
|
|
|
|
|
{
|
2016-02-27 02:15:42 +01:00
|
|
|
target.BlockVolume.Close();
|
2019-03-07 18:16:39 -06:00
|
|
|
await UploadVolumeAndIndex(target, self.Output, options, database).ConfigureAwait(false);
|
2016-02-24 23:48:42 +01:00
|
|
|
target = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
// Make sure they are out of the database
|
|
|
|
|
System.IO.File.Delete(source.BlockVolume.LocalFilename);
|
2019-03-07 18:16:39 -06:00
|
|
|
await database.SafeDeleteRemoteVolumeAsync(source.BlockVolume.RemoteFilename).ConfigureAwait(false);
|
2016-02-24 23:48:42 +01:00
|
|
|
|
|
|
|
|
// Re-inject the target if it has content
|
|
|
|
|
if (target != null)
|
|
|
|
|
lst.Insert(lst.Count == 0 ? 0 : 1, target);
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
2016-02-24 23:48:42 +01:00
|
|
|
|
2019-02-22 19:37:35 -06:00
|
|
|
foreach (var n in lst)
|
2016-02-24 23:48:42 +01:00
|
|
|
{
|
2016-02-27 02:15:42 +01:00
|
|
|
// We ignore the stop signal, but not the pause and terminate
|
2019-03-07 18:16:39 -06:00
|
|
|
await taskreader.ProgressAsync.ConfigureAwait(false);
|
2016-02-27 02:15:42 +01:00
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
n.BlockVolume.Close();
|
2019-03-07 18:16:39 -06:00
|
|
|
await UploadVolumeAndIndex(n, self.Output, options, database).ConfigureAwait(false);
|
2016-02-24 23:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
2019-02-22 19:37:35 -06:00
|
|
|
|
|
|
|
|
private static async Task UploadVolumeAndIndex(SpillVolumeRequest target, IWriteChannel<IUploadRequest> outputChannel, Options options, BackupDatabase database)
|
|
|
|
|
{
|
2019-10-04 21:37:33 -07:00
|
|
|
var blockEntry = target.BlockVolume.CreateFileEntryForUpload(options);
|
2019-02-22 19:37:35 -06:00
|
|
|
|
2019-10-04 22:02:20 -07:00
|
|
|
TemporaryIndexVolume indexVolumeCopy = null;
|
2019-02-22 19:37:35 -06:00
|
|
|
if (target.IndexVolume != null)
|
|
|
|
|
{
|
2019-10-04 22:02:20 -07:00
|
|
|
indexVolumeCopy = new TemporaryIndexVolume(options);
|
|
|
|
|
target.IndexVolume.CopyTo(indexVolumeCopy, false);
|
2019-02-22 19:37:35 -06:00
|
|
|
}
|
|
|
|
|
|
2019-10-04 22:02:20 -07:00
|
|
|
var uploadRequest = new VolumeUploadRequest(target.BlockVolume, blockEntry, indexVolumeCopy, options, database);
|
2019-02-22 19:37:35 -06:00
|
|
|
await outputChannel.WriteAsync(uploadRequest).ConfigureAwait(false);
|
|
|
|
|
}
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
|
|
|
|
}
|