Пример #1
0
        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);
        }
Пример #2
0
        public async Task <GroupAssignment> Assign(ClusterManager metadata, GroupSubscription groupSubscription)
        {
            Dictionary <string, Subscription> subscriptions = groupSubscription.groupSubscription();
            var allSubscribedTopics = new HashSet <string>();

            foreach (var topic in subscriptions.SelectMany(e => e.Value.Topics))
            {
                allSubscribedTopics.Add(topic);
            }

            var partitionsPerTopic = new Dictionary <string, int>();

            foreach (string topic in allSubscribedTopics)
            {
                int numPartitions = (await metadata.GetTopic(topic)).Partitions.Count;
                if (numPartitions > 0)
                {
                    partitionsPerTopic.Add(topic, numPartitions);
                }
                else
                {
                    log.debug("Skipping assignment for topic {} since no metadata is available", topic);
                }
            }

            Dictionary <string, List <TopicPartition> > rawAssignments = Assign(partitionsPerTopic, subscriptions);

            // this class maintains no user data, so just wrap the results
            var assignments = new Dictionary <string, Assignment>();

            foreach (var assignmentEntry in rawAssignments)
            {
                assignments.Add(assignmentEntry.Key, new Assignment(0, assignmentEntry.Value, null));
            }
            return(new GroupAssignment(assignments));
        }
Пример #3
0
        public async Task Subscribe(string topicName)
        {
            topic = await metadataManager.GetTopic(topicName);

            Fetch();
        }