From c08d91a78fa19c2ad5606640c02a15d2bbc00ccf Mon Sep 17 00:00:00 2001 From: aviv Date: Wed, 19 Jun 2024 12:06:55 +0300 Subject: [PATCH] RavenDB-20325 : fix tests for sharded dbs --- .../PeriodicBackup/PeriodicBackupSlowTests.cs | 94 ++++++++++++------- .../RavenTestBase.Backup.cs | 33 ++++++- 2 files changed, 93 insertions(+), 34 deletions(-) diff --git a/test/SlowTests/Server/Documents/PeriodicBackup/PeriodicBackupSlowTests.cs b/test/SlowTests/Server/Documents/PeriodicBackup/PeriodicBackupSlowTests.cs index 6fd3939e31eb..160ee8649d21 100644 --- a/test/SlowTests/Server/Documents/PeriodicBackup/PeriodicBackupSlowTests.cs +++ b/test/SlowTests/Server/Documents/PeriodicBackup/PeriodicBackupSlowTests.cs @@ -3999,7 +3999,7 @@ public async Task can_backup_and_restore_with_deleted_timeseries_ranges(Options } var config = Backup.CreateBackupConfiguration(backupPath); - var backupTaskId = await Backup.UpdateConfigAndRunBackupAsync(Server, config, store); + var backupTaskId = await Backup.RunBackupForDatabaseModeAsync(Server, config, store, options.DatabaseMode); using (var session = store.OpenAsyncSession()) { @@ -4019,20 +4019,29 @@ public async Task can_backup_and_restore_with_deleted_timeseries_ranges(Options Assert.Null(ts); } - await Backup.RunBackupAsync(Server, backupTaskId, store, isFullBackup: false); - - Assert.True(WaitForValue(() => - { - var dir = Directory.GetDirectories(backupPath).First(); - var files = Directory.GetFiles(dir); - return files.Length == 2; - }, expectedVal: true)); + await Backup.RunBackupForDatabaseModeAsync(Server, config, store, options.DatabaseMode, isFullBackup: false, backupTaskId); } using (var store = GetDocumentStore(options)) { - await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), - Directory.GetDirectories(backupPath).First()); + if (options.DatabaseMode == RavenDatabaseMode.Sharded) + { + // import from each shard backup dir + var dirs = Directory.GetDirectories(backupPath); + Assert.Equal(3, dirs.Length); + + foreach (var dir in dirs) + { + await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), dir); + } + } + else + { + var dir = Directory.GetDirectories(backupPath).First(); + Assert.Equal(2, Directory.GetFiles(dir).Length); + + await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), dir); + } using (var session = store.OpenAsyncSession()) { @@ -4069,7 +4078,8 @@ public async Task deleted_ranges_should_be_processed_before_timeseries(Options o } var config = Backup.CreateBackupConfiguration(backupPath); - var backupTaskId = await Backup.UpdateConfigAndRunBackupAsync(Server, config, store); + + var backupTaskId = await Backup.RunBackupForDatabaseModeAsync(Server, config, store, options.DatabaseMode); using (var session = store.OpenAsyncSession()) { @@ -4095,20 +4105,29 @@ public async Task deleted_ranges_should_be_processed_before_timeseries(Options o Assert.Equal(1, ts.Length); } - await Backup.RunBackupAsync(Server, backupTaskId, store, isFullBackup: false); - - Assert.True(WaitForValue(() => - { - var dir = Directory.GetDirectories(backupPath).First(); - var files = Directory.GetFiles(dir); - return files.Length == 2; - }, expectedVal: true)); + await Backup.RunBackupForDatabaseModeAsync(Server, config, store, options.DatabaseMode, isFullBackup: false, backupTaskId); } using (var store = GetDocumentStore(options)) { - await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), - Directory.GetDirectories(backupPath).First()); + if (options.DatabaseMode == RavenDatabaseMode.Sharded) + { + // import from each shard backup dir + var dirs = Directory.GetDirectories(backupPath); + Assert.Equal(3, dirs.Length); + + foreach (var dir in dirs) + { + await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), dir); + } + } + else + { + var dir = Directory.GetDirectories(backupPath).First(); + Assert.Equal(2, Directory.GetFiles(dir).Length); + + await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), dir); + } using (var session = store.OpenAsyncSession()) { @@ -4146,7 +4165,7 @@ public async Task deleted_ranges_should_be_processed_before_timeseries2(Options } var config = Backup.CreateBackupConfiguration(backupPath); - var backupTaskId = await Backup.UpdateConfigAndRunBackupAsync(Server, config, store); + var backupTaskId = await Backup.RunBackupForDatabaseModeAsync(Server, config, store, options.DatabaseMode); using (var session = store.OpenAsyncSession()) { @@ -4187,20 +4206,29 @@ public async Task deleted_ranges_should_be_processed_before_timeseries2(Options Assert.Null(ts); } - await Backup.RunBackupAsync(Server, backupTaskId, store, isFullBackup: false); - - Assert.True(WaitForValue(() => - { - var dir = Directory.GetDirectories(backupPath).First(); - var files = Directory.GetFiles(dir); - return files.Length == 2; - }, expectedVal: true)); + await Backup.RunBackupForDatabaseModeAsync(Server, config, store, options.DatabaseMode, isFullBackup: false, backupTaskId); } using (var store = GetDocumentStore(options)) { - await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), - Directory.GetDirectories(backupPath).First()); + if (options.DatabaseMode == RavenDatabaseMode.Sharded) + { + // import from each shard backup dir + var dirs = Directory.GetDirectories(backupPath); + Assert.Equal(3, dirs.Length); + + foreach (var dir in dirs) + { + await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), dir); + } + } + else + { + var dir = Directory.GetDirectories(backupPath).First(); + Assert.Equal(2, Directory.GetFiles(dir).Length); + + await store.Smuggler.ImportIncrementalAsync(new DatabaseSmugglerImportOptions(), dir); + } using (var session = store.OpenAsyncSession()) { diff --git a/test/Tests.Infrastructure/RavenTestBase.Backup.cs b/test/Tests.Infrastructure/RavenTestBase.Backup.cs index b64a0abd3cfd..d2454c71c2e6 100644 --- a/test/Tests.Infrastructure/RavenTestBase.Backup.cs +++ b/test/Tests.Infrastructure/RavenTestBase.Backup.cs @@ -17,7 +17,6 @@ using Raven.Server.Documents; using Raven.Server.Documents.PeriodicBackup; using Raven.Server.ServerWide; -using Raven.Server.ServerWide.Context; using Raven.Server.Utils; using Raven.Tests.Core.Utils.Entities; using Tests.Infrastructure; @@ -649,6 +648,38 @@ internal async Task HoldBackupExecutionIfNeededAndInvoke(PeriodicBackupRunner.Te tcs.TrySetResult(null); } } + + public async Task RunBackupForDatabaseModeAsync(RavenServer server, PeriodicBackupConfiguration config, IDocumentStore store, RavenDatabaseMode databaseMode, bool isFullBackup = true, long? taskId = null) + { + if (taskId == null) + { + taskId = (await store.Maintenance.SendAsync(new UpdatePeriodicBackupOperation(config))).TaskId; + + if (databaseMode == RavenDatabaseMode.Sharded) + _parent.Sharding.Backup.WaitForResponsibleNodeUpdate(server.ServerStore, store.Database, taskId.Value); + else + WaitForResponsibleNodeUpdate(server.ServerStore, store.Database, taskId.Value); + } + + + WaitHandle[] waitHandles; + if (databaseMode == RavenDatabaseMode.Sharded) + { + waitHandles = await _parent.Sharding.Backup.WaitForBackupsToComplete(new[] { store }); + await _parent.Sharding.Backup.RunBackupAsync(store.Database, taskId.Value, isFullBackup); + + } + + else + { + waitHandles = await WaitForBackupToComplete(store); + await RunBackupAsync(server, taskId.Value, store, isFullBackup); + } + + Assert.True(WaitHandle.WaitAll(waitHandles, TimeSpan.FromMinutes(1)), "backups failed to complete within 60 seconds"); + + return taskId.Value; + } } } }