public void TestDecodeThrottlingReasonCode() { SqlError err = FakeSqlExceptionGenerator.GenerateFakeSqlError(ThrottlingCondition.ThrottlingErrorNumber, Resources.SampleThrottlingErrorMsg); ThrottlingCondition condition = ThrottlingCondition.FromError(err); Assert.AreEqual(ThrottlingMode.RejectAll, condition.ThrottlingMode, "Unexpected throttling mode."); ThrottlingType throttlingType = condition.ThrottledResources.Where(x => x.Item1 == ThrottledResourceType.Cpu).Select(x => x.Item2).FirstOrDefault(); Assert.AreEqual(ThrottlingType.Hard, throttlingType, "Unexpected throttling type."); }
public void UnregisterAsyncTask(string uniqueUserIdentity, ThrottlingType throttlingType) { lock (this.dictionarySynchronizationObject) { if ((throttlingType & ThrottlingType.PerServer) != (ThrottlingType)0) { this._currentConcurrentTaskCount--; } Dictionary <string, int> perUserBudget; if ((throttlingType & ThrottlingType.PerUser) != (ThrottlingType)0 && ((perUserBudget = this._perUserBudget)[uniqueUserIdentity] = perUserBudget[uniqueUserIdentity] - 1) == 0) { this._perUserBudget.Remove(uniqueUserIdentity); } } }
public AsyncServiceManager.AsyncTaskThrottlingStatus RegisterAsyncTask(string uniqueUserIdentity, string commandStringForTrace, ThrottlingType throttlingType) { AsyncServiceManager.AsyncTaskThrottlingStatus asyncTaskThrottlingStatus = AsyncServiceManager.AsyncTaskThrottlingStatus.None; lock (this.dictionarySynchronizationObject) { if ((throttlingType & ThrottlingType.PerUser) != (ThrottlingType)0) { int num = 0; if (this._perUserBudget.TryGetValue(uniqueUserIdentity, out num) && num >= this._perUserMaxConcurrentAsyncTaskCount) { asyncTaskThrottlingStatus = AsyncServiceManager.AsyncTaskThrottlingStatus.PerUserThrottlingHit; } this._perUserBudget[uniqueUserIdentity] = num + 1; } if ((throttlingType & ThrottlingType.PerServer) != (ThrottlingType)0) { if (asyncTaskThrottlingStatus == AsyncServiceManager.AsyncTaskThrottlingStatus.None && this._currentConcurrentTaskCount >= this._perServerMaxConcurrentAsyncTaskCount) { asyncTaskThrottlingStatus = AsyncServiceManager.AsyncTaskThrottlingStatus.PerAppThrottlingHit; } this._currentConcurrentTaskCount++; } } if (asyncTaskThrottlingStatus == AsyncServiceManager.AsyncTaskThrottlingStatus.PerAppThrottlingHit) { EcpEventLogConstants.Tuple_TooManyAsyncTaskInServer.LogEvent(new object[] { this._currentConcurrentTaskCount, this._perServerMaxConcurrentAsyncTaskCount, commandStringForTrace }); } else if (asyncTaskThrottlingStatus == AsyncServiceManager.AsyncTaskThrottlingStatus.PerUserThrottlingHit) { EcpEventLogConstants.Tuple_TooManyAsyncTaskFromCurrentUser.LogEvent(new object[] { this._perUserBudget[uniqueUserIdentity], this._perUserMaxConcurrentAsyncTaskCount, commandStringForTrace }); } return(asyncTaskThrottlingStatus); }
private static PowerShellResults InvokeAsyncCore(Func <PowerShellResults> callback, Action <PowerShellResults> onCompleted, string uniqueUserIdentity, AsyncServiceManager.AsyncTaskBudgetManager asyncTaskBudget, string commandStringForTrace, ThrottlingType throttlingType) { if (string.IsNullOrEmpty(uniqueUserIdentity)) { throw new ArgumentNullException("uniqueUserIdentity cannot be null."); } AsyncServiceManager.AsyncTaskThrottlingStatus asyncTaskThrottlingStatus = asyncTaskBudget.RegisterAsyncTask(uniqueUserIdentity, commandStringForTrace, throttlingType); if (asyncTaskThrottlingStatus != AsyncServiceManager.AsyncTaskThrottlingStatus.None) { LocalizedString value = (asyncTaskThrottlingStatus == AsyncServiceManager.AsyncTaskThrottlingStatus.PerAppThrottlingHit) ? Strings.LongRunPerAppThrottlingHit : Strings.LongRunPerUserThrottlingHit; asyncTaskBudget.UnregisterAsyncTask(uniqueUserIdentity, throttlingType); return(new PowerShellResults { ErrorRecords = new ErrorRecord[] { new ErrorRecord(new InvalidOperationException(value)) } }); } AsyncServiceManager.WorkItem workItem = new AsyncServiceManager.WorkItem(Guid.NewGuid().ToString()); AsyncServiceManager.workItems[workItem.Id] = workItem; CultureInfo currentCulture = CultureInfo.CurrentCulture; IPrincipal currentPrincipal = Thread.CurrentPrincipal; OperationContext currentOperationContext = OperationContext.Current; HttpContext currentHttpContext = HttpContext.Current; commandStringForTrace = ((commandStringForTrace == null) ? string.Empty : commandStringForTrace); RbacPrincipal rbacSession = RbacPrincipal.GetCurrent(false); ThreadPool.QueueUserWorkItem(delegate(object state) { int managedThreadId = Thread.CurrentThread.ManagedThreadId; AsyncServiceManager.WorkItem workItem; AsyncServiceManager.workerThreads[managedThreadId] = workItem; CultureInfo currentCulture = CultureInfo.CurrentCulture; IPrincipal currentPrincipal = Thread.CurrentPrincipal; OperationContext value2 = OperationContext.Current; HttpContext value3 = HttpContext.Current; Thread.CurrentThread.CurrentCulture = (Thread.CurrentThread.CurrentUICulture = currentCulture); Thread.CurrentPrincipal = currentPrincipal; OperationContext.Current = currentOperationContext; HttpContext.Current = AsyncServiceManager.CloneHttpContextForLongRunningThread(currentHttpContext); ActivityContextManager.InitializeActivityContext(HttpContext.Current, ActivityContextLoggerId.LongRunning); PowerShellResults powerShellResults = null; try { EcpEventLogConstants.Tuple_AsyncWebRequestStarted.LogEvent(new object[] { uniqueUserIdentity, managedThreadId, commandStringForTrace }); powerShellResults = callback(); object obj = AsyncServiceManager.workerThreads[managedThreadId]; } catch (Exception exception) { powerShellResults = new PowerShellResults(); powerShellResults.ErrorRecords = new ErrorRecord[] { new ErrorRecord(exception) }; EcpEventLogConstants.Tuple_AsyncWebRequestFailed.LogEvent(new object[] { uniqueUserIdentity, managedThreadId, exception.GetTraceFormatter(), commandStringForTrace }); ErrorHandlingUtil.SendReportForCriticalException(currentHttpContext, exception); DDIHelper.Trace("Async work item {0}, Error: {1}", new object[] { workItem.Id, exception.GetTraceFormatter() }); } finally { AsyncServiceManager.workerThreads.Remove(managedThreadId); lock (workItem) { workItem.Results = powerShellResults; ProgressRecord progressRecord = workItem.LegacyProgressRecord ?? ((workItem.ProgressCalculator == null) ? new ProgressRecord() : workItem.ProgressCalculator.ProgressRecord); powerShellResults.ProgressRecord = progressRecord; progressRecord.HasCompleted = true; progressRecord.IsCancelled = workItem.Cancelled; workItem.FinishedEvent.Set(); } asyncTaskBudget.UnregisterAsyncTask(uniqueUserIdentity, throttlingType); if (onCompleted != null) { onCompleted(powerShellResults); } EcpEventLogConstants.Tuple_AsyncWebRequestEnded.LogEvent(new object[] { uniqueUserIdentity, managedThreadId, commandStringForTrace }); ActivityContextManager.CleanupActivityContext(HttpContext.Current); Thread.CurrentThread.CurrentCulture = (Thread.CurrentThread.CurrentUICulture = currentCulture); Thread.CurrentPrincipal = currentPrincipal; OperationContext.Current = value2; HttpContext.Current = value3; GC.KeepAlive(rbacSession); } }); return(new PowerShellResults { ProgressId = workItem.Id }); }