Exemple #1
0
        public void GrainReference_Test1()
        {
            Guid           guid           = Guid.NewGuid();
            GrainId        regularGrainId = GrainId.GetGrainIdForTesting(guid);
            GrainReference grainRef       = this.environment.InternalGrainFactory.GetGrain(regularGrainId);

            TestGrainReference(grainRef);

            grainRef = GrainReference.FromGrainId(regularGrainId, null, "generic");
            TestGrainReference(grainRef);

            GrainId systemTragetGrainId = GrainId.NewSystemTargetGrainIdByTypeCode(2);

            grainRef = GrainReference.FromGrainId(systemTragetGrainId, null, null, SiloAddress.NewLocalAddress(1));
            this.environment.GrainFactory.BindGrainReference(grainRef);
            TestGrainReference(grainRef);

            GrainId observerGrainId = GrainId.NewClientId();

            grainRef = GrainReference.NewObserverGrainReference(observerGrainId, GuidId.GetNewGuidId(), this.environment.RuntimeClient.GrainReferenceRuntime);
            this.environment.GrainFactory.BindGrainReference(grainRef);
            TestGrainReference(grainRef);

            GrainId geoObserverGrainId = GrainId.NewClientId("clusterid");

            grainRef = GrainReference.NewObserverGrainReference(geoObserverGrainId, GuidId.GetNewGuidId(), this.environment.RuntimeClient.GrainReferenceRuntime);
            this.environment.GrainFactory.BindGrainReference(grainRef);
            TestGrainReference(grainRef);
        }
        public OutsideRuntimeClient(
            ILoggerFactory loggerFactory,
            IOptions <ClientMessagingOptions> clientMessagingOptions,
            IOptions <TypeManagementOptions> typeManagementOptions,
            IOptions <StatisticsOptions> statisticsOptions,
            ApplicationRequestsStatisticsGroup appRequestStatistics,
            StageAnalysisStatisticsGroup schedulerStageStatistics,
            ClientStatisticsManager clientStatisticsManager,
            MessagingTrace messagingTrace)
        {
            this.loggerFactory            = loggerFactory;
            this.statisticsOptions        = statisticsOptions;
            this.appRequestStatistics     = appRequestStatistics;
            this.schedulerStageStatistics = schedulerStageStatistics;
            this.ClientStatistics         = clientStatisticsManager;
            this.messagingTrace           = messagingTrace;
            this.logger            = loggerFactory.CreateLogger <OutsideRuntimeClient>();
            this.handshakeClientId = GrainId.NewClientId();
            callbacks = new ConcurrentDictionary <CorrelationId, CallbackData>();
            this.clientMessagingOptions = clientMessagingOptions.Value;
            this.typeMapRefreshInterval = typeManagementOptions.Value.TypeMapRefreshInterval;

            this.sharedCallbackData = new SharedCallbackData(
                msg => this.UnregisterCallback(msg.Id),
                this.loggerFactory.CreateLogger <CallbackData>(),
                this.clientMessagingOptions,
                this.appRequestStatistics,
                this.clientMessagingOptions.ResponseTimeout);
        }
Exemple #3
0
        public void GrainReference_Test1()
        {
            Guid           guid           = Guid.NewGuid();
            GrainId        regularGrainId = GrainId.GetGrainIdForTesting(guid);
            GrainReference grainRef       = GrainReference.FromGrainId(regularGrainId);

            TestGrainReference(grainRef);

            grainRef = GrainReference.FromGrainId(regularGrainId, "generic");
            TestGrainReference(grainRef);

            GrainId systemTragetGrainId = GrainId.NewSystemTargetGrainIdByTypeCode(2);

            grainRef = GrainReference.FromGrainId(systemTragetGrainId, null, SiloAddress.NewLocalAddress(1));
            TestGrainReference(grainRef);

            GrainId observerGrainId = GrainId.NewClientId();

            grainRef = GrainReference.NewObserverGrainReference(observerGrainId, GuidId.GetNewGuidId());
            TestGrainReference(grainRef);

            GrainId geoObserverGrainId = GrainId.NewClientId("clusterid");

            grainRef = GrainReference.NewObserverGrainReference(geoObserverGrainId, GuidId.GetNewGuidId());
            TestGrainReference(grainRef);
        }
