Esempio 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>();
 }
Esempio 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;
        }
Esempio 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;
 }
Esempio 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)
 {
 }
Esempio 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();
        }
Esempio 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);
        }
Esempio 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>();
        }
Esempio n. 12
0
 public LightstreamerDataService(
     IServiceProvider services,
     IGrainIdentity id,
     Silo silo,
     ILoggerFactory loggerFactory,
     IGrainFactory grainFactory)
     : base(id, silo, loggerFactory)
 {
     GrainFactory = grainFactory;
 }
Esempio 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);
        }
Esempio 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;
        }
Esempio 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;
        }
Esempio 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;
 }
Esempio 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}");
     }
 }
Esempio 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);
        }
Esempio 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;
        }
Esempio 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);
        }
Esempio 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;
 }
Esempio 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;
            });
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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;
        }
        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);
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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;
 }
Esempio 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)
 {
 }
Esempio 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;
 }
Esempio 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;
 }
Esempio n. 35
0
 public static NonReentrentStressGrainWithoutState Create(IGrainIdentity identity, IGrainRuntime runtime)
 => new NonReentrentStressGrainWithoutState(identity, runtime);
Esempio n. 36
0
 private NonReentrentStressGrainWithoutState(IGrainIdentity identity, IGrainRuntime runtime)
     : base(identity, runtime)
 {
 }
Esempio n. 37
0
 internal ClientNotAvailableException(IGrainIdentity clientId) : base("No activation for client " + clientId) { }