public void ReportAndFixIfNeeded(ILog log)
        {
            var state = ThreadPoolUtility.GetPoolState();

            if (state.UsedWorkerThreads < state.MinWorkerThreads &&
                state.UsedIocpThreads < state.MinIocpThreads)
            {
                return;
            }

            var currentTimestamp = DateTime.UtcNow;

            lock (syncObject)
            {
                if (currentTimestamp - lastReportTimestamp < minReportInterval)
                {
                    return;
                }

                lastReportTimestamp = currentTimestamp;
            }

            log.Warn(
                "Looks like you're kinda low on ThreadPool, buddy. " +
                $"Workers: {state.UsedWorkerThreads}/{state.MinWorkerThreads}/{state.MaxWorkerThreads}, " +
                $"IOCP: {state.UsedIocpThreads}/{state.MinIocpThreads}/{state.MaxIocpThreads} (busy/min/max).");

            var currentMultiplier = Math.Min(state.MinWorkerThreads / Environment.ProcessorCount, state.MinIocpThreads / Environment.ProcessorCount);

            if (currentMultiplier < 128)
            {
                log.Info("I will configure ThreadPool for you, buddy!");
                ThreadPoolUtility.Setup(log);
            }
        }
Beispiel #2
0
        private async Task <VostokApplicationRunResult> RunInternalAsync()
        {
            if (settings.ConfigureThreadPool)
            {
                ThreadPoolUtility.Setup(settings.ThreadPoolTuningMultiplier);
            }

            var result = BuildEnvironment();

            if (result != null)
            {
                return(result);
            }

            using (environment)
                using (new ApplicationDisposable(settings.Application, environment, log))
                {
                    result = WarmupEnvironment();

                    if (result != null)
                    {
                        return(result);
                    }

                    result = await InitializeApplicationAsync().ConfigureAwait(false);

                    if (result.State == VostokApplicationState.Initialized)
                    {
                        result = await RunApplicationAsync().ConfigureAwait(false);
                    }

                    return(result);
                }
        }
Beispiel #3
0
        protected TransportTestsBase()
        {
            ILog log = new ConsoleLog();

            Transport = ClusterClientConfigurationHelper.CreateTransport(log);

            ThreadPoolUtility.Setup(log);
        }
Beispiel #4
0
        private void ConfigureHostBeforeRun()
        {
            var cpuUnitsLimit = environment.ApplicationLimits.CpuUnits;

            if (settings.ConfigureThreadPool && cpuUnitsLimit.HasValue)
            {
                ThreadPoolUtility.Setup(settings.ThreadPoolTuningMultiplier, cpuUnitsLimit.Value);
            }
        }
Beispiel #5
0
        private Task <VostokMultiHostRunResult> StartInternalAsync()
        {
            // NOTE: Configure thread pool once there if necessary.
            if (settings.ConfigureThreadPool)
            {
                ThreadPoolUtility.Setup(settings.ThreadPoolTuningMultiplier);
            }

            BuildCommonEnvironment()?.EnsureSuccess();

            StartAddedApplications();

            return(Task.FromResult(ReturnResult(VostokMultiHostState.Running)));
        }
        public static void ReportAndFixIfNeeded(ILog log)
        {
            var state = ThreadPoolUtility.GetPoolState();

            if (state.UsedWorkerThreads < state.MinWorkerThreads &&
                state.UsedIocpThreads < state.MinIocpThreads)
            {
                return;
            }

            var currentTimestamp = DateTime.UtcNow;

            lock (syncObject)
            {
                if (currentTimestamp - lastReportTimestamp < MinReportInterval)
                {
                    return;
                }

                lastReportTimestamp = currentTimestamp;
            }

            log = log.ForContext(typeof(ThreadPoolMonitor));

            log.Warn(
                "Looks like you're kinda low on ThreadPool, buddy. " +
                "Workers: {UsedWorkerThreads}/{MinWorkerThreads}, " +
                "IOCP: {UsedIocpThreads}/{MinIocpThreads} (busy/min).",
                state.UsedWorkerThreads,
                state.MinWorkerThreads,
                state.UsedIocpThreads,
                state.MinIocpThreads);

            var currentMultiplier = Math.Min(
                state.MinWorkerThreads / Environment.ProcessorCount,
                state.MinIocpThreads / Environment.ProcessorCount);

            if (currentMultiplier < TargetMultiplier)
            {
                log.Info("I will configure ThreadPool for you, buddy!");

                ThreadPoolUtility.Setup(TargetMultiplier);

                var newState = ThreadPoolUtility.GetPoolState();

                log.Info("New min worker threads = {MinWorkerThreads}", newState.MinWorkerThreads);
                log.Info("New min IOCP threads = {MinIocpThreads}", newState.MinIocpThreads);
            }
        }
