Skip to content
Draft
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 Brighter.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
<Folder Name="/tools/">
<Project Path="tools/Paramore.Brighter.Test.Generator/Paramore.Brighter.Test.Generator.csproj" />
</Folder>
<Project Path="src/Paramore.Brighter.Analyzer.CodeFixes/Paramore.Brighter.Analyzer.CodeFixes.csproj" />
<Project Path="src/Paramore.Brighter.Analyzer.Package/Paramore.Brighter.Analyzer.Package.csproj" />
<Project Path="src/Paramore.Brighter.Analyzer/Paramore.Brighter.Analyzer.csproj" />
<Project Path="src/Paramore.Brighter.Archive.Azure/Paramore.Brighter.Archive.Azure.csproj" />
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="5.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.3" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.3" />
<PackageVersion Include="Microsoft.Bcl.TimeProvider" Version="10.0.7" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="7.0.1" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="10.0.7" />
Expand Down
3 changes: 3 additions & 0 deletions samples/AsyncAPI/KafkaAsyncAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ THE SOFTWARE. */
new()
{
Topic = new RoutingKey("order.created"),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ THE SOFTWARE. */
{
Topic = new RoutingKey("greeting.event"),
RequestType = typeof(GreetingEvent),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ THE SOFTWARE. */
{
Topic = new RoutingKey("greeting.event"),
RequestType = typeof(GreetingEvent),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ THE SOFTWARE. */
//the same topic for both TaskCreated and TaskUpdated, but different cloud events types
Topic = new RoutingKey("task.update"),
Type = new CloudEventsType("io.goparamore.task.created"),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand All @@ -61,6 +64,9 @@ THE SOFTWARE. */
{
Topic = new RoutingKey("task.update"),
Type = new CloudEventsType("io.goparamore.task.updated"),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ THE SOFTWARE. */
{
Topic = new RoutingKey("greeting.event"),
RequestType = typeof(GreetingEvent),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
MaxInFlightRequestsPerConnection = 1
Expand Down
3 changes: 3 additions & 0 deletions samples/TaskQueue/KafkaTaskQueue/GreetingsSender/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ THE SOFTWARE. */
{
Topic = new RoutingKey("greeting.event"),
RequestType = typeof(GreetingEvent),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ THE SOFTWARE. */
{
Topic = new RoutingKey("greeting.event"),
RequestType = typeof(GreetingEvent),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand Down
3 changes: 3 additions & 0 deletions samples/TaskQueue/MultiBus/GreetingsSender/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ THE SOFTWARE. */
{
Topic = new RoutingKey("greeting.event"),
RequestType = typeof(GreetingEvent),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Partitioner.Murmur2Random,
NumPartitions = 3,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public static IAmAProducerRegistry GetKafkaProducerRegistry<T>() where T: class,
{
Topic = new RoutingKey(typeof(T).Name),
RequestType = typeof(T),
// Murmur2Random is recommended: its MurmurHash2 hash spreads keys evenly across
// partitions, avoiding hot partitions
Partitioner = Paramore.Brighter.MessagingGateway.Kafka.Partitioner.Murmur2Random,
MessageSendMaxRetries = 3,
MessageTimeoutMs = 1000,
MaxInFlightRequestsPerConnection = 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#region License
/* The MIT License (MIT)
Copyright © 2026 Ian Cooper <ian_hammond_cooper@yahoo.co.uk>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

#endregion

using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;

namespace Paramore.Brighter.Analyzer.CodeFixes;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MissingPartitionerCodeFixProvider)), Shared]
public class MissingPartitionerCodeFixProvider : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => [DiagnosticsIds.MissingPartitioner];

public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (root == null)
{
return;
}

foreach (var diagnostic in context.Diagnostics)
{
var objectCreation = root.FindNode(diagnostic.Location.SourceSpan)
.AncestorsAndSelf()
.OfType<BaseObjectCreationExpressionSyntax>()
.FirstOrDefault();

if (objectCreation == null)
{
continue;
}

context.RegisterCodeFix(
CodeAction.Create(
title: $"Set 'Partitioner' to 'Partitioner.{BrighterAnalyzerGlobals.Murmur2RandomPartitionerValue}' (re-partitions the topic)",
createChangedDocument: ct => AddPartitionerAsync(context.Document, objectCreation, ct),
equivalenceKey: nameof(MissingPartitionerCodeFixProvider)),
diagnostic);
}
}

private static async Task<Document> AddPartitionerAsync(
Document document,
BaseObjectCreationExpressionSyntax objectCreation,
CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

var assignment = SyntaxFactory.AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
SyntaxFactory.IdentifierName(BrighterAnalyzerGlobals.PartitionerProperty),
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ParseExpression($"{BrighterAnalyzerGlobals.KafkaNamespace}.{BrighterAnalyzerGlobals.PartitionerEnum}"),
SyntaxFactory.IdentifierName(BrighterAnalyzerGlobals.Murmur2RandomPartitionerValue))
.WithAdditionalAnnotations(Simplifier.Annotation));

InitializerExpressionSyntax initializer;
if (objectCreation.Initializer == null)
{
initializer = SyntaxFactory.InitializerExpression(
SyntaxKind.ObjectInitializerExpression,
SyntaxFactory.SingletonSeparatedList<ExpressionSyntax>(assignment));
}
else if (objectCreation.Initializer.Expressions.Count == 0)
{
// new KafkaPublication { } — keep the existing (empty) braces and trivia.
initializer = objectCreation.Initializer.WithExpressions(
SyntaxFactory.SingletonSeparatedList<ExpressionSyntax>(assignment));
}
else
{
initializer = AddInitializerExpression(objectCreation.Initializer, assignment);
}

var newObjectCreation = objectCreation.WithInitializer(initializer);

// Drop a now-redundant empty argument list: with an initializer present,
// new KafkaPublication { ... } reads better than new KafkaPublication() { ... }.
// Keep the argument list's trailing trivia (the space before the brace).
if (newObjectCreation is ObjectCreationExpressionSyntax { ArgumentList.Arguments.Count: 0 } explicitCreation)
{
newObjectCreation = explicitCreation
.WithType(explicitCreation.Type.WithTrailingTrivia(explicitCreation.ArgumentList.GetTrailingTrivia()))
.WithArgumentList(null);
}

newObjectCreation = newObjectCreation.WithAdditionalAnnotations(Formatter.Annotation);

var newRoot = root!.ReplaceNode(objectCreation, newObjectCreation);
var formatted = await Formatter.FormatAsync(document.WithSyntaxRoot(newRoot), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);

// Reduce the fully qualified Partitioner reference where the using is
// already present; otherwise keep it qualified so the fix always compiles.
return await Simplifier.ReduceAsync(formatted, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
}

private static InitializerExpressionSyntax AddInitializerExpression(
InitializerExpressionSyntax initializer,
ExpressionSyntax expression)
{
// Build the new expression list by hand: AddExpressions would insert the
// separator comma right after the last expression but before its trailing
// trivia, so the comma lands on the wrong line and any trailing comment
// (e.g. "// one per shard") would move onto the new expression.
var nodesAndTokens = initializer.Expressions.GetWithSeparators();

// A trailing comma ("{ A = 1, B = 2, }") carries the closing-brace trivia:
// drop the comma and move its trivia onto the last expression.
if (nodesAndTokens.Count > 0 && nodesAndTokens[nodesAndTokens.Count - 1].IsToken)
{
var trailingComma = nodesAndTokens[nodesAndTokens.Count - 1].AsToken();
nodesAndTokens = nodesAndTokens.RemoveAt(nodesAndTokens.Count - 1);
nodesAndTokens = nodesAndTokens.Replace(
nodesAndTokens[nodesAndTokens.Count - 1],
((ExpressionSyntax)nodesAndTokens[nodesAndTokens.Count - 1].AsNode()!)
.WithTrailingTrivia(trailingComma.TrailingTrivia));
}

var lastExpression = (ExpressionSyntax)nodesAndTokens[nodesAndTokens.Count - 1].AsNode()!;
var trailingTrivia = lastExpression.GetTrailingTrivia();
var beforeEndOfLine = trailingTrivia.TakeWhile(t => !t.IsKind(SyntaxKind.EndOfLineTrivia)).ToList();
var fromEndOfLine = trailingTrivia.SkipWhile(t => !t.IsKind(SyntaxKind.EndOfLineTrivia)).ToList();

var separator = SyntaxFactory.Token(SyntaxKind.CommaToken);
ExpressionSyntax newExpression;
if (fromEndOfLine.Count == 0)
{
// Single-line initializer ("{ Topic = x }"): keep it on one line,
// with single spaces around the new expression.
var hasComment = beforeEndOfLine.Any(IsComment);
if (hasComment)
{
separator = separator.WithTrailingTrivia(beforeEndOfLine);
}

newExpression = expression
.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.Space))
.WithTrailingTrivia(hasComment
? SyntaxFactory.TriviaList(SyntaxFactory.Space)
: SyntaxFactory.TriviaList(beforeEndOfLine));
}
else
{
// Multi-line: comments stay on their line attached to the comma, the
// newline + indent that follows an existing separator (or the open
// brace) leads the new expression, and the newline before the closing
// brace moves behind it.
separator = separator.WithTrailingTrivia(beforeEndOfLine);
var leadingTrivia = nodesAndTokens.Count > 1
? nodesAndTokens[nodesAndTokens.Count - 2].AsToken().TrailingTrivia
: initializer.OpenBraceToken.TrailingTrivia;
newExpression = expression
.WithLeadingTrivia(leadingTrivia)
.WithTrailingTrivia(fromEndOfLine);
}

return initializer.WithExpressions(
SyntaxFactory.SeparatedList<ExpressionSyntax>(
nodesAndTokens
.Replace(nodesAndTokens[nodesAndTokens.Count - 1], lastExpression.WithoutTrailingTrivia())
.Add(separator)
.Add(newExpression)));
}

private static bool IsComment(SyntaxTrivia trivia)
{
return trivia.IsKind(SyntaxKind.SingleLineCommentTrivia) || trivia.IsKind(SyntaxKind.MultiLineCommentTrivia);
}
}
Loading
Loading