コード例 #1
0
        public HostingShutdown(
            ApplicationShutdown appShutdown,
            IServiceBeacon serviceBeacon,
            IServiceLocator serviceLocator,
            IVostokApplicationIdentity identity,
            IMetricContext instanceMetrics,
            ILog log,
            CancellationToken token,
            TimeSpan totalTimeout,
            TimeSpan beaconTimeout,
            bool beaconWaitEnabled,
            bool sendAnnotation)
        {
            this.appShutdown     = appShutdown;
            this.serviceBeacon   = serviceBeacon;
            this.serviceLocator  = serviceLocator;
            this.identity        = identity;
            this.instanceMetrics = instanceMetrics;
            this.log             = log.ForContext <HostingShutdown>();

            this.totalTimeout      = totalTimeout;
            this.beaconTimeout     = beaconTimeout;
            this.beaconWaitEnabled = beaconWaitEnabled;
            this.sendAnnotation    = sendAnnotation;

            hostShutdownBudget = TimeBudget.CreateNew(totalTimeout);
            tokenRegistration  = token.Register(OnHostShutdownTriggered);
        }
コード例 #2
0
 public ApplicationInfoProvider(
     IVostokApplicationIdentity identity,
     IVostokApplicationLimits limits,
     Func <IVostokApplicationReplicationInfo> replication)
 {
     this.identity    = identity;
     this.limits      = limits;
     this.replication = replication;
 }
コード例 #3
0
 public VostokApplicationMetrics(IMetricContext root, IVostokApplicationIdentity identity)
 {
     Root       = root;
     Project    = Root.WithTag(WellKnownApplicationIdentityProperties.Project, identity.Project);
     Subproject = identity.Subproject == null
         ? Project
         : Project.WithTag(WellKnownApplicationIdentityProperties.Subproject, identity.Subproject);
     Environment = Subproject.WithTag(WellKnownApplicationIdentityProperties.Environment, identity.Environment);
     Application = Environment.WithTag(WellKnownApplicationIdentityProperties.Application, identity.Application);
     Instance    = Application.WithTag(WellKnownApplicationIdentityProperties.Instance, identity.Instance);
 }
コード例 #4
0
ファイル: VostokHost.cs プロジェクト: ArXa1L/hosting
        private void LogApplicationIdentity(IVostokApplicationIdentity applicationIdentity)
        {
            var messageTemplate = applicationIdentity.Subproject == null
                ? "Application identity: project: '{Project}', environment: '{Environment}', application: '{Application}', instance: '{Instance}'."
                : "Application identity: project: '{Project}', subproject: '{Subproject}', environment: '{Environment}', application: '{Application}', instance: '{Instance}'.";

            var messageParameters = applicationIdentity.Subproject == null
                ? new object[] { applicationIdentity.Project, applicationIdentity.Environment, applicationIdentity.Application, applicationIdentity.Instance }
                : new object[] { applicationIdentity.Project, applicationIdentity.Subproject, applicationIdentity.Environment, applicationIdentity.Application, applicationIdentity.Instance };

            log.Info(messageTemplate, messageParameters);
        }
コード例 #5
0
ファイル: ILogExtensions.cs プロジェクト: vostok/hosting
 public static ILog WithApplicationIdentityProperties(this ILog log, IVostokApplicationIdentity applicationIdentity)
 {
     log = log.WithProperty(WellKnownApplicationIdentityProperties.Project, applicationIdentity.Project);
     if (applicationIdentity.Subproject != null)
     {
         log = log.WithProperty(WellKnownApplicationIdentityProperties.Subproject, applicationIdentity.Subproject);
     }
     log = log.WithProperty(WellKnownApplicationIdentityProperties.Environment, applicationIdentity.Environment);
     log = log.WithProperty(WellKnownApplicationIdentityProperties.Application, applicationIdentity.Application);
     log = log.WithProperty(WellKnownApplicationIdentityProperties.Instance, applicationIdentity.Instance);
     return(log);
 }
