Пример #1
0
 public static PrepareLogRecord Prepare(long logPosition, Guid correlationId, Guid eventId, long transactionPos, int transactionOffset,
                                        string eventStreamId, int expectedVersion, PrepareFlags flags, string eventType, 
                                        byte[] data, byte[] metadata, DateTime? timeStamp = null)
 {
     return new PrepareLogRecord(logPosition, correlationId, eventId, transactionPos, transactionOffset, 
                                 eventStreamId, expectedVersion, timeStamp ?? DateTime.UtcNow, flags, eventType,
                                 data, metadata);
 }
Пример #2
0
 public static PrepareLogRecord SingleWrite(long logPosition, Guid correlationId, Guid eventId, string eventStreamId, 
                                            int expectedVersion, string eventType, byte[] data, byte[] metadata, 
                                            DateTime? timestamp = null, PrepareFlags? additionalFlags = null)
 {
     return new PrepareLogRecord(logPosition, correlationId, eventId, logPosition, 0, eventStreamId, expectedVersion, 
                                 timestamp ?? DateTime.UtcNow, 
                                 PrepareFlags.Data | PrepareFlags.TransactionBegin | PrepareFlags.TransactionEnd | (additionalFlags ?? PrepareFlags.None), 
                                 eventType, data, metadata);
 }
Пример #3
0
            public PrepareAck(Guid correlationId, long logPosition, PrepareFlags flags)
            {
                Ensure.NotEmptyGuid(correlationId, "correlationId");
                Ensure.Nonnegative(logPosition, "logPosition");

                CorrelationId = correlationId;
                LogPosition   = logPosition;
                Flags         = flags;
            }
Пример #4
0
        protected EventRecord WriteTransactionEvent(Guid correlationId,
                                                    long transactionPos,
                                                    int transactionOffset,
                                                    string eventStreamId,
                                                    long eventNumber,
                                                    string eventData,
                                                    PrepareFlags flags,
                                                    bool retryOnFail = false)
        {
            var prepare = LogRecord.Prepare(WriterCheckpoint.ReadNonFlushed(),
                                            correlationId,
                                            Guid.NewGuid(),
                                            transactionPos,
                                            transactionOffset,
                                            eventStreamId,
                                            ExpectedVersion.Any,
                                            flags,
                                            "some-type",
                                            Helper.UTF8NoBom.GetBytes(eventData),
                                            null);

            if (retryOnFail)
            {
                long firstPos = prepare.LogPosition;
                long newPos;
                if (!Writer.Write(prepare, out newPos))
                {
                    var tPos = prepare.TransactionPosition == prepare.LogPosition
                                                ? newPos
                                                : prepare.TransactionPosition;
                    prepare = new PrepareLogRecord(newPos,
                                                   prepare.CorrelationId,
                                                   prepare.EventId,
                                                   tPos,
                                                   prepare.TransactionOffset,
                                                   prepare.EventStreamId,
                                                   prepare.ExpectedVersion,
                                                   prepare.TimeStamp,
                                                   prepare.Flags,
                                                   prepare.EventType,
                                                   prepare.Data,
                                                   prepare.Metadata);
                    if (!Writer.Write(prepare, out newPos))
                    {
                        Assert.Fail("Second write try failed when first writing prepare at {0}, then at {1}.", firstPos,
                                    prepare.LogPosition);
                    }
                }

                return(new EventRecord(eventNumber, prepare));
            }

            long pos;

            Assert.IsTrue(Writer.Write(prepare, out pos));
            return(new EventRecord(eventNumber, prepare));
        }
