Ejemplo n.º 1
0
        private T GetProbe <T>(IGrainIdentity identity, string grainClassNamePrefix)
            where T : IGrain
        {
            var key = GetKey(identity, typeof(T), grainClassNamePrefix);

            if (_probes.TryGetValue(key, out var grain))
            {
                return((T)grain);
            }

            //If using strict grain probes, throw the exception
            if (_options.StrictGrainProbes)
            {
                throw new Exception($"Probe {identity.IdentityString} does not exist for type {typeof(T).Name}. " +
                                    "Ensure that it is added before the grain is tested.");
            }
            else
            {
                if (_probeFactories.TryGetValue(typeof(T), out var factory))
                {
                    grain = factory(identity);
                }
                else
                {
                    var mock = Activator.CreateInstance(typeof(Mock <>).MakeGenericType(typeof(T))) as IMock <IGrain>;
                    grain = mock?.Object;
                }

                //Save the newly created grain for the next call
                _probes.Add(key, grain);
            }

            return((T)grain);
        }
 public LightstreamerDataService(IServiceProvider services, IGrainIdentity id, Silo silo, ILoggerFactory loggerFactory, IGrainFactory grainFactory) : base(id, silo, loggerFactory)
 {
     _grainFactory  = grainFactory;
     _services      = services;
     _loggerFactory = loggerFactory;
     _logger        = _loggerFactory.CreateLogger <LightstreamerDataService>();
 }
