2025-01-28 08:54:50 +01:00
|
|
|
// Copyright (C) 2025, The Duplicati Team
|
|
|
|
|
// https://duplicati.com, hello@duplicati.com
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
2024-02-28 15:45:30 +01:00
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2016-02-09 09:17:31 +01:00
|
|
|
using CoCoL;
|
|
|
|
|
using Duplicati.Library.Main.Operation.Common;
|
2019-02-22 19:37:35 -06:00
|
|
|
using Duplicati.Library.Main.Volumes;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
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
|
|
|
|
|
{
|
2025-01-28 08:54:50 +01:00
|
|
|
public static Task Run(Channels channels, BackupDatabase database, IBackendManager backendManager, Options options, ITaskReader taskreader)
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
|
|
|
|
return AutomationExtensions.RunTask(
|
2016-02-24 23:48:42 +01:00
|
|
|
new
|
|
|
|
|
{
|
2024-12-09 13:55:22 +01:00
|
|
|
Input = channels.OutputBlocks.AsRead(),
|
|
|
|
|
SpillPickup = channels.SpillPickup.AsWrite(),
|
2016-02-24 23:48:42 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async self =>
|
|
|
|
|
{
|
2018-04-30 14:04:30 +02:00
|
|
|
var noIndexFiles = options.IndexfilePolicy == Options.IndexFileStrategy.None;
|
|
|
|
|
var fullIndexFiles = options.IndexfilePolicy == Options.IndexFileStrategy.Full;
|
2024-10-31 13:42:07 +01:00
|
|
|
var blocklistHashesAdded = 0L;
|
2018-04-30 14:04:30 +02:00
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
BlockVolumeWriter blockvolume = null;
|
2018-04-30 14:04:30 +02:00
|
|
|
TemporaryIndexVolume indexvolume = null;
|
2016-02-24 23:48:42 +01:00
|
|
|
|
2024-12-18 08:27:59 +01:00
|
|
|
System.Diagnostics.Stopwatch sw_workload = new();
|
2024-11-01 13:32:40 +01:00
|
|
|
long allowed_workload_ms = options.CPUIntensity * 100;
|
|
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
try
|
2016-02-09 09:17:31 +01:00
|
|
|
{
|
2019-02-22 19:37:35 -06: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
|
|
|
|
2024-11-01 13:32:40 +01:00
|
|
|
// Check if the process has spent more than allowed workload time
|
|
|
|
|
if (options.CPUIntensity < 10 && sw_workload.ElapsedMilliseconds > allowed_workload_ms)
|
|
|
|
|
{
|
|
|
|
|
// Sleep the remaining time
|
2024-12-18 08:27:59 +01:00
|
|
|
int time_to_sleep = 1000 - (int)allowed_workload_ms;
|
2024-11-01 13:32:40 +01:00
|
|
|
await Task.Delay(time_to_sleep);
|
|
|
|
|
sw_workload.Reset();
|
|
|
|
|
}
|
|
|
|
|
sw_workload.Start();
|
|
|
|
|
|
2024-10-31 13:42:07 +01:00
|
|
|
// We need these blocks to be stored in the full index files
|
|
|
|
|
var isMandatoryBlocklistHash = b.IsBlocklistHashes && fullIndexFiles;
|
|
|
|
|
|
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
|
2024-10-31 13:42:07 +01:00
|
|
|
if (!isMandatoryBlocklistHash && 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);
|
2024-11-01 13:32:40 +01:00
|
|
|
sw_workload.Stop();
|
2016-02-24 23:48:42 +01:00
|
|
|
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);
|
2018-04-30 14:04:30 +02:00
|
|
|
|
2019-02-22 19:37:35 -06:00
|
|
|
indexvolume = noIndexFiles ? null : new TemporaryIndexVolume(options);
|
2024-10-31 13:42:07 +01:00
|
|
|
blocklistHashesAdded = 0;
|
2016-02-24 23:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newBlock = await database.AddBlockAsync(b.HashKey, b.Size, blockvolume.VolumeID);
|
|
|
|
|
b.TaskCompletion.TrySetResult(newBlock);
|
2016-02-20 15:06:29 +01:00
|
|
|
|
2024-10-31 13:42:07 +01:00
|
|
|
// If we are recording blocklist hashes, add them to the index file,
|
|
|
|
|
// even if they are already added as non-blocklist blocks, but filter out duplicates
|
2024-10-31 15:30:25 +01:00
|
|
|
if (indexvolume != null && isMandatoryBlocklistHash)
|
2024-10-31 13:42:07 +01:00
|
|
|
{
|
2024-12-05 16:41:28 +01:00
|
|
|
// This can cause a race between workers,
|
|
|
|
|
// but the side-effect is that the index files are slightly larger
|
2024-10-31 15:30:25 +01:00
|
|
|
if (newBlock || !await database.IsBlocklistHashKnownAsync(b.HashKey))
|
|
|
|
|
{
|
|
|
|
|
blocklistHashesAdded++;
|
|
|
|
|
indexvolume.AddBlockListHash(b.HashKey, b.Size, b.Data);
|
|
|
|
|
}
|
2024-10-31 13:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-24 23:48:42 +01:00
|
|
|
if (newBlock)
|
|
|
|
|
{
|
|
|
|
|
blockvolume.AddBlock(b.HashKey, b.Data, b.Offset, (int)b.Size, b.Hint);
|
2018-04-30 14:04:30 +02:00
|
|
|
if (indexvolume != null)
|
|
|
|
|
indexvolume.AddBlock(b.HashKey, b.Size);
|
2016-02-09 09:17:31 +01:00
|
|
|
|
2016-02-25 19:15:51 +01:00
|
|
|
// If the volume is full, send to upload
|
2016-02-24 23:48:42 +01:00
|
|
|
if (blockvolume.Filesize > options.VolumeSize - options.Blocksize)
|
|
|
|
|
{
|
2017-08-12 14:20:35 +01:00
|
|
|
//When uploading a new volume, we register the volumes and then flush the transaction
|
2016-02-27 02:15:42 +01:00
|
|
|
// this ensures that the local database and remote storage are as closely related as possible
|
|
|
|
|
await database.UpdateRemoteVolumeAsync(blockvolume.RemoteFilename, RemoteVolumeState.Uploading, -1, null);
|
2019-02-22 19:37:35 -06:00
|
|
|
|
2016-02-27 02:15:42 +01:00
|
|
|
blockvolume.Close();
|
|
|
|
|
|
|
|
|
|
await database.CommitTransactionAsync("CommitAddBlockToOutputFlush");
|
2017-08-12 14:20:35 +01:00
|
|
|
|
2025-01-28 08:54:50 +01:00
|
|
|
IndexVolumeWriter indexVolumeCopy = null;
|
2019-03-30 19:55:36 -05:00
|
|
|
if (indexvolume != null)
|
2019-02-22 19:37:35 -06:00
|
|
|
{
|
2025-01-28 08:54:50 +01:00
|
|
|
// TODO: It is much easier to let the BackendManager deal with index files,
|
|
|
|
|
// but it adds a bit of strain to the database
|
|
|
|
|
indexVolumeCopy = await indexvolume.CreateVolume(blockvolume.RemoteFilename, options, database);
|
|
|
|
|
// Create link before upload is started, it will be removed later if upload fails
|
|
|
|
|
await database.AddIndexBlockLinkAsync(indexVolumeCopy.VolumeID, blockvolume.VolumeID).ConfigureAwait(false);
|
2019-02-22 19:37:35 -06:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 08:54:50 +01:00
|
|
|
var blockVolumeCopy = blockvolume;
|
2016-02-27 02:15:42 +01:00
|
|
|
blockvolume = null;
|
2018-04-30 14:04:30 +02:00
|
|
|
indexvolume = null;
|
2020-01-05 21:43:56 -06:00
|
|
|
|
2025-01-28 08:54:50 +01:00
|
|
|
await backendManager.PutAsync(blockVolumeCopy, indexVolumeCopy, null, false, taskreader.ProgressToken);
|
|
|
|
|
|
2016-02-20 15:06:29 +01:00
|
|
|
}
|
2016-02-24 23:48:42 +01:00
|
|
|
|
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
|
2024-12-18 08:27:59 +01:00
|
|
|
await taskreader.ProgressRendevouz().ConfigureAwait(false);
|
2024-11-01 13:32:40 +01:00
|
|
|
|
|
|
|
|
sw_workload.Stop();
|
2016-02-09 09:17:31 +01:00
|
|
|
}
|
2016-02-24 23:48:42 +01:00
|
|
|
}
|
2019-02-22 19:37:35 -06:00
|
|
|
catch (Exception ex)
|
2016-02-24 23:48:42 +01:00
|
|
|
{
|
|
|
|
|
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
|
2024-12-05 16:41:28 +01:00
|
|
|
if (blockvolume != null)
|
2017-08-13 11:58:39 +01:00
|
|
|
{
|
2024-12-05 16:41:28 +01:00
|
|
|
if (blockvolume.SourceSize > 0 || blocklistHashesAdded > 0)
|
|
|
|
|
{
|
|
|
|
|
await self.SpillPickup.WriteAsync(new SpillVolumeRequest(blockvolume, indexvolume));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await database.RemoveRemoteVolumeAsync(blockvolume.RemoteFilename);
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|