Пример #5
0
        protected EventRecord WriteTransactionEvent(Guid correlationId,
                                                    long transactionPos,
                                                    int transactionOffset,
                                                    string eventStreamName,
                                                    long eventNumber,
                                                    string eventData,
                                                    PrepareFlags flags,
                                                    bool retryOnFail = false)
        {
            LogFormatHelper <TLogFormat, TStreamId> .CheckIfExplicitTransactionsSupported();

            _streamNameIndex.GetOrAddId(eventStreamName, out var eventStreamId, out _, out _);

            var prepare = LogRecord.Prepare(_recordFactory, WriterCheckpoint.ReadNonFlushed(),
                                            correlationId,
                                            Guid.NewGuid(),
                                            transactionPos,
                                            transactionOffset,
                                            eventStreamId,
                                            ExpectedVersion.Any,
                                            flags,
                                            "some-type",
                                            Helper.UTF8NoBom.GetBytes(eventData),
                                            null);

            if (retryOnFail)
            {
                long firstPos = prepare.LogPosition;
                long newPos;
                if (!Writer.Write(prepare, out newPos))
                {
                    var tPos = prepare.TransactionPosition == prepare.LogPosition
                                                ? newPos
                                                : prepare.TransactionPosition;
                    prepare = _recordFactory.CopyForRetry(
                        prepare: prepare,
                        logPosition: newPos,
                        transactionPosition: tPos);
                    if (!Writer.Write(prepare, out newPos))
                    {
                        Assert.Fail("Second write try failed when first writing prepare at {0}, then at {1}.", firstPos,
                                    prepare.LogPosition);
                    }
                }

                Assert.AreEqual(eventStreamId, prepare.EventStreamId);
                return(new EventRecord(eventNumber, prepare, eventStreamName));
            }

            long pos;

            Assert.IsTrue(Writer.Write(prepare, out pos));

            Assert.AreEqual(eventStreamId, prepare.EventStreamId);
            return(new EventRecord(eventNumber, prepare, eventStreamName));
        }
Пример #6
0
        void IHandle <StorageMessage.WriteDelete> .Handle(StorageMessage.WriteDelete message)
        {
            Interlocked.Decrement(ref FlushMessagesInQueue);
            try
            {
                if (message.LiveUntil < DateTime.UtcNow)
                {
                    return;
                }

                var eventId = Guid.NewGuid();

                var commitCheck = _indexWriter.CheckCommit(message.EventStreamId, message.ExpectedVersion,
                                                           new[] { eventId });
                if (commitCheck.Decision != CommitDecision.Ok)
                {
                    ActOnCommitCheckFailure(message.Envelope, message.CorrelationId, commitCheck);
                    return;
                }

                if (message.HardDelete)
                {
                    // HARD DELETE
                    const long expectedVersion = EventNumber.DeletedStream - 1;
                    var        record          = LogRecord.DeleteTombstone(Writer.Checkpoint.ReadNonFlushed(), message.CorrelationId,
                                                                           eventId, message.EventStreamId, expectedVersion, PrepareFlags.IsCommitted);
                    var res = WritePrepareWithRetry(record);
                    _indexWriter.PreCommit(new[] { res.Prepare });
                }
                else
                {
                    // SOFT DELETE
                    var metastreamId         = SystemStreams.MetastreamOf(message.EventStreamId);
                    var expectedVersion      = _indexWriter.GetStreamLastEventNumber(metastreamId);
                    var logPosition          = Writer.Checkpoint.ReadNonFlushed();
                    const PrepareFlags flags = PrepareFlags.SingleWrite | PrepareFlags.IsCommitted | PrepareFlags.IsJson;
                    var data = new StreamMetadata(truncateBefore: EventNumber.DeletedStream).ToJsonBytes();
                    var res  = WritePrepareWithRetry(
                        LogRecord.Prepare(logPosition, message.CorrelationId, eventId, logPosition, 0,
                                          metastreamId, expectedVersion, flags, SystemEventTypes.StreamMetadata,
                                          data, null));
                    _indexWriter.PreCommit(new[] { res.Prepare });
                }
            }
            catch (Exception exc)
            {
                Log.ErrorException(exc, "Exception in writer.");
                throw;
            }
            finally
            {
                Flush();
            }
        }
        public IList <IPrepareLogRecord <TStreamId> > CreatePrepareLogRecords(string stream, int expectedVersion, IList <string> eventTypes, IList <Guid> eventIds, long transactionPosition)
        {
            if (eventIds.Count != eventTypes.Count)
            {
                throw new Exception("eventType and eventIds length mismatch!");
            }
            if (eventIds.Count == 0)
            {
                throw new Exception("eventIds is empty");
            }
            if (eventIds.Count == 1)
            {
                return(CreatePrepareLogRecord(stream, expectedVersion, eventTypes[0], eventIds[0], transactionPosition));
            }

            var numEvents = eventTypes.Count;
            var logFormat = LogFormatHelper <TLogFormat, TStreamId> .LogFormat;

            var prepares = new List <IPrepareLogRecord <TStreamId> >();

            logFormat.StreamNameIndex.GetOrAddId(stream, out var streamId, out _, out _);
            for (var i = 0; i < numEvents; i++)
            {
                PrepareFlags flags = PrepareFlags.Data | PrepareFlags.IsCommitted;
                if (i == 0)
                {
                    flags |= PrepareFlags.TransactionBegin;
                }
                if (i == numEvents - 1)
                {
                    flags |= PrepareFlags.TransactionEnd;
                }

                prepares.Add(
                    PrepareLogRecord.Prepare(
                        logFormat.RecordFactory,
                        transactionPosition + RecordOffset * i,
                        Guid.NewGuid(),
                        eventIds[i],
                        transactionPosition,
                        i,
                        streamId,
                        expectedVersion + i,
                        flags,
                        eventTypes[i],
                        new byte[0],
                        new byte[0],
                        DateTime.Now
                        ));
            }

            return(prepares);
        }
