Exemple #1
0
        public void AsyncProducerSendsFewShortFixedMessagesInSeparateSendActions()
        {
            var prodConfig = this.AsyncProducerConfig1;

            using (var producer = new AsyncProducer(prodConfig))
            {
                var req1 = new ProducerRequest(
                    CurrentTestTopic,
                    0,
                    new List <Message> {
                    new Message(Encoding.UTF8.GetBytes("Async Test Message 1"))
                });
                producer.Send(req1);

                var req2 = new ProducerRequest(
                    CurrentTestTopic,
                    0,
                    new List <Message> {
                    new Message(Encoding.UTF8.GetBytes("Async Test Message 2"))
                });
                producer.Send(req2);

                var req3 = new ProducerRequest(
                    CurrentTestTopic,
                    0,
                    new List <Message> {
                    new Message(Encoding.UTF8.GetBytes("Async Test Message 3"))
                });
                producer.Send(req3);
            }
        }
Exemple #2
0
        private int SendMessages(int messagesPerNode, List <SyncProducerConfiguration> configs)
        {
            var count = 0;

            foreach (var syncProducerConfiguration in configs)
            {
                using (var producer = new SyncProducer(syncProducerConfiguration))
                {
                    var messageList = new List <Message>();
                    for (int i = 0; i < messagesPerNode; i++)
                    {
                        string payload1     = "kafka " + i.ToString();
                        byte[] payloadData1 = Encoding.UTF8.GetBytes(payload1);
                        var    msg1         = new Message(payloadData1);
                        messageList.Add(msg1);
                    }

                    var mSet    = new BufferedMessageSet(CompressionCodecs.NoCompressionCodec, messageList);
                    var request = new ProducerRequest(this.CurrentTestTopic, 0, mSet);
                    producer.Send(request);
                    count += mSet.Messages.Count();
                }
            }
            return(count);
        }
        public void GetBytesValidFormat()
        {
            string topicName = "topic";
            ProducerRequest request = new ProducerRequest(
                topicName, 0, new List<Message> { new Message(new byte[10]) });

            // format = len(request) + requesttype + len(topic) + topic + partition + len(messagepack) + message
            // total byte count = 4 + (2 + 2 + 5 + 4 + 4 + 19)
            byte[] bytes = request.GetBytes();
            Assert.IsNotNull(bytes);
            Assert.AreEqual(40, bytes.Length);

            // first 4 bytes = the length of the request
            Assert.AreEqual(36, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray<byte>()), 0));

            // next 2 bytes = the RequestType which in this case should be Produce
            Assert.AreEqual((short)RequestType.Produce, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray<byte>()), 0));

            // next 2 bytes = the length of the topic
            Assert.AreEqual((short)5, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray<byte>()), 0));

            // next 5 bytes = the topic
            Assert.AreEqual(topicName, Encoding.ASCII.GetString(bytes.Skip(8).Take(5).ToArray<byte>()));

            // next 4 bytes = the partition
            Assert.AreEqual(0, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(13).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = the length of the individual messages in the pack
            Assert.AreEqual(19, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(17).Take(4).ToArray<byte>()), 0));

            // fianl bytes = the individual messages in the pack
            Assert.AreEqual(19, bytes.Skip(21).ToArray<byte>().Length);
        }
Exemple #4
0
        public void GetBytesValidFormat()
        {
            string          topicName = "topic";
            ProducerRequest request   = new ProducerRequest(
                topicName, 0, new List <Message> {
                new Message(new byte[10])
            });

            // format = len(request) + requesttype + len(topic) + topic + partition + len(messagepack) + message
            // total byte count = 4 + (2 + 2 + 5 + 4 + 4 + 19)
            byte[] bytes = request.GetBytes();
            Assert.IsNotNull(bytes);
            Assert.AreEqual(40, bytes.Length);

            // first 4 bytes = the length of the request
            Assert.AreEqual(36, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the RequestType which in this case should be Produce
            Assert.AreEqual((short)RequestType.Produce, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray <byte>()), 0));

            // next 2 bytes = the length of the topic
            Assert.AreEqual((short)5, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray <byte>()), 0));

            // next 5 bytes = the topic
            Assert.AreEqual(topicName, Encoding.ASCII.GetString(bytes.Skip(8).Take(5).ToArray <byte>()));

            // next 4 bytes = the partition
            Assert.AreEqual(0, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(13).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = the length of the individual messages in the pack
            Assert.AreEqual(19, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(17).Take(4).ToArray <byte>()), 0));

            // fianl bytes = the individual messages in the pack
            Assert.AreEqual(19, bytes.Skip(21).ToArray <byte>().Length);
        }
        public async Task <long> CreateProducerAsync(ProducerRequest producerRequest)
        {
            var producer = new Producer
            {
                Addess         = producerRequest.Address,
                Name           = producerRequest.Name,
                Phone          = producerRequest.Phone,
                ProducerStatus = ProducerStatus.Active
            };

            DBContext.Producers.Add(producer);
            if (producerRequest.Logo != null)
            {
                var logo = new Asset
                {
                    Name       = producerRequest.Logo.Name,
                    Path       = producerRequest.Logo.Path,
                    ProducerId = producer.Id
                };
                DBContext.Assets.Add(logo);
            }
            await DBContext.SaveChangesAsync();

            return(producer.Id);
        }
Exemple #6
0
        public ProducerResponse Send(ProducerRequest producerRequest)
        {
            var requestSize = producerRequest.SizeInBytes;

            this.producerRequestStats.GetProducerRequestStats(this.BrokerInfo).RequestSizeHist.Update(requestSize);
            this.producerRequestStats.GetProducerRequestAllBrokersStats().RequestSizeHist.Update(requestSize);

            Receive response       = null;
            var     specificTimer  = this.producerRequestStats.GetProducerRequestStats(this.BrokerInfo).RequestTimer;
            var     aggregateTimer = this.producerRequestStats.GetProducerRequestAllBrokersStats().RequestTimer;

            aggregateTimer.Time(() => specificTimer.Time(() =>
            {
                response = this.DoSend(producerRequest, producerRequest.RequiredAcks != 0);
            }));

            if (producerRequest.RequiredAcks != 0)
            {
                return(ProducerResponse.ReadFrom(response.Buffer));
            }
            else
            {
                return(null);
            }
        }