Exemple #4
0
        protected override bool RecordOpenedSocket(Socket sock)
        {
            ThreadTrackingStatistic.FirstClientConnectedStartTracking();
            GrainId client;

            if (!ReceiveSocketPreample(sock, true, out client))
            {
                return(false);
            }

            // refuse clients that are connecting to the wrong cluster
            if (client.Category == UniqueKey.Category.GeoClient)
            {
                if (client.Key.ClusterId != Silo.CurrentSilo.ClusterId)
                {
                    Log.Error(ErrorCode.GatewayAcceptor_WrongClusterId,
                              string.Format(
                                  "Refusing connection by client {0} because of cluster id mismatch: client={1} silo={2}",
                                  client, client.Key.ClusterId, Silo.CurrentSilo.ClusterId));
                    return(false);
                }
            }
            else
            {
                //convert handshake cliendId to a GeoClient ID
                if (!string.IsNullOrEmpty(Silo.CurrentSilo.ClusterId))
                {
                    client = GrainId.NewClientId(client.PrimaryKey, Silo.CurrentSilo.ClusterId);
                }
            }

            gateway.RecordOpenedSocket(sock, client);
            return(true);
        }
Exemple #5
0
        /// <summary>
        /// Handles an incoming (proxied) message by rerouting it immediately and unconditionally,
        /// after some header massaging.
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="receivedOnSocket"></param>
        protected override void HandleMessage(Message msg, Socket receivedOnSocket)
        {
            EventSourceUtils.EmitEvent(msg, OrleansGatewayAcceptorEvent.HandleMessageAction);
            // Don't process messages that have already timed out
            if (msg.IsExpired)
            {
                msg.DropExpiredMessage(MessagingStatisticsGroup.Phase.Receive);
                return;
            }

            gatewayTrafficCounter.Increment();

            // return address translation for geo clients (replace sending address cli/* with gcl/*)
            if (this.multiClusterOptions.HasMultiClusterNetwork && msg.SendingAddress.Grain.Category != UniqueKey.Category.GeoClient)
            {
                msg.SendingGrain = GrainId.NewClientId(msg.SendingAddress.Grain.PrimaryKey, this.siloDetails.ClusterId);
            }

            // Are we overloaded?
            if (this.overloadDetector.Overloaded)
            {
                MessagingStatisticsGroup.OnRejectedMessage(msg);
                Message rejection = this.MessageFactory.CreateRejectionResponse(msg, Message.RejectionTypes.GatewayTooBusy, "Shedding load");
                MessageCenter.TryDeliverToProxy(rejection);
                if (Log.IsEnabled(LogLevel.Debug))
                {
                    Log.Debug("Rejecting a request due to overloading: {0}", msg.ToString());
                }
                loadSheddingCounter.Increment();
                return;
            }

            SiloAddress targetAddress = gateway.TryToReroute(msg);

            msg.SendingSilo = MessageCenter.MyAddress;

            if (targetAddress == null)
            {
                // reroute via Dispatcher
                msg.TargetSilo       = null;
                msg.TargetActivation = null;
                msg.ClearTargetAddress();

                if (msg.TargetGrain.IsSystemTarget)
                {
                    msg.TargetSilo       = MessageCenter.MyAddress;
                    msg.TargetActivation = ActivationId.GetSystemActivation(msg.TargetGrain, MessageCenter.MyAddress);
                }

                MessagingStatisticsGroup.OnMessageReRoute(msg);
                MessageCenter.RerouteMessage(msg);
            }
            else
            {
                // send directly
                msg.TargetSilo = targetAddress;
                MessageCenter.SendMessage(msg);
            }
        }
 public OutsideRuntimeClient()
 {
     this.handshakeClientId = GrainId.NewClientId();
     tryResendMessage       = TryResendMessage;
     unregisterCallback     = msg => UnRegisterCallback(msg.Id);
     callbacks    = new ConcurrentDictionary <CorrelationId, CallbackData>();
     localObjects = new ConcurrentDictionary <GuidId, LocalObjectData>();
 }
