-
Notifications
You must be signed in to change notification settings - Fork 51
Implement Known Endpoints persistence with EF Core insert-only tables #5625
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ccba32e
Implement Known Endpoints persistence with EF Core insert-only tables
johnsimons 94a272f
Make `CompleteDatabaseOperation` asynchronous across persistence tests
johnsimons c425b90
Improve InsertOnlyTableReconciler testability
johnsimons bb06d02
No longer applies
johnsimons 598e783
Make MessageBodyFileResult properties required
johnsimons 510ed9c
Small updates based on feedback
johnsimons ffadf36
Apply suggestions from code review
johnsimons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/ServiceControl.Persistence.EFCore.PostgreSql/Infrastructure/KnownEndpointsReconciler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| namespace ServiceControl.Persistence.EFCore.PostgreSql.Infrastructure; | ||
|
|
||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using ServiceControl.Persistence.EFCore.DbContexts; | ||
| using ServiceControl.Persistence.EFCore.Infrastructure; | ||
|
|
||
| class KnownEndpointsReconciler( | ||
| ILogger<KnownEndpointsReconciler> logger, | ||
| TimeProvider timeProvider, | ||
| IServiceScopeFactory serviceScopeFactory) | ||
| : InsertOnlyTableReconciler( | ||
| logger, timeProvider, serviceScopeFactory, nameof(KnownEndpointsReconciler)) | ||
| { | ||
| protected override Task<int> ReconcileBatch(ServiceControlDbContext dbContext, CancellationToken stoppingToken) => | ||
| ReconcileBatch(dbContext, BatchSize, stoppingToken); | ||
|
|
||
| // Static so tests can execute a batch deterministically without the background service's timer loop. | ||
| // Must be called within an active transaction because of the pg_try_advisory_xact_lock. | ||
| internal static async Task<int> ReconcileBatch(ServiceControlDbContext dbContext, int batchSize, CancellationToken cancellationToken) | ||
| { | ||
| var sql = """ | ||
| WITH lock_check AS ( | ||
| SELECT pg_try_advisory_xact_lock(hashtext('known_endpoints_sync')) AS acquired | ||
| ), | ||
| batch AS ( | ||
| SELECT ctid FROM "known_endpoints_insert_only" | ||
| WHERE (SELECT acquired FROM lock_check) | ||
| LIMIT @batchSize | ||
| ), | ||
| ins AS ( | ||
| INSERT INTO "known_endpoints" ("id", "name", "host_id", "host", "monitored") | ||
| SELECT DISTINCT ON ("known_endpoint_id") "known_endpoint_id", "name", "host_id", "host", FALSE | ||
| FROM "known_endpoints_insert_only" | ||
| WHERE ctid IN (SELECT ctid FROM batch) | ||
| ON CONFLICT ("id") DO NOTHING | ||
| ) | ||
| DELETE FROM "known_endpoints_insert_only" | ||
| WHERE ctid IN (SELECT ctid FROM batch); | ||
| """; | ||
|
|
||
| var rowsAffected = await dbContext.Database.ExecuteSqlRawAsync(sql, [new Npgsql.NpgsqlParameter("@batchSize", batchSize)], cancellationToken); | ||
| return rowsAffected; | ||
| } | ||
| } | ||
93 changes: 93 additions & 0 deletions
93
...erviceControl.Persistence.EFCore.PostgreSql/Migrations/20260720230745_Initial.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
src/ServiceControl.Persistence.EFCore.PostgreSql/Migrations/20260720230745_Initial.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using System; | ||
| using Microsoft.EntityFrameworkCore.Migrations; | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
|
|
||
| #nullable disable | ||
|
|
||
| namespace ServiceControl.Persistence.EFCore.PostgreSql.Migrations | ||
| { | ||
| /// <inheritdoc /> | ||
| public partial class Initial : Migration | ||
| { | ||
| /// <inheritdoc /> | ||
| protected override void Up(MigrationBuilder migrationBuilder) | ||
| { | ||
| migrationBuilder.CreateTable( | ||
| name: "known_endpoints", | ||
| columns: table => new | ||
| { | ||
| id = table.Column<Guid>(type: "uuid", nullable: false), | ||
| name = table.Column<string>(type: "text", nullable: false), | ||
| host_id = table.Column<Guid>(type: "uuid", nullable: false), | ||
| host = table.Column<string>(type: "text", nullable: false), | ||
| monitored = table.Column<bool>(type: "boolean", nullable: false) | ||
| }, | ||
| constraints: table => | ||
| { | ||
| table.PrimaryKey("PK_known_endpoints", x => x.id); | ||
| }); | ||
|
|
||
| migrationBuilder.CreateTable( | ||
| name: "known_endpoints_insert_only", | ||
| columns: table => new | ||
| { | ||
| id = table.Column<long>(type: "bigint", nullable: false) | ||
| .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), | ||
| known_endpoint_id = table.Column<Guid>(type: "uuid", nullable: false), | ||
| name = table.Column<string>(type: "text", nullable: false), | ||
| host_id = table.Column<Guid>(type: "uuid", nullable: false), | ||
| host = table.Column<string>(type: "text", nullable: false) | ||
| }, | ||
| constraints: table => | ||
| { | ||
| table.PrimaryKey("PK_known_endpoints_insert_only", x => x.id); | ||
| }); | ||
|
|
||
| migrationBuilder.CreateIndex( | ||
| name: "IX_known_endpoints_insert_only_known_endpoint_id", | ||
| table: "known_endpoints_insert_only", | ||
| column: "known_endpoint_id"); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void Down(MigrationBuilder migrationBuilder) | ||
| { | ||
| migrationBuilder.DropTable( | ||
| name: "known_endpoints"); | ||
|
|
||
| migrationBuilder.DropTable( | ||
| name: "known_endpoints_insert_only"); | ||
| } | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
...ersistence.EFCore.PostgreSql/Migrations/PostgreSqlServiceControlDbContextModelSnapshot.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // <auto-generated /> | ||
| using System; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Infrastructure; | ||
| using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
| using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
| using ServiceControl.Persistence.EFCore.PostgreSql; | ||
|
|
||
| #nullable disable | ||
|
|
||
| namespace ServiceControl.Persistence.EFCore.PostgreSql.Migrations | ||
| { | ||
| [DbContext(typeof(PostgreSqlServiceControlDbContext))] | ||
| partial class PostgreSqlServiceControlDbContextModelSnapshot : ModelSnapshot | ||
| { | ||
| protected override void BuildModel(ModelBuilder modelBuilder) | ||
| { | ||
| #pragma warning disable 612, 618 | ||
| modelBuilder | ||
| .HasAnnotation("ProductVersion", "10.0.9") | ||
| .HasAnnotation("Relational:MaxIdentifierLength", 63); | ||
|
|
||
| NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); | ||
|
|
||
| modelBuilder.Entity("ServiceControl.Persistence.EFCore.Entities.KnownEndpointEntity", b => | ||
| { | ||
| b.Property<Guid>("Id") | ||
| .HasColumnType("uuid") | ||
| .HasColumnName("id"); | ||
|
|
||
| b.Property<string>("Host") | ||
| .IsRequired() | ||
| .HasColumnType("text") | ||
| .HasColumnName("host"); | ||
|
|
||
| b.Property<Guid>("HostId") | ||
| .HasColumnType("uuid") | ||
| .HasColumnName("host_id"); | ||
|
|
||
| b.Property<bool>("Monitored") | ||
| .HasColumnType("boolean") | ||
| .HasColumnName("monitored"); | ||
|
|
||
| b.Property<string>("Name") | ||
| .IsRequired() | ||
| .HasColumnType("text") | ||
| .HasColumnName("name"); | ||
|
|
||
| b.HasKey("Id"); | ||
|
|
||
| b.ToTable("known_endpoints", (string)null); | ||
| }); | ||
|
|
||
| modelBuilder.Entity("ServiceControl.Persistence.EFCore.Entities.KnownEndpointInsertOnlyEntity", b => | ||
| { | ||
| b.Property<long>("Id") | ||
| .ValueGeneratedOnAdd() | ||
| .HasColumnType("bigint") | ||
| .HasColumnName("id"); | ||
|
|
||
| NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id")); | ||
|
|
||
| b.Property<string>("Host") | ||
| .IsRequired() | ||
| .HasColumnType("text") | ||
| .HasColumnName("host"); | ||
|
|
||
| b.Property<Guid>("HostId") | ||
| .HasColumnType("uuid") | ||
| .HasColumnName("host_id"); | ||
|
|
||
| b.Property<Guid>("KnownEndpointId") | ||
| .HasColumnType("uuid") | ||
| .HasColumnName("known_endpoint_id"); | ||
|
|
||
| b.Property<string>("Name") | ||
| .IsRequired() | ||
| .HasColumnType("text") | ||
| .HasColumnName("name"); | ||
|
|
||
| b.HasKey("Id"); | ||
|
|
||
| b.HasIndex("KnownEndpointId"); | ||
|
|
||
| b.ToTable("known_endpoints_insert_only", (string)null); | ||
| }); | ||
| #pragma warning restore 612, 618 | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,20 +8,29 @@ | |
| <DisableTransitiveProjectReferences>true</DisableTransitiveProjectReferences> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <!-- Private=false & ExcludeAssets=runtime prevent repeatedly including binary dependencies of ServiceControl.Persistence and its dependencies in each persister directory --> | ||
| <ItemGroup Condition="'$(Configuration)' == 'Release'"> | ||
| <!-- Private=false & ExcludeAssets=runtime prevent repeatedly including binary dependencies of ServiceControl.Persistence | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is important otherwise |
||
| and its dependencies in each persister directory. Release-only: the host loads these via PluginAssemblyLoadContext, | ||
| which falls back to the host's own copy. Debug needs them copied locally for dotnet-ef design-time tooling. --> | ||
| <ProjectReference Include="../ServiceControl.Configuration/ServiceControl.Configuration.csproj" Private="false" ExcludeAssets="runtime" /> | ||
| <ProjectReference Include="../ServiceControl.Infrastructure/ServiceControl.Infrastructure.csproj" Private="false" ExcludeAssets="runtime" /> | ||
| <ProjectReference Include="../ServiceControl.Persistence/ServiceControl.Persistence.csproj" Private="false" ExcludeAssets="runtime" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup Condition="'$(Configuration)' != 'Release'"> | ||
| <ProjectReference Include="../ServiceControl.Configuration/ServiceControl.Configuration.csproj" /> | ||
| <ProjectReference Include="../ServiceControl.Infrastructure/ServiceControl.Infrastructure.csproj" /> | ||
| <ProjectReference Include="../ServiceControl.Persistence/ServiceControl.Persistence.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <!-- Not Private=false or ExcludeAssets because the shared EF Core assembly ships inside this persister's folder --> | ||
| <ProjectReference Include="../ServiceControl.Persistence.EFCore/ServiceControl.Persistence.EFCore.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <!-- Design-time only, for dotnet-ef tooling. Debug-only so it (and its Roslyn dependency tail) stays out of the shipped Release artifact --> | ||
| <PackageReference Include="EFCore.NamingConventions" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Design" PrivateAssets="all" Condition="'$(Configuration)' == 'Debug'" /> | ||
| <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" /> | ||
| </ItemGroup> | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need locks so that if the instance is scalled out for some reason, the code still works correctly