Пример #8
0
        public IList <PrepareLogRecord> CreatePrepareLogRecords(string stream, int expectedVersion, IList <string> eventTypes, IList <Guid> eventIds, long transactionPosition)
        {
            if (eventIds.Count != eventTypes.Count)
            {
                throw new Exception("eventType and eventIds length mismatch!");
            }
            if (eventIds.Count == 0)
            {
                throw new Exception("eventIds is empty");
            }
            if (eventIds.Count == 1)
            {
                return(CreatePrepareLogRecord(stream, expectedVersion, eventTypes[0], eventIds[0], transactionPosition));
            }

            var numEvents = eventTypes.Count;

            var prepares = new List <PrepareLogRecord>();

            for (var i = 0; i < numEvents; i++)
            {
                PrepareFlags flags = PrepareFlags.Data | PrepareFlags.IsCommitted;
                if (i == 0)
                {
                    flags |= PrepareFlags.TransactionBegin;
                }
                if (i == numEvents - 1)
                {
                    flags |= PrepareFlags.TransactionEnd;
                }

                prepares.Add(
                    new PrepareLogRecord(
                        transactionPosition + RecordOffset * i,
                        Guid.NewGuid(),
                        eventIds[i],
                        transactionPosition,
                        i,
                        stream,
                        expectedVersion + i,
                        DateTime.Now,
                        flags,
                        eventTypes[i],
                        new byte[0],
                        new byte[0]
                        )
                    );
            }

            return(prepares);
        }
        public Rec(RecType type, int transaction, string streamId, string eventType, DateTime?timestamp, byte version, StreamMetadata metadata = null, PrepareFlags prepareFlags = PrepareFlags.Data)
        {
            Ensure.NotNullOrEmpty(streamId, "streamId");
            Ensure.Nonnegative(transaction, "transaction");

            Type         = type;
            Id           = Guid.NewGuid();
            Transaction  = transaction;
            StreamId     = streamId;
            EventType    = eventType ?? string.Empty;
            TimeStamp    = timestamp ?? DateTime.UtcNow;
            Version      = version;
            Metadata     = metadata;
            PrepareFlags = prepareFlags;
        }
Пример #10
0
        public InternalPrepareRequest(string cqlQuery, string keyspace = null, IDictionary <string, byte[]> payload = null)
        {
            Query    = cqlQuery;
            Keyspace = keyspace;
            _payload = payload;
            if (payload != null)
            {
                _headerFlags |= FrameHeader.HeaderFlag.CustomPayload;
            }

            if (keyspace != null)
            {
                _prepareFlags |= PrepareFlags.WithKeyspace;
            }
        }
        public LogV3StreamWriteRecord(
            long logPosition,
            long transactionPosition,
            int transactionOffset,
            Guid correlationId,
            Guid eventId,
            StreamId eventStreamId,
            long expectedVersion,
            DateTime timeStamp,
            PrepareFlags flags,
            string eventType,
            ReadOnlySpan <byte> data,
            ReadOnlySpan <byte> metadata)
        {
            Ensure.Nonnegative(logPosition, "logPosition");
            Ensure.NotEmptyGuid(correlationId, "correlationId");
            Ensure.NotEmptyGuid(eventId, "eventId");
            Ensure.Nonnegative(transactionPosition, "transactionPosition");
            if (transactionOffset < -1)
            {
                throw new ArgumentOutOfRangeException("transactionOffset");
            }
            if (eventStreamId < LogV3SystemStreams.FirstVirtualStream)
            {
                throw new ArgumentOutOfRangeException("eventStreamId", eventStreamId, null);
            }
            if (expectedVersion < Core.Data.ExpectedVersion.Any)
            {
                throw new ArgumentOutOfRangeException("expectedVersion");
            }
            eventType ??= "";

            Record = RecordCreator.CreateStreamWriteRecordForSingleEvent(
                timeStamp: timeStamp,
                correlationId: correlationId,
                logPosition: logPosition,
                transactionPosition: transactionPosition,
                transactionOffset: transactionOffset,
                streamNumber: eventStreamId,
                startingEventNumber: expectedVersion + 1,
                eventId: eventId,
                // temporarily storing the event type as the system metadata. later it will have a number.
                eventType: eventType,
                eventData: data,
                eventMetadata: metadata,
                // todo: translate
                eventFlags: (Raw.EventFlags)flags);
        }