Exemple #7
0
        /// <summary>
        /// Handles an incoming (proxied) message by rerouting it immediately and unconditionally,
        /// after some header massaging.
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="receivedOnSocket"></param>
        protected override void HandleMessage(Message msg, Socket receivedOnSocket)
        {
            // Don't process messages that have already timed out
            if (msg.IsExpired)
            {
                msg.DropExpiredMessage(MessagingStatisticsGroup.Phase.Receive);
                return;
            }

            gatewayTrafficCounter.Increment();

            // return address translation for geo clients (replace sending address cli/* with gcl/*)
            if (!string.IsNullOrEmpty(Silo.CurrentSilo.ClusterId) && msg.SendingAddress.Grain.Category != UniqueKey.Category.GeoClient)
            {
                msg.SendingGrain = GrainId.NewClientId(msg.SendingAddress.Grain.PrimaryKey, Silo.CurrentSilo.ClusterId);
            }

            // Are we overloaded?
            if ((MessageCenter.Metrics != null) && MessageCenter.Metrics.IsOverloaded)
            {
                MessagingStatisticsGroup.OnRejectedMessage(msg);
                Message rejection = msg.CreateRejectionResponse(Message.RejectionTypes.GatewayTooBusy, "Shedding load");
                MessageCenter.TryDeliverToProxy(rejection);
                if (Log.IsVerbose)
                {
                    Log.Verbose("Rejecting a request due to overloading: {0}", msg.ToString());
                }
                loadSheddingCounter.Increment();
                return;
            }

            SiloAddress targetAddress = gateway.TryToReroute(msg);

            msg.SendingSilo = MessageCenter.MyAddress;

            if (targetAddress == null)
            {
                // reroute via Dispatcher
                msg.TargetSilo       = null;
                msg.TargetActivation = null;
                msg.ClearTargetAddress();

                if (msg.TargetGrain.IsSystemTarget)
                {
                    msg.TargetSilo       = MessageCenter.MyAddress;
                    msg.TargetActivation = ActivationId.GetSystemActivation(msg.TargetGrain, MessageCenter.MyAddress);
                }

                MessagingStatisticsGroup.OnMessageReRoute(msg);
                MessageCenter.RerouteMessage(msg);
            }
            else
            {
                // send directly
                msg.TargetSilo = targetAddress;
                MessageCenter.SendMessage(msg);
            }
        }
Exemple #8
0
 public OutsideRuntimeClient(ILoggerFactory loggerFactory)
 {
     this.loggerFactory     = loggerFactory;
     this.logger            = loggerFactory.CreateLogger <OutsideRuntimeClient>();
     this.handshakeClientId = GrainId.NewClientId();
     tryResendMessage       = TryResendMessage;
     unregisterCallback     = msg => UnRegisterCallback(msg.Id);
     callbacks               = new ConcurrentDictionary <CorrelationId, CallbackData>();
     localObjects            = new ConcurrentDictionary <GuidId, LocalObjectData>();
     this.callBackDataLogger = loggerFactory.CreateLogger <CallbackData>();
     this.timerLogger        = loggerFactory.CreateLogger <SafeTimer>();
 }
