Exemplo n.º 1
0
 public TransactionalStateStorageProviderWrapper(IGrainStorage grainStorage, IGrainActivationContext context, ILoggerFactory loggerFactory)
 {
     this.grainStorage  = grainStorage;
     this.context       = context;
     this.loggerFactory = loggerFactory;
     this.stateStorages = new ConcurrentDictionary <string, IStorage <TransactionalStateRecord <TState> > >();
 }
Exemplo n.º 2
0
        public IStorage <TGrainState> GetStorage <TGrainState>(Grain grain) where TGrainState : new()
        {
            IGrainStorage grainStorage  = grain.GetGrainStorage(ServiceProvider);
            string        grainTypeName = grain.GetType().FullName;

            return(new StateStorageBridge <TGrainState>(grainTypeName, grain.GrainReference, grainStorage, this.loggerFactory));
        }
        public GrainStorageFixture()
        {
            var services = new ServiceCollection();

            services

            // Entity framework
            .AddEntityFrameworkInMemoryDatabase()
            .AddDbContextPool <TestDbContext>(builder =>
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            })
            // Orleans stuff
            .AddSingleton <ITypeResolver, TypeResolver>()

            .ConfigureGrainStorageOptions <TestDbContext, ConfiguredGrainWithCustomGuidKey,
                                           ConfiguredEntityWithCustomGuidKey>(
                options =>
            {
                options
                .UseKey(entity => entity.CustomKey);
            })

            // Storage
            .AddEfGrainStorage <TestDbContext>()
            .AddSingleton <IGrainStorage, EntityFrameworkGrainStorage <TestDbContext> >()
            .AddSingleton <IGrainStorageConvention, TestGrainStorageConvention>()
            .AddSingleton <IEntityTypeResolver, TestEntityTypeResolver>()

            .ConfigureGrainStorageOptions <TestDbContext, ConfiguredGrainWithCustomGuidKey2,
                                           ConfiguredEntityWithCustomGuidKey>(
                options => options
                .UseKey(entity => entity.CustomKey)
                .UseKeyExt(entity => entity.CustomKeyExt))

            .ConfigureGrainStorageOptions <TestDbContext, InvalidConfiguredGrainWithGuidKey,
                                           InvalidConfiguredEntityWithCustomGuidKey>(
                options => options
                .UseKey(entity => entity.CustomKey)
                .UseKeyExt(entity => entity.CustomKeyExt))

            .Configure <GrainStorageConventionOptions>(options =>
            {
                options.DefaultGrainKeyPropertyName         = nameof(EntityWithGuidKey.Id);
                options.DefaultGrainKeyExtPropertyName      = nameof(EntityWithGuidKey.KeyExt);
                options.DefaultPersistenceCheckPropertyName = nameof(EntityWithGuidKey.IsPersisted);
            });

            ServiceProvider = services.BuildServiceProvider();

            Storage = ServiceProvider.GetRequiredService <IGrainStorage>();


            using (IServiceScope scope = ServiceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <TestDbContext>();
                // this is required to make sure data are seeded
                context.Database.EnsureCreated();
            }
        }
Exemplo n.º 4
0
        public StateStorageBridge(string name, GrainReference grainRef, IGrainStorage store, ILoggerFactory loggerFactory)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (grainRef == null)
            {
                throw new ArgumentNullException(nameof(grainRef));
            }
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            this.logger     = loggerFactory.CreateLogger(store.GetType().FullName);
            this.name       = name;
            this.grainRef   = grainRef;
            this.store      = store;
            this.grainState = new GrainState <TState>(new TState());
        }
