|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Channels; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Confluent.Kafka; |
| 8 | +using Confluent.SchemaRegistry; |
| 9 | + |
| 10 | +namespace Take.Elephant.Kafka.SchemaRegistry |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A Kafka event stream consumer that uses Schema Registry for deserialization. |
| 14 | + /// </summary> |
| 15 | + /// <typeparam name="TKey">The type of the message key.</typeparam> |
| 16 | + /// <typeparam name="TEvent">The type of the event.</typeparam> |
| 17 | + public class KafkaSchemaRegistryEventStreamConsumer<TKey, TEvent> : IEventStreamConsumer<TKey, TEvent>, IOpenable, ICloseable, IDisposable |
| 18 | + where TEvent : class |
| 19 | + { |
| 20 | + private readonly IConsumer<TKey, TEvent> _consumer; |
| 21 | + private readonly ISchemaRegistryClient _schemaRegistryClient; |
| 22 | + private readonly bool _ownsSchemaRegistryClient; |
| 23 | + private readonly SemaphoreSlim _consumerStartSemaphore; |
| 24 | + private readonly CancellationTokenSource _cts; |
| 25 | + private readonly Channel<Message<TKey, TEvent>> _channel; |
| 26 | + private Task _consumerTask; |
| 27 | + private bool _closed; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Creates a new instance of <see cref="KafkaSchemaRegistryEventStreamConsumer{TKey, TEvent}"/>. |
| 31 | + /// </summary> |
| 32 | + /// <param name="bootstrapServers">The Kafka bootstrap servers.</param> |
| 33 | + /// <param name="topic">The topic name.</param> |
| 34 | + /// <param name="groupId">The consumer group ID.</param> |
| 35 | + /// <param name="schemaRegistryOptions">The Schema Registry options.</param> |
| 36 | + public KafkaSchemaRegistryEventStreamConsumer( |
| 37 | + string bootstrapServers, |
| 38 | + string topic, |
| 39 | + string groupId, |
| 40 | + SchemaRegistryOptions schemaRegistryOptions) |
| 41 | + : this( |
| 42 | + new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupId }, |
| 43 | + topic, |
| 44 | + schemaRegistryOptions) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Creates a new instance of <see cref="KafkaSchemaRegistryEventStreamConsumer{TKey, TEvent}"/>. |
| 50 | + /// </summary> |
| 51 | + /// <param name="consumerConfig">The consumer configuration.</param> |
| 52 | + /// <param name="topic">The topic name.</param> |
| 53 | + /// <param name="schemaRegistryOptions">The Schema Registry options.</param> |
| 54 | + public KafkaSchemaRegistryEventStreamConsumer( |
| 55 | + ConsumerConfig consumerConfig, |
| 56 | + string topic, |
| 57 | + SchemaRegistryOptions schemaRegistryOptions) |
| 58 | + : this( |
| 59 | + consumerConfig, |
| 60 | + topic, |
| 61 | + new CachedSchemaRegistryClient(schemaRegistryOptions.SchemaRegistryConfig), |
| 62 | + schemaRegistryOptions, |
| 63 | + ownsSchemaRegistryClient: true) |
| 64 | + { |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Creates a new instance of <see cref="KafkaSchemaRegistryEventStreamConsumer{TKey, TEvent}"/>. |
| 69 | + /// </summary> |
| 70 | + /// <param name="consumerConfig">The consumer configuration.</param> |
| 71 | + /// <param name="topic">The topic name.</param> |
| 72 | + /// <param name="schemaRegistryClient">The Schema Registry client.</param> |
| 73 | + /// <param name="schemaRegistryOptions">The Schema Registry options.</param> |
| 74 | + public KafkaSchemaRegistryEventStreamConsumer( |
| 75 | + ConsumerConfig consumerConfig, |
| 76 | + string topic, |
| 77 | + ISchemaRegistryClient schemaRegistryClient, |
| 78 | + SchemaRegistryOptions schemaRegistryOptions) |
| 79 | + : this(consumerConfig, topic, schemaRegistryClient, schemaRegistryOptions, ownsSchemaRegistryClient: false) |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + private KafkaSchemaRegistryEventStreamConsumer( |
| 84 | + ConsumerConfig consumerConfig, |
| 85 | + string topic, |
| 86 | + ISchemaRegistryClient schemaRegistryClient, |
| 87 | + SchemaRegistryOptions schemaRegistryOptions, |
| 88 | + bool ownsSchemaRegistryClient) |
| 89 | + { |
| 90 | + if (string.IsNullOrWhiteSpace(topic)) |
| 91 | + throw new ArgumentException("Value cannot be null or whitespace.", nameof(topic)); |
| 92 | + |
| 93 | + Topic = topic; |
| 94 | + _schemaRegistryClient = schemaRegistryClient ?? throw new ArgumentNullException(nameof(schemaRegistryClient)); |
| 95 | + _ownsSchemaRegistryClient = ownsSchemaRegistryClient; |
| 96 | + |
| 97 | + var deserializer = SchemaRegistrySerializerFactory.CreateDeserializer<TEvent>(schemaRegistryClient, schemaRegistryOptions); |
| 98 | + _consumer = new ConsumerBuilder<TKey, TEvent>(consumerConfig) |
| 99 | + .SetValueDeserializer(deserializer) |
| 100 | + .Build(); |
| 101 | + |
| 102 | + _consumerStartSemaphore = new SemaphoreSlim(1, 1); |
| 103 | + _cts = new CancellationTokenSource(); |
| 104 | + _channel = Channel.CreateBounded<Message<TKey, TEvent>>(1); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Creates a new instance of <see cref="KafkaSchemaRegistryEventStreamConsumer{TKey, TEvent}"/> using a pre-built consumer. |
| 109 | + /// </summary> |
| 110 | + /// <param name="consumer">The Kafka consumer.</param> |
| 111 | + /// <param name="topic">The topic name.</param> |
| 112 | + /// <param name="schemaRegistryClient">The Schema Registry client (optional, for lifecycle management).</param> |
| 113 | + public KafkaSchemaRegistryEventStreamConsumer( |
| 114 | + IConsumer<TKey, TEvent> consumer, |
| 115 | + string topic, |
| 116 | + ISchemaRegistryClient schemaRegistryClient = null) |
| 117 | + { |
| 118 | + if (string.IsNullOrWhiteSpace(topic)) |
| 119 | + throw new ArgumentException("Value cannot be null or whitespace.", nameof(topic)); |
| 120 | + |
| 121 | + _consumer = consumer ?? throw new ArgumentNullException(nameof(consumer)); |
| 122 | + Topic = topic; |
| 123 | + _schemaRegistryClient = schemaRegistryClient; |
| 124 | + _ownsSchemaRegistryClient = false; |
| 125 | + _consumerStartSemaphore = new SemaphoreSlim(1, 1); |
| 126 | + _cts = new CancellationTokenSource(); |
| 127 | + _channel = Channel.CreateBounded<Message<TKey, TEvent>>(1); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Gets the topic name. |
| 132 | + /// </summary> |
| 133 | + public string Topic { get; } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Occurs when the consumer fails. |
| 137 | + /// </summary> |
| 138 | + public event EventHandler<ExceptionEventArgs> ConsumerFailed; |
| 139 | + |
| 140 | + /// <inheritdoc /> |
| 141 | + public virtual async Task<(TKey key, TEvent item)> ConsumeOrDefaultAsync(CancellationToken cancellationToken) |
| 142 | + { |
| 143 | + await StartConsumerTaskIfNotAsync(cancellationToken); |
| 144 | + if (_channel.Reader.TryRead(out var message)) |
| 145 | + { |
| 146 | + return (message.Key, message.Value); |
| 147 | + } |
| 148 | + |
| 149 | + return default; |
| 150 | + } |
| 151 | + |
| 152 | + /// <inheritdoc /> |
| 153 | + public Task OpenAsync(CancellationToken cancellationToken) |
| 154 | + { |
| 155 | + return StartConsumerTaskIfNotAsync(cancellationToken); |
| 156 | + } |
| 157 | + |
| 158 | + /// <inheritdoc /> |
| 159 | + public virtual async Task CloseAsync(CancellationToken cancellationToken) |
| 160 | + { |
| 161 | + if (!_closed) |
| 162 | + { |
| 163 | + _closed = true; |
| 164 | + await _cts.CancelAsync(); |
| 165 | + if (_consumerTask != null) |
| 166 | + { |
| 167 | + await _consumerTask; |
| 168 | + } |
| 169 | + _consumer.Close(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /// <inheritdoc /> |
| 174 | + public void Dispose() |
| 175 | + { |
| 176 | + Dispose(true); |
| 177 | + GC.SuppressFinalize(this); |
| 178 | + } |
| 179 | + |
| 180 | + protected virtual void Dispose(bool disposing) |
| 181 | + { |
| 182 | + if (disposing) |
| 183 | + { |
| 184 | + if (!_closed) |
| 185 | + { |
| 186 | + _consumer.Close(); |
| 187 | + } |
| 188 | + |
| 189 | + _consumer.Dispose(); |
| 190 | + _cts.Dispose(); |
| 191 | + _consumerStartSemaphore.Dispose(); |
| 192 | + |
| 193 | + if (_ownsSchemaRegistryClient) |
| 194 | + { |
| 195 | + _schemaRegistryClient?.Dispose(); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private async Task StartConsumerTaskIfNotAsync(CancellationToken cancellationToken) |
| 201 | + { |
| 202 | + if (_consumerTask != null) return; |
| 203 | + |
| 204 | + await _consumerStartSemaphore.WaitAsync(cancellationToken); |
| 205 | + try |
| 206 | + { |
| 207 | + if (_consumerTask == null) |
| 208 | + { |
| 209 | + _consumerTask = Task |
| 210 | + .Factory |
| 211 | + .StartNew( |
| 212 | + () => ConsumeAsync(_cts.Token), |
| 213 | + TaskCreationOptions.LongRunning) |
| 214 | + .Unwrap(); |
| 215 | + } |
| 216 | + } |
| 217 | + finally |
| 218 | + { |
| 219 | + _consumerStartSemaphore.Release(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + private async Task ConsumeAsync(CancellationToken cancellationToken) |
| 224 | + { |
| 225 | + if (_consumer.Subscription.All(s => s != Topic)) |
| 226 | + { |
| 227 | + _consumer.Subscribe(Topic); |
| 228 | + } |
| 229 | + |
| 230 | + while (!cancellationToken.IsCancellationRequested) |
| 231 | + { |
| 232 | + try |
| 233 | + { |
| 234 | + var result = _consumer.Consume(cancellationToken); |
| 235 | + await _channel.Writer.WriteAsync(result.Message, cancellationToken); |
| 236 | + } |
| 237 | + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| 238 | + { |
| 239 | + break; |
| 240 | + } |
| 241 | + catch (Exception ex) |
| 242 | + { |
| 243 | + var handler = ConsumerFailed; |
| 244 | + if (handler != null) |
| 245 | + { |
| 246 | + handler.Invoke(this, new ExceptionEventArgs(ex)); |
| 247 | + } |
| 248 | + else |
| 249 | + { |
| 250 | + Trace.TraceError("An unhandled exception occurred on KafkaSchemaRegistryEventStreamConsumer: {0}", ex); |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + _consumer.Unsubscribe(); |
| 256 | + _channel.Writer.Complete(); |
| 257 | + } |
| 258 | + } |
| 259 | +} |
| 260 | + |
0 commit comments