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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class sql_server_ordered_envelope_ids : IDisposable
public void Dispose()
{
// The id generator is static, so put the default back for every other test in this assembly
Envelope.IdGenerator = NewId.NextSequentialGuid;
Envelope.IdGenerator = EnvelopeIds.NextSequentialGuid;
}

[Fact]
Expand All @@ -40,7 +40,7 @@ public async Task a_polecat_only_application_switches_automatically()

host.Services.GetRequiredService<WolverineOptions>().EnvelopeIdGeneration
.ShouldBe(EnvelopeIdGeneration.NewIdSqlServerOrder);
Envelope.IdGenerator.Method.ShouldBe(typeof(NewId).GetMethod(nameof(NewId.NextGuid), Type.EmptyTypes));
Envelope.IdGenerator.Method.ShouldBe(typeof(EnvelopeIds).GetMethod(nameof(EnvelopeIds.NextGuid), Type.EmptyTypes));
}

[Fact]
Expand All @@ -67,6 +67,6 @@ public async Task integrate_with_wolverine_can_opt_out()

host.Services.GetRequiredService<WolverineOptions>().EnvelopeIdGeneration
.ShouldBe(EnvelopeIdGeneration.NewId);
Envelope.IdGenerator.Method.ShouldBe(typeof(NewId).GetMethod(nameof(NewId.NextSequentialGuid), Type.EmptyTypes));
Envelope.IdGenerator.Method.ShouldBe(typeof(EnvelopeIds).GetMethod(nameof(EnvelopeIds.NextSequentialGuid), Type.EmptyTypes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public class sql_server_ordered_envelope_ids : IDisposable
public void Dispose()
{
// The id generator is static, so put the default back for every other test in this assembly
Envelope.IdGenerator = NewId.NextSequentialGuid;
Envelope.IdGenerator = EnvelopeIds.NextSequentialGuid;
}

private static readonly System.Reflection.MethodInfo SqlServerOrder =
typeof(NewId).GetMethod(nameof(NewId.NextGuid), Type.EmptyTypes)!;
typeof(EnvelopeIds).GetMethod(nameof(EnvelopeIds.NextGuid), Type.EmptyTypes)!;

private static readonly System.Reflection.MethodInfo DefaultOrder =
typeof(NewId).GetMethod(nameof(NewId.NextSequentialGuid), Type.EmptyTypes)!;
typeof(EnvelopeIds).GetMethod(nameof(EnvelopeIds.NextSequentialGuid), Type.EmptyTypes)!;

[Fact]
public async Task a_sql_server_only_application_switches_automatically()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class store_preferred_envelope_id_generation : IDisposable
{
public void Dispose()
{
Envelope.IdGenerator = NewId.NextSequentialGuid;
Envelope.IdGenerator = EnvelopeIds.NextSequentialGuid;
}

private static IMessageStore storePreferring(EnvelopeIdGeneration? preference)
Expand Down Expand Up @@ -91,6 +91,6 @@ public async Task an_application_without_message_storage_keeps_the_default()
.StartAsync(TestContext.Current.CancellationToken);

host.GetRuntime().Options.EnvelopeIdGeneration.ShouldBe(EnvelopeIdGeneration.NewId);
Envelope.IdGenerator.Method.ShouldBe(typeof(NewId).GetMethod(nameof(NewId.NextSequentialGuid), Type.EmptyTypes));
Envelope.IdGenerator.Method.ShouldBe(typeof(EnvelopeIds).GetMethod(nameof(EnvelopeIds.NextSequentialGuid), Type.EmptyTypes));
}
}
54 changes: 49 additions & 5 deletions src/Testing/CoreTests/envelope_id_generation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class envelope_id_generation : IDisposable
public void Dispose()
{
// Reset to default after each test
Envelope.IdGenerator = NewId.NextSequentialGuid;
Envelope.IdGenerator = EnvelopeIds.NextSequentialGuid;
}

[Fact]
Expand All @@ -27,7 +27,7 @@ public void default_envelope_id_generation_is_new_id()
[Fact]
public void new_id_mode_produces_non_empty_guids()
{
Envelope.IdGenerator = NewId.NextSequentialGuid;
Envelope.IdGenerator = EnvelopeIds.NextSequentialGuid;

var envelope = new Envelope();
envelope.Id.ShouldNotBe(Guid.Empty);
Expand Down Expand Up @@ -118,7 +118,7 @@ public async Task sql_server_order_is_opt_in_and_uses_new_id_next_guid()
#endregion
}).StartAsync(cancellationToken: TestContext.Current.CancellationToken);