Пример #12
0
 public IPrepareLogRecord <long> CreatePrepare(
     long logPosition,
     Guid correlationId,
     Guid eventId,
     long transactionPosition,
     int transactionOffset,
     long eventStreamId,
     long expectedVersion,
     DateTime timeStamp,
     PrepareFlags flags,
     string eventType,
     ReadOnlyMemory <byte> data,
     ReadOnlyMemory <byte> metadata)
 {
     throw new NotImplementedException();
 }
Пример #13
0
        public PrepareLogRecord(long logPosition,
                                Guid correlationId,
                                Guid eventId,
                                long transactionPosition,
                                int transactionOffset,
                                string eventStreamId,
                                long expectedVersion,
                                DateTime timeStamp,
                                PrepareFlags flags,
                                string eventType,
                                byte[] data,
                                byte[] metadata,
                                byte prepareRecordVersion = PrepareRecordVersion)
            : base(LogRecordType.Prepare, prepareRecordVersion, logPosition)
        {
            Ensure.NotEmptyGuid(correlationId, "correlationId");
            Ensure.NotEmptyGuid(eventId, "eventId");
            Ensure.Nonnegative(transactionPosition, "transactionPosition");
            if (transactionOffset < -1)
            {
                throw new ArgumentOutOfRangeException("transactionOffset");
            }
            Ensure.NotNullOrEmpty(eventStreamId, "eventStreamId");
            if (expectedVersion < Core.Data.ExpectedVersion.Any)
            {
                throw new ArgumentOutOfRangeException("expectedVersion");
            }
            Ensure.NotNull(data, "data");

            Flags = flags;
            TransactionPosition = transactionPosition;
            TransactionOffset   = transactionOffset;
            ExpectedVersion     = expectedVersion;
            EventStreamId       = eventStreamId;

            EventId       = eventId;
            CorrelationId = correlationId;
            TimeStamp     = timeStamp;
            EventType     = eventType ?? string.Empty;
            Data          = data;
            Metadata      = metadata ?? NoData;
            if (InMemorySize > TFConsts.MaxLogRecordSize)
            {
                throw new Exception("Record too large.");
            }
        }
Пример #14
0
 private LogRecord CreateLogRecordV0(Rec rec, TransactionInfo transInfo, int transOffset, long logPos,
                                     long expectedVersion, byte[] data, PrepareFlags flags)
 {
     return(new PrepareLogRecord(logPos,
                                 Guid.NewGuid(),
                                 rec.Id,
                                 transInfo.TransactionPosition,
                                 transOffset,
                                 rec.StreamId,
                                 expectedVersion,
                                 rec.TimeStamp,
                                 flags,
                                 rec.EventType,
                                 data,
                                 null,
                                 LogRecordVersion.LogRecordV0));
 }
Пример #15
0
        public EventRecord(int eventNumber, PrepareLogRecord prepare)
        {
            Ensure.Nonnegative(eventNumber, "eventNumber");

            EventNumber         = eventNumber;
            LogPosition         = prepare.LogPosition;
            CorrelationId       = prepare.CorrelationId;
            EventId             = prepare.EventId;
            TransactionPosition = prepare.TransactionPosition;
            TransactionOffset   = prepare.TransactionOffset;
            EventStreamId       = prepare.EventStreamId;
            ExpectedVersion     = prepare.ExpectedVersion;
            TimeStamp           = prepare.TimeStamp;
            Flags     = prepare.Flags;
            EventType = prepare.EventType ?? string.Empty;
            Data      = prepare.Data ?? Empty.ByteArray;
            Metadata  = prepare.Metadata ?? Empty.ByteArray;
        }