Exemple #7
0
        public async Task <ActionResult> Create(Data.ProducerDto input, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var producerRequest = new ProducerRequest
                {
                    Address = input.Address,
                    Name    = input.Name,
                    Phone   = input.Phone
                };
                if (file != null)
                {
                    string root         = AppDomain.CurrentDomain.BaseDirectory;
                    string fileLocation = Path.Combine(root, "Content", "Images");
                    if (!Directory.Exists(fileLocation))
                    {
                        Directory.CreateDirectory(fileLocation);
                    }
                    file.SaveAs($"{fileLocation}/{file.FileName}");
                    producerRequest.Logo = new Image
                    {
                        Name = file.FileName,
                        Path = $"/Content/Images/{file.FileName}"
                    };
                }
                input.Id = await producerDAO.CreateProducerAsync(producerRequest);

                if (input.Id > 0)
                {
                    TempData["SaveProducer"] = "Thêm hãng sản xuất thành công!";
                    return(RedirectToAction("Index", "Producer"));
                }
            }
            return(View(input));
        }
Exemple #8
0
        public void SimpleSyncProducerSendsLotsOfMessagesIncreasingTheSizeAndConsumerConnectorGetsThemBack()
        {
            var prodConfig     = this.SyncProducerConfig1;
            var consumerConfig = this.ZooKeeperBasedConsumerConfig;
            var consConf       = this.ConsumerConfig1;

            consumerConfig.AutoCommitInterval = 1000;
            int    numberOfMessagesToSend = 2000;
            string topic = CurrentTestTopic;

            var msgList = new List <Message>();

            using (var producer = new SyncProducer(prodConfig))
            {
                for (int i = 0; i < numberOfMessagesToSend; i++)
                {
                    string payload     = CreatePayloadByNumber(i);
                    byte[] payloadData = Encoding.UTF8.GetBytes(payload);
                    var    msg         = new Message(payloadData);
                    msgList.Add(msg);
                    var producerRequest = new ProducerRequest(topic, 0, new List <Message>()
                    {
                        msg
                    });
                    producer.Send(producerRequest);
                }
            }

            Thread.Sleep(3000);

            // now consuming
            int messageNumberCounter = 0;

            using (IConsumerConnector consumerConnector = new ZookeeperConsumerConnector(consumerConfig, true))
            {
                var topicCount = new Dictionary <string, int> {
                    { topic, 1 }
                };
                var messages = consumerConnector.CreateMessageStreams(topicCount);
                var sets     = messages[topic];

                try
                {
                    foreach (var set in sets)
                    {
                        foreach (var message in set)
                        {
                            Assert.AreEqual(CreatePayloadByNumber(messageNumberCounter), Encoding.UTF8.GetString(message.Payload));
                            messageNumberCounter++;
                        }
                    }
                }
                catch (ConsumerTimeoutException)
                {
                    // do nothing, this is expected
                }
            }

            Assert.AreEqual(numberOfMessagesToSend, messageNumberCounter);
        }
Exemple #9
0
        public void MaxFetchSizeBugShouldNotAppearWhenSmallFetchSizeAndSingleMessageSmallerThanFetchSize()
        {
            var prodConfig     = this.SyncProducerConfig1;
            var consumerConfig = this.ZooKeeperBasedConsumerConfig;

            consumerConfig.FetchSize          = 256;
            consumerConfig.NumberOfTries      = 1;
            consumerConfig.AutoCommitInterval = 1000;
            int    numberOfMessagesToSend = 100;
            string topic = CurrentTestTopic;

            var msgList = new List <Message>();

            using (var producer = new SyncProducer(prodConfig))
            {
                for (int i = 0; i < numberOfMessagesToSend; i++)
                {
                    string payload     = CreatePayloadByNumber(i + 100);
                    byte[] payloadData = Encoding.UTF8.GetBytes(payload);
                    var    msg         = new Message(payloadData);
                    msgList.Add(msg);
                    var producerRequest = new ProducerRequest(topic, 0, new List <Message>()
                    {
                        msg
                    });
                    producer.Send(producerRequest);
                }
            }

            // now consuming
            int messageNumberCounter = 0;

            using (IConsumerConnector consumerConnector = new ZookeeperConsumerConnector(consumerConfig, true))
            {
                var topicCount = new Dictionary <string, int> {
                    { topic, 1 }
                };
                var messages = consumerConnector.CreateMessageStreams(topicCount);
                var sets     = messages[topic];

                try
                {
                    foreach (var set in sets)
                    {
                        foreach (var message in set)
                        {
                            Assert.AreEqual(CreatePayloadByNumber(messageNumberCounter + 100), Encoding.UTF8.GetString(message.Payload));
                            messageNumberCounter++;
                        }
                    }
                }
                catch (ConsumerTimeoutException)
                {
                    // do nothing, this is expected
                }
            }

            Assert.AreEqual(numberOfMessagesToSend, messageNumberCounter);
        }
Exemple #10
0
 /// <summary>
 /// Writes a producer request to the server asynchronously.
 /// </summary>
 /// <param name="request">The request to make.</param>
 public void BeginWrite(ProducerRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.NotNull(request, "request");
     NetworkStream stream = client.GetStream();
     byte[] data = request.RequestBuffer.GetBuffer();
     stream.BeginWrite(data, 0, data.Length, asyncResult => ((NetworkStream)asyncResult.AsyncState).EndWrite(asyncResult), stream);
 }
Exemple #11
0
        public void SimpleSyncProducerSends2CompressedMessagesAndConsumerConnectorGetsThemBack()
        {
            var prodConfig     = this.SyncProducerConfig1;
            var consumerConfig = this.ZooKeeperBasedConsumerConfig;

            // first producing
            string payload1 = "kafka 1.";

            byte[] payloadData1 = Encoding.UTF8.GetBytes(payload1);
            var    msg1         = new Message(payloadData1);

            string payload2 = "kafka 2.";

            byte[] payloadData2 = Encoding.UTF8.GetBytes(payload2);
            var    msg2         = new Message(payloadData2);

            Message compressedMessage = CompressionUtils.Compress(new List <Message> {
                msg1, msg2
            }, CompressionCodecs.DefaultCompressionCodec);
            var producerRequest = new ProducerRequest(CurrentTestTopic, 0, new List <Message> {
                compressedMessage
            });

            using (var producer = new SyncProducer(prodConfig))
            {
                producer.Send(producerRequest);
            }

            // now consuming
            var resultMessages = new List <Message>();

            using (IConsumerConnector consumerConnector = new ZookeeperConsumerConnector(consumerConfig, true))
            {
                var topicCount = new Dictionary <string, int> {
                    { CurrentTestTopic, 1 }
                };
                var messages = consumerConnector.CreateMessageStreams(topicCount);
                var sets     = messages[CurrentTestTopic];
                try
                {
                    foreach (var set in sets)
                    {
                        foreach (var message in set)
                        {
                            resultMessages.Add(message);
                        }
                    }
                }
                catch (ConsumerTimeoutException)
                {
                    // do nothing, this is expected
                }
            }

            Assert.AreEqual(2, resultMessages.Count);
            Assert.AreEqual(msg1.ToString(), resultMessages[0].ToString());
            Assert.AreEqual(msg2.ToString(), resultMessages[1].ToString());
        }
