Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RavenDB-22279 - ClusterWideTransaction_WhenRestoreFromIncrementalBackupAfterStoreAndDelete_ShouldDeleteInTheDestination fails on sharding #18462

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Raven.Server.Documents.Handlers.Batches;
using Raven.Server.Documents.Handlers.Processors.Batches;
using Raven.Server.Documents.Sharding.Handlers.Batches;
using Raven.Server.Documents.Sharding.Operations;
using Raven.Server.Rachis;
using Raven.Server.ServerWide;
using Raven.Server.ServerWide.Commands;
Expand All @@ -27,12 +28,12 @@ public ShardedClusterTransactionRequestProcessor(ShardedDatabaseRequestHandler r

public override Task WaitForDatabaseCompletion(Task<HashSet<string>> onDatabaseCompletionTask, long index, ClusterTransactionOptions options, CancellationToken token)
{
return Task.CompletedTask;
return RequestHandler.DatabaseContext.Cluster.WaitForExecutionOnShardsAsync(index, token).AsTask();
}

protected override ClusterTransactionCommand CreateClusterTransactionCommand(
ArraySegment<BatchRequestParser.CommandData> parsedCommands,
ClusterTransactionCommand.ClusterTransactionOptions options,
ClusterTransactionOptions options,
string raftRequestId)
{
return new ClusterTransactionCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public async ValueTask WaitForExecutionOnAllNodesAsync(long index, CancellationT
/// <summary>
/// Wait for indexes to be applied on the cluster nodes where the physical database shards are available
/// </summary>
public async ValueTask WaitForExecutionOnShardsAsync(long index)
public async ValueTask WaitForExecutionOnShardsAsync(long index, CancellationToken token = default)
{
var op = new WaitForIndexNotificationOperation(index);
await _context.ShardExecutor.ExecuteParallelForAllAsync(op);
await _context.ShardExecutor.ExecuteParallelForAllAsync(op, token);
}
}
}
65 changes: 63 additions & 2 deletions test/SlowTests/Issues/RavenDB-21050.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,18 @@ public async Task ClusterWideTransaction_WhenRestoreFromIncrementalBackupAfterSt
session.Delete(id);
await session.SaveChangesAsync();
}

var backupStatus2 = await source.Maintenance.SendAsync(new StartBackupOperation(false, backupTaskId));
await backupStatus2.WaitForCompletionAsync(TimeSpan.FromMinutes(5));

var restoreConfig = new RestoreBackupConfiguration { BackupLocation = Directory.GetDirectories(backupPath).First(), DatabaseName = destination.Database };
var path = Directory.GetDirectories(backupPath).First();
if (options.DatabaseMode == RavenDatabaseMode.Sharded)
{
int shardNumber = await Sharding.GetShardNumberForAsync(source, id);
path = Directory.GetDirectories(backupPath).First(p => p.Contains($"${shardNumber}"));
}

var restoreConfig = new RestoreBackupConfiguration { BackupLocation = path, DatabaseName = destination.Database };
using (Backup.RestoreDatabase(destination, restoreConfig))
{
using (var session = destination.OpenAsyncSession())
Expand All @@ -62,6 +69,60 @@ public async Task ClusterWideTransaction_WhenRestoreFromIncrementalBackupAfterSt
}
}

}
}


[RavenTheory(RavenTestCategory.BackupExportImport)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sharding as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

[InlineData(true)]
[InlineData(false)]
public async Task ClusterWideTransaction_Restore_FromShardedToSharded(bool delete)
{
var backupPath = NewDataPath(suffix: "BackupFolder");
const string id = "TestObjs/0";

using (var source = Sharding.GetDocumentStore())
using (var destination = new DocumentStore { Urls = new[] { Server.WebUrl }, Database = $"restored_{source.Database}" }.Initialize())
{
var config = new PeriodicBackupConfiguration { LocalSettings = new LocalSettings { FolderPath = backupPath }, IncrementalBackupFrequency = "0 0 */12 * *" };
var backupTaskId = (await source.Maintenance.SendAsync(new UpdatePeriodicBackupOperation(config))).TaskId;

using (var session = source.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
await session.StoreAsync(new TestObj(), id);
await session.SaveChangesAsync();
}

var backupStatus = await source.Maintenance.SendAsync(new StartBackupOperation(false, backupTaskId));
await backupStatus.WaitForCompletionAsync(TimeSpan.FromMinutes(5));

if (delete)
{
using (var session = source.OpenAsyncSession(new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
{
session.Delete(id);
await session.SaveChangesAsync();
}

var backupStatus2 = await source.Maintenance.SendAsync(new StartBackupOperation(false, backupTaskId));
await backupStatus2.WaitForCompletionAsync(TimeSpan.FromMinutes(5));
}

var paths = Directory.GetDirectories(backupPath);

using (Backup.RestoreDatabase(destination, new RestoreBackupConfiguration
{
DatabaseName = destination.Database,
ShardRestoreSettings = Sharding.Backup.GenerateShardRestoreSettings(paths, await Sharding.GetShardingConfigurationAsync(source))
}))
using (var session = destination.OpenAsyncSession())
{
var shouldBeDeleted = await session.LoadAsync<TestObj>(id);
if (delete)
Assert.Null(shouldBeDeleted);
else
Assert.NotNull(shouldBeDeleted);
}

}
}
Expand Down