Пример #16
0
        public EventRecord(long eventNumber, IPrepareLogRecord prepare, string eventStreamId, string eventType)
        {
            Ensure.Nonnegative(eventNumber, "eventNumber");
            Ensure.NotNull(eventStreamId, "eventStreamId");

            EventNumber         = eventNumber;
            LogPosition         = prepare.LogPosition;
            CorrelationId       = prepare.CorrelationId;
            EventId             = prepare.EventId;
            TransactionPosition = prepare.TransactionPosition;
            TransactionOffset   = prepare.TransactionOffset;
            EventStreamId       = eventStreamId;
            ExpectedVersion     = prepare.ExpectedVersion;
            TimeStamp           = prepare.TimeStamp;

            Flags     = prepare.Flags;
            EventType = eventType ?? string.Empty;
            Data      = prepare.Data;
            Metadata  = prepare.Metadata;
        }
Пример #17
0
        internal PrepareLogRecord(BinaryReader reader, byte version) : base(LogRecordType.Prepare, version)
        {
            LogPosition         = reader.ReadInt64();
            Flags               = (PrepareFlags)reader.ReadUInt16();
            TransactionPosition = reader.ReadInt64();
            ExpectedVersion     = reader.ReadInt32();
            EventStreamId       = reader.ReadString();
            EventId             = new Guid(reader.ReadBytes(16));
            CorrelationId       = new Guid(reader.ReadBytes(16));
            TimeStamp           = new DateTime(reader.ReadInt64());
            EventType           = reader.ReadString();

            var dataCount = reader.ReadInt32();

            Data = dataCount == 0 ? NoData : reader.ReadBytes(dataCount);

            var metadataCount = reader.ReadInt32();

            Metadata = metadataCount == 0 ? NoData : reader.ReadBytes(metadataCount);
        }
Пример #18
0
        // called from tests only
        public EventRecord(long eventNumber,
                           long logPosition,
                           Guid correlationId,
                           Guid eventId,
                           long transactionPosition,
                           int transactionOffset,
                           string eventStreamId,
                           long expectedVersion,
                           DateTime timeStamp,
                           PrepareFlags flags,
                           string eventType,
                           byte[] data,
                           byte[] metadata)
        {
            Ensure.Nonnegative(logPosition, "logPosition");
            Ensure.Nonnegative(transactionPosition, "transactionPosition");
            if (transactionOffset < -1)
            {
                throw new ArgumentOutOfRangeException("transactionOffset");
            }
            Ensure.NotNull(eventStreamId, "eventStreamId");
            Ensure.Nonnegative(eventNumber, "eventNumber");
            Ensure.NotEmptyGuid(eventId, "eventId");
            Ensure.NotNull(data, "data");

            EventNumber         = eventNumber;
            LogPosition         = logPosition;
            CorrelationId       = correlationId;
            EventId             = eventId;
            TransactionPosition = transactionPosition;
            TransactionOffset   = transactionOffset;
            EventStreamId       = eventStreamId;
            ExpectedVersion     = expectedVersion;
            TimeStamp           = timeStamp;
            Flags     = flags;
            EventType = eventType ?? string.Empty;
            Data      = data ?? Empty.ByteArray;
            Metadata  = metadata ?? Empty.ByteArray;
        }
Пример #19
0
        protected IPrepareLogRecord <TStreamId> WritePrepare(string eventStreamName,
                                                             long expectedVersion,
                                                             Guid eventId                 = default(Guid),
                                                             string eventType             = null,
                                                             string data                  = null,
                                                             PrepareFlags additionalFlags = PrepareFlags.None)
        {
            GetOrReserve(eventStreamName, out var eventStreamId, out var pos);
            var prepare = LogRecord.SingleWrite(_recordFactory, pos,
                                                Guid.NewGuid(),
                                                eventId == default(Guid) ? Guid.NewGuid() : eventId,
                                                eventStreamId,
                                                expectedVersion,
                                                eventType.IsEmptyString() ? "some-type" : eventType,
                                                data.IsEmptyString() ? LogRecord.NoData : Helper.UTF8NoBom.GetBytes(data),
                                                LogRecord.NoData,
                                                DateTime.UtcNow,
                                                additionalFlags);

            Assert.IsTrue(Writer.Write(prepare, out pos));

            return(prepare);
        }
