Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageVersion Include="ByteSize" Version="2.1.2" />
<PackageVersion Include="Caliburn.Micro" Version="5.0.258" />
<PackageVersion Include="DnsClient" Version="1.8.0" />
<PackageVersion Include="EFCore.NamingConventions" Version="10.0.1" />
<PackageVersion Include="FluentValidation" Version="12.1.1" />
<PackageVersion Include="Fody" Version="6.9.3" />
<PackageVersion Include="GitHubActionsTestLogger" Version="3.0.4" />
Expand Down
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

Copy link
Copy Markdown
Member Author

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

),
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;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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");
}
}
}
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ServiceControl.Persistence.EFCore.PostgreSql;
using Microsoft.Extensions.DependencyInjection;
using ServiceControl.Persistence.EFCore.Abstractions;
using ServiceControl.Persistence.EFCore.DbContexts;
using ServiceControl.Persistence.EFCore.PostgreSql.Infrastructure;

class PostgreSqlPersistence(PostgreSqlPersisterSettings settings) : BasePersistence, IPersistence
{
Expand All @@ -12,6 +13,8 @@ public void AddPersistence(IServiceCollection services)
RegisterSettings(services);
ConfigureDbContext(services);
RegisterDataStores(services);

services.AddHostedService<KnownEndpointsReconciler>();
}

public void AddInstaller(IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ namespace ServiceControl.Persistence.EFCore.PostgreSql;

public class PostgreSqlServiceControlDbContext(DbContextOptions<PostgreSqlServiceControlDbContext> options) : ServiceControlDbContext(options)
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);

// Use snake_case naming convention for PostgreSQL
optionsBuilder.UseSnakeCaseNamingConvention();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is important otherwise dotnet ef migrations does not work, It can't find dependencies

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>
Expand Down
Loading
Loading