Пример #1
0
        private void SendRequestMessage(
            GrainReference target,
            Message message,
            TaskCompletionSource <object> context,
            Action <Message, TaskCompletionSource <object> > callback,
            string debugContext,
            InvokeMethodOptions options,
            string genericArguments = null)
        {
            // fill in sender
            if (message.SendingSilo == null)
            {
                message.SendingSilo = MySilo;
            }
            if (!String.IsNullOrEmpty(genericArguments))
            {
                message.GenericGrainType = genericArguments;
            }

            SchedulingContext schedulingContext = RuntimeContext.Current != null ?
                                                  RuntimeContext.Current.ActivationContext as SchedulingContext : null;

            ActivationData sendingActivation = null;

            if (schedulingContext == null)
            {
                throw new InvalidOperationException(
                          String.Format("Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is not set to SchedulingContext) "
                                        + "RuntimeContext.Current={1} TaskScheduler.Current={2}",
                                        message,
                                        RuntimeContext.Current == null ? "null" : RuntimeContext.Current.ToString(),
                                        TaskScheduler.Current));
            }
            switch (schedulingContext.ContextType)
            {
            case SchedulingContextType.SystemThread:
                throw new ArgumentException(
                          String.Format("Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is of SchedulingContextType.SystemThread type)", message), "context");

            case SchedulingContextType.Activation:
                message.SendingActivation = schedulingContext.Activation.ActivationId;
                message.SendingGrain      = schedulingContext.Activation.Grain;
                sendingActivation         = schedulingContext.Activation;
                break;

            case SchedulingContextType.SystemTarget:
                message.SendingActivation = schedulingContext.SystemTarget.ActivationId;
                message.SendingGrain      = ((ISystemTargetBase)schedulingContext.SystemTarget).GrainId;
                break;
            }

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

            message.TargetGrain = targetGrainId;
            if (targetGrainId.IsSystemTarget)
            {
                SiloAddress targetSilo = (target.SystemTargetSilo ?? MySilo);
                message.TargetSilo       = targetSilo;
                message.TargetActivation = ActivationId.GetSystemActivation(targetGrainId, targetSilo);
                message.Category         = targetGrainId.Equals(Constants.MembershipOracleId) ?
                                           Message.Categories.Ping : Message.Categories.System;
            }
            if (target.IsObserverReference)
            {
                message.TargetObserverId = target.ObserverId;
            }

            if (debugContext != null)
            {
                message.DebugContext = debugContext;
            }

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

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

            if (message.IsExpirableMessage(Config.Globals))
            {
                message.TimeToLive = ResponseTimeout;
            }

            if (!oneWay)
            {
                var callbackData = new CallbackData(
                    callback,
                    tryResendMessage,
                    context,
                    message,
                    unregisterCallback,
                    Config.Globals);
                callbacks.TryAdd(message.Id, callbackData);
                callbackData.StartTimer(ResponseTimeout);
            }

            if (targetGrainId.IsSystemTarget)
            {
                // Messages to system targets bypass the task system and get sent "in-line"
                this.Dispatcher.TransportMessage(message);
            }
            else
            {
                this.Dispatcher.SendMessage(message, sendingActivation);
            }
        }
Пример #2
0
        private void SendRequestMessage(GrainReference target, Message message, TaskCompletionSource<object> context, Action<Message, TaskCompletionSource<object>> callback, string debugContext = null, InvokeMethodOptions options = InvokeMethodOptions.None, string genericArguments = null)
        {
            var targetGrainId = target.GrainId;
            var oneWay = (options & InvokeMethodOptions.OneWay) != 0;
            message.SendingGrain = CurrentActivationAddress.Grain;
            message.SendingActivation = CurrentActivationAddress.Activation;
            message.TargetGrain = targetGrainId;
            if (!String.IsNullOrEmpty(genericArguments))
                message.GenericGrainType = genericArguments;

            if (targetGrainId.IsSystemTarget)
            {
                // If the silo isn't be supplied, it will be filled in by the sender to be the gateway silo
                message.TargetSilo = target.SystemTargetSilo;
                if (target.SystemTargetSilo != null)
                {
                    message.TargetActivation = ActivationId.GetSystemActivation(targetGrainId, target.SystemTargetSilo);
                }
            }
            // Client sending messages to another client (observer). Yes, we support that.
            if (target.IsObserverReference)
            {
                message.TargetObserverId = target.ObserverId;
            }
            
            if (debugContext != null)
            {
                message.DebugContext = debugContext;
            }
            if (message.IsExpirableMessage(config))
            {
                // don't set expiration for system target messages.
                message.Expiration = DateTime.UtcNow + responseTimeout + Constants.MAXIMUM_CLOCK_SKEW;
            }

            if (!oneWay)
            {
                var callbackData = new CallbackData(callback, TryResendMessage, context, message, () => UnRegisterCallback(message.Id), config);
                callbacks.TryAdd(message.Id, callbackData);
                callbackData.StartTimer(responseTimeout);
            }

            if (logger.IsVerbose2) logger.Verbose2("Send {0}", message);
            transport.SendMessage(message);
        }