Exemplo n.º 5
0
        private async Task Test_PersistenceProvider_Read(string grainTypeName, IGrainStorage store,
                                                         GrainState <TestStoreGrainState> grainState = null, GrainId grainId = default)
        {
            var reference = (GrainReference)this.fixture.InternalGrainFactory.GetGrain(grainId.IsDefault ? (GrainId)LegacyGrainId.NewId() : grainId);

            if (grainState == null)
            {
                grainState = new GrainState <TestStoreGrainState>(new TestStoreGrainState());
            }
            var storedGrainState = new GrainState <TestStoreGrainState>(new TestStoreGrainState());

            Stopwatch sw = new Stopwatch();

            sw.Start();

            await store.ReadStateAsync(grainTypeName, reference, storedGrainState);

            TimeSpan readTime = sw.Elapsed;

            this.output.WriteLine("{0} - Read time = {1}", store.GetType().FullName, readTime);

            var storedState = storedGrainState.State;

            Assert.Equal(grainState.State.A, storedState.A);
            Assert.Equal(grainState.State.B, storedState.B);
            Assert.Equal(grainState.State.C, storedState.C);
        }
 public TransactionalStateStorageProviderWrapper(IGrainStorage grainStorage, IGrainActivationContext context)
 {
     this.grainStorage  = grainStorage;
     this.context       = context;
     this.loggerFactory = context.ActivationServices.GetRequiredService <ILoggerFactory>();
     this.stateStorages = new ConcurrentDictionary <string, IStorage <TransactionalStateRecord <TState> > >();
 }
 public StateStorageBridgeActivator(string name, ClusterActorSystem system, IGrainStorage storage, ILoggerFactory loggerFactory)
 {
     this.name          = name;
     this.system        = system;
     this.storage       = storage;
     this.loggerFactory = loggerFactory;
 }
        public Task <string> CheckProviderType()
        {
            IGrainStorage grainStorage = this.GetGrainStorage(this.ServiceProvider);

            Assert.NotNull(grainStorage);
            return(Task.FromResult(grainStorage.GetType().FullName));
        }
Exemplo n.º 9
0
 public TransactionalStateStorageProviderWrapper(IGrainStorage grainStorage, string stateName, IGrainContext context, ILoggerFactory loggerFactory)
 {
     this.grainStorage  = grainStorage;
     this.context       = context;
     this.loggerFactory = loggerFactory;
     this.stateName     = stateName;
 }
        public ITransactionalStateStorage <TState> Create <TState>(string storageName)
            where TState : class, new()
        {
            // Try to get ITransactionalStateStorage from factory
            ITransactionalStateStorageFactory factory = string.IsNullOrEmpty(storageName)
                ? this.context.ActivationServices.GetService <ITransactionalStateStorageFactory>()
                : this.context.ActivationServices.GetServiceByName <ITransactionalStateStorageFactory>(storageName);

            if (factory != null)
            {
                return(factory.Create <TState>());
            }

            // Else try to get storage provider and wrap it
            IGrainStorage grainStorage = string.IsNullOrEmpty(storageName)
                ? this.context.ActivationServices.GetService <IGrainStorage>()
                : this.context.ActivationServices.GetServiceByName <IGrainStorage>(storageName);

            if (grainStorage != null)
            {
                return(new TransactionalStateStorageProviderWrapper <TState>(grainStorage, context));
            }

            throw (string.IsNullOrEmpty(storageName))
                ? new InvalidOperationException($"No default {nameof(ITransactionalStateStorageFactory)} nor {nameof(IStorageProvider)} was found while attempting to create transactional state storage.")
                : new InvalidOperationException($"No {nameof(ITransactionalStateStorageFactory)} nor {nameof(IStorageProvider)} with the name {storageName} was found while attempting to create transactional state storage.");
        }
Exemplo n.º 11
0
 /// <summary>
 /// Default constructor which creates the decorated storage provider
 /// </summary>
 public FaultInjectionGrainStorage(IGrainStorage realStorageProvider, string name, ILoggerFactory loggerFactory,
                                   IGrainFactory grainFactory, FaultInjectionGrainStorageOptions faultInjectionOptions)
 {
     this.realStorageProvider = realStorageProvider;
     this.logger       = loggerFactory.CreateLogger($"{this.GetType().FullName}.{name}");
     this.grainFactory = grainFactory;
     this.options      = faultInjectionOptions;
 }
