mirror of
https://github.com/ppy/osu.git
synced 2025-01-15 13:33:03 +08:00
Merge pull request #18997 from peppy/fix-realm-backup-ctor-fail
Fix realm backup creation failing when run from `RealmAccess` constructor
This commit is contained in:
commit
b49a1aab8a
@ -138,7 +138,7 @@ namespace osu.Game.Tests.Collections.IO
|
|||||||
{
|
{
|
||||||
string firstRunName;
|
string firstRunName;
|
||||||
|
|
||||||
using (var host = new CleanRunHeadlessGameHost(bypassCleanup: true))
|
using (var host = new CleanRunHeadlessGameHost(bypassCleanupOnDispose: true))
|
||||||
{
|
{
|
||||||
firstRunName = host.Name;
|
firstRunName = host.Name;
|
||||||
|
|
||||||
|
@ -315,6 +315,26 @@ namespace osu.Game.Tests.NonVisual
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestBackupCreatedOnCorruptRealm()
|
||||||
|
{
|
||||||
|
using (var host = new CustomTestHeadlessGameHost())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.WriteAllText(host.InitialStorage.GetFullPath(OsuGameBase.CLIENT_DATABASE_FILENAME, true), "i am definitely not a realm file");
|
||||||
|
|
||||||
|
LoadOsuIntoHost(host);
|
||||||
|
|
||||||
|
Assert.That(host.InitialStorage.GetFiles(string.Empty, "*_corrupt.realm"), Has.One.Items);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
host.Exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static string getDefaultLocationFor(CustomTestHeadlessGameHost host)
|
private static string getDefaultLocationFor(CustomTestHeadlessGameHost host)
|
||||||
{
|
{
|
||||||
string path = Path.Combine(TestRunHeadlessGameHost.TemporaryTestDirectory, host.Name);
|
string path = Path.Combine(TestRunHeadlessGameHost.TemporaryTestDirectory, host.Name);
|
||||||
@ -347,7 +367,7 @@ namespace osu.Game.Tests.NonVisual
|
|||||||
public Storage InitialStorage { get; }
|
public Storage InitialStorage { get; }
|
||||||
|
|
||||||
public CustomTestHeadlessGameHost([CallerMemberName] string callingMethodName = @"")
|
public CustomTestHeadlessGameHost([CallerMemberName] string callingMethodName = @"")
|
||||||
: base(callingMethodName: callingMethodName)
|
: base(callingMethodName: callingMethodName, bypassCleanupOnSetup: true)
|
||||||
{
|
{
|
||||||
string defaultStorageLocation = getDefaultLocationFor(this);
|
string defaultStorageLocation = getDefaultLocationFor(this);
|
||||||
|
|
||||||
|
@ -132,11 +132,12 @@ namespace osu.Game.Database
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
realm.CreateBackup(Path.Combine(backup_folder, $"client.{backupSuffix}.realm"), realmBlockOperations);
|
realm.CreateBackup(Path.Combine(backup_folder, $"client.{backupSuffix}.realm"));
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
// Above call will dispose of the blocking token when done.
|
// Once the backup is created, we need to stop blocking operations so the migration can complete.
|
||||||
|
realmBlockOperations.Dispose();
|
||||||
// Clean up here so we don't accidentally dispose twice.
|
// Clean up here so we don't accidentally dispose twice.
|
||||||
realmBlockOperations = null;
|
realmBlockOperations = null;
|
||||||
}
|
}
|
||||||
|
@ -184,14 +184,14 @@ namespace osu.Game.Database
|
|||||||
|
|
||||||
// If a newer version database already exists, don't backup again. We can presume that the first backup is the one we care about.
|
// If a newer version database already exists, don't backup again. We can presume that the first backup is the one we care about.
|
||||||
if (!storage.Exists(newerVersionFilename))
|
if (!storage.Exists(newerVersionFilename))
|
||||||
CreateBackup(newerVersionFilename);
|
createBackup(newerVersionFilename);
|
||||||
|
|
||||||
storage.Delete(Filename);
|
storage.Delete(Filename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Logger.Error(e, "Realm startup failed with unrecoverable error; starting with a fresh database. A backup of your database has been made.");
|
Logger.Error(e, "Realm startup failed with unrecoverable error; starting with a fresh database. A backup of your database has been made.");
|
||||||
CreateBackup($"{Filename.Replace(realm_extension, string.Empty)}_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}_corrupt{realm_extension}");
|
createBackup($"{Filename.Replace(realm_extension, string.Empty)}_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}_corrupt{realm_extension}");
|
||||||
storage.Delete(Filename);
|
storage.Delete(Filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +236,7 @@ namespace osu.Game.Database
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For extra safety, also store the temporarily-used database which we are about to replace.
|
// For extra safety, also store the temporarily-used database which we are about to replace.
|
||||||
CreateBackup($"{Filename.Replace(realm_extension, string.Empty)}_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}_newer_version_before_recovery{realm_extension}");
|
createBackup($"{Filename.Replace(realm_extension, string.Empty)}_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}_newer_version_before_recovery{realm_extension}");
|
||||||
|
|
||||||
storage.Delete(Filename);
|
storage.Delete(Filename);
|
||||||
|
|
||||||
@ -778,28 +778,37 @@ namespace osu.Game.Database
|
|||||||
private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
|
private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
|
||||||
efContextFactory?.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
|
efContextFactory?.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
|
||||||
|
|
||||||
public void CreateBackup(string backupFilename, IDisposable? blockAllOperations = null)
|
/// <summary>
|
||||||
|
/// Create a full realm backup.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="backupFilename">The filename for the backup.</param>
|
||||||
|
public void CreateBackup(string backupFilename)
|
||||||
{
|
{
|
||||||
using (blockAllOperations ?? BlockAllOperations("creating backup"))
|
if (realmRetrievalLock.CurrentCount != 0)
|
||||||
|
throw new InvalidOperationException($"Call {nameof(BlockAllOperations)} before creating a backup.");
|
||||||
|
|
||||||
|
createBackup(backupFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createBackup(string backupFilename)
|
||||||
|
{
|
||||||
|
Logger.Log($"Creating full realm database backup at {backupFilename}", LoggingTarget.Database);
|
||||||
|
|
||||||
|
int attempts = 10;
|
||||||
|
|
||||||
|
while (attempts-- > 0)
|
||||||
{
|
{
|
||||||
Logger.Log($"Creating full realm database backup at {backupFilename}", LoggingTarget.Database);
|
try
|
||||||
|
|
||||||
int attempts = 10;
|
|
||||||
|
|
||||||
while (attempts-- > 0)
|
|
||||||
{
|
{
|
||||||
try
|
using (var source = storage.GetStream(Filename, mode: FileMode.Open))
|
||||||
{
|
using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew))
|
||||||
using (var source = storage.GetStream(Filename, mode: FileMode.Open))
|
source.CopyTo(destination);
|
||||||
using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew))
|
return;
|
||||||
source.CopyTo(destination);
|
}
|
||||||
return;
|
catch (IOException)
|
||||||
}
|
{
|
||||||
catch (IOException)
|
// file may be locked during use.
|
||||||
{
|
Thread.Sleep(500);
|
||||||
// file may be locked during use.
|
|
||||||
Thread.Sleep(500);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,30 +15,38 @@ namespace osu.Game.Tests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class CleanRunHeadlessGameHost : TestRunHeadlessGameHost
|
public class CleanRunHeadlessGameHost : TestRunHeadlessGameHost
|
||||||
{
|
{
|
||||||
|
private readonly bool bypassCleanupOnSetup;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new instance.
|
/// Create a new instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bindIPC">Whether to bind IPC channels.</param>
|
/// <param name="bindIPC">Whether to bind IPC channels.</param>
|
||||||
/// <param name="realtime">Whether the host should be forced to run in realtime, rather than accelerated test time.</param>
|
/// <param name="realtime">Whether the host should be forced to run in realtime, rather than accelerated test time.</param>
|
||||||
/// <param name="bypassCleanup">Whether to bypass directory cleanup on host disposal. Should be used only if a subsequent test relies on the files still existing.</param>
|
/// <param name="bypassCleanupOnSetup">Whether to bypass directory cleanup on <see cref="SetupForRun"/>.</param>
|
||||||
|
/// <param name="bypassCleanupOnDispose">Whether to bypass directory cleanup on host disposal. Should be used only if a subsequent test relies on the files still existing.</param>
|
||||||
/// <param name="callingMethodName">The name of the calling method, used for test file isolation and clean-up.</param>
|
/// <param name="callingMethodName">The name of the calling method, used for test file isolation and clean-up.</param>
|
||||||
public CleanRunHeadlessGameHost(bool bindIPC = false, bool realtime = true, bool bypassCleanup = false, [CallerMemberName] string callingMethodName = @"")
|
public CleanRunHeadlessGameHost(bool bindIPC = false, bool realtime = true, bool bypassCleanupOnSetup = false, bool bypassCleanupOnDispose = false,
|
||||||
|
[CallerMemberName] string callingMethodName = @"")
|
||||||
: base($"{callingMethodName}-{Guid.NewGuid()}", new HostOptions
|
: base($"{callingMethodName}-{Guid.NewGuid()}", new HostOptions
|
||||||
{
|
{
|
||||||
BindIPC = bindIPC,
|
BindIPC = bindIPC,
|
||||||
}, bypassCleanup: bypassCleanup, realtime: realtime)
|
}, bypassCleanup: bypassCleanupOnDispose, realtime: realtime)
|
||||||
{
|
{
|
||||||
|
this.bypassCleanupOnSetup = bypassCleanupOnSetup;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void SetupForRun()
|
protected override void SetupForRun()
|
||||||
{
|
{
|
||||||
try
|
if (!bypassCleanupOnSetup)
|
||||||
{
|
{
|
||||||
Storage.DeleteDirectory(string.Empty);
|
try
|
||||||
}
|
{
|
||||||
catch
|
Storage.DeleteDirectory(string.Empty);
|
||||||
{
|
}
|
||||||
// May fail if a logging target has already been set via OsuStorage.ChangeTargetStorage.
|
catch
|
||||||
|
{
|
||||||
|
// May fail if a logging target has already been set via OsuStorage.ChangeTargetStorage.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// base call needs to be run *after* storage is emptied, as it updates the (static) logger's storage and may start writing
|
// base call needs to be run *after* storage is emptied, as it updates the (static) logger's storage and may start writing
|
||||||
|
Loading…
Reference in New Issue
Block a user