Пример #1
0
        /// <inheritdoc cref="IBroker.GetProducer" />
        public virtual IProducer GetProducer(IProducerEndpoint endpoint)
        {
            Check.NotNull(endpoint, nameof(endpoint));

            if (_producers == null)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            return(_producers.GetOrAdd(
                       endpoint,
                       _ =>
            {
                _logger.LogInformation(
                    IntegrationEventIds.CreatingNewProducer,
                    "Creating new producer for endpoint {endpointName}. (Total producers: {ProducerCount})",
                    endpoint.Name,
                    _producers.Count + 1);

                return InstantiateProducer(
                    (TProducerEndpoint)endpoint,
                    _serviceProvider.GetRequiredService <IBrokerBehaviorsProvider <IProducerBehavior> >(),
                    _serviceProvider);
            }));
        }
Пример #2
0
        private async Task ReadChannelOnceAsync(
            ChannelReader <ConsumeResult <byte[]?, byte[]?> > channelReader,
            int channelIndex,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            _logger.LogTrace(
                IntegrationEventIds.LowLevelTracing,
                "Reading channel {channelIndex}... (consumerId: {consumerId})",
                channelIndex,
                _consumer.Id);

            var consumeResult = await channelReader.ReadAsync(cancellationToken).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            if (consumeResult.IsPartitionEOF)
            {
                _logger.LogInformation(
                    KafkaEventIds.EndOfPartition,
                    "Partition EOF reached: {topic}[{partition}]@{offset}. (consumerId: {consumerId})",
                    consumeResult.Topic,
                    consumeResult.Partition.Value,
                    consumeResult.Offset,
                    _consumer.Id);
                return;
            }

            // Checking if the message was sent to the subscribed topic is necessary
            // when reusing the same consumer for multiple topics.
            if (!_consumer.Endpoint.Names.Any(
                    endpointName =>
                    consumeResult.Topic.Equals(endpointName, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }

            if (_messagesLimiterSemaphoreSlim != null)
            {
                await _messagesLimiterSemaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);
            }

            try
            {
                await _consumer.HandleMessageAsync(consumeResult.Message, consumeResult.TopicPartitionOffset)
                .ConfigureAwait(false);
            }
            finally
            {
                _messagesLimiterSemaphoreSlim?.Release();
            }
        }
Пример #3
0
        private IEnumerable <TopicPartitionOffset> OnPartitionsAssigned(
            KafkaConsumer ownerConsumer,
            IConsumer <byte[]?, byte[]?> consumer,
            List <TopicPartition> partitions)
        {
            partitions.ForEach(
                partition =>
            {
                _logger.LogInformation(
                    KafkaEventIds.PartitionsAssigned,
                    "Assigned partition {topic} {partition}, member id: {memberId}",
                    partition.Topic,
                    partition.Partition,
                    consumer.MemberId);
            });

            var partitionsAssignedEvent = new KafkaPartitionsAssignedEvent(partitions, consumer.MemberId);

            CreateScopeAndPublishEvent(partitionsAssignedEvent);

            foreach (var topicPartitionOffset in partitionsAssignedEvent.Partitions)
            {
                if (topicPartitionOffset.Offset != Offset.Unset)
                {
                    _logger.LogDebug(
                        KafkaEventIds.PartitionOffsetReset,
                        "{topic} {partition} offset will be reset to {offset}.",
                        topicPartitionOffset.Topic,
                        topicPartitionOffset.Partition,
                        topicPartitionOffset.Offset);
                }
            }

            ownerConsumer.OnPartitionsAssigned(
                partitionsAssignedEvent.Partitions.Select(
                    topicPartitionOffset =>
                    topicPartitionOffset.TopicPartition).ToList());

            return(partitionsAssignedEvent.Partitions);
        }
Пример #4
0
        private static void OnPartitionsRevoked(
            List <TopicPartitionOffset> partitions,
            KafkaConsumer consumer,
            ISilverbackIntegrationLogger logger)
        {
            consumer.OnPartitionsRevoked();

            partitions.ForEach(
                partition =>
            {
                logger.LogInformation(
                    KafkaEventIds.PartitionsRevoked,
                    "Revoked partition {topic}[{partition}], member id: {memberId}",
                    partition.Topic,
                    partition.Partition.Value,
                    consumer.MemberId);
            });

            consumer.Endpoint.Events.PartitionsRevokedHandler?.Invoke(partitions, consumer);
        }
Пример #5
0
        private static IEnumerable <TopicPartitionOffset> OnPartitionsAssigned(
            List <TopicPartition> partitions,
            KafkaConsumer consumer,
            ISilverbackIntegrationLogger logger)
        {
            partitions.ForEach(
                partition =>
            {
                logger.LogInformation(
                    KafkaEventIds.PartitionsAssigned,
                    "Assigned partition {topic}[{partition}], member id: {memberId}",
                    partition.Topic,
                    partition.Partition.Value,
                    consumer.MemberId);
            });

            var topicPartitionOffsets =
                consumer.Endpoint.Events.PartitionsAssignedHandler?.Invoke(partitions, consumer).ToList() ??
                partitions.Select(partition => new TopicPartitionOffset(partition, Offset.Unset)).ToList();

            foreach (var topicPartitionOffset in topicPartitionOffsets)
            {
                if (topicPartitionOffset.Offset != Offset.Unset)
                {
                    logger.LogDebug(
                        KafkaEventIds.PartitionOffsetReset,
                        "{topic}[{partition}] offset will be reset to {offset}.",
                        topicPartitionOffset.Topic,
                        topicPartitionOffset.Partition.Value,
                        topicPartitionOffset.Offset);
                }
            }

            consumer.OnPartitionsAssigned(
                topicPartitionOffsets.Select(
                    topicPartitionOffset =>
                    topicPartitionOffset.TopicPartition).ToList());

            return(topicPartitionOffsets);
        }