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

fix: exception when creating duplicated topics #500

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions src/KafkaFlow/Clusters/ClusterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,25 @@ public async Task<IEnumerable<TopicPartitionOffset>> GetConsumerGroupOffsetsAsyn
public async Task CreateIfNotExistsAsync(IEnumerable<TopicConfiguration> configurations)
{
try
{
var topics = configurations
{
var duplicatedTopics = configurations
.GroupBy(c => c, new DuplicateTopicConfigurationEqualityComparer())
.Where(g => g.Count() > 1)
.Select(g => g.Key.Name)
.ToList();

foreach (var duplicatedTopic in duplicatedTopics)
{
_logHandler.Warning(
"Topic {Topic} declaration is duplicated. First topic configuration will be used",
new
{
duplicatedTopic,
});
}

var topics = configurations
.Distinct(new DuplicateTopicConfigurationEqualityComparer())
.Select(
topicConfiguration => new TopicSpecification
{
Expand Down Expand Up @@ -152,5 +169,12 @@ public void Dispose()
{
_lazyAdminClient.Value.Dispose();
}
}

private class DuplicateTopicConfigurationEqualityComparer : IEqualityComparer<TopicConfiguration>
{
public bool Equals(TopicConfiguration x, TopicConfiguration y) => x?.Name == y?.Name;

public int GetHashCode(TopicConfiguration obj) => obj.Name.GetHashCode();
}
}
Loading