Exemplo n.º 12
0
        private void InstallLogViewAdaptor(
            Factory <Grain, ILogConsistencyProtocolServices> protocolServicesFactory,
            ILogViewAdaptorFactory factory,
            IGrainStorage grainStorage)
        {
            // encapsulate runtime services used by consistency adaptors
            ILogConsistencyProtocolServices svc = protocolServicesFactory(this);

            TView state = (TView)Activator.CreateInstance(typeof(TView));

            this.InstallAdaptor(factory, state, this.GetType().FullName, grainStorage, svc);
        }
Exemplo n.º 13
0
        public IStorage <TGrainState> GetStorage <TGrainState>(IGrainContext grainContext)
        {
            if (grainContext is null)
            {
                throw new ArgumentNullException(nameof(grainContext));
            }
            var           grainType     = grainContext.GrainInstance?.GetType() ?? throw new ArgumentNullException(nameof(IGrainContext.GrainInstance));
            IGrainStorage grainStorage  = GrainStorageHelpers.GetGrainStorage(grainType, ServiceProvider);
            string        grainTypeName = grainContext.GrainInstance.GetType().FullName;

            return(new StateStorageBridge <TGrainState>(grainTypeName, grainContext.GrainReference, grainStorage, this.loggerFactory));
        }
Exemplo n.º 14
0
        public GrainStorageFixture()
        {
            var services = new ServiceCollection();

            services
            .AddEntityFrameworkInMemoryDatabase()
            .AddDbContextPool <TestDbContext>(builder =>
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            })

            .AddSingleton <IGrainStorageConvention, GrainStorageConvention>()
            // Simple key grains with default key properties on state model
            // should work out of the box without configuration.

            .ConfigureGrainStorageOptions <TestDbContext, ConfiguredGrainWithCustomGuidKey,
                                           ConfiguredEntityWithCustomGuidKey>(
                options => options
                .UseKey(entity => entity.CustomKey))

            .ConfigureGrainStorageOptions <TestDbContext, ConfiguredGrainWithCustomGuidKey2,
                                           ConfiguredEntityWithCustomGuidKey>(
                options => options
                .UseKey(entity => entity.CustomKey)
                .UseKeyExt(entity => entity.CustomKeyExt))

            .ConfigureGrainStorageOptions <TestDbContext, InvalidConfiguredGrainWithGuidKey,
                                           InvalidConfiguredEntityWithCustomGuidKey>(
                options => options
                .UseKey(entity => entity.CustomKey)
                .UseKeyExt(entity => entity.CustomKeyExt))

            .AddSingleton <IGrainStorage, EntityFrameworkGrainStorage <TestDbContext> >()
            .Configure <GrainStorageConventionOptions>(options =>
            {
                options.DefaultGrainKeyPropertyName         = nameof(EntityWithGuidKey.Id);
                options.DefaultGrainKeyExtPropertyName      = nameof(EntityWithGuidKey.KeyExt);
                options.DefaultPersistenceCheckPropertyName = nameof(EntityWithGuidKey.IsPersisted);
            });

            ServiceProvider = services.BuildServiceProvider();

            Storage = ServiceProvider.GetRequiredService <IGrainStorage>();


            using (IServiceScope scope = ServiceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <TestDbContext>();
                // this is required to make sure data are seeded
                context.Database.EnsureCreated();
            }
        }
        /// <summary>
        /// Aquire the storage provider associated with the grain type.
        /// </summary>
        /// <returns></returns>
        public static IGrainStorage GetGrainStorage(this Type grainType, IServiceProvider services)
        {
            var           attr            = grainType.GetCustomAttributes <StorageProviderAttribute>(true).FirstOrDefault();
            IGrainStorage storageProvider = attr != null
                ? services.GetServiceByName <IGrainStorage>(attr.ProviderName)
                : services.GetService <IGrainStorage>();

            if (storageProvider == null)
            {
                ThrowMissingProviderException(grainType, attr?.ProviderName);
            }
            return(storageProvider);
        }
        private Task OnSetupState(CancellationToken ct)
        {
            if (ct.IsCancellationRequested)
            {
                return(Task.CompletedTask);
            }
            IGrainActivationContext activationContext = this.ServiceProvider.GetRequiredService <IGrainActivationContext>();
            Factory <Grain, IMultiClusterRegistrationStrategy, ILogConsistencyProtocolServices> protocolServicesFactory = this.ServiceProvider.GetRequiredService <Factory <Grain, IMultiClusterRegistrationStrategy, ILogConsistencyProtocolServices> >();
            ILogViewAdaptorFactory consistencyProvider = SetupLogConsistencyProvider(activationContext);
            IGrainStorage          grainStorage        = consistencyProvider.UsesStorageProvider ? this.GetGrainStorage(this.ServiceProvider) : null;

            InstallLogViewAdaptor(activationContext.RegistrationStrategy, protocolServicesFactory, consistencyProvider, grainStorage);
            return(Task.CompletedTask);
        }
 private void EnsureGrainStorage()
 {
     if (_grainStorage == null)
     {
         var implementation = TypeCodeMapper.GetImplementation(this.SiloIndexManager.GrainTypeResolver, typeof(V));
         if (implementation == null || (_grainClassName = implementation.GrainClass) == null ||
             !this.SiloIndexManager.CachedTypeResolver.TryResolveType(_grainClassName, out Type grainClassType))
         {
             throw new IndexException($"The grain implementation class {implementation.GrainClass} for grain" +
                                      " interface {IndexUtils.GetFullTypeName(typeof(V))} was not resolved.");
         }
         _grainStorage = grainClassType.GetGrainStorage(this.SiloIndexManager.ServiceProvider);
     }
 }
