Skip to content

Commit

Permalink
RavenDB-20325 : fix tests for sharded dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
aviv committed Jun 19, 2024
1 parent e676098 commit 36a2b0a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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())
{
Expand Down Expand Up @@ -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())
{
Expand All @@ -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())
{
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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())
{
Expand Down
30 changes: 29 additions & 1 deletion test/Tests.Infrastructure/RavenTestBase.Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -649,6 +648,35 @@ internal async Task HoldBackupExecutionIfNeededAndInvoke(PeriodicBackupRunner.Te
tcs.TrySetResult(null);
}
}

public async Task<long> 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;
}
}
}
}

0 comments on commit 36a2b0a

Please sign in to comment.