Exemple #12
0
        public void IsValidTrue()
        {
            ProducerRequest request = new ProducerRequest(
                "topic", 0, new List <Message> {
                new Message(new byte[10])
            });

            Assert.IsTrue(request.IsValid());
        }
Exemple #13
0
 /// <summary>
 /// Sends a request to Kafka.
 /// </summary>
 /// <param name="request">The request to send to Kafka.</param>
 public void Send(ProducerRequest request)
 {
     if (request.IsValid())
     {
         using (KafkaConnection connection = new KafkaConnection(Server, Port))
         {
             connection.Write(request);
         }
     }
 }
 public RequestResponseSerializationTest()
 {
     this.producerRequest       = SerializationTestUtils.CreateTestProducerRequest();
     this.producerResponse      = SerializationTestUtils.CreateTestProducerResponse();
     this.fetchRequest          = SerializationTestUtils.CreateTestFetchRequest();
     this.offsetRequest         = SerializationTestUtils.CreateTestOffsetRequest();
     this.offsetResponse        = SerializationTestUtils.CreateTestOffsetResponse();
     this.topicMetadataRequest  = SerializationTestUtils.CreateTestTopicMetadataRequest();
     this.topicMetadataResponse = SerializationTestUtils.CreateTestTopicMetadataResponse();
 }
Exemple #15
0
        /// <summary>
        /// Sends request to Kafka server asynchronously
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="callback">
        /// The callback invoked when a request is finished being sent.
        /// </param>
        public void Send(ProducerRequest request, MessageSent <ProducerRequest> callback)
        {
            this.EnsuresNotDisposed();
            Guard.NotNull(request, "request");
            Guard.NotNull(request.MessageSet, "request.MessageSet");
            Guard.NotNull(request.MessageSet.Messages, "request.MessageSet.Messages");
            Guard.Assert <ArgumentException>(
                () => request.MessageSet.Messages.All(x => x.PayloadSize <= this.Config.MaxMessageSize));

            connection.BeginWrite(request, callback);
        }
Exemple #16
0
        public void OneMessageIsSentAndReceivedThenExceptionsWhenNoMessageThenAnotherMessageIsSentAndReceived()
        {
            var prodConfig     = this.SyncProducerConfig1;
            var consumerConfig = this.ZooKeeperBasedConsumerConfig;

            // first producing
            string payload1 = "kafka 1.";

            byte[] payloadData1 = Encoding.UTF8.GetBytes(payload1);
            var    msg1         = new Message(payloadData1);

            using (var producer = new SyncProducer(prodConfig))
            {
                var producerRequest = new ProducerRequest(CurrentTestTopic, 0, new List <Message> {
                    msg1
                });
                producer.Send(producerRequest);

                // now consuming
                using (IConsumerConnector consumerConnector = new ZookeeperConsumerConnector(consumerConfig, true))
                {
                    var topicCount = new Dictionary <string, int> {
                        { CurrentTestTopic, 1 }
                    };
                    var messages = consumerConnector.CreateMessageStreams(topicCount);
                    var sets     = messages[CurrentTestTopic];
                    KafkaMessageStream myStream = sets[0];
                    var enumerator = myStream.GetEnumerator();

                    Assert.IsTrue(enumerator.MoveNext());
                    Assert.AreEqual(msg1.ToString(), enumerator.Current.ToString());

                    Assert.Throws <ConsumerTimeoutException>(() => enumerator.MoveNext());

                    Assert.Throws <IllegalStateException>(() => enumerator.MoveNext()); // iterator is in failed state

                    enumerator.Reset();

                    // producing again
                    string payload2     = "kafka 2.";
                    byte[] payloadData2 = Encoding.UTF8.GetBytes(payload2);
                    var    msg2         = new Message(payloadData2);

                    var producerRequest2 = new ProducerRequest(CurrentTestTopic, 0, new List <Message> {
                        msg2
                    });
                    producer.Send(producerRequest2);
                    Thread.Sleep(3000);

                    Assert.IsTrue(enumerator.MoveNext());
                    Assert.AreEqual(msg2.ToString(), enumerator.Current.ToString());
                }
            }
        }
    public void TestSerializationAndDeserialization()
    {
        var buffer = ByteBuffer.Allocate(this.producerRequest.SizeInBytes);

        this.producerRequest.WriteTo(buffer);
        buffer.Rewind();
        var deserializedProducerRequest = ProducerRequest.ReadFrom(buffer);

        Assert.Equal(this.producerRequest, deserializedProducerRequest);

        buffer = ByteBuffer.Allocate(this.producerResponse.SizeInBytes);
        this.producerResponse.WriteTo(buffer);
        buffer.Rewind();
        var deserializedProducerResponse = ProducerResponse.ReadFrom(buffer);

        Assert.Equal(this.producerResponse, deserializedProducerResponse);

        buffer = ByteBuffer.Allocate(this.fetchRequest.SizeInBytes);
        this.fetchRequest.WriteTo(buffer);
        buffer.Rewind();
        var deserializedFetchRequest = FetchRequest.ReadFrom(buffer);

        Assert.Equal(this.fetchRequest, deserializedFetchRequest);

        buffer = ByteBuffer.Allocate(this.offsetRequest.SizeInBytes);
        this.offsetRequest.WriteTo(buffer);
        buffer.Rewind();
        var deserializedOffsetRequest = OffsetRequest.ReadFrom(buffer);

        Assert.Equal(this.offsetRequest, deserializedOffsetRequest);

        buffer = ByteBuffer.Allocate(this.offsetResponse.SizeInBytes);
        this.offsetResponse.WriteTo(buffer);
        buffer.Rewind();
        var deserializedOffsetResponse = OffsetResponse.ReadFrom(buffer);

        Assert.Equal(this.offsetResponse, deserializedOffsetResponse);

        buffer = ByteBuffer.Allocate(this.topicMetadataRequest.SizeInBytes);
        this.topicMetadataRequest.WriteTo(buffer);
        buffer.Rewind();
        var deserializedTopicMetadataRequest = TopicMetadataRequest.ReadFrom(buffer);

        Assert.Equal(this.topicMetadataRequest, deserializedTopicMetadataRequest);

        buffer = ByteBuffer.Allocate(this.topicMetadataResponse.SizeInBytes);
        this.topicMetadataResponse.WriteTo(buffer);
        buffer.Rewind();
        var deserializedTopicMetadataResponse = TopicMetadataResponse.ReadFrom(buffer);

        Assert.Equal(this.topicMetadataResponse, deserializedTopicMetadataResponse);
    }