Exemplo n.º 18
0
        private Task OnSetupState(CancellationToken ct)
        {
            if (ct.IsCancellationRequested)
            {
                return(Task.CompletedTask);
            }
            IGrainContextAccessor grainContextAccessor = this.ServiceProvider.GetRequiredService <IGrainContextAccessor>();
            Factory <IGrainContext, ILogConsistencyProtocolServices> protocolServicesFactory = this.ServiceProvider.GetRequiredService <Factory <IGrainContext, ILogConsistencyProtocolServices> >();
            var grainContext = grainContextAccessor.GrainContext;
            ILogViewAdaptorFactory consistencyProvider = SetupLogConsistencyProvider(grainContext);
            IGrainStorage          grainStorage        = consistencyProvider.UsesStorageProvider ? GrainStorageHelpers.GetGrainStorage(grainContext?.GrainInstance.GetType(), this.ServiceProvider) : null;

            InstallLogViewAdaptor(grainContext, protocolServicesFactory, consistencyProvider, grainStorage);
            return(Task.CompletedTask);
        }
 private async Task EnsureStorage()
 {
     if (this.StorageProvider == null)
     {
         using (await _writeLock.LockAsync())
         {
             if (this.StorageProvider == null)   // Make sure another thread didn't get it
             {
                 var readGrainReference = this._recoveryGrainReference ?? _lazyParent.Value;
                 this.StorageProvider = typeof(IndexWorkflowQueueGrainService).GetGrainStorage(this.SiloIndexManager.ServiceProvider);
                 await this.StorageProvider.ReadStateAsync(_grainTypeName, readGrainReference, this.queueState);
             }
         }
     }
 }
Exemplo n.º 20
0
        public IPersistentState <TState> Create <TState>(IGrainContext context, IPersistentStateConfiguration cfg)
        {
            IGrainStorage storageProvider = !string.IsNullOrWhiteSpace(cfg.StorageName)
                ? context.ActivationServices.GetServiceByName <IGrainStorage>(cfg.StorageName)
                : context.ActivationServices.GetService <IGrainStorage>();

            if (storageProvider == null)
            {
                ThrowMissingProviderException(context, cfg);
            }
            string fullStateName = GetFullStateName(context, cfg);
            var    bridge        = new PersistentStateBridge <TState>(fullStateName, context, storageProvider);

            bridge.Participate(context.ObservableLifecycle);
            return(bridge);
        }