Пример #3
0
        private void SendRequestMessage(
            GrainReference target, 
            Message message, 
            TaskCompletionSource<object> context,
            Action<Message, TaskCompletionSource<object>> callback,
            string debugContext, 
            InvokeMethodOptions options,
            string genericArguments = null)
        {
            // fill in sender
            if (message.SendingSilo == null)
                message.SendingSilo = MySilo;
            if (!String.IsNullOrEmpty(genericArguments))
                message.GenericGrainType = genericArguments;

            SchedulingContext schedulingContext = RuntimeContext.Current != null ? 
                RuntimeContext.Current.ActivationContext as SchedulingContext : null;

            ActivationData sendingActivation = null;
            if (schedulingContext == null)
            {
                throw new InvalidExpressionException(
                    String.Format("Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is not set to SchedulingContext) "
                        + "RuntimeContext.Current={1} TaskScheduler.Current={2}",
                        message,
                        RuntimeContext.Current == null ? "null" : RuntimeContext.Current.ToString(),
                        TaskScheduler.Current));
            }
            switch (schedulingContext.ContextType)
            {
                case SchedulingContextType.SystemThread:
                    throw new ArgumentException(
                        String.Format("Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is of SchedulingContextType.SystemThread type)", message), "context");

                case SchedulingContextType.Activation:
                    message.SendingActivation = schedulingContext.Activation.ActivationId;
                    message.SendingGrain = schedulingContext.Activation.Grain;
                    sendingActivation = schedulingContext.Activation;
                    break;

                case SchedulingContextType.SystemTarget:
                    message.SendingActivation = schedulingContext.SystemTarget.ActivationId;
                    message.SendingGrain = schedulingContext.SystemTarget.GrainId;
                    break;
            }

            // fill in destination
            var targetGrainId = target.GrainId;
            message.TargetGrain = targetGrainId;
            if (targetGrainId.IsSystemTarget)
            {
                SiloAddress targetSilo = (target.SystemTargetSilo ?? MySilo);
                message.TargetSilo = targetSilo;
                message.TargetActivation = ActivationId.GetSystemActivation(targetGrainId, targetSilo);
                message.Category = targetGrainId.Equals(Constants.MembershipOracleId) ? 
                    Message.Categories.Ping : Message.Categories.System;
            }
            if (target.IsObserverReference)
            {
                message.TargetObserverId = target.ObserverId;
            }

            if (debugContext != null)
                message.DebugContext = debugContext;

            var oneWay = (options & InvokeMethodOptions.OneWay) != 0;
            if (context == null && !oneWay)
                logger.Warn(ErrorCode.IGC_SendRequest_NullContext, "Null context {0}: {1}", message, new StackTrace());

            if (message.IsExpirableMessage(Config.Globals))
                message.Expiration = DateTime.UtcNow + ResponseTimeout + Constants.MAXIMUM_CLOCK_SKEW;
            
            if (!oneWay)
            {
                var callbackData = new CallbackData(
                    callback, 
                    TryResendMessage, 
                    context,
                    message,
                    () => UnRegisterCallback(message.Id),
                    Config.Globals);
                callbacks.TryAdd(message.Id, callbackData);
                callbackData.StartTimer(ResponseTimeout);
            }

            if (targetGrainId.IsSystemTarget)
            {
                // Messages to system targets bypass the task system and get sent "in-line"
                dispatcher.TransportMessage(message);
            }
            else
            {
                dispatcher.SendMessage(message, sendingActivation);
            }
        }