Exemple #18
0
 /// <summary>
 /// Sends request to Kafka server asynchronously
 /// </summary>
 /// <param name="request">
 /// The request.
 /// </param>
 public void Send(ProducerRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.NotNull(request, "request");
     Guard.Assert <ArgumentException>(() => request.MessageSet.Messages.All(x => x.PayloadSize <= this.Config.MaxMessageSize));
     if (this.callbackHandler != null)
     {
         this.Send(request, this.callbackHandler.Handle);
     }
     else
     {
         this.connection.BeginWrite(request);
     }
 }
        /// <summary>
        /// Sends request to Kafka server synchronously
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        public ProducerResponse Send(ProducerRequest request)
        {
            this.EnsuresNotDisposed();

            foreach (var topicData in request.Data)
            {
                foreach (var partitionData in topicData.PartitionData)
                {
                    VerifyMessageSize(partitionData.MessageSet.Messages);
                }
            }

            return(this.connection.Send(request));
        }
Exemple #20
0
        public void AsyncProducerSendsCompressedAndConsumerReceivesSingleSimpleMessage()
        {
            var prodConfig     = this.AsyncProducerConfig1;
            var consumerConfig = this.ConsumerConfig1;

            var sourceMessage     = new Message(Encoding.UTF8.GetBytes("test message"));
            var compressedMessage = CompressionUtils.Compress(new List <Message> {
                sourceMessage
            }, CompressionCodecs.GZIPCompressionCodec);

            long currentOffset = TestHelper.GetCurrentKafkaOffset(CurrentTestTopic, consumerConfig);

            using (var producer = new AsyncProducer(prodConfig))
            {
                var producerRequest = new ProducerRequest(
                    CurrentTestTopic, 0, new List <Message> {
                    compressedMessage
                });
                producer.Send(producerRequest);
            }

            IConsumer consumer = new Consumer(consumerConfig);
            var       request  = new FetchRequest(CurrentTestTopic, 0, currentOffset);

            BufferedMessageSet response;
            int totalWaitTimeInMiliseconds = 0;
            int waitSingle = 100;

            while (true)
            {
                Thread.Sleep(waitSingle);
                response = consumer.Fetch(request);
                if (response != null && response.Messages.Count() > 0)
                {
                    break;
                }

                totalWaitTimeInMiliseconds += waitSingle;
                if (totalWaitTimeInMiliseconds >= MaxTestWaitTimeInMiliseconds)
                {
                    break;
                }
            }

            Assert.NotNull(response);
            Assert.AreEqual(1, response.Messages.Count());
            Message resultMessage = response.Messages.First();

            Assert.AreEqual(compressedMessage.ToString(), resultMessage.ToString());
        }
Exemple #21
0
        public void ProducerSendsMessageWithLongTopic()
        {
            var prodConfig = this.SyncProducerConfig1;

            var    msg   = new Message(Encoding.UTF8.GetBytes("test message"));
            string topic = "ThisIsAVeryLongTopicThisIsAVeryLongTopicThisIsAVeryLongTopicThisIsAVeryLongTopicThisIsAVeryLongTopicThisIsAVeryLongTopic";

            using (var producer = new SyncProducer(prodConfig))
            {
                var producerRequest = new ProducerRequest(topic, 0, new List <Message> {
                    msg
                });
                producer.Send(producerRequest);
            }
        }
        public void TestProduceRequestWithNoResponse()
        {
            var props = TestUtils.GetSyncProducerConfig(this.Configs[0].Port);

            var correlationId = 0;
            var clientId      = SyncProducerConfig.DefaultClientId;
            var ackTimeoutMs  = SyncProducerConfig.DefaultAckTimeout;
            var ack           = (short)0;
            var emptyRequest  = new ProducerRequest(
                correlationId, clientId, ack, ackTimeoutMs, new Dictionary <TopicAndPartition, ByteBufferMessageSet>());
            var producer = new SyncProducer(props);
            var response = producer.Send(emptyRequest);

            Assert.True(response == null);
        }
        public void TestEmptyProduceRequest()
        {
            var props = TestUtils.GetSyncProducerConfig(this.Configs[0].Port);

            var   correlationId = 0;
            var   clientId      = SyncProducerConfig.DefaultClientId;
            var   acktimeoutMs  = SyncProducerConfig.DefaultAckTimeout;
            short ack           = 1;
            var   emptyResult   = new ProducerRequest(
                correlationId, clientId, ack, acktimeoutMs, new Dictionary <TopicAndPartition, ByteBufferMessageSet>());

            var producer = new SyncProducer(props);
            var response = producer.Send(emptyResult);

            Assert.True(response != null);
            Assert.True(!response.HasError() && response.Status.Count() == 0);
        }
Exemple #24
0
 private void VerifyRequest(RequestOrResponse request)
 {
     /**
      * This seems a little convoluted, but the idea is to turn on verification simply changing log4j settings
      * Also, when verification is turned on, care should be taken to see that the logs don't fill up with unnecessary
      * Data. So, leaving the rest of the logging at TRACE, while errors should be logged at ERROR level
      */
     if (Logger.IsDebugEnabled)
     {
         var buffer = new BoundedByteBufferSend(request).Buffer;
         Logger.Debug("Verifying sendbuffer of size " + buffer.Limit());
         var requestTypeId = buffer.GetShort();
         if (requestTypeId == RequestKeys.ProduceKey)
         {
             var innerRequest = ProducerRequest.ReadFrom(buffer);
             Logger.Debug(innerRequest.ToString());
         }
     }
 }
        public async Task <bool> EditProducerAsync(ProducerRequest producerRequest)
        {
            var producer = await DBContext.Producers.FirstOrDefaultAsync(x => x.Id == producerRequest.Id);

            producer.Name   = producerRequest.Name;
            producer.Phone  = producerRequest.Phone;
            producer.Addess = producerRequest.Address;
            var assets = await DBContext.Assets.Where(x => x.ProducerId == producerRequest.Id).ToListAsync();

            assets.ForEach(x => DBContext.Assets.Remove(x));
            if (producerRequest.Logo != null)
            {
                var logo = new Asset
                {
                    Name       = producerRequest.Logo.Name,
                    Path       = producerRequest.Logo.Path,
                    ProducerId = producer.Id
                };
                DBContext.Assets.Add(logo);
            }
            return(await DBContext.SaveChangesAsync() > 0);
        }