Exemplo n.º 21
0
 private void EnsureGrainStorage()
 {
     if (_grainStorage == null)
     {
         var implementation = TypeCodeMapper.GetImplementation(this.SiloIndexManager.GrainTypeResolver, typeof(V));
         if (implementation == null || (grainImplClass = implementation.GrainClass) == null ||
             !this.SiloIndexManager.CachedTypeResolver.TryResolveType(grainImplClass, out Type implType))
         {
             throw new IndexException($"The grain implementation class {implementation.GrainClass} for grain" +
                                      " interface {IndexUtils.GetFullTypeName(typeof(V))} was not resolved.");
         }
         _grainStorage = implType.GetGrainStorage(this.SiloIndexManager.ServiceProvider);
         bool isFaultTolerant = ApplicationPartsIndexableGrainLoader.IsSubclassOfRawGenericType(typeof(IndexableGrain <,>), implType);
         _indexedFieldPrefix = isFaultTolerant ? "UserState." : string.Empty;
     }
 }
Exemplo n.º 22
0
 public ILogViewAdaptor <TLogView, TLogEntry> MakeLogViewAdaptor <TLogView, TLogEntry>(
     ILogViewAdaptorHost <TLogView, TLogEntry> hostGrain,
     TLogView initialState,
     string grainTypeName,
     IGrainStorage grainStorage,
     ILogConsistencyProtocolServices services)
     where TLogView : class, new()
     where TLogEntry : class
 {
     return(new LogViewAdaptor <TLogView, TLogEntry>(
                hostGrain,
                initialState,
                grainStorage,
                grainTypeName,
                _snapshotStrategy,
                services,
                _options.UseIndependentEventStorage,
                _eventStorage));
 }
Exemplo n.º 23
0
        public GrainStorageFixture()
        {
            var services = new ServiceCollection();

            services
            .AddLogging(logging => logging
                        .AddConsole()
                        .SetMinimumLevel(LogLevel.Trace)
                        )

            // Entity framework
            .AddEntityFrameworkInMemoryDatabase()
            .AddDbContextPool <TestDbContext>(builder =>
            {
                builder.UseInMemoryDatabase(Guid.NewGuid().ToString());
                builder.EnableSensitiveDataLogging();
            })
            // Orleans stuff
            .AddSingleton <ITypeResolver, TypeResolver>()
            // Storage
            .AddEfGrainStorage <TestDbContext>()
            .AddSingleton <IGrainStorage, EntityFrameworkGrainStorage <TestDbContext> >()
            .AddSingleton <IGrainStorageConvention, TestGrainStorageConvention>()
            .AddSingleton <IEntityTypeResolver, TestEntityTypeResolver>();


            ConfigureGrainStorage(services);

            ServiceProvider = services.BuildServiceProvider();

            Storage = ServiceProvider.GetRequiredService <IGrainStorage>();


            using (IServiceScope scope = ServiceProvider.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <TestDbContext>();
                // this is required to make sure data are seeded
                context.Database.EnsureCreated();
            }
        }
Exemplo n.º 24
0
        internal IndexWorkflowQueueBase(SiloIndexManager siloIndexManager, Type grainInterfaceType, int queueSequenceNumber, SiloAddress silo,
                                        bool isDefinedAsFaultTolerantGrain, Func <GrainReference> parentFunc)
        {
            queueState          = new IndexWorkflowQueueState(silo);
            _grainInterfaceType = grainInterfaceType;
            _queueSeqNum        = queueSequenceNumber;

            _workflowRecordsTail = null;
            __grainStorage       = null;
            __handler            = null;
            _isHandlerWorkerIdle = 1;

            _isDefinedAsFaultTolerantGrain = isDefinedAsFaultTolerantGrain;

            _writeLock            = new AsyncLock();
            _writeRequestIdGen    = 0;
            _pendingWriteRequests = new HashSet <int>();

            _silo            = silo;
            SiloIndexManager = siloIndexManager;
            _lazyParent      = new Lazy <GrainReference>(parentFunc, true);
        }
        public LogViewAdaptor(
            ILogViewAdaptorHost <TLogView, TLogEntry> host,
            TLogView initialState,
            IGrainStorage grainStorage,
            string grainTypeName,
            Func <SnapshotStrategyInfo, bool> snapshotStrategy,
            ILogConsistencyProtocolServices services,
            bool useIndependentEventStorage,
            IGrainEventStorage eventStorage)
            : base(host, initialState, services)
        {
            _grainStorage               = grainStorage;
            _grainTypeName              = grainTypeName;
            _snapshotStrategy           = snapshotStrategy;
            _useIndependentEventStorage = useIndependentEventStorage;

            if (useIndependentEventStorage)
            {
                _eventStorage = eventStorage
                                ?? throw new ArgumentNullException(nameof(eventStorage),
                                                                   "Must set eventStorage when useIndependentEventStorage is true");
            }
        }
 public GrainStorageUpdateTests(GrainStorageFixture storageFixture)
 {
     _storage         = storageFixture.Storage;
     _serviceProvider = storageFixture.ServiceProvider;
 }
