Ejemplo n.º 1
0
        /// <summary>Constructor to use for grain services</summary>
        protected GrainService(GrainId grainId, Silo silo, ILoggerFactory loggerFactory)
            : base(SystemTargetGrainId.Create(grainId.Type, silo.SiloAddress), silo.SiloAddress, lowPriority: true, loggerFactory: loggerFactory)
        {
            typeName = this.GetType().FullName;
            Logger   = loggerFactory.CreateLogger(typeName);

            ring = silo.RingProvider;
            StoppedCancellationTokenSource = new CancellationTokenSource();
        }
Ejemplo n.º 2
0
 internal SystemTarget(SystemTargetGrainId grainId, SiloAddress silo, bool lowPriority, ILoggerFactory loggerFactory)
 {
     this.id   = grainId;
     this.Silo = silo;
     this.ActivationAddress = ActivationAddress.GetAddress(this.Silo, this.id.GrainId, this.ActivationId);
     this.IsLowPriority     = lowPriority;
     this.ActivationId      = ActivationId.GetDeterministic(grainId.GrainId);
     this.timerLogger       = loggerFactory.CreateLogger <GrainTimer>();
 }
Ejemplo n.º 3
0
        /// <summary>Constructs a reference to the grain with the specified Id.</summary>
        /// <param name="grainId">The Id of the grain to refer to.</param>
        /// <param name="genericArgument">Type arguments in case of a generic grain.</param>
        /// <param name="runtime">The runtime which this grain reference is bound to.</param>
        private GrainReference(GrainId grainId, string genericArgument, IGrainReferenceRuntime runtime)
        {
            GrainId = grainId;
            this.genericArguments = genericArgument;
            this.runtime          = runtime;
            if (string.IsNullOrEmpty(genericArgument))
            {
                genericArguments = null; // always keep it null instead of empty.
            }

            // SystemTarget checks
            var isSystemTarget = grainId.IsSystemTarget();

            if (SystemTargetGrainId.TryParse(grainId, out var systemTargetId))
            {
                this.SystemTargetSilo = systemTargetId.GetSiloAddress();
                if (SystemTargetSilo == null)
                {
                    throw new ArgumentNullException("systemTargetSilo", String.Format("Trying to create a GrainReference for SystemTarget grain id {0}, but passing null systemTargetSilo.", grainId));
                }

                if (genericArguments != null)
                {
                    throw new ArgumentException(String.Format("Trying to create a GrainReference for SystemTarget grain id {0}, and also passing non-null genericArguments {1}.", grainId, genericArguments), "genericArgument");
                }
            }

            // ObserverId checks
            var isClient = grainId.IsClient();

            if (isClient)
            {
                // Note: we can probably just remove this check - it serves little purpose.
                if (!ObserverGrainId.TryParse(grainId, out _))
                {
                    throw new ArgumentNullException("observerId", String.Format("Trying to create a GrainReference for Observer with Client grain id {0}, but passing null observerId.", grainId));
                }

                if (genericArguments != null)
                {
                    throw new ArgumentException(String.Format("Trying to create a GrainReference for Client grain id {0}, and also passing non-null genericArguments {1}.", grainId, genericArguments), "genericArgument");
                }
            }
        }
Ejemplo n.º 4
0
 public GrainReference MakeGrainServiceReference(int typeData, string systemGrainId, SiloAddress siloAddress)
 => (GrainReference)this.runtimeClient.InternalGrainFactory.GetGrain(SystemTargetGrainId.CreateGrainServiceGrainId(typeData, systemGrainId, siloAddress));
Ejemplo n.º 5
0
 internal SystemTarget(GrainType grainType, SiloAddress silo, bool lowPriority, ILoggerFactory loggerFactory)
     : this(SystemTargetGrainId.Create(grainType, silo), silo, lowPriority, loggerFactory)
 {
 }
Ejemplo n.º 6
0
 internal SystemTarget(GrainType grainType, SiloAddress silo, ILoggerFactory loggerFactory)
     : this(SystemTargetGrainId.Create(grainType, silo), silo, false, loggerFactory)
 {
 }
