internal static void UpdateSubmissionExceptionStatisticRecords(MessageStatus messageStatus, int lastOccurrencesPerException, int callstacksPerBucket, MailItemSubmitter mailItemSubmitter)
        {
            MapiSubmissionInfo mapiSubmissionInfo = mailItemSubmitter.SubmissionInfo as MapiSubmissionInfo;

            StoreDriverSubmission.SubmissionOccurrenceRecord submissionOccurrenceRecord = new StoreDriverSubmission.SubmissionOccurrenceRecord(DateTime.UtcNow, mailItemSubmitter.StartTime, mailItemSubmitter.SubmissionInfo.MdbGuid, (mapiSubmissionInfo == null) ? Guid.Empty : mapiSubmissionInfo.MailboxGuid, (mapiSubmissionInfo == null) ? null : mapiSubmissionInfo.EntryId, (mapiSubmissionInfo == null) ? null : mapiSubmissionInfo.ParentEntryId, (mapiSubmissionInfo == null) ? 0L : mapiSubmissionInfo.EventCounter, mailItemSubmitter.OrganizationId, mailItemSubmitter.ErrorCode, mailItemSubmitter.SubmissionInfo.MailboxFqdn, mailItemSubmitter.Item.HasMessageItem ? mailItemSubmitter.Item.Item.InternetMessageId : null, mailItemSubmitter.SubmissionConnectionId, mailItemSubmitter.MessageSize, mailItemSubmitter.RecipientCount, mailItemSubmitter.Result.Sender, mailItemSubmitter.Stage);
            if (lastOccurrencesPerException > 0)
            {
                string key = StoreDriverSubmission.GenerateExceptionKey(messageStatus);
                lock (StoreDriverSubmission.submissionExceptionStatisticRecords)
                {
                    StoreDriverSubmission.ExceptionStatisticRecord <StoreDriverSubmission.SubmissionOccurrenceRecord> value;
                    if (!StoreDriverSubmission.submissionExceptionStatisticRecords.TryGetValue(key, out value))
                    {
                        value = default(StoreDriverSubmission.ExceptionStatisticRecord <StoreDriverSubmission.SubmissionOccurrenceRecord>);
                        value.LastOccurrences = new Queue <StoreDriverSubmission.SubmissionOccurrenceRecord>(lastOccurrencesPerException);
                    }
                    if (value.LastOccurrences.Count == lastOccurrencesPerException)
                    {
                        value.LastOccurrences.Dequeue();
                    }
                    value.LastOccurrences.Enqueue(submissionOccurrenceRecord);
                    value.CountSinceServiceStart++;
                    StoreDriverSubmission.submissionExceptionStatisticRecords[key] = value;
                }
            }
            StoreDriverSubmission.UpdateSubmissionExceptionCallstackRecords(messageStatus, callstacksPerBucket, submissionOccurrenceRecord);
        }
 public MapiSubmissionItem(MapiSubmissionInfo mapiSubmissionInfo, MailItemSubmitter context, IStoreDriverTracer storeDriverTracer) : base("mapi", context, mapiSubmissionInfo, storeDriverTracer)
 {
     if (mapiSubmissionInfo == null)
     {
         throw new ArgumentNullException("mapiSubmissionInfo");
     }
     this.mapiSubmissionInfo = mapiSubmissionInfo;
 }
        public XElement GetDiagnosticInfo()
        {
            XElement root = new XElement("CurrentSubmissions");

            this.map.ForEach(null, delegate(Thread thread, MailItemSubmitter mailItemSubmitter)
            {
                SubmissionInfo submissionInfo = mailItemSubmitter.SubmissionInfo;
                string mailboxHopLatency      = submissionInfo.MailboxHopLatency;
                Guid mdbGuid                = submissionInfo.MdbGuid;
                IPAddress networkAddress    = submissionInfo.NetworkAddress;
                DateTime originalCreateTime = submissionInfo.OriginalCreateTime;
                string content              = null;
                string content2             = null;
                long num     = 0L;
                byte[] array = null;
                MapiSubmissionInfo mapiSubmissionInfo = (MapiSubmissionInfo)mailItemSubmitter.SubmissionInfo;
                num      = mapiSubmissionInfo.EventCounter;
                array    = mapiSubmissionInfo.EntryId;
                content  = mailItemSubmitter.Result.MessageId;
                content2 = mailItemSubmitter.Result.Sender;
                thread.Suspend();
                StackTrace content3;
                try
                {
                    content3 = new StackTrace(thread, true);
                }
                finally
                {
                    thread.Resume();
                }
                XElement xelement = new XElement("Submission");
                xelement.Add(new object[]
                {
                    new XElement("ThreadID", thread.ManagedThreadId),
                    new XElement("ConnectionID", mailItemSubmitter.SubmissionConnectionId),
                    new XElement("Duration", (default(DateTime) == mailItemSubmitter.StartTime) ? TimeSpan.Zero : (DateTime.UtcNow - mailItemSubmitter.StartTime)),
                    new XElement("MailboxServer", submissionInfo.MailboxFqdn),
                    new XElement("MailboxServerIP", submissionInfo.NetworkAddress),
                    new XElement("MdbName", submissionInfo.DatabaseName),
                    new XElement("MdbGuid", submissionInfo.MdbGuid),
                    new XElement("OriginalCreationTime", submissionInfo.OriginalCreateTime),
                    new XElement("MessageID", content),
                    new XElement("Sender", content2),
                    new XElement("EventCounter", num),
                    new XElement("EntryID", (array == null) ? null : BitConverter.ToString(array)),
                    new XElement("Stage", mailItemSubmitter.Stage),
                    new XElement("ErrorCode", mailItemSubmitter.ErrorCode),
                    new XElement("MessageSize", mailItemSubmitter.MessageSize),
                    new XElement("RecipientCount", mailItemSubmitter.RecipientCount),
                    new XElement("RpcLatency", mailItemSubmitter.RpcLatency),
                    new XElement("StackTrace", content3),
                    LatencyFormatter.GetDiagnosticInfo(mailItemSubmitter.LatencyTracker)
                });
                root.Add(xelement);
            });
            return(root);
        }
 private string GetSubmissionContext(MapiSubmissionInfo submissionInfo)
 {
     return(string.Format(CultureInfo.InvariantCulture, "MDB:{0}, Mailbox:{1}, Event:{2}, CreationTime:{3}", new object[]
     {
         submissionInfo.MdbGuid,
         submissionInfo.MailboxGuid,
         submissionInfo.EventCounter,
         submissionInfo.OriginalCreateTime.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffZ", DateTimeFormatInfo.InvariantInfo)
     }));
 }
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing)
         {
             base.DisposeMessageItem();
             if (base.Session != null)
             {
                 if (base.Context != null)
                 {
                     LatencyTracker.BeginTrackLatency(LatencyComponent.MailboxTransportSubmissionStoreDriverSubmissionStoreDisposeSession, base.Context.LatencyTracker);
                 }
                 try
                 {
                     base.DisposeStoreSession();
                 }
                 finally
                 {
                     if (base.Context != null)
                     {
                         TimeSpan additionalLatency = LatencyTracker.EndTrackLatency(LatencyComponent.MailboxTransportSubmissionStoreDriverSubmissionStoreDisposeSession, base.Context.LatencyTracker);
                         base.Context.AddRpcLatency(additionalLatency, "Session dispose");
                     }
                 }
             }
             if (base.Context != null)
             {
                 MapiSubmissionInfo mapiSubmissionInfo = (MapiSubmissionInfo)base.Info;
                 base.StoreDriverTracer.MapiStoreDriverSubmissionTracer.TracePass(base.StoreDriverTracer.MessageProbeActivityId, 0L, "Event {0}, Mailbox {1}, Mdb {2}, RPC latency {4}", new object[]
                 {
                     mapiSubmissionInfo.EventCounter,
                     mapiSubmissionInfo.MailboxGuid,
                     mapiSubmissionInfo.MdbGuid,
                     base.Context.RpcLatency
                 });
             }
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
        public MailSubmissionResult SubmitMessage(string serverDN, Guid mailboxGuid, Guid mdbGuid, string databaseName, long eventCounter, byte[] entryId, byte[] parentEntryId, string serverFqdn, IPAddress networkAddressBytes, DateTime originalCreateTime, bool isPublicFolder, TenantPartitionHint tenantHint, string mailboxHopLatency, QuarantineHandler quarantineHandler, SubmissionPoisonHandler submissionPoisonHandler, LatencyTracker latencyTracker)
        {
            ArgumentValidator.ThrowIfNullOrEmpty("serverDN", serverDN);
            ArgumentValidator.ThrowIfEmpty("mailboxGuid", mailboxGuid);
            ArgumentValidator.ThrowIfEmpty("mdbGuid", mdbGuid);
            ArgumentValidator.ThrowIfNullOrEmpty("databaseName", databaseName);
            ArgumentValidator.ThrowIfNull("entryId", entryId);
            ArgumentValidator.ThrowIfInvalidValue <int>("entryId", entryId.Length, (int value) => value > 0);
            ArgumentValidator.ThrowIfNull("parentEntryId", parentEntryId);
            ArgumentValidator.ThrowIfInvalidValue <int>("parentEntryId", parentEntryId.Length, (int value) => value > 0);
            ArgumentValidator.ThrowIfNullOrEmpty("serverFqdn", serverFqdn);
            ArgumentValidator.ThrowIfNull("networkAddressBytes", networkAddressBytes);
            ArgumentValidator.ThrowIfNullOrEmpty("mailboxHopLatency", mailboxHopLatency);
            ArgumentValidator.ThrowIfNull("quarantineHandler", quarantineHandler);
            ArgumentValidator.ThrowIfNull("submissionPoisonHandler", submissionPoisonHandler);
            bool shouldDeprioritize = false;

            if (SubmissionConfiguration.Instance.App.SenderRateDeprioritizationEnabled)
            {
                long num = this.rateTrackerForDeprioritization.IncrementSenderRate(mailboxGuid, originalCreateTime);
                if (num > (long)SubmissionConfiguration.Instance.App.SenderRateDeprioritizationThreshold)
                {
                    shouldDeprioritize = true;
                }
            }
            bool shouldThrottle = false;

            if (SubmissionConfiguration.Instance.App.SenderRateThrottlingEnabled)
            {
                long num2 = this.rateTrackerForThrottling.IncrementSenderRate(mailboxGuid, originalCreateTime);
                if (num2 > (long)SubmissionConfiguration.Instance.App.SenderRateThrottlingThreshold)
                {
                    shouldThrottle = true;
                    this.rateTrackerForThrottling.ResetSenderRate(mailboxGuid, originalCreateTime);
                }
            }
            MapiSubmissionInfo submissionInfo = new MapiSubmissionInfo(serverDN, mailboxGuid, entryId, parentEntryId, eventCounter, serverFqdn, networkAddressBytes, mdbGuid, databaseName, originalCreateTime, isPublicFolder, tenantHint, mailboxHopLatency, latencyTracker, shouldDeprioritize, shouldThrottle, this.storeDriverTracer);

            return(this.SubmitMessageImpl(submissionInfo, submissionPoisonHandler, quarantineHandler));
        }
        private MailSubmissionResult SubmitMessageImpl(MapiSubmissionInfo submissionInfo, SubmissionPoisonHandler submissionPoisonHandler, QuarantineHandler quarantineHandler)
        {
            MailSubmissionResult mailSubmissionResult = new MailSubmissionResult();

            if (this.Retired)
            {
                mailSubmissionResult.ErrorCode = 2214592514U;
                return(mailSubmissionResult);
            }
            string mailboxFqdn = submissionInfo.MailboxFqdn;
            string database    = submissionInfo.MdbGuid.ToString();
            MailSubmissionResult result;

            using (submissionInfo.GetTraceFilter())
            {
                using (SubmissionConnectionWrapper connection = SubmissionConnectionPool.GetConnection(mailboxFqdn, database))
                {
                    using (SubmissionThreadLimiter submissionThreadLimiter = new SubmissionThreadLimiter())
                    {
                        SubmissionPoisonContext submissionPoisonContext = null;
                        try
                        {
                            submissionThreadLimiter.BeginSubmission(connection.Id, mailboxFqdn, database);
                            if (this.Retired)
                            {
                                mailSubmissionResult.ErrorCode = 2214592514U;
                                connection.SubmissionAborted("Retiring.");
                                result = mailSubmissionResult;
                            }
                            else
                            {
                                this.storeDriverTracer.StoreDriverSubmissionTracer.TracePfdPass <int, MapiSubmissionInfo>(this.storeDriverTracer.MessageProbeActivityId, 0L, "PFD ESD {0} Processing SubmitMessage for {1}", 27547, submissionInfo);
                                submissionPoisonContext = submissionInfo.GetPoisonContext();
                                MailItemSubmitter mailItemSubmitter2;
                                MailItemSubmitter mailItemSubmitter = mailItemSubmitter2 = new MailItemSubmitter(connection.Id, submissionInfo, this.sendAsManager, submissionPoisonHandler, submissionPoisonContext, this);
                                try
                                {
                                    QuarantineInfoContext quarantineInfoContext;
                                    TimeSpan timeSpan;
                                    if (SubmissionConfiguration.Instance.App.EnableMailboxQuarantine && quarantineHandler.IsResourceQuarantined(submissionPoisonContext.ResourceGuid, out quarantineInfoContext, out timeSpan))
                                    {
                                        mailSubmissionResult.ErrorCode          = 1140850696U;
                                        mailSubmissionResult.QuarantineTimeSpan = timeSpan;
                                        mailSubmissionResult.DiagnosticInfo     = string.Format("{0}:{1}, QuarantineRemainingTimeSpan:{2}", "QuarantineStart", quarantineInfoContext.QuarantineStartTime, timeSpan);
                                        connection.SubmissionFailed(string.Format("Resource {0} is in quarantined state", submissionPoisonContext.ResourceGuid));
                                        return(mailSubmissionResult);
                                    }
                                    if (this.PoisonMessageDectionEnabled && submissionPoisonHandler.VerifyPoisonMessage(submissionPoisonContext))
                                    {
                                        this.LogPoisonMessageMTL(submissionInfo, submissionPoisonContext);
                                        if (SubmissionConfiguration.Instance.App.EnableSendNdrForPoisonMessage)
                                        {
                                            if (!submissionPoisonHandler.VerifyPoisonNdrSent(submissionPoisonContext))
                                            {
                                                mailItemSubmitter.HandlePoisonMessageNdrSubmission();
                                                mailSubmissionResult.ErrorCode = StoreDriverSubmissionUtils.MapSubmissionStatusErrorCodeToPoisonErrorCode(mailItemSubmitter.Result.ErrorCode);
                                            }
                                        }
                                        else
                                        {
                                            mailSubmissionResult.ErrorCode = 3U;
                                        }
                                        connection.SubmissionAborted(string.Format("Poison Context Info: Resource = {0};EventCounter = {1}.", submissionPoisonContext.ResourceGuid, submissionPoisonContext.MapiEventCounter));
                                        return(mailSubmissionResult);
                                    }
                                    Thread currentThread = Thread.CurrentThread;
                                    try
                                    {
                                        this.SubmissionsInProgress[currentThread] = mailItemSubmitter;
                                        mailItemSubmitter.Submit();
                                        mailSubmissionResult.RemoteHostName = mailItemSubmitter.Result.RemoteHostName;
                                    }
                                    finally
                                    {
                                        this.SubmissionsInProgress.Remove(currentThread);
                                    }
                                }
                                finally
                                {
                                    if (mailItemSubmitter2 != null)
                                    {
                                        ((IDisposable)mailItemSubmitter2).Dispose();
                                    }
                                }
                                if (mailItemSubmitter.Result.ErrorCode == 0U)
                                {
                                    connection.SubmissionSuccessful(mailItemSubmitter.MessageSize, mailItemSubmitter.RecipientCount);
                                }
                                else
                                {
                                    StringBuilder stringBuilder = new StringBuilder();
                                    stringBuilder.Append("HResult: ");
                                    stringBuilder.Append(mailItemSubmitter.Result.ErrorCode.ToString());
                                    stringBuilder.Append("; DiagnosticInfo: ");
                                    stringBuilder.Append(mailItemSubmitter.Result.DiagnosticInfo);
                                    connection.SubmissionFailed(stringBuilder.ToString());
                                }
                                if (mailItemSubmitter.Result.ErrorCode == 3U)
                                {
                                    submissionInfo.LogEvent(SubmissionInfo.Event.StoreDriverSubmissionPoisonMessageInSubmission);
                                }
                                result = mailItemSubmitter.Result;
                            }
                        }
                        catch (ThreadLimitExceededException ex)
                        {
                            connection.SubmissionAborted(ex.Message);
                            mailSubmissionResult.ErrorCode      = 1090519042U;
                            mailSubmissionResult.DiagnosticInfo = ex.Message;
                            result = mailSubmissionResult;
                        }
                        catch (Exception exception)
                        {
                            try
                            {
                                submissionPoisonHandler.SavePoisonContext(submissionPoisonContext);
                                string exceptionDiagnosticInfo = StorageExceptionHandler.GetExceptionDiagnosticInfo(exception);
                                connection.SubmissionFailed("Exception: " + exceptionDiagnosticInfo);
                                submissionInfo.LogEvent(SubmissionInfo.Event.StoreDriverSubmissionPoisonMessage, exception);
                                FailFast.Fail(exception);
                            }
                            catch (Exception ex2)
                            {
                                StoreDriverSubmission.LogEvent(MSExchangeStoreDriverSubmissionEventLogConstants.Tuple_StoreDriverSubmissionFailFastFailure, null, new object[]
                                {
                                    ex2
                                });
                            }
                            result = mailSubmissionResult;
                        }
                    }
                }
            }
            return(result);
        }