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

GraphUpdatesSqlServerTest does not use retrying execution strategy #21463

Closed
smitpatel opened this issue Jun 30, 2020 · 1 comment · Fixed by #25836
Closed

GraphUpdatesSqlServerTest does not use retrying execution strategy #21463

smitpatel opened this issue Jun 30, 2020 · 1 comment · Fixed by #25836
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@smitpatel
Copy link
Contributor

See #21441

@ajcvickers
Copy link
Contributor

@smitpatel @AndriySvyryd I have investigated this and it appears to be a product bug. Specifically, query looks up whether or not a retrying execution strategy is in use by looking at the execution strategy configured for the context:

        [EntityFrameworkInternal]
        public CompiledQueryCacheKeyGeneratorDependencies(
            [NotNull] IModel model,
            [NotNull] ICurrentDbContext currentContext,
            [NotNull] IExecutionStrategyFactory executionStrategyFactory)
        {
            Check.NotNull(model, nameof(model));
            Check.NotNull(currentContext, nameof(currentContext));
            Check.NotNull(executionStrategyFactory, nameof(executionStrategyFactory));

            Model = model;
            CurrentContext = currentContext;
            IsRetryingExecutionStrategy = executionStrategyFactory.Create().RetriesOnFailure;
        }

However, if an external execution strategy has been created, then looking up RetriesOnFailure like this is broken, since executionStrategyFactory.Create() will not return the execution factory that is in use. We do this in our tests, but an application executing a query in a custom execution factory would also hit this same issue.

Here's a stand-alone repro:

public class Foo
{
    public int Id { get; set; }
}

public class SomeDbContext : DbContext
{
    private static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().SetMinimumLevel(LogLevel.Information));

    public DbSet<Foo> Foos { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            //.UseSqlite("Data Source=test.db")
            .UseSqlServer(Your.ConnectionString, b => b.ExecutionStrategy(d => new MySqlServerRetryingExecutionStrategyDi(d)))
            .UseLoggerFactory(ContextLoggerFactory)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

public class Program
{
    public static async Task Main()
    {
        using (var context = new SomeDbContext())
        {
            await context.Database.EnsureDeletedAsync();
            await context.Database.EnsureCreatedAsync();

            context.Add(new Foo());
            
            await context.SaveChangesAsync();
        }
        
        using (var context = new SomeDbContext())
        {
            var myExecutionStrategy = new MySqlServerRetryingExecutionStrategySo(context);
            myExecutionStrategy.Execute(0, (c, s) => c.Set<Foo>().ToList(), null);
        }
    }
}

public class MySqlServerRetryingExecutionStrategyDi : SqlServerRetryingExecutionStrategy
{
    public MySqlServerRetryingExecutionStrategyDi(ExecutionStrategyDependencies dependencies) : base(dependencies)
    {
    }

    public override bool RetriesOnFailure
    {
        get
        {
            Console.WriteLine("Calling RetriesOnFailure on D.I. execution strategy");
            return base.RetriesOnFailure;
        }
    }
}

public class MySqlServerRetryingExecutionStrategySo : SqlServerRetryingExecutionStrategy
{
    public MySqlServerRetryingExecutionStrategySo(DbContext context) : base(context)
    {
    }

    public override bool RetriesOnFailure
    {
        get
        {
            Console.WriteLine("Calling RetriesOnFailure on stand-alone execution strategy");
            return base.RetriesOnFailure;
        }
    }
}

Output:

warn: Microsoft.EntityFrameworkCore.Model.Validation[10400]
      Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
Calling RetriesOnFailure on D.I. execution strategy
Calling RetriesOnFailure on D.I. execution strategy
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 5.0.0 initialized 'SomeDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: SensitiveDataLoggingEnabled 
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (26ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
      IF SERVERPROPERTY('EngineEdition') <> 5
      BEGIN
          ALTER DATABASE [Blogs] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
      END;
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (50ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
      DROP DATABASE [Blogs];
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (316ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
      CREATE DATABASE [Blogs];
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (134ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
      IF SERVERPROPERTY('EngineEdition') <> 5
      BEGIN
          ALTER DATABASE [Blogs] SET READ_COMMITTED_SNAPSHOT ON;
      END;
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [Foos] (
          [Id] int NOT NULL IDENTITY,
          CONSTRAINT [PK_Foos] PRIMARY KEY ([Id])
      );
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SET NOCOUNT ON;
      INSERT INTO [Foos]
      DEFAULT VALUES;
      SELECT [Id]
      FROM [Foos]
      WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
Calling RetriesOnFailure on D.I. execution strategy
Calling RetriesOnFailure on D.I. execution strategy
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 5.0.0 initialized 'SomeDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: SensitiveDataLoggingEnabled 
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [f].[Id]
      FROM [Foos] AS [f]

@ajcvickers ajcvickers removed this from the MQ milestone Nov 26, 2020
@ajcvickers ajcvickers added this to the 6.0.0 milestone Nov 30, 2020
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 2, 2021
@AndriySvyryd AndriySvyryd removed their assignment Sep 2, 2021
AndriySvyryd added a commit that referenced this issue Sep 2, 2021
Don't pass null to ExecutionStrategy.ShouldRetryOn.
Use the currently executing execution strategy to determine whether buffering is needed.

Fixes #21350
Fixes #23019
Fixes #21463
AndriySvyryd added a commit that referenced this issue Sep 2, 2021
Don't pass null to ExecutionStrategy.ShouldRetryOn.
Use the currently executing execution strategy to determine whether buffering is needed.

Fixes #21350
Fixes #23019
Fixes #21463
AndriySvyryd added a commit that referenced this issue Sep 2, 2021
Don't pass null to ExecutionStrategy.ShouldRetryOn.
Use the currently executing execution strategy to determine whether buffering is needed.

Fixes #21350
Fixes #23019
Fixes #21463
AndriySvyryd added a commit that referenced this issue Sep 3, 2021
Don't pass null to ExecutionStrategy.ShouldRetryOn.
Use the currently executing strategy to determine whether buffering is needed.

Fixes #21350
Fixes #23019
Fixes #21463
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Sep 4, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants