public SQLiteStateStoreMigration(
            ILogger <SQLiteStateStoreMigration> logger,
            DbUpMigration.Factory factory,
            ISQLiteDbFactory sqLiteDbFactory,
            IClaptrapIdentity identity,
            IStorageMigrationContainer storageMigrationContainer,
            ISQLiteStateStoreOptions options)
        {
            var locator        = options.RelationalStateStoreLocator;
            var stateTableName = locator.GetStateTableName(identity);
            var connectionName = locator.GetConnectionName(identity);

            var migrationOptions = new DbUpMigrationOptions(
                new[] { Assembly.GetExecutingAssembly() },
                fileName => fileName.EndsWith("-state.sql"),
                new Dictionary <string, string>
            {
                { "StateTableName", stateTableName }
            },
                () =>
            {
                var dbConnection = sqLiteDbFactory.GetConnection(connectionName);
                var builder      = DeployChanges.To.SQLiteDatabase(new SharedConnection(dbConnection));
                return(builder, dbConnection);
            });

            var migration = factory.Invoke(logger, migrationOptions);

            var migrationKey =
                $"{nameof(SQLiteStateStoreMigration)}_{connectionName}_{stateTableName}";

            _migrationTask = storageMigrationContainer.CreateTask(migrationKey, migration);
        }
Example #2
0
        public IEnumerable <IClaptrapSharedModule> GetClaptrapSharedModules(IClaptrapIdentity identity)
        {
            var design = _claptrapDesignStore.FindDesign(identity);
            var re     = new MySqlMigrationModule(design);

            yield return(re);
        }
Example #3
0
 public AccountHistoryBalanceMinion(IClaptrapIdentity identity,
                                    IClaptrapFactory claptrapFactory,
                                    IClaptrapAccessor claptrapAccessor) : base(identity,
                                                                               claptrapFactory,
                                                                               claptrapAccessor)
 {
 }
Example #4
0
        public IMinionProxy CreateProxy(IClaptrapIdentity minionId)
        {
            if (_minionDesignsDic.TryGetValue(minionId.TypeCode, out var design))
            {
                try
                {
                    var grainInterfaceType = design.ClaptrapBoxInterfaceType;
                    _logger.LogTrace(
                        "try to activate claptrap box grain in type : {type}, id : {id}",
                        design.ClaptrapBoxInterfaceType,
                        minionId.Id);
                    var grain = (IClaptrapMinionGrain)_grainFactory.GetGrain(grainInterfaceType, minionId.Id);
                    _logger.LogTrace(
                        "success to activate claptrap grain box in type : {type}, id : {id}",
                        design.ClaptrapBoxInterfaceType,
                        minionId.Id);
                    var proxy = _grainMinionProxyFactory.Invoke(grain);
                    return(proxy);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "something error while calling minions, {identity}", minionId);
                }
            }
            else
            {
                _logger.LogTrace(
                    "can not found minion design in design store for type code : {typeCode}",
                    minionId.TypeCode);
            }

            return(EmptyMinionProxy.Instance);
        }
Example #5
0
 public TestMinionProxy(
     IClaptrapIdentity minionIdentity,
     ConcurrentQueue <ReceivedItem> bag)
 {
     _minionIdentity = minionIdentity;
     _bag            = bag;
 }
Example #6
0
        public IState Map(StateEntity stateEntity, IClaptrapIdentity identity)
        {
            var stateData = _stateDataStringSerializer.Deserialize(identity, stateEntity.StateData);
            var re        = new DataState(identity, stateData, stateEntity.Version);

            return(re);
        }
        public MongoDBStateEntitySaver(
            IClaptrapIdentity identity,
            ChannelBatchOperator <StateEntity> .Factory batchOperatorFactory,
            IDbFactory dbFactory,
            IMongoDBStateStoreLocatorOptions options,
            IBatchOperatorContainer batchOperatorContainer)
        {
            var locator = options.MongoDBStateStoreLocator;

            _connectionName      = locator.GetConnectionName(identity);
            _databaseName        = locator.GetDatabaseName(identity);
            _stateCollectionName = locator.GetStateCollectionName(identity);
            var operatorKey = new BatchOperatorKey()
                              .With(nameof(MongoDBStateEntitySaver))
                              .With(_connectionName)
                              .With(_databaseName)
                              .With(_stateCollectionName);

            _batchOperator = (IBatchOperator <StateEntity>)batchOperatorContainer.GetOrAdd(
                operatorKey, () => batchOperatorFactory.Invoke(
                    new BatchOperatorOptions <StateEntity>(options)
            {
                DoManyFunc = (entities, cacheData) => SaveManyCoreMany(dbFactory, entities)
            }));
        }
Example #8
0
 public ClaptrapMasterModule(
     IClaptrapDesign claptrapDesign,
     IClaptrapIdentity identity)
 {
     _claptrapDesign = claptrapDesign;
     _identity       = identity;
 }
Example #9
0
        public IEnumerable <IClaptrapMasterModule> GetClaptrapMasterClaptrapModules(IClaptrapIdentity identity)
        {
            var claptrapDesign = _claptrapDesignStore.FindDesign(identity);
            var re             = new ClaptrapMasterModule(claptrapDesign, identity);

            yield return(re);
        }
Example #10
0
        public static SQLiteConnection CreateInMemoryConnection(IClaptrapIdentity claptrapIdentity)
        {
            var connectionString =
                $"Data Source={claptrapIdentity.TypeCode}_{claptrapIdentity.Id};Mode=Memory;Cache=Shared";

            return(new SQLiteConnection(connectionString));
        }
Example #11
0
 public Account(IClaptrapIdentity identity,
                IClaptrapFactory claptrapFactory,
                IClaptrapAccessor claptrapAccessor) : base(identity,
                                                           claptrapFactory,
                                                           claptrapAccessor)
 {
 }
 /// <summary>
 /// Create a SagaClaptrap to handle saga flow
 /// </summary>
 /// <param name="scope"></param>
 /// <param name="masterIdentity"></param>
 /// <param name="flowKey"></param>
 /// <param name="userDataType"></param>
 /// <returns></returns>
 public static IDisposableSagaClaptrap CreateSagaClaptrap(this ILifetimeScope scope,
                                                          IClaptrapIdentity masterIdentity,
                                                          string flowKey,
                                                          Type?userDataType = null)
 =>
 scope.CreateSagaClaptrap(() =>
                          new SagaClaptrapIdentity(masterIdentity, flowKey, userDataType ?? typeof(object)));
Example #13
0
        public TInterface Create(IClaptrapIdentity claptrapIdentity)
        {
            var inner = _relationalStoreFactory.Create(claptrapIdentity);
            var re    = _func.Invoke(inner);

            return(re);
        }
 public MetricsEventSaverMigration(
     IEventSaverMigration eventSaverMigration,
     IClaptrapIdentity identity)
 {
     _eventSaverMigration = eventSaverMigration;
     _identity            = identity;
 }
Example #15
0
        public InsertValuesMySqlEventEntitySaver(
            ChannelBatchOperator <EventEntity> .Factory batchOperatorFactory,
            IClaptrapIdentity identity,
            IDbFactory dbFactory,
            IMySqlEventStoreOptions options,
            IBatchOperatorContainer batchOperatorContainer,
            ISqlTemplateCache sqlTemplateCache)
        {
            var locator = options.RelationalEventStoreLocator;

            _connectionName   = locator.GetConnectionName(identity);
            _schemaName       = locator.GetSchemaName(identity);
            _eventTableName   = locator.GetEventTableName(identity);
            _dbFactory        = dbFactory;
            _sqlTemplateCache = sqlTemplateCache;
            var operatorKey = new BatchOperatorKey()
                              .With(nameof(InsertValuesMySqlEventEntitySaver))
                              .With(_connectionName)
                              .With(_schemaName)
                              .With(_eventTableName);

            _batchOperator = (IBatchOperator <EventEntity>)batchOperatorContainer.GetOrAdd(
                operatorKey, () => batchOperatorFactory.Invoke(
                    new BatchOperatorOptions <EventEntity>(options)
            {
                DoManyFunc     = (entities, cacheData) => SaveManyAsync(entities),
                DoManyFuncName = $"event batch saver for {operatorKey.AsStringKey()}"
            }));
        }
Example #16
0
 public ClaptrapSharedModule(
     IClaptrapDesign claptrapDesign,
     IClaptrapIdentity identity)
 {
     _claptrapDesign = claptrapDesign;
     _identity       = identity;
 }
 public WakeMinionClaptrapLifetimeInterceptor(
     IClaptrapIdentity identity,
     IMinionActivator minionActivator)
 {
     _identity        = identity;
     _minionActivator = minionActivator;
 }