Envelope.IdGenerator.Method.ShouldBe(typeof(NewId).GetMethod(nameof(NewId.NextGuid), Type.EmptyTypes));
Envelope.IdGenerator.Method.ShouldBe(typeof(EnvelopeIds).GetMethod(nameof(EnvelopeIds.NextGuid), Type.EmptyTypes));

await host.InvokeMessageAndWaitAsync(new GuidV7TestMessage("sql server"));
GuidV7TestHandler.LastReceived.ShouldBe("sql server");
Expand All @@ -128,7 +128,7 @@ public async Task sql_server_order_is_opt_in_and_uses_new_id_next_guid()
[Fact]
public void sql_server_order_ids_sort_in_sql_server_order()
{
Envelope.IdGenerator = NewId.NextGuid;
Envelope.IdGenerator = EnvelopeIds.NextGuid;

var ids = Enumerable.Range(0, 1000).Select(_ => new SqlGuid(new Envelope().Id)).ToArray();

Expand All @@ -142,7 +142,7 @@ public void sql_server_order_ids_sort_in_sql_server_order()
[Fact]
public void default_ids_do_not_sort_in_sql_server_order()
{
Envelope.IdGenerator = NewId.NextSequentialGuid;
Envelope.IdGenerator = EnvelopeIds.NextSequentialGuid;

var ids = Enumerable.Range(0, 1000).Select(_ => new Envelope().Id).ToArray();

Expand All @@ -160,6 +160,50 @@ public void default_ids_do_not_sort_in_sql_server_order()
outOfOrder.ShouldBeGreaterThan(0);
}

// Two processes on one machine share NewId's network identity, and each counts its own sequence from zero
// within a tick, so without the process id they draw the very same ids. A fixed tick puts both "processes"
// in the same moment.
[Fact]
public void the_default_ids_differ_between_two_processes_drawing_them_at_the_same_moment()
{
var tick = new FixedTicks();

var withoutProcessIds = draw(EnvelopeIds.Create(tick, null))
.Intersect(draw(EnvelopeIds.Create(tick, null)));
withoutProcessIds.ShouldNotBeEmpty("NewId's own layout collides across processes");

draw(EnvelopeIds.Create(tick, new FixedProcessId(1001)))
.Intersect(draw(EnvelopeIds.Create(tick, new FixedProcessId(1002))))
.ShouldBeEmpty();
}

[Fact]
public async Task default_ids_are_unique_across_threads()
{
var ids = new ConcurrentBag<Guid>();
var tasks = Enumerable.Range(0, 10).Select(_ => Task.Run(() =>
{
for (var i = 0; i < 1000; i++) ids.Add(EnvelopeIds.NextSequentialGuid());
}, TestContext.Current.CancellationToken)).ToArray();

await Task.WhenAll(tasks);

ids.Distinct().Count().ShouldBe(10_000);
}

private static Guid[] draw(INewIdGenerator generator) =>
Enumerable.Range(0, 1000).Select(_ => generator.NextSequentialGuid()).ToArray();

private class FixedTicks : ITickProvider
{
public long Ticks { get; } = DateTime.UtcNow.Ticks;
}

private class FixedProcessId(int id) : IProcessIdProvider
{
public byte[] GetProcessId() => BitConverter.GetBytes(id);
}

[Fact]
public void all_newid_usages_respect_id_generator()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Wolverine/Envelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ public void SetMessageType(Type messageType)
public string?[] AcceptedContentTypes { get; set; } = DefaultAcceptedContentTypes;

/// <summary>
/// The delegate used to generate unique envelope IDs. Defaults to NewId.NextSequentialGuid.
/// The delegate used to generate unique envelope IDs. Defaults to <see cref="EnvelopeIds.NextSequentialGuid"/>.
/// Set via <see cref="WolverineOptions.EnvelopeIdGeneration"/> at startup.
/// </summary>
internal static Func<Guid> IdGenerator = NewId.NextSequentialGuid;
internal static Func<Guid> IdGenerator = EnvelopeIds.NextSequentialGuid;

/// <summary>
/// Specific message id for this envelope
Expand Down
38 changes: 38 additions & 0 deletions src/Wolverine/EnvelopeIds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using MassTransit;
using MassTransit.NewIdProviders;

namespace Wolverine;

/// <summary>
/// The NewId generator behind <see cref="EnvelopeIdGeneration.NewId"/> and
/// <see cref="EnvelopeIdGeneration.NewIdSqlServerOrder"/>.
/// </summary>
/// <remarks>
/// The static <c>NewId</c> builds an id from the machine's network identity, a timestamp and a sequence each
/// process counts on its own, so two processes on one machine draw the same ids at the same moment -- one
/// node's cascaded message then collides with another node's in a shared inbox, and the inbox's duplicate
/// detection drops one of them. This generator mixes the process id in as well. It is Wolverine's own instance,
/// rather than <c>NewId.SetProcessIdProvider()</c>, so the ids anything else in the process draws from
/// <c>NewId</c> (MassTransit, application code) are left exactly as they were.
/// </remarks>
internal static class EnvelopeIds
{
private static readonly Lazy<NewIdGenerator> _generator =
new(() => Create(new DateTimeTickProvider(), new CurrentProcessIdProvider()));

/// <summary>
/// The same tick and worker id providers the static <c>NewId</c> uses, plus a process id
/// </summary>
internal static NewIdGenerator Create(ITickProvider ticks, IProcessIdProvider? processId) =>
new(ticks, new BestPossibleWorkerIdProvider(), processId, 0);

/// <summary>
/// Ordered by time the way .NET and PostgreSQL compare GUIDs
/// </summary>
public static Guid NextSequentialGuid() => _generator.Value.NextSequentialGuid();

/// <summary>
/// Ordered by time the way SQL Server compares a <c>uniqueidentifier</c>
/// </summary>
public static Guid NextGuid() => _generator.Value.NextGuid();
}
4 changes: 2 additions & 2 deletions src/Wolverine/Runtime/WolverineRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ internal static Func<Guid> IdGeneratorFor(EnvelopeIdGeneration generation)
return generation switch
{
EnvelopeIdGeneration.GuidV7 => Guid.CreateVersion7,
EnvelopeIdGeneration.NewIdSqlServerOrder => MassTransit.NewId.NextGuid,
_ => MassTransit.NewId.NextSequentialGuid
EnvelopeIdGeneration.NewIdSqlServerOrder => EnvelopeIds.NextGuid,
_ => EnvelopeIds.NextSequentialGuid
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/Wolverine/WolverineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ public enum EnvelopeIdGeneration
{
/// <summary>
/// Default. Uses MassTransit's NewId algorithm (<c>NewId.NextSequentialGuid()</c>), which builds
/// GUIDs from machine identity (MAC/DNS) and a timestamp, laid out so they sort in time order the way
/// .NET and PostgreSQL compare GUIDs. SQL Server compares a <c>uniqueidentifier</c> starting from its
/// GUIDs from machine identity (MAC/DNS), the process id and a timestamp, laid out so they sort in time
/// order the way .NET and PostgreSQL compare GUIDs. Wolverine adds the process id to NewId's own layout
/// so that several processes on one machine cannot draw the same ids. SQL Server compares a <c>uniqueidentifier</c> starting from its
/// last six bytes, so these ids arrive in effectively random order there and fragment SQL Server's
/// clustered envelope tables; see <see cref="NewIdSqlServerOrder"/>. Can produce duplicates in cloud
/// environments where multiple instances share network identity (containers, Cloud Run, serverless).
Expand Down
Loading