Skip to content

Commit

Permalink
RavenDB-20610 : Operations : avoid using HttpContext.RequestAborted a…
Browse files Browse the repository at this point in the history
…s part of the OperationCancelToken if the operation is not awaited
  • Loading branch information
aviv86 authored and ppekrol committed Jul 7, 2023
1 parent ee38116 commit 9b95d7b
Show file tree
Hide file tree
Showing 18 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class AbstractDatabaseRequestHandler<TOperationContext> : Reques

public abstract char IdentityPartsSeparator { get; }

public abstract OperationCancelToken CreateTimeLimitedOperationToken();
public abstract OperationCancelToken CreateTimeLimitedOperationToken(bool useRequestAbortedToken = true);

public abstract OperationCancelToken CreateTimeLimitedQueryToken();

Expand Down
6 changes: 4 additions & 2 deletions src/Raven.Server/Documents/DatabaseRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ public override async Task WaitForIndexToBeAppliedAsync(TransactionOperationCont
}
}

public override OperationCancelToken CreateTimeLimitedOperationToken()
public override OperationCancelToken CreateTimeLimitedOperationToken(bool useRequestAbortedToken = true)
{
return new OperationCancelToken(Database.Configuration.Databases.OperationTimeout.AsTimeSpan, Database.DatabaseShutdown, HttpContext.RequestAborted);
if (useRequestAbortedToken)
return new OperationCancelToken(Database.Configuration.Databases.OperationTimeout.AsTimeSpan, Database.DatabaseShutdown, HttpContext.RequestAborted);
return new OperationCancelToken(Database.Configuration.Databases.OperationTimeout.AsTimeSpan, Database.DatabaseShutdown);
}

public override OperationCancelToken CreateTimeLimitedQueryToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Raven.Server.Documents.Operations;
using Raven.Server.Json;
using Raven.Server.Routing;
using Raven.Server.ServerWide;
using Raven.Server.ServerWide.Context;
using Sparrow.Json;
using Sparrow.Json.Parsing;
Expand Down Expand Up @@ -134,7 +135,7 @@ public async Task OptimizeIndex()
if (index == null)
IndexDoesNotExistException.ThrowFor(name);

var token = CreateOperationToken();
var token = new OperationCancelToken(Database.DatabaseShutdown);
var result = new IndexOptimizeResult(index.Name);
var operationId = Database.Operations.GetNextOperationId();
var t = Database.Operations.AddLocalOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ protected override async ValueTask HandleCurrentNodeAsync()
}

var operationId = RequestHandler.Database.Operations.GetNextOperationId();
var token = RequestHandler.CreateTimeLimitedQueryOperationToken();
var token = new OperationCancelToken(RequestHandler.Database.Configuration.Databases.QueryOperationTimeout.AsTimeSpan,
RequestHandler.Database.DatabaseShutdown);