Exemple #9
0
        protected override async Task RunInternal()
        {
            var(grainId, protocolVersion, siloAddress) = await ConnectionPreamble.Read(this.Context);

            if (protocolVersion >= NetworkProtocolVersion.Version2)
            {
                await ConnectionPreamble.Write(
                    this.Context,
                    Constants.SiloDirectConnectionId,
                    this.connectionOptions.ProtocolVersion,
                    this.myAddress);
            }

            if (grainId.Equals(Constants.SiloDirectConnectionId))
            {
                throw new InvalidOperationException($"Unexpected direct silo connection on proxy endpoint from {siloAddress?.ToString() ?? "unknown silo"}");
            }

            // refuse clients that are connecting to the wrong cluster
            if (grainId.Category == UniqueKey.Category.GeoClient)
            {
                if (grainId.Key.ClusterId != this.siloDetails.ClusterId)
                {
                    var message = string.Format(
                        "Refusing connection by client {0} because of cluster id mismatch: client={1} silo={2}",
                        grainId, grainId.Key.ClusterId, this.siloDetails.ClusterId);
                    this.Log.Error(ErrorCode.GatewayAcceptor_WrongClusterId, message);
                    throw new InvalidOperationException(message);
                }
            }
            else
            {
                //convert handshake cliendId to a GeoClient ID
                if (this.multiClusterOptions.HasMultiClusterNetwork)
                {
                    grainId = GrainId.NewClientId(grainId.PrimaryKey, this.siloDetails.ClusterId);
                }
            }

            try
            {
                this.gateway.RecordOpenedConnection(this, grainId);
                await base.RunInternal();
            }
            finally
            {
                this.gateway.RecordClosedConnection(this);
            }
        }
 public OutsideRuntimeClient(
     ILoggerFactory loggerFactory,
     IOptions <ClientMessagingOptions> clientMessagingOptions,
     IOptions <TypeManagementOptions> typeManagementOptions)
 {
     this.loggerFactory     = loggerFactory;
     this.logger            = loggerFactory.CreateLogger <OutsideRuntimeClient>();
     this.handshakeClientId = GrainId.NewClientId();
     tryResendMessage       = TryResendMessage;
     unregisterCallback     = msg => UnRegisterCallback(msg.Id);
     callbacks                   = new ConcurrentDictionary <CorrelationId, CallbackData>();
     localObjects                = new ConcurrentDictionary <GuidId, LocalObjectData>();
     this.callBackDataLogger     = loggerFactory.CreateLogger <CallbackData>();
     this.timerLogger            = loggerFactory.CreateLogger <SafeTimer>();
     this.clientMessagingOptions = clientMessagingOptions.Value;
     this.typeMapRefreshInterval = typeManagementOptions.Value.TypeMapRefreshInterval;
     this.responseTimeout        = clientMessagingOptions.Value.ResponseTimeout;
 }