Ejemplo n.º 7
0
        public void SendRequest(
            GrainReference target,
            InvokeMethodRequest request,
            TaskCompletionSource <object> context,
            InvokeMethodOptions options)
        {
            var message = this.messageFactory.CreateMessage(request, options);

            message.InterfaceType    = target.InterfaceType;
            message.InterfaceVersion = target.InterfaceVersion;

            // fill in sender
            if (message.SendingSilo == null)
            {
                message.SendingSilo = MySilo;
            }

            IGrainContext sendingActivation = RuntimeContext.CurrentGrainContext;

            if (sendingActivation == null)
            {
                var clientAddress = this.HostedClient.Address;
                message.SendingGrain      = clientAddress.Grain;
                message.SendingActivation = clientAddress.Activation;
            }
            else
            {
                message.SendingActivation = sendingActivation.ActivationId;
                message.SendingGrain      = sendingActivation.GrainId;
            }

            // fill in destination
            var targetGrainId = target.GrainId;

            message.TargetGrain = targetGrainId;
            SharedCallbackData sharedData;

            if (SystemTargetGrainId.TryParse(targetGrainId, out var systemTargetGrainId))
            {
                message.TargetSilo       = systemTargetGrainId.GetSiloAddress();
                message.TargetActivation = ActivationId.GetDeterministic(targetGrainId);
                message.Category         = targetGrainId.Type.Equals(Constants.MembershipOracleType) ?
                                           Message.Categories.Ping : Message.Categories.System;
                sharedData = this.systemSharedCallbackData;
            }
            else
            {
                sharedData = this.sharedCallbackData;
            }

            var oneWay = (options & InvokeMethodOptions.OneWay) != 0;

            if (context is null && !oneWay)
            {
                this.logger.Warn(ErrorCode.IGC_SendRequest_NullContext, "Null context {0}: {1}", message, Utils.GetStackTrace());
            }

            if (message.IsExpirableMessage(this.messagingOptions.DropExpiredMessages))
            {
                message.TimeToLive = sharedData.ResponseTimeout;
            }

            if (!oneWay)
            {
                var callbackData = new CallbackData(sharedData, context, message);
                callbacks.TryAdd(message.Id, callbackData);
            }

            this.messagingTrace.OnSendRequest(message);
            this.Dispatcher.SendMessage(message, sendingActivation);
        }
        private async Task RunAsync()
        {
            try
            {
                var grainFactory     = _services.GetRequiredService <IInternalGrainFactory>();
                var cancellationTask = _cancellation.Token.WhenCancelled();
                while (!_cancellation.IsCancellationRequested)
                {
                    var gateway = _gatewayManager.GetLiveGateway();
                    try
                    {
                        var provider    = grainFactory.GetGrain <IClusterManifestSystemTarget>(SystemTargetGrainId.Create(Constants.ManifestProviderType, gateway).GrainId);
                        var refreshTask = provider.GetClusterManifest().AsTask();
                        var task        = await Task.WhenAny(cancellationTask, refreshTask).ConfigureAwait(false);

                        if (ReferenceEquals(task, cancellationTask))
                        {
                            return;
                        }

                        if (!_updates.TryPublish(await refreshTask))
                        {
                            await Task.Delay(StandardExtensions.Min(_typeManagementOptions.TypeMapRefreshInterval, TimeSpan.FromMilliseconds(500)));

                            continue;
                        }

                        _initialized.TrySetResult(true);

                        if (_logger.IsEnabled(LogLevel.Debug))
                        {
                            _logger.LogDebug("Refreshed cluster manifest");
                        }

                        await Task.WhenAny(cancellationTask, Task.Delay(_typeManagementOptions.TypeMapRefreshInterval));
                    }
                    catch (Exception exception)
                    {
                        _logger.LogWarning(exception, "Error trying to get cluster manifest from gateway {Gateway}", gateway);
                        await Task.Delay(StandardExtensions.Min(_typeManagementOptions.TypeMapRefreshInterval, TimeSpan.FromSeconds(5)));
                    }
                }
            }
            finally
            {
                _initialized.TrySetResult(false);

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Stopped refreshing cluster manifest");
                }
            }
        }
Ejemplo n.º 9
0
 public GrainReference MakeGrainServiceReference(int typeData, string systemGrainId, SiloAddress siloAddress)
 => GrainReference.FromGrainId(SystemTargetGrainId.CreateGrainServiceGrainId(typeData, systemGrainId, siloAddress), this.runtimeClient.GrainReferenceRuntime);