This PR has a large blast radius because it takes the final step and bumps up the Controller to be fully async. We have historically done a piece-by-piece update, so all operations were already async but the controller interface was kept synchronous. With this update, the controller is now fully async and all tests are updated. Most places where the new C# compiler warns about function names not ending in `Async` were also adressed, giving a massive refactor change. Functionally, no changes are done.
189 lines
7.7 KiB
C#
189 lines
7.7 KiB
C#
// Copyright (C) 2026, 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
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Duplicati.Library.Main.Database;
|
|
using Duplicati.Library.Utility;
|
|
|
|
namespace Duplicati.Library.Main.Operation.Common
|
|
{
|
|
/// <summary>
|
|
/// Asynchronous interface that ensures all requests
|
|
/// to the database are performed in a sequential manner
|
|
/// </summary>
|
|
internal class DatabaseCommon : SingleRunner, IDisposable
|
|
{
|
|
/// <summary>
|
|
/// The tag used for log messages
|
|
/// </summary>
|
|
private static readonly string LOGTAG = Logging.Log.LogTagFromType<DatabaseCommon>();
|
|
|
|
protected readonly LocalDatabase m_db;
|
|
protected readonly Options m_options;
|
|
|
|
public DatabaseCommon(LocalDatabase db, Options options)
|
|
: base()
|
|
{
|
|
m_db = db;
|
|
m_options = options;
|
|
}
|
|
|
|
public Task<long> RegisterRemoteVolumeAsync(string name, RemoteVolumeType type, RemoteVolumeState state, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.RegisterRemoteVolumeAsync(name, type, state, cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task UpdateRemoteVolumeAsync(string name, RemoteVolumeState state, long size, string hash, bool suppressCleanup, TimeSpan deleteGraceTime, bool? setArchived, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.UpdateRemoteVolumeAsync(name, state, size, hash, suppressCleanup, deleteGraceTime, setArchived, cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public async Task FlushBackendMessagesAndCommitAsync(IBackendManager backendManager, CancellationToken cancellationToken)
|
|
{
|
|
await FlushPendingBackendMessagesAsync(backendManager, cancellationToken).ConfigureAwait(false);
|
|
await CommitTransactionAsync("FlushBackendMessagesAndCommitAsync", true, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
private Task FlushPendingBackendMessagesAsync(IBackendManager backendManager, CancellationToken cancellationToken)
|
|
=> RunOnMainAsync(async () => await backendManager.FlushPendingMessagesAsync(m_db, cancellationToken).ConfigureAwait(false));
|
|
|
|
public Task CommitTransactionAsync(string message, bool restart, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
{
|
|
if (m_options.Dryrun)
|
|
{
|
|
await m_db.Transaction
|
|
.RollBackAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
using (new Logging.Timer(LOGTAG, "CommitTransactionAsync", message))
|
|
await m_db.Transaction
|
|
.CommitAsync(message, restart, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
public Task RollbackTransactionAsync(CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db.Transaction
|
|
.RollBackAsync(cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task RenameRemoteFileAsync(string oldname, string newname, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.RenameRemoteFileAsync(oldname, newname, cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task LogRemoteOperationAsync(string operation, string path, string data, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.LogRemoteOperationAsync(operation, path, data, cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task<LocalDatabase.IBlock[]> GetBlocksAsync(long volumeid, CancellationToken cancellationToken)
|
|
{
|
|
// TODO: Figure out how to return the enumerable, while keeping the lock
|
|
// and not creating the entire result in memory
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.GetBlocksAsync(volumeid, cancellationToken)
|
|
.ToArrayAsync(cancellationToken: cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task<RemoteVolumeEntry> GetVolumeInfoAsync(string remotename, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.GetRemoteVolumeAsync(remotename, cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task<(string Hash, byte[] Buffer, int Size)[]> GetBlocklistsAsync(long volumeid, int blocksize, int hashsize, CancellationToken cancellationToken)
|
|
{
|
|
// TODO: Figure out how to return the enumerable, while keeping the lock
|
|
// and not creating the entire result in memory
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.GetBlocklistsAsync(volumeid, blocksize, hashsize, cancellationToken)
|
|
.ToArrayAsync(cancellationToken: cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task<long> GetRemoteVolumeIDAsync(string remotename, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.GetRemoteVolumeIDAsync(remotename, cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
public Task AddIndexBlockLinkAsync(long indexVolumeID, long blockVolumeID, CancellationToken cancellationToken)
|
|
{
|
|
return RunOnMainAsync(async () =>
|
|
await m_db
|
|
.AddIndexBlockLinkAsync(indexVolumeID, blockVolumeID, cancellationToken)
|
|
.ConfigureAwait(false)
|
|
);
|
|
}
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
{
|
|
if (m_workerSource.IsCancellationRequested)
|
|
return;
|
|
|
|
base.Dispose(isDisposing);
|
|
}
|
|
|
|
}
|
|
}
|
|
|