_ = RequestHandler.Database.Operations.AddLocalOperation(
operationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected AbstractAdminRevisionsHandlerProcessorForEnforceRevisionsConfiguration

public override async ValueTask ExecuteAsync()
{
var token = RequestHandler.CreateTimeLimitedOperationToken();
var token = RequestHandler.CreateTimeLimitedOperationToken(useRequestAbortedToken: false);
var operationId = RequestHandler.GetLongQueryString("operationId", false) ?? GetNextOperationId();

ScheduleEnforceConfigurationOperation(operationId, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ await ServerStore.Operations.AddLocalOperation(
if (debugInfoType.HasFlag(DebugInfoPackageContentType.ServerWide))
{
await WriteServerInfo(archive, context, localEndpointClient, token.Token);
await WriteServerInfo(archive, context, localEndpointClient, token.Token);
}
if (debugInfoType.HasFlag(DebugInfoPackageContentType.Databases))
Expand All @@ -211,8 +211,8 @@ await ServerStore.Operations.AddLocalOperation(
if (debugInfoType.HasFlag(DebugInfoPackageContentType.LogFile))
{
await WriteLogFile(archive, token.Token);
}
await WriteLogFile(archive, token.Token);
}
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public override async ValueTask ExecuteAsync()
ServerStore.ConcurrentBackupsCounter.StartBackup(backupName, Logger);
try
{
var cancelToken = RequestHandler.CreateOperationToken();

var cancelToken = new OperationCancelToken(ServerStore.ServerShutdown);
ScheduleBackup(backupConfiguration, operationId, backupName, sw, startTime, cancelToken);

await using (var writer = new AsyncBlittableJsonTextWriter(context, RequestHandler.ResponseBodyStream()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void ExecuteQueryOperation(IndexQueryServerSide query, long operationId,
IDisposable returnContextToPool,
OperationType operationType)
{
var token = RequestHandler.CreateTimeLimitedQueryOperationToken();
var token = new OperationCancelToken(RequestHandler.Database.Configuration.Databases.QueryOperationTimeout.AsTimeSpan, RequestHandler.Database.DatabaseShutdown);

var description = GetOperationDescription(query);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override async ValueTask ExecuteAsync()
configuration = JsonDeserializationServer.RevertRevisions(json);
}

var token = RequestHandler.CreateTimeLimitedOperationToken();
var token = RequestHandler.CreateTimeLimitedOperationToken(useRequestAbortedToken: false);
var operationId = RequestHandler.GetLongQueryString("operationId", required: false) ?? GetNextOperationId();

ScheduleRevertRevisions(operationId, configuration, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private void ExecuteCollectionOperation(
DocumentsOperationContext docsContext, IDisposable returnToContextPool, OperationType operationType, string collectionName, long operationId,
HashSet<string> excludeIds)
{
var token = RequestHandler.CreateTimeLimitedCollectionOperationToken();
var token = new OperationCancelToken(RequestHandler.Database.Configuration.Databases.CollectionOperationTimeout.AsTimeSpan, RequestHandler.Database.DatabaseShutdown);

var collectionRunner = new StudioCollectionRunner(RequestHandler.Database, docsContext, excludeIds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Raven.Client.Documents.Operations.Backups;
using Raven.Client.Util;
using Raven.Server.Documents.Handlers.Processors.OngoingTasks;
using Raven.Server.ServerWide;
using Raven.Server.ServerWide.Context;

namespace Raven.Server.Documents.Sharding.Handlers.Processors.OngoingTasks
Expand All @@ -17,7 +18,8 @@ public ShardedOngoingTasksHandlerProcessorForBackupDatabaseNow([NotNull] Sharded

protected override ValueTask<bool> ScheduleBackupOperationAsync(long taskId, bool isFullBackup, long operationId, DateTime? _)
{
var token = RequestHandler.CreateTimeLimitedOperationToken();
var token = new OperationCancelToken(RequestHandler.DatabaseContext.Configuration.Databases.OperationTimeout.AsTimeSpan, RequestHandler.DatabaseContext.DatabaseShutdown);

var startTime = SystemTime.UtcNow;
var t = RequestHandler.DatabaseContext.Operations.AddRemoteOperation<OperationIdResult<StartBackupOperationResult>, ShardedBackupResult, ShardedBackupProgress>(operationId,
Server.Documents.Operations.OperationType.DatabaseBackup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Raven.Server.Documents.Operations;
using Raven.Server.Documents.Queries;
using Raven.Server.NotificationCenter;
using Raven.Server.ServerWide;
using Raven.Server.ServerWide.Context;
using Sparrow.Json;
using Sparrow.Utils;
Expand Down Expand Up @@ -46,7 +47,7 @@ protected override void ScheduleOperation(TransactionOperationContext asyncOpera
throw new NotSupportedInShardingException("Query with limit is not supported in patch / delete by query operation");
}

var token = RequestHandler.CreateTimeLimitedOperationToken();
var token = new OperationCancelToken(RequestHandler.DatabaseContext.Configuration.Databases.OperationTimeout.AsTimeSpan, RequestHandler.DatabaseContext.DatabaseShutdown);

var op = GetOperation(query, operationId, options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Raven.Server.Documents.Commands.Studio;
using Raven.Server.Documents.Handlers.Processors.Studio;
using Raven.Server.Documents.Operations;
using Raven.Server.ServerWide;
using Raven.Server.ServerWide.Context;

namespace Raven.Server.Documents.Sharding.Handlers.Processors.Studio
Expand All @@ -20,7 +21,7 @@ protected override void ScheduleDeleteCollection(TransactionOperationContext con
{
using (returnToContextPool)
{
var token = RequestHandler.CreateTimeLimitedOperationToken();
var token = new OperationCancelToken(RequestHandler.DatabaseContext.Configuration.Databases.OperationTimeout.AsTimeSpan, RequestHandler.DatabaseContext.DatabaseShutdown);

var shardToIds = ShardLocator.GetDocumentIdsByShards(context, RequestHandler.DatabaseContext, excludeIds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Raven.Server.Json;
using Raven.Server.Rachis;
using Raven.Server.Routing;
using Raven.Server.ServerWide;
using Raven.Server.ServerWide.Context;
using Raven.Server.Utils;
using Raven.Server.Web;
Expand Down Expand Up @@ -47,7 +48,7 @@ public async Task StartResharding()
ToBucket = toBucket,
ToShard = toShard
};
var token = CreateOperationToken();
var token = new OperationCancelToken(ServerStore.ServerShutdown);
_ = ServerStore.Operations.AddLocalOperation(operationId, OperationType.Resharding, $"Move to shard {toShard} buckets [{fromBucket}-{toBucket}]", opDesc, async action =>
{
var result = new ReshardingResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ public override OperationCancelToken CreateOperationToken(TimeSpan cancelAfter)
return new OperationCancelToken(cancelAfter, DatabaseContext.DatabaseShutdown, HttpContext.RequestAborted);
}

public override OperationCancelToken CreateTimeLimitedOperationToken()
public override OperationCancelToken CreateTimeLimitedOperationToken(bool useRequestAbortedToken = true)
{
return new OperationCancelToken(DatabaseContext.Configuration.Databases.OperationTimeout.AsTimeSpan, DatabaseContext.DatabaseShutdown, HttpContext.RequestAborted);
if (useRequestAbortedToken)
return new OperationCancelToken(DatabaseContext.Configuration.Databases.OperationTimeout.AsTimeSpan, DatabaseContext.DatabaseShutdown, HttpContext.RequestAborted);
return new OperationCancelToken(DatabaseContext.Configuration.Databases.OperationTimeout.AsTimeSpan, DatabaseContext.DatabaseShutdown);

}

public override OperationCancelToken CreateTimeLimitedQueryToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.AspNetCore.WebUtilities;
Expand All @@ -21,7 +20,6 @@
using Raven.Client.Documents.Conventions;
using Raven.Client.Documents.Operations;
using Raven.Client.Documents.Smuggler;
using Raven.Client.Exceptions.Security;
using Raven.Client.Util;
using Raven.Server.Documents;
using Raven.Server.Documents.Handlers.Processors.Smuggler;
Expand Down Expand Up @@ -336,7 +334,7 @@ public async Task MigrateFromAnotherDatabase()
}

var operationId = GetLongQueryString("operationId", false) ?? Database.Operations.GetNextOperationId();
var token = CreateOperationToken();
var token = new OperationCancelToken(Database.DatabaseShutdown);
var transformScript = migrationConfiguration.TransformScript;

_ = Database.Operations.AddLocalOperation(
Expand Down
4 changes: 2 additions & 2 deletions src/Raven.Server/Web/Studio/SqlMigrationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Raven.Server.Documents.Operations;
using Raven.Server.Json;
using Raven.Server.Routing;
using Raven.Server.ServerWide;
using Raven.Server.ServerWide.Context;
using Raven.Server.SqlMigration;
using Raven.Server.SqlMigration.Model;
Expand Down Expand Up @@ -57,8 +58,7 @@ public async Task ImportSql()

var dbDriver = DatabaseDriverDispatcher.CreateDriver(sourceSqlDatabase.Provider, sourceSqlDatabase.ConnectionString, sourceSqlDatabase.Schemas);
var schema = dbDriver.FindSchema();
var token = CreateOperationToken();

var token = new OperationCancelToken(Database.DatabaseShutdown);
var result = new MigrationResult(migrationRequest.Settings);

var collectionsCount = migrationRequest.Settings.Collections.Count;
Expand Down
10 changes: 6 additions & 4 deletions src/Raven.Server/Web/System/AdminDatabasesHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,8 @@ public async Task RestoreDatabase()

await ServerStore.EnsureNotPassiveAsync();

var cancelToken = CreateOperationToken();
var cancelToken = new OperationCancelToken(ServerStore.ServerShutdown);

var operationId = ServerStore.Operations.GetNextOperationId();

_ = ServerStore.Operations.AddLocalOperation(
Expand Down Expand Up @@ -1043,8 +1044,7 @@ public async Task CompactDatabase()
}

var database = await ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(compactSettings.DatabaseName).ConfigureAwait(false);

var token = CreateOperationToken();
var token = new OperationCancelToken(ServerStore.ServerShutdown);
var compactDatabaseTask = new CompactDatabaseTask(
ServerStore,
compactSettings.DatabaseName,
Expand Down Expand Up @@ -1224,7 +1224,9 @@ public async Task MigrateDatabaseOffline()
}
var (commandline, tmpFile) = configuration.GenerateExporterCommandLine();
var processStartInfo = new ProcessStartInfo(dataExporter, commandline);
var token = CreateOperationToken();

var token = new OperationCancelToken(ServerStore.ServerShutdown);

Task timeout = null;
if (configuration.Timeout.HasValue)
{
Expand Down

0 comments on commit 9b95d7b

Please sign in to comment.