Example #18
0
        public async Task WakeAsync(IClaptrapIdentity identity)
        {
            if (!_minionLookUp.TryGetValue(identity.TypeCode, out var minions))
            {
                _logger.LogDebug($"There is no minions for {identity}", identity);
                return;
            }

            try
            {
                await Task.WhenAll(WakeAllAsync());
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to wake all minions");
            }

            IEnumerable <Task> WakeAllAsync()
            {
                foreach (var minionDesign in minions)
                {
                    var grain = _grainFactory.GetGrain(minionDesign.ClaptrapBoxInterfaceType, identity.Id);
                    if (grain is IClaptrapMinionGrain minionGrain)
                    {
                        yield return(minionGrain.WakeAsync());
                    }
                    else
                    {
                        _logger.LogDebug("{type} is not {minionGrain}, can`t to wake it up",
                                         minionDesign.ClaptrapBoxInterfaceType,
                                         nameof(IClaptrapMinionGrain));
                    }
                }
            }
        }
Example #19
0
        public IMinionProxy CreateProxy(IClaptrapIdentity minionId)
        {
            var actorProxy = _actorProxyFactory.Create(new ActorId(minionId.Id), minionId.TypeCode);
            var proxy      = _factory.Invoke(actorProxy);

            return(proxy);
        }
        public Task SendToMinionsAsync(IClaptrapIdentity masterId, IEvent @event)
        {
            _ = _inited.Value;
            var sender = _senderManager.Get(@event);

            return(sender.SendTopicAsync(@event));
        }
 public MetricsClaptrapLifetimeInterceptor(
     IClaptrapIdentity identity,
     IClaptrapDesign claptrapDesign)
 {
     _identity       = identity;
     _claptrapDesign = claptrapDesign;
 }
