Example #1
0
        public async Task <List <EventInfo <K> > > GetListAsync(K stateId, string typeCode, Int64 startVersion, Int64 endVersion, DateTime?startTime = null)
        {
            var   collectionList = mongoAttr.GetCollectionList(mongoStorage, mongoStorage.Config.SysStartTime, startTime);
            var   list           = new List <EventInfo <K> >();
            Int64 readVersion    = 0;

            foreach (var collection in collectionList)
            {
                var filterBuilder = Builders <BsonDocument> .Filter;
                var filter        = filterBuilder.Eq("StateId", stateId) & filterBuilder.Eq("TypeCode", typeCode) & filterBuilder.Gt("Version", startVersion);
                var cursor        = await mongoStorage.GetCollection <BsonDocument>(mongoAttr.EventDataBase, collection.Name).FindAsync <BsonDocument>(filter, cancellationToken: new CancellationTokenSource(3000).Token);

                foreach (var document in cursor.ToEnumerable())
                {
                    var type      = MessageTypeMapping.GetType(typeCode);
                    var data      = document["Data"].AsByteArray;
                    var eventInfo = new EventInfo <K>();
                    eventInfo.IsComplete = document["IsComplete"].AsBoolean;
                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        var @event = Serializer.Deserialize(type, ms) as IEventBase <K>;
                        eventInfo.Event = @event;
                    }
                    if (readVersion <= endVersion)
                    {
                        list.Add(eventInfo);
                    }
                }
                if (readVersion >= endVersion)
                {
                    break;
                }
            }
            return(list.OrderBy(e => e.Event.Version).ToList());
        }
Example #2
0
 public async Task Tell(W message)
 {
     var type = MessageTypeMapping.GetType(message.TypeCode);
     if (type != null)
     {
         using (var ems = new MemoryStream(message.BinaryBytes))
         {
             var @event = Serializer.Deserialize(type, ems) as IEventBase<K>;
             if (@event != null)
             {
                 if (@event.Version == this.State.Version + 1)
                 {
                     await OnExecution(@event);
                     this.State.IncrementDoingVersion();//标记将要处理的Version
                     EventHandle.Apply(this.State, @event);
                     this.State.UpdateVersion(@event);//更新处理完成的Version
                     await OnExecuted(@event);
                     await SaveSnapshotAsync();
                 }
                 else if (@event.Version > this.State.Version)
                 {
                     var eventList = await EventStorage.GetListAsync(this.GrainId, this.State.Version, @event.Version);
                     foreach (var item in eventList)
                     {
                         await OnExecution(@event);
                         this.State.IncrementDoingVersion();//标记将要处理的Version
                         EventHandle.Apply(this.State, @event);
                         this.State.UpdateVersion(@event);//更新处理完成的Version
                         await OnExecuted(@event);
                         await SaveSnapshotAsync();
                     }
                 }
                 if (@event.Version == this.State.Version + 1)
                 {
                     await OnExecution(@event);
                     this.State.IncrementDoingVersion();//标记将要处理的Version
                     EventHandle.Apply(this.State, @event);
                     this.State.UpdateVersion(@event);//更新处理完成的Version
                     await OnExecuted(@event);
                     await SaveSnapshotAsync();
                 }
                 if (@event.Version > this.State.Version)
                 {
                     throw new Exception($"Event version of the error,Type={ThisType.FullName},StateId={this.GrainId.ToString()},StateVersion={this.State.Version},EventVersion={@event.Version}");
                 }
             }
         }
     }
 }
Example #3
0
        public async Task Tell(W message)
        {
            var type = MessageTypeMapping.GetType(message.TypeCode);

            if (type != null)
            {
                using (var ems = new MemoryStream(message.BinaryBytes))
                {
                    var @event = Serializer.Deserialize(type, ems) as IEventBase <K>;
                    if (@event != null)
                    {
                        if (@event.Version == this.State.Version + 1)
                        {
                            await OnExecution(@event);

                            @event.Apply(State);
                            await OnExecuted(@event);
                            await SaveSnapshotAsync();
                        }
                        else if (@event.Version > this.State.Version)
                        {
                            var eventList = await EventStorage.GetListAsync(this.GrainId, this.State.Version, @event.Version);

                            foreach (var item in eventList)
                            {
                                await OnExecution(@event);

                                item.Event.Apply(State);
                                await OnExecuted(@event);
                                await SaveSnapshotAsync();
                            }
                        }
                        if (@event.Version == this.State.Version + 1)
                        {
                            await OnExecution(@event);

                            @event.Apply(State);
                            await OnExecuted(@event);
                            await SaveSnapshotAsync();
                        }
                        if (@event.Version > this.State.Version)
                        {
                            throw new Exception($"ESReplicaGrain出现严重BUG,Type={ThisType.FullName},StateId={this.GrainId.ToString()},StateVersion={this.State.Version},EventVersion={@event.Version}");
                        }
                    }
                }
            }
        }