Ejemplo n.º 3
0
        public ChangeFeedService(
            CosmosDBStreamConfigurator configurator,
            IServiceProvider serviceProvider,
            IGrainIdentity id,
            IOptions <SiloOptions> siloOptions,
            Silo silo,
            ILoggerFactory loggerFactory)
            : base(id, silo, loggerFactory)
        {
            this._logger = loggerFactory.CreateLogger <ChangeFeedService>();

            var processor = new List <IProcessor>();

            foreach (var setting in configurator.Settings)
            {
                processor.Add(ChangeFeedProcessor.Create(
                                  setting.Key,
                                  siloOptions.Value.SiloName,
                                  setting.Value.Options,
                                  serviceProvider,
                                  TaskScheduler.Current,
                                  (IStreamMapper)ActivatorUtilities.CreateInstance(serviceProvider, setting.Value.MapperType)
                                  ));
            }

            this._processors = processor;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Grain implementers do NOT have to expose this constructor but can choose to do so.
 /// This constructor is particularly useful for unit testing where test code can create a Grain and replace
 /// the IGrainIdentity and IGrainRuntime with test doubles (mocks/stubs).
 /// </summary>
 /// <param name="identity"></param>
 /// <param name="runtime"></param>
 protected Grain(IGrainIdentity identity, IGrainRuntime runtime)
 {
     Identity        = identity;
     Runtime         = runtime;
     GrainFactory    = runtime.GrainFactory;
     ServiceProvider = runtime.ServiceProvider;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Grain implementers do NOT have to expose this constructor but can choose to do so.
 /// This constructor is particularly useful for unit testing where test code can create a Grain and replace
 /// the IGrainIdentity and IGrainRuntime with test doubles (mocks/stubs).
 /// </summary>
 /// <param name="identity"></param>
 /// <param name="runtime"></param>
 protected Grain(IGrainIdentity identity, IGrainRuntime runtime)
 {
     Identity = identity;
     Runtime = runtime;
     GrainFactory = runtime.GrainFactory;
     ServiceProvider = runtime.ServiceProvider;
 }
 public MyEventConsumerManagerGrain(
     IEnumerable <IEventConsumer> eventConsumers,
     IGrainIdentity identity,
     IGrainRuntime runtime)
     : base(eventConsumers, identity, runtime)
 {
 }
Ejemplo n.º 7
0
 public PlacementTarget(IGrainIdentity grainIdentity, Dictionary <string, object> requestContextData, int interfaceId, ushort interfaceVersion)
 {
     this.GrainIdentity      = grainIdentity;
     this.InterfaceId        = interfaceId;
     this.InterfaceVersion   = interfaceVersion;
     this.RequestContextData = requestContextData;
 }
 public StocksProxyGrainService(IServiceProvider serviceProvider,
                                IGrainIdentity id,
                                Silo silo,
                                ILoggerFactory loggerFactory,
                                IGrainFactory grainFactory)
 {
     GrainFactory = grainFactory;
 }
        /// <summary>Constructor to use for grain services</summary>
        protected GrainService(IGrainIdentity grainId, Silo silo, ILoggerFactory loggerFactory) : base((GrainId)grainId, silo.SiloAddress, lowPriority: true, loggerFactory: loggerFactory)
        {
            typeName = this.GetType().FullName;
            Logger   = loggerFactory.CreateLogger(typeName);

            scheduler = silo.LocalScheduler;
            ring      = silo.RingProvider;
            StoppedCancellationTokenSource = new CancellationTokenSource();
        }
Ejemplo n.º 10
0
        internal Mock <T> AddProbe <T>(IGrainIdentity identity, string classPrefix = null)
            where T : class, IGrain
        {
            var key  = GetKey(identity, typeof(T), classPrefix);
            var mock = new Mock <T>();

            _probes.Add(key, mock.Object);
            return(mock);
        }
Ejemplo n.º 11
0
        public LocalGrainPinner(IGrainFactory grainFactory, IOptions <LocalPinOptions> options,
                                IGrainIdentity id, Silo silo, ILoggerFactory loggerFactory)
            : base(id, silo, loggerFactory)
        {
            _grainFactory = grainFactory ?? throw new ArgumentNullException(nameof(grainFactory));
            _pinOptions   = options ?? throw new ArgumentNullException(nameof(options));

            _pinnedGrains = new List <ILocalPinnedGrain>();
        }
Ejemplo n.º 12
0
 public LightstreamerDataService(
     IServiceProvider services,
     IGrainIdentity id,
     Silo silo,
     ILoggerFactory loggerFactory,
     IGrainFactory grainFactory)
     : base(id, silo, loggerFactory)
 {
     GrainFactory = grainFactory;
 }
Ejemplo n.º 13
0
        protected EventConsumerManagerGrain(
            IEnumerable <IEventConsumer> eventConsumers,
            IGrainIdentity identity,
            IGrainRuntime runtime)
            : base(identity, runtime)
        {
            Guard.NotNull(eventConsumers, nameof(eventConsumers));

            this.eventConsumers = eventConsumers;
        }
        public IStorage AddStorage <T>(IGrainIdentity identity) where T : Grain
        {
            var key = GetKey(identity.IdentityString, typeof(T));

            var storage = new TestStorage();

            _storages.Add(key, storage);

            return(storage);
        }
Ejemplo n.º 15
0
        /// <summary>Constructor to use for grain services</summary>
        protected GrainService(IGrainIdentity grainId, Silo silo, IGrainServiceConfiguration config) : base((GrainId)grainId, silo.SiloAddress, lowPriority: true)
        {
            typeName = this.GetType().FullName;
            Logger   = LogManager.GetLogger(typeName);

            scheduler = silo.LocalScheduler;
            ring      = silo.RingProvider;
            StoppedCancellationTokenSource = new CancellationTokenSource();
            Config = config;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Create a new instance of a grain
        /// </summary>
        /// <param name="grainType"></param>
        /// <param name="identity">Identity for the new grain</param>
        /// <returns></returns>
        public Grain CreateGrainInstance(Type grainType, IGrainIdentity identity)
        {
            var activator = _typeActivatorCache.GetOrAdd(grainType, _createFactory);
            var grain = (Grain)activator(_services, arguments: null);

            // Inject runtime hooks into grain instance
            grain.Runtime = _grainRuntime;
            grain.Identity = identity;

            return grain;
        }
Ejemplo n.º 17
0
 public SchedulerService(
     IGrainIdentity id,
     Silo silo,
     ILoggerFactory loggerFactory,
     IClusterClient clusterClient)
     : base(id, silo, loggerFactory)
 {
     grainId = id.IdentityString;
     logger  = loggerFactory.CreateLogger(ConstLogging.JobFacCategorySchedulerService);
     client  = clusterClient;
 }
Ejemplo n.º 18
0
 private static string GetKey(IGrainIdentity identity, Type stateType, string classPrefix = null)
 {
     if (classPrefix == null)
     {
         return($"{stateType.FullName}-{identity.IdentityString}");
     }
     else
     {
         return($"{stateType.FullName}-{classPrefix}-{identity.IdentityString}");
     }
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Create a new instance of a grain
        /// </summary>
        /// <param name="grainType">The grain type.</param>
        /// <param name="identity">Identity for the new grain</param>
        /// <returns>The newly created grain.</returns>
        public Grain CreateGrainInstance(Type grainType, IGrainIdentity identity)
        {
            var activator = this.typeActivatorCache.GetOrAdd(grainType, this.createFactory);
            var grain     = (Grain)activator(this.services, arguments: null);

            // Inject runtime hooks into grain instance
            grain.Runtime  = this.grainRuntime.Value;
            grain.Identity = identity;

            return(grain);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Create a new instance of a grain
        /// </summary>
        /// <param name="grainType"></param>
        /// <param name="identity">Identity for the new grain</param>
        /// <returns></returns>
        public Grain CreateGrainInstance(Type grainType, IGrainIdentity identity)
        {
            var grain = _services != null
                ? (Grain) _services.GetService(grainType)
                : (Grain) Activator.CreateInstance(grainType);

            // Inject runtime hooks into grain instance
            grain.Runtime = _grainRuntime;
            grain.Identity = identity;

            return grain;
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Create a new instance of a grain
        /// </summary>
        /// <param name="grainType"></param>
        /// <param name="identity">Identity for the new grain</param>
        /// <returns></returns>
        public Grain CreateGrainInstance(Type grainType, IGrainIdentity identity)
        {
            var grain = _services != null
                ? (Grain)_services.GetService(grainType)
                : (Grain)Activator.CreateInstance(grainType);

            // Inject runtime hooks into grain instance
            grain.Runtime  = _grainRuntime;
            grain.Identity = identity;

            return(grain);
        }
Ejemplo n.º 22
0
 public DeviceMonitorService(IServiceProvider services, IGrainIdentity id, Silo silo,
                             Microsoft.Extensions.Logging.ILoggerFactory loggerFactory,
                             IGrainFactory grainFactory, MsvClientCtrlSDK msv, RestClient client)
     : base(id, silo, loggerFactory)
 {
     Logger = Sobey.Core.Log.LoggerManager.GetLogger("MonitorService");
     _lstTimerScheduleDevice = new List <ChannelInfo>();
     _timer        = null;
     _msvClient    = msv;
     _restClient   = client;
     _grainFactory = grainFactory;
     _grainKey     = string.Empty;
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Constructor. Grain implementers do not have to expose this
        /// constructor but can choose to do so. This constructor is
        /// particularly useful for unit testing where test code can
        /// create a Grain and replace the IGrainIdentity and IGrainRuntime
        /// with test doubles.
        /// </summary>
        /// <param name="identity">IGrainIdentity</param>
        /// <param name="runtime">IGrainRuntime</param>
        protected Grain(IGrainIdentity identity, IGrainRuntime runtime)
        {
            this.Identity = identity;
            this.Runtime  = runtime;
            if (StreamProviderDictionaryMachineId == null)
            {
                StreamProviderDictionaryMachineId = ActorModel.Runtime.CreateMachine(
                    typeof(StreamProviderDictionaryMachine), "StreamProviderDictionaryMachine");
            }

            ActorModel.RegisterCleanUpAction(() =>
            {
                StreamProviderDictionaryMachineId = null;
            });
        }
Ejemplo n.º 24
0
        private T GetProbe <T>(IGrainIdentity identity, string grainClassNamePrefix) where T : IGrain
        {
            if (grainClassNamePrefix != null)
            {
                throw new ArgumentException($"Class prefix not supported in {nameof(TestGrainFactory)}",
                                            $"{nameof(grainClassNamePrefix)}");
            }

            var key = GetKey(identity, typeof(T));

            IGrain grain;

            if (_probes.TryGetValue(key, out grain))
            {
                return((T)grain);
            }

            //If using strict grain probes, throw the exception
            if (_options.StrictGrainProbes)
            {
                throw new Exception(
                          $"Probe {identity.IdentityString} does not exist for type {typeof(T).Name}. Ensure that it is added before the grain is tested.");
            }
            else
            {
                IMock <IGrain> mock;
                Func <IGrainIdentity, IMock <IGrain> > factory;

                if (_probeFactories.TryGetValue(typeof(T), out factory))
                {
                    mock = factory(identity);
                }
                else
                {
                    mock = Activator.CreateInstance(typeof(Mock <>).MakeGenericType(typeof(T))) as IMock <IGrain>;
                }

                grain = mock?.Object;

                //Save the newly created grain for the next call
                _probes.Add(key, grain);
            }

            return((T)grain);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Create a new instance of a grain
        /// </summary>
        /// <param name="grainType"></param>
        /// <param name="identity">Identity for the new grain</param>
        /// <param name="stateType">If the grain is a stateful grain, the type of the state it persists.</param>
        /// <param name="storage">If the grain is a stateful grain, the storage used to persist the state.</param>
        /// <returns></returns>
        public Grain CreateGrainInstance(Type grainType, IGrainIdentity identity, Type stateType, IStorage storage)
        {
            //Create a new instance of the grain
            var grain = CreateGrainInstance(grainType, identity);

            var statefulGrain = grain as IStatefulGrain;

            if (statefulGrain == null)
            {
                return(grain);
            }

            //Inject state and storage data into the grain
            statefulGrain.GrainState.State = Activator.CreateInstance(stateType);
            statefulGrain.SetStorage(storage);

            return(grain);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Create a new instance of a grain
        /// </summary>
        /// <param name="grainType"></param>
        /// <param name="identity">Identity for the new grain</param>
        /// <returns></returns>
        public Grain CreateGrainInstance(Type grainType, IGrainIdentity identity, Type stateType,
            IStorageProvider storageProvider)
        {
            //Create a new instance of the grain
            var grain = CreateGrainInstance(grainType, identity);

            var statefulGrain = grain as IStatefulGrain;

            if (statefulGrain == null)
                return grain;

            var storage = new GrainStateStorageBridge(grainType.FullName, statefulGrain, storageProvider);

            //Inject state and storage data into the grain
            statefulGrain.GrainState.State = Activator.CreateInstance(stateType);
            statefulGrain.SetStorage(storage);

            return grain;
        }
Ejemplo n.º 27
0
        private async Task <T> CreateGrainAsync <T>(IGrainIdentity identity) where T : Grain
        {
            if (_isGrainCreated)
            {
                throw new Exception(
                          "A grain has already been created in this silo. Only 1 grain per test silo should every be created. Add grain probes for supporting grains.");
            }

            _isGrainCreated = true;

            Grain grain;

            var grainContext = new TestGrainActivationContext
            {
                ActivationServices  = ServiceProvider,
                GrainIdentity       = identity,
                GrainType           = typeof(T),
                ObservableLifecycle = _grainLifecycle,
            };

            //Create a stateless grain
            grain = _grainCreator.CreateGrainInstance(grainContext) as T;

            if (grain == null)
            {
                throw new Exception($"Unable to instantiate grain {typeof(T)} properly");
            }

            //Check if there are any reminders for this grain
            var remindable = grain as IRemindable;

            //Set the reminder target
            if (remindable != null)
            {
                ReminderRegistry.SetGrainTarget(remindable);
            }

            //Trigger the lifecycle hook that will get the grain's state from the runtime
            await _grainLifecycle.TriggerStartAsync();

            return(grain as T);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Create a new instance of a grain
        /// </summary>
        /// <param name="grainType">The grain type.</param>
        /// <param name="identity">Identity for the new grain</param>
        /// <param name="stateType">If the grain is a stateful grain, the type of the state it persists.</param>
        /// <param name="storageProvider">If the grain is a stateful grain, the storage provider used to persist the state.</param>
        /// <returns>The newly created grain.</returns>
        public Grain CreateGrainInstance(Type grainType, IGrainIdentity identity, Type stateType, IStorageProvider storageProvider)
        {
            // Create a new instance of the grain
            var grain = this.CreateGrainInstance(grainType, identity);

            var statefulGrain = grain as IStatefulGrain;

            if (statefulGrain == null)
            {
                return(grain);
            }

            var storage = new GrainStateStorageBridge(grainType.FullName, statefulGrain, storageProvider);

            //Inject state and storage data into the grain
            statefulGrain.GrainState.State = Activator.CreateInstance(stateType);
            statefulGrain.SetStorage(storage);

            return(grain);
        }
Ejemplo n.º 29
0
        private async Task <T> CreateGrainAsync <T>(IGrainIdentity identity)
            where T : Grain
        {
            if (_isGrainCreated)
            {
                throw new Exception("A grain has already been created in this silo. Only 1 grain per test silo should ever be created. Add grain probes for supporting grains.");
            }

            // Add state attribute mapping for storage facets
            this.AddService <IAttributeToFactoryMapper <PersistentStateAttribute> >(StorageManager.stateAttributeFactoryMapper);

            _isGrainCreated = true;
            Grain grain;
            var   grainContext = new TestGrainActivationContext
            {
                ActivationServices  = ServiceProvider,
                GrainIdentity       = identity,
                GrainType           = typeof(T),
                ObservableLifecycle = _grainLifecycle,
            };

            //Create a stateless grain
            grain = _grainCreator.CreateGrainInstance(grainContext) as T;
            if (grain == null)
            {
                throw new Exception($"Unable to instantiate grain {typeof(T)} properly");
            }

            //Check if there are any reminders for this grain and set the reminder target
            if (grain is IRemindable remindable)
            {
                ReminderRegistry.SetGrainTarget(remindable);
            }

            //Trigger the lifecycle hook that will get the grain's state from the runtime
            await _grainLifecycle.TriggerStartAsync().ConfigureAwait(false);

            return(grain as T);
        }
Ejemplo n.º 30
0
        private static T CreateGrain <T>(IGrainIdentity identity) where T : Grain
        {
            Grain grain;

            //Check to see if the grain is stateful
            if (IsSubclassOfRawGeneric(typeof(Grain <>), typeof(T)))
            {
                var grainGenericArgs = typeof(T).BaseType?.GenericTypeArguments;

                if (grainGenericArgs == null || grainGenericArgs.Length == 0)
                {
                    throw new Exception($"Unable to get grain state type info for {typeof(T)}");
                }

                //Get the state type
                var stateType = grainGenericArgs[0];

                //Create a new stateful grain
                grain = GrainCreator.CreateGrainInstance(typeof(T), identity, stateType, StorageProvider);
            }
            else
            {
                //Create a stateless grain
                grain = GrainCreator.CreateGrainInstance(typeof(T), identity) as T;
            }

            if (grain == null)
            {
                return(null);
            }

            //Emulate the grain's lifecycle
            grain.OnActivateAsync();

            return(grain as T);
        }
Ejemplo n.º 31
0
 /// <summary>
 /// Grain implementers do NOT have to expose this constructor but can choose to do so.
 /// This constructor is particularly useful for unit testing where test code can create a Grain and replace
 /// the IGrainIdentity and IGrainRuntime with test doubles (mocks/stubs).
 /// </summary>
 /// <param name="identity"></param>
 /// <param name="runtime"></param>
 protected Grain(IGrainIdentity identity, IGrainRuntime runtime)
 {
     Identity = identity;
     Runtime  = runtime;
 }
Ejemplo n.º 32
0
 /// <summary>
 /// This constructor is particularly useful for unit testing where test code can create a Grain and replace
 /// the IGrainIdentity, IGrainRuntime and State with test doubles (mocks/stubs).
 /// </summary>
 protected LogConsistentGrain(IGrainIdentity identity, IGrainRuntime runtime)
     : base(identity, runtime)
 {
 }
Ejemplo n.º 33
0
 /// <summary>
 /// Grain implementers do NOT have to expose this constructor but can choose to do so.
 /// This constructor is particularly useful for unit testing where test code can create a Grain and replace
 /// the IGrainIdentity and IGrainRuntime with test doubles (mocks/stubs).
 /// </summary>
 /// <param name="identity"></param>
 /// <param name="runtime"></param>
 protected Grain(IGrainIdentity identity, IGrainRuntime runtime)
 {
     Identity = identity;
     Runtime = runtime;
     GrainFactory = runtime.GrainFactory;
 }
Ejemplo n.º 34
0
 /// <summary>
 /// Grain implementers do NOT have to expose this constructor but can choose to do so.
 /// This constructor is particularly useful for unit testing where test code can create a Grain and replace
 /// the IGrainIdentity and IGrainRuntime with test doubles (mocks/stubs).
 /// </summary>
 /// <param name="identity"></param>
 /// <param name="runtime"></param>
 protected Grain(IGrainIdentity identity, IGrainRuntime runtime)
 {
     Identity = identity;
     Runtime = runtime;
 }
Ejemplo n.º 35
0
 public static NonReentrentStressGrainWithoutState Create(IGrainIdentity identity, IGrainRuntime runtime)
 => new NonReentrentStressGrainWithoutState(identity, runtime);
Ejemplo n.º 36
0
 private NonReentrentStressGrainWithoutState(IGrainIdentity identity, IGrainRuntime runtime)
     : base(identity, runtime)
 {
 }
Ejemplo n.º 37
0
 internal ClientNotAvailableException(IGrainIdentity clientId) : base("No activation for client " + clientId) { }