Exemple #26
0
        public void ProducerSendsMessage()
        {
            var prodConfig = this.SyncProducerConfig1;

            string payload1 = "kafka 1.";

            byte[] payloadData1 = Encoding.UTF8.GetBytes(payload1);
            var    msg1         = new Message(payloadData1);

            string payload2 = "kafka 2.";

            byte[] payloadData2 = Encoding.UTF8.GetBytes(payload2);
            var    msg2         = new Message(payloadData2);

            using (var producer = new SyncProducer(prodConfig))
            {
                var producerRequest = new ProducerRequest(CurrentTestTopic, 0, new List <Message> {
                    msg1, msg2
                });
                producer.Send(producerRequest);
            }
        }
Exemple #27
0
 /// <summary>
 /// Writes a producer request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="ProducerRequest"/> to send to the server.</param>
 public void Write(ProducerRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.Assert<ArgumentNullException>(() => request != null);
     this.Write(request.RequestBuffer.GetBuffer(), Timeout.Infinite);
 }
Exemple #28
0
 /// <summary>
 /// Writes a producer request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="ProducerRequest"/> to send to the server.</param>
 public void Write(ProducerRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.NotNull(request, "request");
     this.Write(request.RequestBuffer.GetBuffer());
 }
Exemple #29
0
        /// <summary>
        /// Writes a producer request to the server asynchronously.
        /// </summary>
        /// <param name="request">The request to make.</param>
        /// <param name="callback">The code to execute once the message is completely sent.</param>
        /// <remarks>
        /// Do not dispose connection till callback is invoked, 
        /// otherwise underlying network stream will be closed.
        /// </remarks>
        public void BeginWrite(ProducerRequest request, MessageSent<ProducerRequest> callback)
        {
            this.EnsuresNotDisposed();
            Guard.NotNull(request, "request");
            if (callback == null)
            {
                this.BeginWrite(request);
                return;
            }

            NetworkStream stream = client.GetStream();
            var ctx = new RequestContext<ProducerRequest>(stream, request);

            byte[] data = request.RequestBuffer.GetBuffer();
            stream.BeginWrite(
                data,
                0,
                data.Length,
                delegate(IAsyncResult asyncResult)
                    {
                        var context = (RequestContext<ProducerRequest>)asyncResult.AsyncState;
                        callback(context);
                        context.NetworkStream.EndWrite(asyncResult);
                    },
                ctx);
        }
        public void GetBytesValidStructure()
        {
            string topicName = "topic";
            int correlationId = 1;
            string clientId = "TestClient";
            short requiredAcks = 5;
            int ackTimeout = 345;

            var partition = 2;
            short error = 0;
            var payload = Encoding.UTF8.GetBytes("testMessage");
            BufferedMessageSet messageSet = new BufferedMessageSet(new List<Message>() { new Message(payload) }, 0);

            var partitionData = new PartitionData(partition, ErrorMapper.ToError(error), messageSet);

            var topicData = new TopicData(topicName, new List<PartitionData>() { partitionData });

            var request = new ProducerRequest(correlationId, clientId, requiredAcks, ackTimeout, new List<TopicData>() { topicData });

            int requestSize = 2 + //request type id
                              2 + //versionId
                              4 + //correlation id
                              request.GetShortStringWriteLength(clientId) + // actual client id
                              2 + //required acks
                              4 + //ack timeout
                              4 + //data count
                                  //=== data part
                              request.GetShortStringWriteLength(topicName) + //topic
                              4 + //partition data count
                              4 + //partition id
                              4 + //messages set size
                              messageSet.SetSize;

            var ms = new MemoryStream();
            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);

            // add 4 bytes for the length of the message at the beginning
            Assert.AreEqual(requestSize + 4, bytes.Length);

            // first 4 bytes = the message length
            Assert.AreEqual(requestSize, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray<byte>()), 0));

            // next 2 bytes = the request type
            Assert.AreEqual((short)RequestTypes.Produce, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray<byte>()), 0));

            // next 2 bytes = the version id
            Assert.AreEqual((short)ProducerRequest.CurrentVersion, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray<byte>()), 0));

            // next 2 bytes = the correlation id
            Assert.AreEqual(correlationId, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(8).Take(4).ToArray<byte>()), 0));

            // next 2 bytes = the client id length
            Assert.AreEqual((short)clientId.Length, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(12).Take(2).ToArray<byte>()), 0));

            // next few bytes = the client id
            Assert.AreEqual(clientId, Encoding.ASCII.GetString(bytes.Skip(14).Take(clientId.Length).ToArray<byte>()));

            // next 2 bytes = the required acks
            Assert.AreEqual((short)requiredAcks, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(14 + clientId.Length).Take(2).ToArray<byte>()), 0));

            // next 4 bytes = the ack timeout
            Assert.AreEqual(ackTimeout, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(16 + clientId.Length).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = the data count
            Assert.AreEqual(1, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(20 + clientId.Length).Take(4).ToArray<byte>()), 0));

            // next 2 bytes = the tppic length
            Assert.AreEqual((short)topicName.Length, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(24 + clientId.Length).Take(2).ToArray<byte>()), 0));

            // next few bytes = the topic
            Assert.AreEqual(topicName, Encoding.ASCII.GetString(bytes.Skip(26 + clientId.Length).Take(topicName.Length).ToArray<byte>()));

            // next 4 bytes = the partition data count
            Assert.AreEqual(topicData.PartitionData.Count(), BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(26 + clientId.Length + topicName.Length).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = the partition
            Assert.AreEqual(partition, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(30 + clientId.Length + topicName.Length).Take(4).ToArray<byte>()), 0));

            // skipping MessageSet check - this could be done separately in another unit test
        }
Exemple #31
0
        public void IsValidFalseNoMessages()
        {
            ProducerRequest request = new ProducerRequest("topic", 0, null);

            Assert.IsFalse(request.IsValid());
        }
Exemple #32
0
 /// <summary>
 /// Writes a producer request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="ProducerRequest"/> to send to the server.</param>
 public void Write(ProducerRequest request)
 {
     Write(request.GetBytes());
 }