Пример #4
0
        public void SendRequest(
            GrainReference target,
            InvokeMethodRequest request,
            TaskCompletionSource <object> context,
            string debugContext,
            InvokeMethodOptions options,
            string genericArguments = null)
        {
            var message = this.messageFactory.CreateMessage(request, options);

            // fill in sender
            if (message.SendingSilo == null)
            {
                message.SendingSilo = MySilo;
            }
            if (!String.IsNullOrEmpty(genericArguments))
            {
                message.GenericGrainType = genericArguments;
            }

            SchedulingContext schedulingContext = RuntimeContext.CurrentActivationContext as SchedulingContext;

            ActivationData sendingActivation = null;

            if (schedulingContext == null)
            {
                var clientAddress = this.HostedClient.ClientAddress;
                message.SendingGrain      = clientAddress.Grain;
                message.SendingActivation = clientAddress.Activation;
            }
            else
            {
                switch (schedulingContext.ContextType)
                {
                case SchedulingContextType.SystemThread:
                    throw new ArgumentException(
                              String.Format(
                                  "Trying to send a message {0} on a silo not from within grain and not from within system target (RuntimeContext is of SchedulingContextType.SystemThread type)",
                                  message),
                              "context");

                case SchedulingContextType.Activation:
                    message.SendingActivation = schedulingContext.Activation.ActivationId;
                    message.SendingGrain      = schedulingContext.Activation.Grain;
                    sendingActivation         = schedulingContext.Activation;
                    break;

                case SchedulingContextType.SystemTarget:
                    message.SendingActivation = schedulingContext.SystemTarget.ActivationId;
                    message.SendingGrain      = ((ISystemTargetBase)schedulingContext.SystemTarget).GrainId;
                    break;
                }
            }

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

            message.TargetGrain = targetGrainId;
            SharedCallbackData sharedData;

            if (targetGrainId.IsSystemTarget)
            {
                SiloAddress targetSilo = (target.SystemTargetSilo ?? MySilo);
                message.TargetSilo       = targetSilo;
                message.TargetActivation = ActivationId.GetSystemActivation(targetGrainId, targetSilo);
                message.Category         = targetGrainId.Equals(Constants.MembershipOracleId) ?
                                           Message.Categories.Ping : Message.Categories.System;
                sharedData = this.systemSharedCallbackData;
            }
            else
            {
                sharedData = this.sharedCallbackData;
            }

            if (target.IsObserverReference)
            {
                message.TargetObserverId = target.ObserverId;
            }

            if (debugContext != null)
            {
                message.DebugContext = debugContext;
            }

            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);

            if (targetGrainId.IsSystemTarget)
            {
                // Messages to system targets bypass the task system and get sent "in-line"
                this.Dispatcher.TransportMessage(message);
            }
            else
            {
                this.Dispatcher.SendMessage(message, sendingActivation);
            }
        }
Пример #5
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);
        }
Пример #6
0
        public void SendRequest(
            GrainReference target,
            InvokeMethodRequest request,
            TaskCompletionSource <object> context,
            InvokeMethodOptions options,
            string genericArguments)
        {
            var message = this.messageFactory.CreateMessage(request, options);

            // fill in sender
            if (message.SendingSilo == null)
            {
                message.SendingSilo = MySilo;
            }
            if (!String.IsNullOrEmpty(genericArguments))
            {
                message.GenericGrainType = genericArguments;
            }

            IGrainContext sendingActivation = RuntimeContext.CurrentGrainContext;

            if (sendingActivation == null)
            {
                var clientAddress = this.HostedClient.ClientAddress;
                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 (targetGrainId.IsSystemTarget())
            {
                SiloAddress targetSilo = (target.SystemTargetSilo ?? MySilo);
                message.TargetSilo       = targetSilo;
                message.TargetActivation = ActivationId.GetDeterministic(targetGrainId);
                message.Category         = targetGrainId.Equals(Constants.MembershipOracleId) ?
                                           Message.Categories.Ping : Message.Categories.System;
                sharedData = this.systemSharedCallbackData;
            }
            else
            {
                sharedData = this.sharedCallbackData;
            }

            if (target.IsObserverReference)
            {
                message.TargetObserverId = target.ObserverId;
            }

            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);

            if (targetGrainId.IsSystemTarget())
            {
                // Messages to system targets bypass the task system and get sent "in-line"
                this.Dispatcher.TransportMessage(message);
            }
            else
            {
                this.Dispatcher.SendMessage(message, sendingActivation);
            }
        }