Exemplo n.º 27
0
 /// <summary>
 /// Make log view adaptor
 /// </summary>
 /// <typeparam name="TView">The type of the view</typeparam>
 /// <typeparam name="TEntry">The type of the log entries</typeparam>
 /// <param name="hostGrain">The grain that is hosting this adaptor</param>
 /// <param name="initialState">The initial state for this view</param>
 /// <param name="grainTypeName">The type name of the grain</param>
 /// <param name="grainStorage">Storage provider</param>
 /// <param name="services">Runtime services for multi-cluster coherence protocols</param>
 public ILogViewAdaptor <TView, TEntry> MakeLogViewAdaptor <TView, TEntry>(ILogViewAdaptorHost <TView, TEntry> hostGrain, TView initialState, string grainTypeName, IGrainStorage grainStorage, ILogConsistencyProtocolServices services)
     where TView : class, new()
     where TEntry : class
 {
     return(new LogViewAdaptor <TView, TEntry>(hostGrain, initialState, grainStorage, grainTypeName, services));
 }
 /// <summary>
 /// called right after grain construction to install the log view adaptor
 /// </summary>
 /// <param name="factory"> The adaptor factory to use </param>
 /// <param name="state"> The initial state of the view </param>
 /// <param name="grainTypeName"> The type name of the grain </param>
 /// <param name="grainStorage"> The grain storage, if needed </param>
 /// <param name="services"> Protocol services </param>
 protected abstract void InstallAdaptor(ILogViewAdaptorFactory factory, object state, string grainTypeName, IGrainStorage grainStorage, ILogConsistencyProtocolServices services);
Exemplo n.º 29
0
        private async Task <GrainState <TestStoreGrainState> > Test_PersistenceProvider_WriteClearRead(string grainTypeName,
                                                                                                       IGrainStorage store, GrainState <TestStoreGrainState> grainState = null, GrainId grainId = default)
        {
            GrainReference reference = (GrainReference)this.fixture.InternalGrainFactory.GetGrain(grainId.IsDefault ? (GrainId)LegacyGrainId.NewId() : grainId);

            if (grainState == null)
            {
                grainState = TestStoreGrainState.NewRandomState();
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            await store.WriteStateAsync(grainTypeName, reference, grainState);

            TimeSpan writeTime = sw.Elapsed;

            sw.Restart();

            await store.ClearStateAsync(grainTypeName, reference, grainState);

            var storedGrainState = new GrainState <TestStoreGrainState>
            {
                State = new TestStoreGrainState()
            };
            await store.ReadStateAsync(grainTypeName, reference, storedGrainState);

            TimeSpan readTime = sw.Elapsed;

            this.output.WriteLine("{0} - Write time = {1} Read time = {2}", store.GetType().FullName, writeTime, readTime);
            Assert.NotNull(storedGrainState.State);
            Assert.Equal(default(string), storedGrainState.State.A);
            Assert.Equal(default(int), storedGrainState.State.B);
            Assert.Equal(default(long), storedGrainState.State.C);

            return(storedGrainState);
        }
Exemplo n.º 30
0
 public PersistentStateBridge(string fullStateName, IGrainContext context, IGrainStorage storageProvider)
 {
     this.fullStateName   = fullStateName;
     this.context         = context;
     this.storageProvider = storageProvider;
 }