internal static string BuildExceptionMessage(List <Exception> exceptions,
                                                     int retry,
                                                     int allCount,
                                                     int remainFailedCount,
                                                     ProduceDispatchSeralizeResult <K> outstandingProduceRequests)
        {
            var sb = new StringBuilder();

            sb.AppendFormat(
                "Failed to send messages after {0} tries. FailedProducerDatas not empty. Success Count:{1} Failed Count: {2}.",
                retry, allCount - remainFailedCount, remainFailedCount);

            if (exceptions != null && exceptions.Any())
            {
                var builder = new StringBuilder();
                var count   = 0;
                foreach (var e in exceptions)
                {
                    builder.AppendFormat("{0} th:", count);
                    builder.AppendFormat("{0} {1} {2} \r\n", e.Message, e.StackTrace, e.Source);
                    count++;
                }
                sb.AppendFormat("\r\n================Internal exceptions: {0}   {1} ", count, builder);
            }

            if (outstandingProduceRequests.FailedProducerDatas != null)
            {
                sb.Append("\r\n================Failed sent message key: ");
                sb.Append(string.Join(",", outstandingProduceRequests.FailedProducerDatas.Select(r => string.Format(
                                                                                                     "Topic: {0} Key: {1}"
                                                                                                     , r.Topic
                                                                                                     , r.Key))));
            }

            if (outstandingProduceRequests.FailedDetail != null)
            {
                sb.Append("\r\n================Failed Detail: ");
                sb.Append(string.Join(",",
                                      outstandingProduceRequests.FailedDetail.Select(r => string.Format("Broker:{0},{1},{2} \t", r.Item1,
                                                                                                        r.Item2, r.Item3))));
            }

            if (outstandingProduceRequests.Exceptions != null && outstandingProduceRequests.Exceptions.Any())
            {
                var builder = new StringBuilder();
                var count   = 0;
                foreach (var e in outstandingProduceRequests.Exceptions)
                {
                    builder.AppendFormat("{0} th:", count);
                    builder.AppendFormat("{0} {1} {2} \r\n", e.Message, e.StackTrace, e.Source);
                    count++;
                }
                sb.AppendFormat("\r\n================ProduceDispatchSeralizeResult Internal exceptions: {0}   {1} ",
                                count, builder);
            }

            return(sb.ToString());
        }
 public FailedToSendMessageException(string s,
                                     List <Exception> exceptions,
                                     ProduceDispatchSeralizeResult <K> outstandingProduceRequests,
                                     int all,
                                     int failed)
     : base(s)
 {
     ProduceDispatchSeralizeResult = outstandingProduceRequests;
     LastProduceExceptions         = exceptions;
     CountAll    = all;
     CountFailed = failed;
 }
Exemplo n.º 3
0
        public void Handle(IEnumerable <ProducerData <TK, TV> > events)
        {
            IEnumerable <ProducerData <TK, Message> > serializedData             = this.Serialize(events);
            ProduceDispatchSeralizeResult <TK>        outstandingProduceRequests = new ProduceDispatchSeralizeResult <TK>(new List <Exception> {
            }, serializedData, null, true);
            var remainingRetries = this.producerConfig.ProducerRetries;
            int currentRetryMs   = producerConfig.ProducerRetryExponentialBackoffMinMs;

            var brokers = this.producerConfig.Brokers;

            if (producerConfig.Verbose)
            {
                Logger.DebugFormat("Handle,producerConfig.Brokers.Count={0},broker[0]={1}", brokers.Count, brokers.Any() ? brokers[0].ToString() : "NO broker");
            }

            while (remainingRetries > 0 && outstandingProduceRequests.HasDataNeedDispatch)
            {
                try
                {
                    ProduceDispatchSeralizeResult <TK> currentOutstandingRequests =
                        this.DispatchSerializedData(outstandingProduceRequests.FailedProducerDatas, remainingRetries > 1 ? false : true);
                    outstandingProduceRequests = currentOutstandingRequests;
                    if (outstandingProduceRequests.HasDataNeedDispatch)
                    {
                        currentRetryMs = ExponentialRetry(currentRetryMs);
                        remainingRetries--;
                    }
                    else
                    {
                        currentRetryMs = producerConfig.ProducerRetryExponentialBackoffMinMs;
                        break;
                    }
                }
                catch (Exception e)
                {
                    remainingRetries--;
                    if (remainingRetries > 0)
                    {
                        continue;
                    }
                    int    allCount          = events.Count();
                    int    remainFailedCount = outstandingProduceRequests.FailedProducerDatas.ToList().Count();
                    string message           = FailedToSendMessageException <TK> .BuildExceptionMessage(new List <Exception>() { e }, producerConfig.ProducerRetries, allCount, remainFailedCount, outstandingProduceRequests);

                    Logger.Error(message);
                    throw new FailedToSendMessageException <TK>(message, new List <Exception>()
                    {
                        e
                    }, outstandingProduceRequests, allCount, remainFailedCount);
                }
            }

            if (outstandingProduceRequests.HasDataNeedDispatch)
            {
                int    allCount          = events.Count();
                int    remainFailedCount = outstandingProduceRequests.FailedProducerDatas.ToList().Count();
                string message           = FailedToSendMessageException <TK> .BuildExceptionMessage(new List <Exception>(), producerConfig.ProducerRetries, allCount, remainFailedCount, outstandingProduceRequests);

                Logger.Error(message);
                throw new FailedToSendMessageException <TK>(message, new List <Exception>(), outstandingProduceRequests, allCount, remainFailedCount);
            }
        }