Example #4
0
        public async Task Tell(W msg)
        {
            var type = MessageTypeMapping.GetType(msg.TypeCode);

            if (type != null)
            {
                using (var ems = new MemoryStream(msg.BinaryBytes))
                {
                    var message = Serializer.Deserialize(type, ems);
                    if (message != null)
                    {
                        if (!outsideMsgTypecodeList.Contains(msg.TypeCode))
                        {
                            if (message is IEvent @event)
                            {
                                if (@event.Version == this.State.Version + 1)
                                {
                                    await EventExcute(@event);
                                }
                                else if (@event.Version > this.State.Version)
                                {
                                    while (true)
                                    {
                                        var eventList = await EventStorage.GetListAsync(this.GrainId, this.State.Version, this.State.Version + 1000, this.State.VersionTime);

                                        foreach (var item in eventList)
                                        {
                                            await EventExcute(item.Event);
                                        }
                                        if (this.State.Version >= @event.Version)
                                        {
                                            break;
                                        }
                                    }
                                    ;
                                }
                            }
                        }
                        else if (message is IMessage value)
                        {
                            await NotEventMsgExcute(value);
                        }
                    }
                }
            }
        }
Example #5
0
        public Task Notice(byte[] bytes)
        {
            var serializer = Global.IocProvider.GetService <ISerializer>();

            using (var ms = new MemoryStream(bytes))
            {
                var msg  = serializer.Deserialize <TMessageWrapper>(ms);
                var type = MessageTypeMapping.GetType(msg.TypeCode);
                if (type == null)
                {
                    throw new Exception($"TypeCode for { msg.TypeCode } type does not exist");
                }
                using (var ems = new MemoryStream(msg.BinaryBytes))
                {
                    return(this.Notice(bytes, msg, serializer.Deserialize(type, ems)));
                }
            }
        }
Example #6
0
        public Task Notice(byte[] data)
        {
            var serializer = Global.IocProvider.GetService <ISerializer>();

            using (var ms = new MemoryStream(data))
            {
                var msg  = serializer.Deserialize <TMessageWrapper>(ms);
                var type = MessageTypeMapping.GetType(msg.TypeCode);
                if (type == null)
                {
                    throw new Exception("TypeCode为" + msg.TypeCode + "的Type不存在");
                }
                using (var ems = new MemoryStream(msg.BinaryBytes))
                {
                    return(this.Notice(data, msg, serializer.Deserialize(type, ems)));
                }
            }
        }
Example #7
0
        public async Task <List <EventInfo <K> > > GetListAsync(K stateId, string typeCode, Int64 startVersion, Int64 endVersion, DateTime?startTime = null)
        {
            var tableList = await tableInfo.GetTableList(startTime);

            var   list        = new List <EventInfo <K> >();
            Int64 readVersion = 0;

            using (var conn = tableInfo.CreateConnection())
            {
                foreach (var table in tableList)
                {
                    var sql = $"SELECT typecode,data,IsComplete from {table.Name} WHERE stateid=@StateId and typecode=@TypeCode and version>@Start and version<=@End";

                    var sqlEventList = await conn.QueryAsync <SqlEvent>(sql, new { StateId = stateId, TypeCode = typeCode, Start = startVersion, End = endVersion });

                    foreach (var sqlEvent in sqlEventList)
                    {
                        var type      = MessageTypeMapping.GetType(sqlEvent.TypeCode);
                        var eventInfo = new EventInfo <K>();
                        eventInfo.IsComplete = sqlEvent.IsComplete;
                        using (var ms = new MemoryStream(sqlEvent.Data))
                        {
                            var @event = Serializer.Deserialize(type, ms) as IEventBase <K>;
                            readVersion     = @event.Version;
                            eventInfo.Event = @event;
                        }
                        if (readVersion <= endVersion)
                        {
                            list.Add(eventInfo);
                        }
                    }
                    if (readVersion >= endVersion)
                    {
                        break;
                    }
                }
            }
            return(list.OrderBy(e => e.Event.Version).ToList());
        }