Exemple #33
0
 /// <summary>
 /// Sends request to Kafka server synchronously
 /// </summary>
 /// <param name="request">
 /// The request.
 /// </param>
 public void Send(ProducerRequest request)
 {
     Guard.NotNull(request, "request");
     SendRequest(request);
 }
        /// <summary>
        /// Constructs and sends the produce request based on a map from (topic, partition) -> messages
        /// </summary>
        /// <param name="brokerId">brokerId the broker that will receive the request</param>
        /// <param name="messagesPerTopic"></param>
        /// <returns> the set (topic, partitions) messages which incurred an error sending or processing</returns>
        private List <TopicAndPartition> Send(int brokerId, IDictionary <TopicAndPartition, ByteBufferMessageSet> messagesPerTopic)
        {
            if (brokerId < 0)
            {
                Logger.WarnFormat("Failed to send Data since partitions {0} don't have a leader", string.Join(",", messagesPerTopic.Select(m => m.Key.Partiton)));
                return(new List <TopicAndPartition>(messagesPerTopic.Keys));
            }

            if (messagesPerTopic.Count > 0)
            {
                var currentCorrelationId  = this.correlationId.GetAndIncrement();
                var producerRequest       = new ProducerRequest(currentCorrelationId, this.Config.ClientId, this.Config.RequestRequiredAcks, this.Config.RequestTimeoutMs, messagesPerTopic);
                var failedTopicPartitions = new List <TopicAndPartition>();

                try
                {
                    var syncProducer = this.producerPool.GetProducer(brokerId);
                    Logger.DebugFormat(
                        "Producer sending messages with correlation id {0} for topics {1} to broker {2} on {3}:{4}",
                        currentCorrelationId,
                        string.Join(",", messagesPerTopic.Keys),
                        brokerId,
                        syncProducer.Config.Host,
                        syncProducer.Config.Port);

                    var response = syncProducer.Send(producerRequest);

                    if (response != null)
                    {
                        if (response.Status.Count() != producerRequest.Data.Count())
                        {
                            throw new KafkaException(
                                      string.Format(
                                          "Incomplete response ({0}) for producer request ({1})", response, producerRequest));
                        }

                        if (Logger.IsDebugEnabled)
                        {
                            var successfullySentData = response.Status.Where(s => s.Value.Error == ErrorMapping.NoError).ToList();
                            foreach (var m in successfullySentData)
                            {
                                var iter = messagesPerTopic[m.Key].Iterator();
                                while (iter.HasNext())
                                {
                                    var message = iter.Next();
                                    Logger.DebugFormat(
                                        "Successfully sent messsage: {0}",
                                        message.Message.IsNull() ? null : Util.ReadString(message.Message.Payload));
                                }
                            }
                        }

                        var failedPartitionsAndStatus = response.Status.Where(s => s.Value.Error != ErrorMapping.NoError).ToList();
                        failedTopicPartitions =
                            failedPartitionsAndStatus.Select(partitionStatus => partitionStatus.Key).ToList();
                        if (failedTopicPartitions.Any())
                        {
                            var errorString = string.Join(
                                ",",
                                failedPartitionsAndStatus.OrderBy(x => x.Key.Topic)
                                .ThenBy(x => x.Key.Partiton)
                                .Select(
                                    kvp =>
                            {
                                var topicAndPartiton = kvp.Key;
                                var status           = kvp.Value;
                                return(topicAndPartiton.ToString() + ": "
                                       + ErrorMapping.ExceptionFor(status.Error)
                                       .GetType()
                                       .Name);
                            }));
                            Logger.WarnFormat("Produce request with correlation id {0} failed due to {1}", currentCorrelationId, errorString);
                        }

                        return(failedTopicPartitions);
                    }
                    else
                    {
                        return(new List <TopicAndPartition>());
                    }
                }
                catch (Exception e)
                {
                    Logger.Warn(
                        string.Format(
                            "Failed to send producer request with correlation id {0} to broker {1} with Data for partitions {2}",
                            currentCorrelationId,
                            brokerId,
                            string.Join(",", messagesPerTopic.Select(m => m.Key))),
                        e);
                    return(new List <TopicAndPartition>(messagesPerTopic.Keys));
                }
            }
            else
            {
                return(new List <TopicAndPartition>());
            }
        }
        public void GetBytesValidStructure()
        {
            string topicName     = "topic";
            int    correlationId = 1;
            string clientId      = "TestClient";
            short  requiredAcks  = 5;
            int    ackTimeout    = 345;

            var   partition = 2;
            short error     = 0;
            var   payload   = Encoding.UTF8.GetBytes("testMessage");
            BufferedMessageSet messageSet = new BufferedMessageSet(new List <Message>()
            {
                new Message(payload)
            }, 0);

            var partitionData = new PartitionData(partition, ErrorMapper.ToError(error), messageSet);

            var topicData = new TopicData(topicName, new List <PartitionData>()
            {
                partitionData
            });

            var request = new ProducerRequest(correlationId, clientId, requiredAcks, ackTimeout, new List <TopicData>()
            {
                topicData
            });

            int requestSize = 2 +                                            //request type id
                              2 +                                            //versionId
                              4 +                                            //correlation id
                              request.GetShortStringWriteLength(clientId) +  // actual client id
                              2 +                                            //required acks
                              4 +                                            //ack timeout
                              4 +                                            //data count
                                                                             //=== data part
                              request.GetShortStringWriteLength(topicName) + //topic
                              4 +                                            //partition data count
                              4 +                                            //partition id
                              4 +                                            //messages set size
                              messageSet.SetSize;

            var ms = new MemoryStream();

            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);

            // add 4 bytes for the length of the message at the beginning
            Assert.AreEqual(requestSize + 4, bytes.Length);

            // first 4 bytes = the message length
            Assert.AreEqual(requestSize, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the request type
            Assert.AreEqual((short)RequestTypes.Produce, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray <byte>()), 0));

            // next 2 bytes = the version id
            Assert.AreEqual((short)ProducerRequest.CurrentVersion, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray <byte>()), 0));

            // next 2 bytes = the correlation id
            Assert.AreEqual(correlationId, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(8).Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the client id length
            Assert.AreEqual((short)clientId.Length, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(12).Take(2).ToArray <byte>()), 0));

            // next few bytes = the client id
            Assert.AreEqual(clientId, Encoding.ASCII.GetString(bytes.Skip(14).Take(clientId.Length).ToArray <byte>()));

            // next 2 bytes = the required acks
            Assert.AreEqual((short)requiredAcks, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(14 + clientId.Length).Take(2).ToArray <byte>()), 0));

            // next 4 bytes = the ack timeout
            Assert.AreEqual(ackTimeout, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(16 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = the data count
            Assert.AreEqual(1, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(20 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the tppic length
            Assert.AreEqual((short)topicName.Length, BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(24 + clientId.Length).Take(2).ToArray <byte>()), 0));

            // next few bytes = the topic
            Assert.AreEqual(topicName, Encoding.ASCII.GetString(bytes.Skip(26 + clientId.Length).Take(topicName.Length).ToArray <byte>()));

            // next 4 bytes = the partition data count
            Assert.AreEqual(topicData.PartitionData.Count(), BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(26 + clientId.Length + topicName.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = the partition
            Assert.AreEqual(partition, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(30 + clientId.Length + topicName.Length).Take(4).ToArray <byte>()), 0));

            // skipping MessageSet check - this could be done separately in another unit test
        }
Exemple #36
0
        /// <summary>
        /// Writes a producer request to the server asynchronously.
        /// </summary>
        /// <param name="request">The request to make.</param>
        /// <param name="callback">The code to execute once the message is completely sent.</param>
        public void BeginWrite(ProducerRequest request, MessageSent<ProducerRequest> callback)
        {
            NetworkStream stream = _client.GetStream();
            RequestContext<ProducerRequest> ctx = new RequestContext<ProducerRequest>(stream, request);

            byte[] data = request.GetBytes();
            stream.BeginWrite(
                data, 
                0, 
                data.Length, 
                delegate(IAsyncResult asyncResult)
                {
                    RequestContext<ProducerRequest> context = (RequestContext<ProducerRequest>)asyncResult.AsyncState;

                    if (callback != null)
                    {
                        callback(context);
                    }

                    context.NetworkStream.EndWrite(asyncResult);
                    context.NetworkStream.Dispose();
                }, 
                ctx);
        }
Exemple #37
0
        public void ConsumerConnectorConsumesTwoDifferentCompressedTopics()
        {
            var prodConfig     = this.SyncProducerConfig1;
            var consumerConfig = this.ZooKeeperBasedConsumerConfig;

            string topic1 = CurrentTestTopic + "1";
            string topic2 = CurrentTestTopic + "2";

            // first producing
            string payload1 = "kafka 1.";

            byte[]  payloadData1       = Encoding.UTF8.GetBytes(payload1);
            var     msg1               = new Message(payloadData1);
            Message compressedMessage1 = CompressionUtils.Compress(new List <Message> {
                msg1
            }, CompressionCodecs.GZIPCompressionCodec);

            string payload2 = "kafka 2.";

            byte[]  payloadData2       = Encoding.UTF8.GetBytes(payload2);
            var     msg2               = new Message(payloadData2);
            Message compressedMessage2 = CompressionUtils.Compress(new List <Message> {
                msg2
            }, CompressionCodecs.GZIPCompressionCodec);

            var producerRequest1 = new ProducerRequest(topic1, 0, new List <Message> {
                compressedMessage1
            });
            var producerRequest2 = new ProducerRequest(topic2, 0, new List <Message> {
                compressedMessage2
            });

            using (var producer = new SyncProducer(prodConfig))
            {
                producer.Send(producerRequest1);
                producer.Send(producerRequest2);
            }

            // now consuming
            var resultMessages1 = new List <Message>();
            var resultMessages2 = new List <Message>();

            using (IConsumerConnector consumerConnector = new ZookeeperConsumerConnector(consumerConfig, true))
            {
                var topicCount = new Dictionary <string, int> {
                    { topic1, 1 }, { topic2, 1 }
                };
                var messages = consumerConnector.CreateMessageStreams(topicCount);

                Assert.IsTrue(messages.ContainsKey(topic1));
                Assert.IsTrue(messages.ContainsKey(topic2));

                var sets1 = messages[topic1];
                try
                {
                    foreach (var set in sets1)
                    {
                        foreach (var message in set)
                        {
                            resultMessages1.Add(message);
                        }
                    }
                }
                catch (ConsumerTimeoutException)
                {
                    // do nothing, this is expected
                }

                var sets2 = messages[topic2];
                try
                {
                    foreach (var set in sets2)
                    {
                        foreach (var message in set)
                        {
                            resultMessages2.Add(message);
                        }
                    }
                }
                catch (ConsumerTimeoutException)
                {
                    // do nothing, this is expected
                }
            }

            Assert.AreEqual(1, resultMessages1.Count);
            Assert.AreEqual(msg1.ToString(), resultMessages1[0].ToString());

            Assert.AreEqual(1, resultMessages2.Count);
            Assert.AreEqual(msg2.ToString(), resultMessages2[0].ToString());
        }
Exemple #38
0
        /// <summary>
        /// Send message of one broker.
        /// </summary>
        /// <param name="brokerId"></param>
        /// <param name="messagesPerTopic"></param>
        /// <returns></returns>
        private ProducerSendResult <IEnumerable <Tuple <TopicAndPartition, ProducerResponseStatus> > > Send(int brokerId, IDictionary <TopicAndPartition, BufferedMessageSet> messagesPerTopic)
        {
            try
            {
                if (brokerId < 0)
                {
                    throw new NoLeaderForPartitionException(
                              string.Format("No leader for some partition(s).  And it try write to on invalid broker {0}.  The assigned TopicAndPartition for the data is :{1} ", brokerId, messagesPerTopic.Any() ? messagesPerTopic.First().Key.ToString() : "(null)"));
                }
                if (messagesPerTopic.Any())
                {
                    var producerRequest = new ProducerRequest(NextCorrelationId,
                                                              this.producerConfig.ClientId,
                                                              this.producerConfig.RequiredAcks,
                                                              this.producerConfig.AckTimeout,
                                                              messagesPerTopic);
                    ISyncProducer syncProducer = null;
                    try
                    {
                        syncProducer = this.syncProducerPool.GetProducer(brokerId);
                    }
                    catch (UnavailableProducerException e)
                    {
                        Logger.Error(e.Message);
                        // When initializing producer pool, some broker might be unavailable, and now it is healthy and is leader for some partitions.
                        // A new producer should be added to the pool, creating a TCP connection to the broker.
                        var broker =
                            this.brokerPartitionInfo.GetBrokerPartitionLeaders(messagesPerTopic.Keys.First().Topic)
                            .Values.FirstOrDefault(b => b.Id == brokerId);
                        if (broker != null)
                        {
                            this.syncProducerPool.AddProducer(broker);
                            syncProducer = this.syncProducerPool.GetProducer(brokerId);
                        }
                    }

                    if (producerConfig.Verbose)
                    {
                        Logger.DebugFormat("Kafka producer before sent messages for topics {0} to broker {1}", messagesPerTopic, brokerId);
                    }
                    ProducerResponse response = syncProducer.Send(producerRequest);
                    if (this.producerConfig.Verbose)
                    {
                        string msg = string.Format("Kafka producer sent messages for topics {0} to broker {1} on {2}:{3}",
                                                   messagesPerTopic, brokerId, syncProducer.Config.Host, syncProducer.Config.Port);
                        Logger.Debug(msg);
                    }

                    if (response != null)
                    {
                        int statusCount = response.Statuses.Count();
                        //In java version
                        //https://git-wip-us.apache.org/repos/asf?p=kafka.git;a=blob;f=core/src/main/scala/kafka/producer/async/DefaultEventHandler.scala;h=821901e4f434dfd9eec6eceabfc2e1e65507a57c;hb=HEAD#l260
                        //The producerRequest.data just the messagesPerTopic.  So there compare the statusCount with producerRequest.data.size
                        //But in this C# version, the producerRequest.Data already grouped by topic.  So here need compare with messagesPerTopic.Count()
                        int requestCount = messagesPerTopic.Count();
                        if (statusCount != requestCount)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.AppendFormat("Incomplete response count {0} for producer request count {1}. ", statusCount, requestCount);
                            sb.AppendFormat(" Broker {0} on {1}:{2}", brokerId, syncProducer.Config.Host, syncProducer.Config.Port);
                            sb.Append(" Message detail:");
                            sb.Append(string.Join(",", messagesPerTopic.Select(r => string.Format("{0},{1}", r.Key.Topic, r.Key.PartitionId))));
                            sb.Append(" Response status detail which has error:");
                            sb.Append(string.Join(",", response.Statuses.Where(r => r.Value.Error != (short)ErrorMapping.NoError).Select(r => r.ToString())));
                            throw new FailedToSendMessageException <TK>(sb.ToString());
                        }
                        return(new ProducerSendResult <IEnumerable <Tuple <TopicAndPartition, ProducerResponseStatus> > >(response.Statuses.Where(s => s.Value.Error != (short)ErrorMapping.NoError)
                                                                                                                          .Select(s => new Tuple <TopicAndPartition, ProducerResponseStatus>(s.Key, s.Value))));
                    }
                }
            }
            catch (NoLeaderForPartitionException e)
            {
                Logger.Error(ExceptionUtil.GetExceptionDetailInfo(e));
                return(new ProducerSendResult <IEnumerable <Tuple <TopicAndPartition, ProducerResponseStatus> > >(messagesPerTopic.Keys.Select(
                                                                                                                      s => new Tuple <TopicAndPartition, ProducerResponseStatus>(s, new ProducerResponseStatus {
                    Error = ErrorMapping.NotLeaderForPartitionCode
                })), e));
            }
            catch (Exception e)
            {
                Logger.Error(ExceptionUtil.GetExceptionDetailInfo(e));
                return(new ProducerSendResult <IEnumerable <Tuple <TopicAndPartition, ProducerResponseStatus> > >(messagesPerTopic.Keys.Select(
                                                                                                                      s => new Tuple <TopicAndPartition, ProducerResponseStatus>(s, new ProducerResponseStatus {
                    Error = ErrorMapping.UnknownCode
                })), e));
            }

            return(new ProducerSendResult <IEnumerable <Tuple <TopicAndPartition, ProducerResponseStatus> > >(Enumerable.Empty <Tuple <TopicAndPartition, ProducerResponseStatus> >()));
        }