Пример #20
0
        public PrepareLogRecord(long logPosition,
                                Guid correlationId,
                                Guid eventId,
                                long transactionPosition,
                                string eventStreamId,
                                int expectedVersion,
                                DateTime timeStamp,
                                PrepareFlags flags,
                                string eventType,
                                byte[] data,
                                byte[] metadata)
            : base(LogRecordType.Prepare, PrepareRecordVersion)
        {
            Ensure.Nonnegative(logPosition, "logPosition");
            Ensure.NotEmptyGuid(correlationId, "correlationId");
            Ensure.NotEmptyGuid(eventId, "eventId");
            Ensure.Nonnegative(transactionPosition, "transactionPosition");
            Ensure.NotNullOrEmpty(eventStreamId, "eventStreamId");
            if (expectedVersion < Core.Data.ExpectedVersion.Any)
            {
                throw new ArgumentOutOfRangeException("expectedVersion");
            }
            Ensure.NotNull(data, "data");

            LogPosition         = logPosition;
            Flags               = flags;
            TransactionPosition = transactionPosition;
            ExpectedVersion     = expectedVersion;
            EventStreamId       = eventStreamId;

            EventId       = eventId;
            CorrelationId = correlationId;
            TimeStamp     = timeStamp;
            EventType     = eventType ?? string.Empty;
            Data          = data;
            Metadata      = metadata ?? NoData;
        }
Пример #21
0
        internal PrepareLogRecord(BinaryReader reader, byte version, long logPosition) : base(LogRecordType.Prepare,
                                                                                              version, logPosition)
        {
            if (version != LogRecordVersion.LogRecordV0 && version != LogRecordVersion.LogRecordV1)
            {
                throw new ArgumentException(string.Format(
                                                "PrepareRecord version {0} is incorrect. Supported version: {1}.", version, PrepareRecordVersion));
            }

            Flags = (PrepareFlags)reader.ReadUInt16();
            TransactionPosition = reader.ReadInt64();
            TransactionOffset   = reader.ReadInt32();
            ExpectedVersion     = version == LogRecordVersion.LogRecordV0 ? reader.ReadInt32() : reader.ReadInt64();

            if (version == LogRecordVersion.LogRecordV0)
            {
                ExpectedVersion = ExpectedVersion == int.MaxValue - 1 ? long.MaxValue - 1 : ExpectedVersion;
            }

            EventStreamId = reader.ReadString();
            EventId       = new Guid(reader.ReadBytes(16));
            CorrelationId = new Guid(reader.ReadBytes(16));
            TimeStamp     = new DateTime(reader.ReadInt64());
            EventType     = reader.ReadString();

            var dataCount = reader.ReadInt32();

            Data = dataCount == 0 ? NoData : reader.ReadBytes(dataCount);

            var metadataCount = reader.ReadInt32();

            Metadata = metadataCount == 0 ? NoData : reader.ReadBytes(metadataCount);
            if (InMemorySize > TFConsts.MaxLogRecordSize)
            {
                throw new Exception("Record too large.");
            }
        }
Пример #22
0
 public PrepareAck(Guid correlationId, long logPosition, PrepareFlags flags)
 {
     CorrelationId = correlationId;
     LogPosition = logPosition;
     Flags = flags;
 }
Пример #23
0
 public static PrepareLogRecord DeleteTombstone(long logPosition, Guid correlationId, Guid eventId,
                                                string eventStreamId, int expectedVersion, PrepareFlags additionalFlags = PrepareFlags.None)
 {
     return new PrepareLogRecord(logPosition, correlationId, eventId, logPosition, 0, eventStreamId, 
                                 expectedVersion, DateTime.UtcNow, 
                                 PrepareFlags.StreamDelete | PrepareFlags.TransactionBegin | PrepareFlags.TransactionEnd | additionalFlags, 
                                 SystemEventTypes.StreamDeleted, NoData, NoData);
 }