Exemple #11
0
        public OutsideRuntimeClient(ClientConfiguration cfg, bool secondary = false)
        {
            this.typeCache         = new TypeMetadataCache();
            this.assemblyProcessor = new AssemblyProcessor(this.typeCache);
            this.grainFactory      = new GrainFactory(this, this.typeCache);

            if (cfg == null)
            {
                Console.WriteLine("An attempt to create an OutsideRuntimeClient with null ClientConfiguration object.");
                throw new ArgumentException("OutsideRuntimeClient was attempted to be created with null ClientConfiguration object.", "cfg");
            }

            this.config = cfg;

            if (!LogManager.IsInitialized)
            {
                LogManager.Initialize(config);
            }
            StatisticsCollector.Initialize(config);
            SerializationManager.Initialize(cfg.SerializationProviders, cfg.FallbackSerializationProvider);
            this.assemblyProcessor.Initialize();

            logger    = LogManager.GetLogger("OutsideRuntimeClient", LoggerType.Runtime);
            appLogger = LogManager.GetLogger("Application", LoggerType.Application);

            BufferPool.InitGlobalBufferPool(config);
            this.handshakeClientId = GrainId.NewClientId();

            try
            {
                LoadAdditionalAssemblies();

                callbacks    = new ConcurrentDictionary <CorrelationId, CallbackData>();
                localObjects = new ConcurrentDictionary <GuidId, LocalObjectData>();

                if (!secondary)
                {
                    UnobservedExceptionsHandlerClass.SetUnobservedExceptionHandler(UnhandledException);
                }
                AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;

                // Ensure SerializationManager static constructor is called before AssemblyLoad event is invoked
                SerializationManager.GetDeserializer(typeof(String));

                clientProviderRuntime     = new ClientProviderRuntime(grainFactory, null);
                statisticsProviderManager = new StatisticsProviderManager("Statistics", clientProviderRuntime);
                var statsProviderName = statisticsProviderManager.LoadProvider(config.ProviderConfigurations)
                                        .WaitForResultWithThrow(initTimeout);
                if (statsProviderName != null)
                {
                    config.StatisticsProviderName = statsProviderName;
                }

                responseTimeout = Debugger.IsAttached ? Constants.DEFAULT_RESPONSE_TIMEOUT : config.ResponseTimeout;
                var localAddress = ClusterConfiguration.GetLocalIPAddress(config.PreferredFamily, config.NetInterface);

                // Client init / sign-on message
                logger.Info(ErrorCode.ClientInitializing, string.Format(
                                "{0} Initializing OutsideRuntimeClient on {1} at {2} Client Id = {3} {0}",
                                BARS, config.DNSHostName, localAddress, handshakeClientId));
                string startMsg = string.Format("{0} Starting OutsideRuntimeClient with runtime Version='{1}' in AppDomain={2}",
                                                BARS, RuntimeVersion.Current, PrintAppDomainDetails());
                startMsg = string.Format("{0} Config= " + Environment.NewLine + " {1}", startMsg, config);
                logger.Info(ErrorCode.ClientStarting, startMsg);

                if (TestOnlyThrowExceptionDuringInit)
                {
                    throw new InvalidOperationException("TestOnlyThrowExceptionDuringInit");
                }

                config.CheckGatewayProviderSettings();

                var generation          = -SiloAddress.AllocateNewGeneration(); // Client generations are negative
                var gatewayListProvider = GatewayProviderFactory.CreateGatewayListProvider(config)
                                          .WithTimeout(initTimeout).Result;
                transport = new ProxiedMessageCenter(config, localAddress, generation, handshakeClientId, gatewayListProvider);

                if (StatisticsCollector.CollectThreadTimeTrackingStats)
                {
                    incomingMessagesThreadTimeTracking = new ThreadTrackingStatistic("ClientReceiver");
                }
            }
            catch (Exception exc)
            {
                if (logger != null)
                {
                    logger.Error(ErrorCode.Runtime_Error_100319, "OutsideRuntimeClient constructor failed.", exc);
                }
                ConstructorReset();
                throw;
            }
        }
