-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TransactionalFlow and TransactionalSink support (#86)
* Major update of producers core to match alpakka codebase and add features * Added FlowWithContext implementation * Implemented source/flow with context integration test * Fixed producer stage reports handling * Optimized test timing * Fixed sample compilation errors * Implemented transactional source * Implemented transactional producer sink and flow * Added tests for future check (failing, so skipped) * Added InternalApi markers to not-working APIs
- Loading branch information
1 parent
21fd138
commit ce80944
Showing
21 changed files
with
1,046 additions
and
36 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/Akka.Streams.Kafka.Tests/Integration/TransactionalIntegrationTests.cs
This file contains 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,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Akka.Streams.Dsl; | ||
using Akka.Streams.Kafka.Dsl; | ||
using Akka.Streams.Kafka.Helpers; | ||
using Akka.Streams.Kafka.Messages; | ||
using Akka.Streams.Kafka.Settings; | ||
using Confluent.Kafka; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.Streams.Kafka.Tests.Integration | ||
{ | ||
public class TransactionalIntegrationTests : KafkaIntegrationTests | ||
{ | ||
public TransactionalIntegrationTests(ITestOutputHelper output, KafkaFixture fixture) | ||
: base(nameof(TransactionalIntegrationTests), output, fixture) | ||
{ | ||
} | ||
|
||
[Fact(Skip = "Missing producer transactions support, see https://github.com/akkadotnet/Akka.Streams.Kafka/issues/85")] | ||
public async Task Transactional_source_with_sink_Should_work() | ||
{ | ||
var settings = CreateConsumerSettings<string>(CreateGroup(1)); | ||
var sourceTopic = CreateTopic(1); | ||
var targetTopic = CreateTopic(2); | ||
var transactionalId = Guid.NewGuid().ToString(); | ||
const int totalMessages = 10; | ||
|
||
var control = KafkaConsumer.TransactionalSource(settings, Subscriptions.Topics(sourceTopic)) | ||
.Via(Business<TransactionalMessage<Null, string>>()) | ||
.Select(message => | ||
{ | ||
return ProducerMessage.Single( | ||
new ProducerRecord<Null, string>(targetTopic, message.Record.Key, message.Record.Value), | ||
passThrough: message.PartitionOffset); | ||
}) | ||
.ToMaterialized(KafkaProducer.TransactionalSink(ProducerSettings, transactionalId), Keep.Both) | ||
.MapMaterializedValue(DrainingControl<NotUsed>.Create) | ||
.Run(Materializer); | ||
|
||
var consumer = ConsumeStrings(targetTopic, totalMessages); | ||
|
||
await ProduceStrings(sourceTopic, Enumerable.Range(1, totalMessages), ProducerSettings); | ||
|
||
AssertTaskCompletesWithin(TimeSpan.FromSeconds(totalMessages), consumer.IsShutdown); | ||
AssertTaskCompletesWithin(TimeSpan.FromSeconds(totalMessages), control.DrainAndShutdown()); | ||
|
||
consumer.DrainAndShutdown().Result.Should().HaveCount(totalMessages); | ||
} | ||
|
||
private Flow<T, T, NotUsed> Business<T>() => Flow.Create<T>(); | ||
|
||
private DrainingControl<IImmutableList<ConsumeResult<Null, string>>> ConsumeStrings(string topic, int count) | ||
{ | ||
return KafkaConsumer.PlainSource(CreateConsumerSettings<string>(CreateGroup(1)), Subscriptions.Topics(topic)) | ||
.Take(count) | ||
.ToMaterialized(Sink.Seq<ConsumeResult<Null, string>>(), Keep.Both) | ||
.MapMaterializedValue(DrainingControl<IImmutableList<ConsumeResult<Null, string>>>.Create) | ||
.Run(Materializer); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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,16 @@ | ||
using Akka.Streams.Util; | ||
using Akka.Util; | ||
|
||
namespace Akka.Streams.Kafka.Extensions | ||
{ | ||
public static class OptionExtensions | ||
{ | ||
/// <summary> | ||
/// Gets option value, if any - otherwise returns default value provided | ||
/// </summary> | ||
public static T GetOrElse<T>(this Option<T> option, T defaultValue) | ||
{ | ||
return option.HasValue ? option.Value : defaultValue; | ||
} | ||
} | ||
} |
This file contains 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 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 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 @@ | ||
using System.Collections.Immutable; | ||
using System.Threading.Tasks; | ||
using Akka.Streams.Kafka.Stages.Consumers; | ||
using Confluent.Kafka; | ||
|
||
namespace Akka.Streams.Kafka.Messages | ||
{ | ||
/// <summary> | ||
/// Committed marker | ||
/// </summary> | ||
internal interface ICommittedMarker | ||
{ | ||
/// <summary> | ||
/// Marks offsets as already committed | ||
/// </summary> | ||
Task Committed(IImmutableDictionary<TopicPartition, OffsetAndMetadata> offsets); | ||
|
||
/// <summary> | ||
/// Marks committing failure | ||
/// </summary> | ||
void Failed(); | ||
} | ||
|
||
/// <summary> | ||
/// Used by <see cref="TransactionalMessageBuilder{K,V}"/> | ||
/// </summary> | ||
internal sealed class PartitionOffsetCommittedMarker : GroupTopicPartitionOffset | ||
{ | ||
/// <summary> | ||
/// Committed marker | ||
/// </summary> | ||
public ICommittedMarker CommittedMarker { get; } | ||
|
||
public PartitionOffsetCommittedMarker(string groupId, string topic, int partition, Offset offset, ICommittedMarker committedMarker) | ||
: base(groupId, topic, partition, offset) | ||
{ | ||
CommittedMarker = committedMarker; | ||
} | ||
|
||
public PartitionOffsetCommittedMarker(GroupTopicPartition groupTopicPartition, Offset offset, ICommittedMarker committedMarker) | ||
: base(groupTopicPartition, offset) | ||
{ | ||
CommittedMarker = committedMarker; | ||
} | ||
} | ||
} |
This file contains 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 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,30 @@ | ||
using Akka.Streams.Kafka.Dsl; | ||
using Confluent.Kafka; | ||
|
||
namespace Akka.Streams.Kafka.Messages | ||
{ | ||
/// <summary> | ||
/// Output element of <see cref="KafkaConsumer.TransactionalSource{K,V}"/> | ||
/// The offset is automatically committed as by the Producer | ||
/// </summary> | ||
public sealed class TransactionalMessage<K, V> | ||
{ | ||
/// <summary> | ||
/// TransactionalMessage | ||
/// </summary> | ||
public TransactionalMessage(ConsumeResult<K, V> record, GroupTopicPartitionOffset partitionOffset) | ||
{ | ||
Record = record; | ||
PartitionOffset = partitionOffset; | ||
} | ||
|
||
/// <summary> | ||
/// Consumed record | ||
/// </summary> | ||
public ConsumeResult<K, V> Record { get; } | ||
/// <summary> | ||
/// Partition offset | ||
/// </summary> | ||
public GroupTopicPartitionOffset PartitionOffset { get; } | ||
} | ||
} |
Oops, something went wrong.