Пример #24
0
        protected EventRecord WriteTransactionEvent(Guid correlationId,
                                                    long transactionPos,
                                                    int transactionOffset,
                                                    string eventStreamId,
                                                    int eventNumber,
                                                    string eventData,
                                                    PrepareFlags flags,
                                                    bool retryOnFail = false)
        {
            var prepare = LogRecord.Prepare(WriterCheckpoint.ReadNonFlushed(),
                                            correlationId,
                                            Guid.NewGuid(),
                                            transactionPos,
                                            transactionOffset,
                                            eventStreamId,
                                            ExpectedVersion.Any,
                                            flags,
                                            "some-type",
                                            Helper.UTF8NoBom.GetBytes(eventData),
                                            null);

            if (retryOnFail)
            {
                long firstPos = prepare.LogPosition;
                long newPos;
                if (!Writer.Write(prepare, out newPos))
                {
                    var tPos = prepare.TransactionPosition == prepare.LogPosition ? newPos : prepare.TransactionPosition;
                    prepare = new PrepareLogRecord(newPos,
                                                   prepare.CorrelationId,
                                                   prepare.EventId,
                                                   tPos,
                                                   prepare.TransactionOffset,
                                                   prepare.EventStreamId,
                                                   prepare.ExpectedVersion,
                                                   prepare.TimeStamp,
                                                   prepare.Flags,
                                                   prepare.EventType,
                                                   prepare.Data,
                                                   prepare.Metadata);
                    if (!Writer.Write(prepare, out newPos))
                        Assert.Fail("Second write try failed when first writing prepare at {0}, then at {1}.", firstPos, prepare.LogPosition);
                }
                return new EventRecord(eventNumber, prepare);
            }

            long pos;
            Assert.IsTrue(Writer.Write(prepare, out pos));
            return new EventRecord(eventNumber, prepare);
        }
            public PrepareAck(Guid correlationId, IPEndPoint vnodeEndPoint, long logPosition, PrepareFlags flags)
            {
                Ensure.NotEmptyGuid(correlationId, "correlationId");
                Ensure.NotNull(vnodeEndPoint, "vnodeEndPoint");
                Ensure.Nonnegative(logPosition, "logPosition");

                CorrelationId = correlationId;
                VNodeEndPoint = vnodeEndPoint;
                LogPosition = logPosition;
                Flags = flags;
            }
Пример #26
0
 public static bool HasNoneOf(this PrepareFlags flags, PrepareFlags flagSet)
 {
     return((flags & flagSet) == 0);
 }
Пример #27
0
 public static bool HasAnyOf(this PrepareFlags flags, PrepareFlags flagSet)
 {
     return((flags & flagSet) != 0);
 }
Пример #28
0
 public static bool HasAllOf(this PrepareFlags flags, PrepareFlags flagSet)
 {
     return((flags & flagSet) == flagSet);
 }
Пример #29
0
 public static PrepareLogRecord DeleteTombstone(long logPosition, Guid correlationId, Guid eventId,
                                                string eventStreamId, int expectedVersion, PrepareFlags additionalFlags = PrepareFlags.None)
 {
     return(new PrepareLogRecord(logPosition, correlationId, eventId, logPosition, 0, eventStreamId,
                                 expectedVersion, DateTime.UtcNow,
                                 PrepareFlags.StreamDelete | PrepareFlags.TransactionBegin | PrepareFlags.TransactionEnd | additionalFlags,
                                 SystemEventTypes.StreamDeleted, NoData, NoData));
 }
Пример #30
0
 public static PrepareLogRecord Prepare(long logPosition, Guid correlationId, Guid eventId, long transactionPos, int transactionOffset,
                                        string eventStreamId, int expectedVersion, PrepareFlags flags, string eventType,
                                        byte[] data, byte[] metadata, DateTime?timeStamp = null)
 {
     return(new PrepareLogRecord(logPosition, correlationId, eventId, transactionPos, transactionOffset,
                                 eventStreamId, expectedVersion, timeStamp ?? DateTime.UtcNow, flags, eventType,
                                 data, metadata));
 }