Exemple #12
0
        public OutsideRuntimeClient(ClientConfiguration cfg, bool secondary = false)
        {
            if (cfg == null)
            {
                Console.WriteLine("An attempt to create an OutsideRuntimeClient with null ClientConfiguration object.");
                throw new ArgumentException("OutsideRuntimeClient was attempted to be created with null ClientConfiguration object.", "cfg");
            }

            var services = new ServiceCollection();

            services.AddSingleton(cfg);
            services.AddSingleton <TypeMetadataCache>();
            services.AddSingleton <AssemblyProcessor>();
            services.AddSingleton(this);
            services.AddSingleton <IRuntimeClient>(this);
            services.AddSingleton <GrainFactory>();
            services.AddFromExisting <IGrainFactory, GrainFactory>();
            services.AddFromExisting <IInternalGrainFactory, GrainFactory>();
            services.AddFromExisting <IGrainReferenceConverter, GrainFactory>();
            services.AddSingleton <ClientProviderRuntime>();
            services.AddFromExisting <IMessagingConfiguration, ClientConfiguration>();
            services.AddSingleton <IGatewayListProvider>(
                sp => ActivatorUtilities.CreateInstance <GatewayProviderFactory>(sp).CreateGatewayListProvider());
            services.AddSingleton <SerializationManager>();
            services.AddSingleton <MessageFactory>();
            services.AddSingleton <Func <string, Logger> >(LogManager.GetLogger);
            services.AddSingleton <StreamProviderManager>();
            services.AddSingleton <ClientStatisticsManager>();
            services.AddFromExisting <IStreamProviderManager, StreamProviderManager>();
            services.AddSingleton <CodeGeneratorManager>();

            this.ServiceProvider = services.BuildServiceProvider();

            this.InternalGrainFactory = this.ServiceProvider.GetRequiredService <IInternalGrainFactory>();
            this.ClientStatistics     = this.ServiceProvider.GetRequiredService <ClientStatisticsManager>();
            this.SerializationManager = this.ServiceProvider.GetRequiredService <SerializationManager>();
            this.messageFactory       = this.ServiceProvider.GetService <MessageFactory>();

            this.config = cfg;

            if (!LogManager.IsInitialized)
            {
                LogManager.Initialize(config);
            }
            StatisticsCollector.Initialize(config);
            this.assemblyProcessor = this.ServiceProvider.GetRequiredService <AssemblyProcessor>();
            this.assemblyProcessor.Initialize();

            logger    = LogManager.GetLogger("OutsideRuntimeClient", LoggerType.Runtime);
            appLogger = LogManager.GetLogger("Application", LoggerType.Application);

            BufferPool.InitGlobalBufferPool(config);
            this.handshakeClientId = GrainId.NewClientId();

            tryResendMessage   = TryResendMessage;
            unregisterCallback = msg => UnRegisterCallback(msg.Id);

            try
            {
                LoadAdditionalAssemblies();

                callbacks    = new ConcurrentDictionary <CorrelationId, CallbackData>();
                localObjects = new ConcurrentDictionary <GuidId, LocalObjectData>();

                if (!secondary)
                {
                    UnobservedExceptionsHandlerClass.SetUnobservedExceptionHandler(UnhandledException);
                }
                AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;

                clientProviderRuntime     = this.ServiceProvider.GetRequiredService <ClientProviderRuntime>();
                statisticsProviderManager = new StatisticsProviderManager("Statistics", clientProviderRuntime);
                var statsProviderName = statisticsProviderManager.LoadProvider(config.ProviderConfigurations)
                                        .WaitForResultWithThrow(initTimeout);
                if (statsProviderName != null)
                {
                    config.StatisticsProviderName = statsProviderName;
                }

                responseTimeout = Debugger.IsAttached ? Constants.DEFAULT_RESPONSE_TIMEOUT : config.ResponseTimeout;
                var localAddress = ClusterConfiguration.GetLocalIPAddress(config.PreferredFamily, config.NetInterface);

                // Client init / sign-on message
                logger.Info(ErrorCode.ClientInitializing, string.Format(
                                "{0} Initializing OutsideRuntimeClient on {1} at {2} Client Id = {3} {0}",
                                BARS, config.DNSHostName, localAddress, handshakeClientId));
                string startMsg = string.Format("{0} Starting OutsideRuntimeClient with runtime Version='{1}' in AppDomain={2}",
                                                BARS, RuntimeVersion.Current, PrintAppDomainDetails());
                startMsg = string.Format("{0} Config= " + Environment.NewLine + " {1}", startMsg, config);
                logger.Info(ErrorCode.ClientStarting, startMsg);

                if (TestOnlyThrowExceptionDuringInit)
                {
                    throw new InvalidOperationException("TestOnlyThrowExceptionDuringInit");
                }

                config.CheckGatewayProviderSettings();

                var generation          = -SiloAddress.AllocateNewGeneration(); // Client generations are negative
                var gatewayListProvider = this.ServiceProvider.GetRequiredService <IGatewayListProvider>();
                gatewayListProvider.InitializeGatewayListProvider(cfg, LogManager.GetLogger(gatewayListProvider.GetType().Name))
                .WaitWithThrow(initTimeout);
                transport = ActivatorUtilities.CreateInstance <ProxiedMessageCenter>(this.ServiceProvider, localAddress, generation, handshakeClientId);

                if (StatisticsCollector.CollectThreadTimeTrackingStats)
                {
                    incomingMessagesThreadTimeTracking = new ThreadTrackingStatistic("ClientReceiver");
                }
            }
            catch (Exception exc)
            {
                if (logger != null)
                {
                    logger.Error(ErrorCode.Runtime_Error_100319, "OutsideRuntimeClient constructor failed.", exc);
                }
                ConstructorReset();
                throw;
            }
        }