예제 #1
0
        private EpochRecord WriteEpochRecordWithRetry(int epochNumber, Guid epochId, long lastEpochPosition,
                                                      Guid instanceId)
        {
            long pos   = _writer.Checkpoint.ReadNonFlushed();
            var  epoch = new EpochRecord(pos, epochNumber, epochId, lastEpochPosition, DateTime.UtcNow, instanceId);
            var  rec   = new SystemLogRecord(epoch.EpochPosition, epoch.TimeStamp, SystemRecordType.Epoch,
                                             SystemRecordSerialization.Json, epoch.AsSerialized());

            Log.Debug(
                "=== Writing E{epochNumber}@{epochPosition}:{epochId:B} (previous epoch at {lastEpochPosition}). L={leaderId:B}.",
                epochNumber, epoch.EpochPosition, epochId, lastEpochPosition, epoch.LeaderInstanceId);
            if (!_writer.Write(rec, out pos))
            {
                epoch = new EpochRecord(pos, epochNumber, epochId, lastEpochPosition, DateTime.UtcNow, instanceId);
                rec   = new SystemLogRecord(epoch.EpochPosition, epoch.TimeStamp, SystemRecordType.Epoch,
                                            SystemRecordSerialization.Json, epoch.AsSerialized());
                if (!_writer.Write(rec, out pos))
                {
                    throw new Exception($"Second write try failed at {epoch.EpochPosition}.");
                }
            }
            _writer.Flush();
            _bus.Publish(new SystemMessage.EpochWritten(epoch));
            return(epoch);
        }
예제 #2
0
        public ISystemLogRecord CreateEpoch(EpochRecord epoch)
        {
            var result = new SystemLogRecord(
                logPosition: epoch.EpochPosition,
                timeStamp: epoch.TimeStamp,
                systemRecordType: SystemRecordType.Epoch,
                systemRecordSerialization: SystemRecordSerialization.Json,
                data: epoch.AsSerialized());

            return(result);
        }
예제 #3
0
        private EpochRecord WriteEpoch(int epochNumber, long lastPos, Guid instanceId)
        {
            long pos   = _writer.Checkpoint.ReadNonFlushed();
            var  epoch = new EpochRecord(pos, epochNumber, Guid.NewGuid(), lastPos, DateTime.UtcNow, instanceId);
            var  rec   = new SystemLogRecord(epoch.EpochPosition, epoch.TimeStamp, SystemRecordType.Epoch,
                                             SystemRecordSerialization.Json, epoch.AsSerialized());

            _writer.Write(rec, out _);
            _writer.Flush();
            return(epoch);
        }
예제 #4
0
        public override void When()
        {
            _epochId     = Guid.NewGuid();
            _epochNumber = 7;
            var epoch = new EpochRecord(0, _epochNumber, _epochId, -1, DateTime.UtcNow, Guid.Empty);
            var rec   = new SystemLogRecord(epoch.EpochPosition, epoch.TimeStamp, SystemRecordType.Epoch,
                                            SystemRecordSerialization.Json, epoch.AsSerialized());

            Assert.True(Writer.Write(rec, out _));
            Writer.Flush();
        }
예제 #5
0
        void WriteEpochInformationWithRetry(EpochRecord epoch)
        {
            if (!TryGetExpectedVersionForEpochInformation(epoch, out var expectedVersion))
            {
                expectedVersion = ExpectedVersion.NoStream;
            }

            var originalLogPosition = _writer.Checkpoint.ReadNonFlushed();

            var epochInformation = LogRecord.Prepare(
                factory: _recordFactory,
                logPosition: originalLogPosition,
                correlationId: Guid.NewGuid(),
                eventId: Guid.NewGuid(),
                transactionPos: originalLogPosition,
                transactionOffset: 0,
                eventStreamId: GetEpochInformationStream(),
                expectedVersion: expectedVersion,
                flags: PrepareFlags.SingleWrite | PrepareFlags.IsCommitted | PrepareFlags.IsJson,
                eventType: GetEpochInformationEventType(),
                data: epoch.AsSerialized(),
                metadata: Empty.ByteArray);

            if (_writer.Write(epochInformation, out var retryLogPosition))
            {
                return;
            }

            epochInformation = epochInformation.CopyForRetry(retryLogPosition, retryLogPosition);

            if (_writer.Write(epochInformation, out _))
            {
                return;
            }

            throw new Exception(
                      string.Format("Second write try failed when first writing $epoch-information at {0}, then at {1}.",
                                    originalLogPosition,
                                    retryLogPosition));
        }
예제 #6
0
        private EpochRecord WriteEpochRecordWithRetry(int epochNumber, Guid epochId, long lastEpochPosition)
        {
            long pos   = _writer.Checkpoint.ReadNonFlushed();
            var  epoch = new EpochRecord(pos, epochNumber, epochId, lastEpochPosition, DateTime.UtcNow);
            var  rec   = new SystemLogRecord(epoch.EpochPosition, epoch.TimeStamp, SystemRecordType.Epoch, SystemRecordSerialization.Json, epoch.AsSerialized());

            if (!_writer.Write(rec, out pos))
            {
                epoch = new EpochRecord(pos, epochNumber, epochId, lastEpochPosition, DateTime.UtcNow);
                rec   = new SystemLogRecord(epoch.EpochPosition, epoch.TimeStamp, SystemRecordType.Epoch, SystemRecordSerialization.Json, epoch.AsSerialized());
                if (!_writer.Write(rec, out pos))
                {
                    throw new Exception(string.Format("Second write try failed at {0}.", epoch.EpochPosition));
                }
            }
            Log.Debug("=== Writing E{0}@{1}:{2:B} (previous epoch at {3}).", epochNumber, epoch.EpochPosition, epochId, lastEpochPosition);
            return(epoch);
        }