Пример #31
0
        public PrepareLogRecord(long logPosition,
                                Guid correlationId,
                                Guid eventId,
                                long transactionPosition,
                                int transactionOffset,
                                string eventStreamId,
                                long expectedVersion,
                                DateTime timeStamp,
                                PrepareFlags flags,
                                string eventType,
                                byte[] data,
                                byte[] metadata,
                                byte prepareRecordVersion = PrepareRecordVersion)
            : base(LogRecordType.Prepare, prepareRecordVersion, logPosition)
        {
            Ensure.NotEmptyGuid(correlationId, "correlationId");
            Ensure.NotEmptyGuid(eventId, "eventId");
            Ensure.Nonnegative(transactionPosition, "transactionPosition");
            if (transactionOffset < -1)
            {
                throw new ArgumentOutOfRangeException("transactionOffset");
            }
            Ensure.NotNullOrEmpty(eventStreamId, "eventStreamId");
            if (expectedVersion < Core.Data.ExpectedVersion.Any)
            {
                throw new ArgumentOutOfRangeException("expectedVersion");
            }
            Ensure.NotNull(data, "data");

            Flags = flags;
            TransactionPosition = transactionPosition;
            TransactionOffset   = transactionOffset;
            ExpectedVersion     = expectedVersion;
            EventStreamId       = eventStreamId;

            EventId       = eventId;
            CorrelationId = correlationId;
            TimeStamp     = timeStamp;
            EventType     = eventType ?? string.Empty;
            Data          = data;

            if (metadata == null)
            {
                Metadata = NoData;
            }
            else
            {
                var json = Encoding.UTF8.GetString(metadata);
                if (string.IsNullOrWhiteSpace(json))
                {
                    Metadata = NoData;
                }
                else
                {
                    const string pattern = @"""createdEpochOverride"":(\d{13})";
                    var          match   = Regex.Match(json, pattern);
                    if (!match.Success)
                    {
                        Metadata = metadata;
                    }
                    else
                    {
                        // Override timestamp.
                        var createdEpochOverride = long.Parse(match.Groups[1].Value);
                        var dt = new DateTime(1970, 1, 1);
                        dt        = dt.AddMilliseconds(createdEpochOverride);
                        TimeStamp = dt;

                        // Remove createdEpochOverride from metadata.
                        json     = Regex.Replace(json, @"""createdEpochOverride"":\d{13}", string.Empty);
                        json     = Regex.Replace(json, @",\}$", "}");
                        Metadata = Encoding.UTF8.GetBytes(json);
                    }
                }
            }

            if (InMemorySize > TFConsts.MaxLogRecordSize)
            {
                throw new Exception("Record too large.");
            }
        }
Пример #32
0
        private PrepareLogRecord WriteTransactionEventV0(Guid correlationId, long logPosition, long transactionPosition,
                                                         int transactionOffset, string eventStreamId, long eventNumber, string eventData, PrepareFlags flags)
        {
            var prepare = new PrepareLogRecord(logPosition, correlationId, Guid.NewGuid(), transactionPosition,
                                               transactionOffset,
                                               eventStreamId, ExpectedVersion.Any, DateTime.UtcNow, flags,
                                               "testEventType", Encoding.UTF8.GetBytes(eventData), new byte[0],
                                               LogRecordVersion.LogRecordV0);
            long pos;

            Writer.Write(prepare, out pos);
            return(prepare);
        }
 public static Rec Prepare(int transaction, string stream, string eventType = null, DateTime?timestamp = null, StreamMetadata metadata = null, PrepareFlags prepareFlags = PrepareFlags.Data, byte version = PrepareLogRecord.PrepareRecordVersion)
 {
     return(new Rec(RecType.Prepare, transaction, stream, eventType, timestamp, version, metadata, prepareFlags));
 }
Пример #34
0
        private LogRecord CreateLogRecordV0(Rec rec, TransactionInfo transInfo, int transOffset, long logPos,
                                            long expectedVersion, ReadOnlyMemory <byte> data, PrepareFlags flags)
        {
            LogFormatHelper <TLogFormat, TStreamId> .EnsureV0PrepareSupported();

            return(new PrepareLogRecord(logPos,
                                        Guid.NewGuid(),
                                        rec.Id,
                                        transInfo.TransactionPosition,
                                        transOffset,
                                        rec.StreamId,
                                        expectedVersion,
                                        rec.TimeStamp,
                                        flags,
                                        rec.EventType,
                                        data,
                                        null,
                                        LogRecordVersion.LogRecordV0));
        }
Пример #35
0
            public PrepareAck(Guid correlationId, long logPosition, PrepareFlags flags)
            {
                Ensure.NotEmptyGuid(correlationId, "correlationId");
                Ensure.Nonnegative(logPosition, "logPosition");

                CorrelationId = correlationId;
                LogPosition = logPosition;
                Flags = flags;
            }
Пример #36
0
 public PrepareAck(Guid correlationId, long logPosition, PrepareFlags flags)
 {
     CorrelationId = correlationId;
     LogPosition   = logPosition;
     Flags         = flags;
 }