Example #22
0
        public async Task SendToMinionsAsync(IClaptrapIdentity masterId, IEvent @event)
        {
            if (_minionDesignsLookup.Contains(masterId.TypeCode))
            {
                try
                {
                    var minionDesigns = _minionDesignsLookup[masterId.TypeCode];
                    await Task.WhenAll(SendCore(minionDesigns));

                    IEnumerable <Task> SendCore(IEnumerable <IClaptrapDesign> designs)
                    {
                        foreach (var design in designs)
                        {
                            var proxy = _minionLocator.CreateProxy(new ClaptrapIdentity(masterId.Id,
                                                                                        design.ClaptrapTypeCode));
                            yield return(proxy.MasterEventReceivedAsync(new[] { @event }));
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "something error while calling minions, {identity}", masterId);
                }
            }
            else
            {
                _logger.LogTrace(
                    "can not found minion design in design store for type code : {typeCode}",
                    masterId.TypeCode);
            }
        }
Example #23
0
        public SQLiteStateEntitySaver(
            ChannelBatchOperator <StateEntity> .Factory batchOperatorFactory,
            IClaptrapIdentity identity,
            ISQLiteDbFactory sqLiteDbFactory,
            ISQLiteStateStoreOptions options,
            ISqlTemplateCache sqlTemplateCache,
            IBatchOperatorContainer batchOperatorContainer)
        {
            _sqlTemplateCache = sqlTemplateCache;
            var locator        = options.RelationalStateStoreLocator;
            var stateTableName = locator.GetStateTableName(identity);

            _stateTableName = stateTableName;
            _connectionName = locator.GetConnectionName(identity);

            var operatorKey = new BatchOperatorKey()
                              .With(nameof(SQLiteStateEntitySaver))
                              .With(_connectionName)
                              .With(stateTableName);

            _batchOperator = (IBatchOperator <StateEntity>)batchOperatorContainer.GetOrAdd(
                operatorKey, () => batchOperatorFactory.Invoke(
                    new BatchOperatorOptions <StateEntity>(options)
            {
                DoManyFunc = (entities, cacheData) =>
                             SaveManyCoreMany(sqLiteDbFactory, entities, (string[])cacheData ![UpsertSqlKey]),
 public MetricsStateLoaderMigration(
     IStateLoaderMigration stateLoaderMigration,
     IClaptrapIdentity identity)
 {
     _stateLoaderMigration = stateLoaderMigration;
     _identity             = identity;
 }
        public PostgreSQLEventStoreMigration(
            ILogger <PostgreSQLEventStoreMigration> logger,
            DbUpMigration.Factory factory,
            IClaptrapIdentity identity,
            IDbFactory dbFactory,
            IStorageMigrationContainer storageMigrationContainer,
            IPostgreSQLEventStoreOptions options)
        {
            var(connectionName, schemaName, eventTableName) =
                options.RelationalEventStoreLocator.GetNames(identity);
            var migrationOptions = new DbUpMigrationOptions(
                new[] { Assembly.GetExecutingAssembly() },
                fileName => fileName.EndsWith("-event.sql"),
                new Dictionary <string, string>
            {
                { "SchemaName", schemaName },
                { "EventTableName", eventTableName }
            },
                () =>
                (DeployChanges
                 .To.PostgresqlDatabase(dbFactory.GetConnectionString(connectionName)), null));

            var migration    = factory.Invoke(logger, migrationOptions);
            var migrationKey =
                $"{nameof(PostgreSQLEventStoreMigration)}_{connectionName}_{schemaName}_{eventTableName}";

            _migrationTask = storageMigrationContainer.CreateTask(migrationKey, migration);
        }
Example #26
0
        public MySqlStateEntitySaver(
            BatchOperator <StateEntity> .Factory batchOperatorFactory,
            IClaptrapIdentity identity,
            IDbFactory dbFactory,
            IRelationalStateStoreLocatorOptions options,
            ISqlTemplateCache sqlTemplateCache,
            IBatchOperatorContainer batchOperatorContainer)
        {
            var locator = options.RelationalStateStoreLocator;

            _connectionName   = locator.GetConnectionName(identity);
            _schemaName       = locator.GetSchemaName(identity);
            _tableName        = locator.GetStateTableName(identity);
            _sqlTemplateCache = sqlTemplateCache;

            var operatorKey = new BatchOperatorKey()
                              .With(nameof(MySqlStateEntitySaver))
                              .With(_connectionName)
                              .With(_tableName);

            _batchOperator = (IBatchOperator <StateEntity>)batchOperatorContainer.GetOrAdd(
                operatorKey, () => batchOperatorFactory.Invoke(
                    new BatchOperatorOptions <StateEntity>(options)
            {
                DoManyFunc = (entities, cacheData) =>
                             SaveManyCoreMany(dbFactory, entities),
            }));
        }
 public CustomFactoryClaptrap(IClaptrapIdentity identity,
                              IClaptrapFactory claptrapFactory,
                              IClaptrapAccessor claptrapAccessor) : base(identity,
                                                                         claptrapFactory,
                                                                         claptrapAccessor)
 {
 }
Example #28
0
        public static T GetClaptrap <T>(this IActorProxyFactory factory, IClaptrapIdentity identity)
            where T : IActor
        {
            var re = factory.CreateActorProxy <T>(new ActorId(identity.Id), identity.TypeCode);

            return(re);
        }
Example #29
0
        public PostgreSQLStateEntitySaver(
            BatchOperator <StateEntity> .Factory batchOperatorFactory,
            IBatchOperatorContainer batchOperatorContainer,
            IDbFactory dbFactory,
            IClaptrapIdentity identity,
            IPostgreSQLStateStoreOptions options)
        {
            var locator = options.RelationalStateStoreLocator;

            var(connectionName, schemaName, stateTableName) = locator.GetNames(identity);
            _connectionName = connectionName;
            _schemaName     = schemaName;
            _stateTableName = stateTableName;

            var operatorKey = new BatchOperatorKey()
                              .With(nameof(PostgreSQLStateEntitySaver))
                              .With(_connectionName)
                              .With(_schemaName)
                              .With(_stateTableName);

            _batchOperator = (IBatchOperator <StateEntity>)batchOperatorContainer.GetOrAdd(
                operatorKey, () => batchOperatorFactory.Invoke(
                    new BatchOperatorOptions <StateEntity>(options)
            {
                DoManyFunc = (entities, cacheData) =>
                             SaveManyCoreMany(dbFactory, entities, (string)cacheData ![UpsertSqlKey]),
        public async Task WakeAsync(IClaptrapIdentity identity)
        {
            if (!_minionLookUp.TryGetValue(identity.TypeCode, out var minions))
            {
                _logger.LogDebug($"There is no minions for {identity}", identity);
                return;
            }

            try
            {
                await Task.WhenAll(WakeAllAsync());
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to wake all minions");
            }

            IEnumerable <Task> WakeAllAsync()
            {
                foreach (var minionDesign in minions)
                {
                    var actorProxy = _actorProxyFactory.Create(new ActorId(identity.Id), minionDesign.ClaptrapTypeCode);
                    yield return(actorProxy.InvokeMethodAsync(nameof(IClaptrapMinionActor.WakeAsync)));
                }
            }
        }