コード例 #6
0
        internal VostokHostingEnvironment(
            [NotNull] HostingShutdown hostingShutdown,
            [NotNull] ApplicationShutdown applicationShutdown,
            [NotNull] IVostokApplicationIdentity applicationIdentity,
            [NotNull] IVostokApplicationLimits applicationLimits,
            [NotNull] Func <IVostokApplicationReplicationInfo> replicationInfoProvider,
            [NotNull] IVostokApplicationMetrics metrics,
            [NotNull] IVostokApplicationDiagnostics diagnostics,
            [NotNull] ILog log,
            [NotNull] ITracer tracer,
            [NotNull] IHerculesSink herculesSink,
            [NotNull] IConfigurationSource configurationSource,
            [NotNull] IConfigurationSource secretConfigurationSource,
            [NotNull] IConfigurationProvider configurationProvider,
            [NotNull] IConfigurationProvider secretConfigurationProvider,
            [NotNull] IClusterConfigClient clusterConfigClient,
            [NotNull] IServiceBeacon serviceBeacon,
            [CanBeNull] int?port,
            [NotNull] IServiceLocator serviceLocator,
            [NotNull] IContextGlobals contextGlobals,
            [NotNull] IContextProperties contextProperties,
            [NotNull] IContextConfiguration contextConfiguration,
            [NotNull] IDatacenters datacenters,
            [NotNull] IVostokHostExtensions hostExtensions,
            [NotNull] Action onDispose)
        {
            this.hostingShutdown         = hostingShutdown ?? throw new ArgumentNullException(nameof(hostingShutdown));
            this.applicationShutdown     = applicationShutdown ?? throw new ArgumentNullException(nameof(applicationShutdown));
            this.replicationInfoProvider = replicationInfoProvider ?? throw new ArgumentNullException(nameof(replicationInfoProvider));
            this.onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose));

            ApplicationIdentity = applicationIdentity ?? throw new ArgumentNullException(nameof(applicationIdentity));
            ApplicationLimits   = applicationLimits ?? throw new ArgumentNullException(nameof(applicationLimits));
            Metrics             = metrics ?? throw new ArgumentNullException(nameof(metrics));
            Diagnostics         = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics));
            Log                         = log ?? throw new ArgumentNullException(nameof(log));
            Tracer                      = tracer ?? throw new ArgumentNullException(nameof(tracer));
            HerculesSink                = herculesSink ?? throw new ArgumentNullException(nameof(herculesSink));
            ConfigurationSource         = configurationSource ?? throw new ArgumentNullException(nameof(configurationSource));
            ConfigurationProvider       = configurationProvider ?? throw new ArgumentNullException(nameof(configurationProvider));
            SecretConfigurationSource   = secretConfigurationSource ?? throw new ArgumentNullException(nameof(secretConfigurationSource));
            SecretConfigurationProvider = secretConfigurationProvider ?? throw new ArgumentNullException(nameof(secretConfigurationProvider));
            ClusterConfigClient         = clusterConfigClient ?? throw new ArgumentNullException(nameof(clusterConfigClient));
            ServiceBeacon               = serviceBeacon ?? throw new ArgumentNullException(nameof(serviceBeacon));
            Port                        = port;
            ServiceLocator              = serviceLocator ?? throw new ArgumentNullException(nameof(serviceLocator));
            ContextGlobals              = contextGlobals ?? throw new ArgumentNullException(nameof(contextGlobals));
            ContextProperties           = contextProperties ?? throw new ArgumentNullException(nameof(contextProperties));
            ContextConfiguration        = contextConfiguration ?? throw new ArgumentNullException(nameof(contextConfiguration));
            Datacenters                 = datacenters ?? throw new ArgumentNullException(nameof(datacenters));
            HostExtensions              = hostExtensions ?? throw new ArgumentNullException(nameof(hostExtensions));
        }
コード例 #7
0
        internal static IVostokApplicationIdentity AddServiceNameSuffix(IVostokApplicationIdentity identity, string suffix)
        {
            if (string.IsNullOrEmpty(suffix))
            {
                return(identity);
            }

            return(new ApplicationIdentity(
                       identity.Project,
                       identity.Subproject,
                       identity.Environment,
                       $"{identity.Application}{suffix}",
                       identity.Instance));
        }
コード例 #8
0
        public VostokApplicationMetrics(IMetricContext root, IVostokApplicationIdentity identity)
        {
            Root       = root;
            Project    = Root.WithTag(WellKnownApplicationIdentityProperties.Project, identity.Project);
            Subproject = identity.Subproject == null
                ? Project
                : Project.WithTag(WellKnownApplicationIdentityProperties.Subproject, identity.Subproject);
            Environment = Subproject.WithTag(WellKnownApplicationIdentityProperties.Environment, identity.Environment);
            Application = Environment.WithTag(WellKnownApplicationIdentityProperties.Application, identity.Application);

            var instance = identity.Instance;

            if (string.Equals(instance, EnvironmentInfo.Host, StringComparison.InvariantCultureIgnoreCase))
            {
                instance = instance.ToLowerInvariant();
            }
            Instance = Application.WithTag(WellKnownApplicationIdentityProperties.Instance, instance);
        }