Beispiel #7
0
        private void CheckAndUpdate()
        {
            var newThreadPoolMultiplier = settingsProvider(configProvider).ThreadPoolMultiplier;
            var newCpuUnits             = applicationLimits.CpuUnits;

            if (WereSettingsUpdated(newThreadPoolMultiplier, newCpuUnits))
            {
                ThreadPoolUtility.Setup(newThreadPoolMultiplier, newCpuUnits);

                previousThreadPoolMultiplier = newThreadPoolMultiplier;
                previousCpuUnits             = newCpuUnits;

                LogThreadPoolSettings(newThreadPoolMultiplier, newCpuUnits);
            }
        }
Beispiel #8
0
        /// <summary>
        /// <para>Launches the provided <see cref="IVostokApplication"/>.</para>
        /// <para>Performs following operations:</para>
        /// <list type="bullet">
        ///     <item><description>Creates an instance of <see cref="IVostokHostingEnvironment"/> using <see cref="VostokHostSettings.EnvironmentSetup"/>.</description></item>
        ///     <item><description>Configures thread pool if <see cref="VostokHostSettings.ConfigureThreadPool"/> specified.</description></item>
        ///     <item><description>Configures static providers if <see cref="VostokHostSettings.ConfigureStaticProviders"/> specified.</description></item>
        ///     <item><description>Calls <see cref="IVostokApplication.InitializeAsync"/>.</description></item>
        ///     <item><description>Calls <see cref="IVostokApplication.RunAsync"/>.</description></item>
        ///     <item><description>Stops application if <see cref="ShutdownTokenSource"/> has been canceled.</description></item>
        /// </list>
        /// <para>May throw an exception if an error occurs during environment creation.</para>
        /// <para>Does not rethrow exceptions from <see cref="IVostokApplication"/>, stores them in result's <see cref="VostokApplicationRunResult.Error"/> property.</para>
        /// </summary>
        public virtual async Task <VostokApplicationRunResult> RunAsync()
        {
            if (!launchedOnce.TrySetTrue())
            {
                throw new InvalidOperationException("Application can't be launched multiple times.");
            }

            if (settings.ConfigureThreadPool)
            {
                ThreadPoolUtility.Setup(settings.ThreadPoolTuningMultiplier);
            }

            var result = BuildEnvironment();

            if (result != null)
            {
                return(result);
            }

            using (environment)
                using (settings.Application as IDisposable)
                {
                    result = WarmupEnvironment();

                    if (result != null)
                    {
                        return(result);
                    }

                    result = await InitializeApplicationAsync().ConfigureAwait(false);

                    if (result.State == VostokApplicationState.Initialized)
                    {
                        result = await RunApplicationAsync().ConfigureAwait(false);
                    }

                    return(result);
                }
        }
Beispiel #9
0
 public void OneTimeSetUp()
 {
     ThreadPoolUtility.Setup();
     LogProvider.Configure(new SynchronousConsoleLog());
 }
 public void OneTimeSetUp()
 {
     ThreadPoolUtility.Setup();
 }