Exemple #39
0
 public void IsValidFalseNoMessages()
 {
     ProducerRequest request = new ProducerRequest("topic", 0, null);
     Assert.IsFalse(request.IsValid());
 }
Exemple #40
0
 public void IsValidFalseNoTopic()
 {
     ProducerRequest request = new ProducerRequest(null, 0, null);
     Assert.IsFalse(request.IsValid());
 }
Exemple #41
0
        /// <summary>
        /// Send a request to Kafka asynchronously.
        /// </summary>
        /// <remarks>
        /// If the callback is not specified then the method behaves as a fire-and-forget call
        /// with the callback being ignored.  By the time this callback is executed, the 
        /// <see cref="RequestContext.NetworkStream"/> will already have been closed given an 
        /// internal call <see cref="NetworkStream.EndWrite"/>.
        /// </remarks>
        /// <param name="request">The request to send to Kafka.</param>
        /// <param name="callback">
        /// A block of code to execute once the request has been sent to Kafka.  This value may 
        /// be set to null.
        /// </param>
        public void SendAsync(ProducerRequest request, MessageSent<ProducerRequest> callback)
        {
            if (request.IsValid())
            {
                KafkaConnection connection = new KafkaConnection(Server, Port);

                if (callback == null)
                {
                    // fire and forget
                    connection.BeginWrite(request.GetBytes());
                }
                else
                {
                    // execute with callback
                    connection.BeginWrite(request, callback);
                }
            }
        }
        /// <summary>
        /// Sends request to Kafka server synchronously
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        public ProducerResponse Send(ProducerRequest request)
        {
            this.EnsuresNotDisposed();

            foreach (var topicData in request.Data)
            {
                foreach (var partitionData in topicData.PartitionData)
                {
                    VerifyMessageSize(partitionData.MessageSet.Messages);
                }
            }

            return this.connection.Send(request);
        }
Exemple #43
0
 /// <summary>
 /// Writes a producer request to the server asynchronously.
 /// </summary>
 /// <param name="request">The request to make.</param>
 public void BeginWrite(ProducerRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.Assert<ArgumentNullException>(() => request != null);
     NetworkStream stream = client.GetStream();
     byte[] data = request.RequestBuffer.GetBuffer();
     stream.BeginWrite(data, 0, data.Length, asyncResult => ((NetworkStream)asyncResult.AsyncState).EndWrite(asyncResult), stream);
 }
Exemple #44
0
 public void IsValidTrue()
 {
     ProducerRequest request = new ProducerRequest(
         "topic", 0, new List<Message> { new Message(new byte[10]) });
     Assert.IsTrue(request.IsValid());
 }
Exemple #45
0
        public void IsValidFalseNoTopic()
        {
            ProducerRequest request = new ProducerRequest(null, 0, null);

            Assert.IsFalse(request.IsValid());
        }