コード例 #9
0
        public static (HostingShutdown hosting, ApplicationShutdown application) Create(
            IServiceBeacon serviceBeacon,
            IServiceLocator serviceLocator,
            IVostokApplicationIdentity identity,
            IMetricContext instanceMetrics,
            ILog log,
            int?port,
            IReadOnlyList <CancellationToken> tokens,
            TimeSpan totalTimeout,
            TimeSpan beaconTimeout,
            bool beaconWaitEnabled,
            bool sendAnnotation)
        {
            var hasRealBeacon  = serviceBeacon is ServiceBeacon;
            var hasRealLocator = serviceLocator is ServiceLocator;

            // (iloktionov): No point in waiting for beacon deregistration for apps without external port or when SD is disabled.
            beaconWaitEnabled &= port.HasValue && hasRealBeacon && hasRealLocator;

            // (iloktionov): No point in reducing app shutdown timeout right from the start when SD is disabled.
            beaconTimeout = hasRealBeacon ? TimeSpanArithmetics.Min(beaconTimeout, totalTimeout.Divide(3)) : TimeSpan.Zero;

            // (iloktionov): Artificially reduce initial app shutdown timeout by beacon shutdown timeout so that it's value doesn't drop abruptly on shutdown.
            var applicationShutdown = new ApplicationShutdown(log, totalTimeout - beaconTimeout);

            var hostingToken = tokens.Any() ? CancellationTokenSource.CreateLinkedTokenSource(tokens.ToArray()).Token : default;

            var hostingShutdown = new HostingShutdown(
                applicationShutdown,
                serviceBeacon,
                serviceLocator,
                identity,
                instanceMetrics,
                log,
                hostingToken,
                totalTimeout,
                beaconTimeout,
                beaconWaitEnabled,
                sendAnnotation);

            return(hostingShutdown, applicationShutdown);
        }
コード例 #10
0
        public IServiceBeacon Build(BuildContext context)
        {
            applicationIdentity = context.ApplicationIdentity;

            if (!enabled)
            {
                context.LogDisabled("ServiceBeacon");
                return(new DevNullServiceBeacon(CreateReplicaInfo(context)));
            }

            var zooKeeperClient = context.ZooKeeperClient;

            if (zooKeeperClient == null)
            {
                context.LogDisabled("ServiceBeacon", "disabled ZooKeeperClient");
                return(new DevNullServiceBeacon(CreateReplicaInfo(context)));
            }

            return(CreateBeacon(zooKeeperClient, context));
        }
コード例 #11
0
        public static string FormatServiceName([CanBeNull] this IVostokApplicationIdentity identity)
        {
            if (identity == null)
            {
                return(null);
            }

            var result = new StringBuilder();

            result.Append(identity.Project);

            if (identity.Subproject != null)
            {
                result.Append(".");
                result.Append(identity.Subproject);
            }

            result.Append(".");
            result.Append(identity.Application);

            return(result.ToString());
        }
コード例 #12
0
        public static IEnumerable <Substitution> Provide(
            IVostokApplicationIdentity identity,
            IClusterConfigClient clusterConfig,
            IServiceBeacon beacon,
            IDatacenters datacenters)
        {
            if (identity != null)
            {
                yield return(new Substitution(VostokConfigurationPlaceholders.IdentityProject, () => identity.Project));

                yield return(new Substitution(VostokConfigurationPlaceholders.IdentitySubproject, () => identity.Subproject));

                yield return(new Substitution(VostokConfigurationPlaceholders.IdentityEnvironment, () => identity.Environment));

                yield return(new Substitution(VostokConfigurationPlaceholders.IdentityApplication, () => identity.Application));

                yield return(new Substitution(VostokConfigurationPlaceholders.IdentityInstance, () => identity.Instance));
            }

            if (beacon != null)
            {
                yield return(new Substitution(VostokConfigurationPlaceholders.ServiceDiscoveryEnvironment, () => beacon.ReplicaInfo.Environment));

                yield return(new Substitution(VostokConfigurationPlaceholders.ServiceDiscoveryApplication, () => beacon.ReplicaInfo.Application));
            }

            if (datacenters != null)
            {
                yield return(new Substitution(VostokConfigurationPlaceholders.LocalDatacenter, datacenters.GetLocalDatacenter));
            }

            if (clusterConfig != null)
            {
                yield return(new Substitution(VostokConfigurationPlaceholders.ClusterConfigZone, () => (clusterConfig as ClusterConfigClient)?.Zone ?? "default"));
            }
        }
コード例 #13
0
ファイル: AnnotationsHelper.cs プロジェクト: lurlrlrl/hosting
 public static void ReportLaunching(IVostokApplicationIdentity identity, IMetricContext context)
 => context.SendAnnotation($"Instance {identity.Instance} is launching (just started) on host {EnvironmentInfo.Host}.",
                           (HostTag, EnvironmentInfo.Host), (EventTypeTag, "Launching"));