public async ValueTask <ProduceResponse> Produce(Message <TKey, TValue> message) { var record = ProducerRecord.Create(message, keySerializer, valueSerializer); record.Topic = topic; var t = await cluster.GetTopic(record.Topic); if (message.PartitionId.HasValue) { record.PartitionId = message.PartitionId.Value; } else if (t.Partitions.Count == 1) { record.PartitionId = 0; } else { record.PartitionId = await partitioner.GetPartition(t, record.KeyBytes); } if (!record.PartitionId.HasValue) { throw new InvalidOperationException("PartitionId not specified"); } var partitionLeader = t.Partitions[record.PartitionId.Value].Leader; var b = cluster.GetBroker(partitionLeader); var response = await b.Produce(record); if (response.Topics.Count > 1 || response.Topics[0].Partitions.Count > 1) { throw new KafkaException("Expected single partition in Produce request"); } var partitionResponse = response.Topics[0].Partitions[0]; switch (partitionResponse.ErrorCode) { case ResponseError.LEADER_NOT_AVAILABLE: case ResponseError.NOT_LEADER_FOR_PARTITION: case ResponseError.PREFERRED_LEADER_NOT_AVAILABLE: throw new KafkaException("oops"); } return(response); }
private void Fetch(CancellationToken cancellationToken = default) { var channel = Channel.CreateBounded <Message <TKey, TValue> >(new BoundedChannelOptions(100) { SingleReader = true }); messageReader = channel.Reader; var messageWriter = channel.Writer; foreach (var p in topic.Partitions) { var broker = metadataManager.GetBroker(p.Leader); _ = Fetch(messageWriter, topic.TopicName, p, broker, cancellationToken); } }