public static IEnumerable <object[]> MetadataTestCases() { var builder = StreamMetadata.Create( maxCount: 0xDEAD, maxAge: TimeSpan.FromSeconds(0xFAD), truncateBefore: 0xBEEF, cacheControl: TimeSpan.FromSeconds(0xF00L), acl: new StreamAcl(SystemRoles.All, SystemRoles.All, SystemRoles.All, SystemRoles.All, SystemRoles.All)).Copy() .SetCustomProperty(nameof(String), "f") .SetCustomProperty(nameof(Int32), 1) .SetCustomProperty(nameof(Double), 2.0) .SetCustomProperty(nameof(Int64), int.MaxValue + 1L) .SetCustomProperty(nameof(Boolean), true) .SetCustomProperty(nameof(Nullable), default(int?)) .SetCustomPropertyWithValueAsRawJsonString(nameof(JObject), @"{ ""subProperty"": 999 }"); var createStreams = new[] { true, false }; var metadatas = new[] { builder, null }; foreach (var(expectedVersion, displayName) in ExpectedVersions) { foreach (var useSsl in UseSsl) { foreach (var createStream in createStreams) { foreach (var metadata in metadatas) { yield return(new object[] { expectedVersion, displayName, useSsl, createStream, metadata }); } } } } }
public async Task for_existing_stream_returns_set_metadata(bool useSsl) { var streamName = $"{GetStreamName()}_{useSsl}"; var connection = _fixture.Connections[useSsl]; var metadata = StreamMetadata.Create( maxCount: 0xDEAD, maxAge: TimeSpan.FromSeconds(0xFAD), truncateBefore: 0xBEEF, cacheControl: TimeSpan.FromSeconds(0xF00L), acl: new StreamAcl(SystemRoles.All, SystemRoles.All, SystemRoles.All, SystemRoles.All, SystemRoles.All)); await connection.SetStreamMetadataAsync(streamName, ExpectedVersion.NoStream, metadata).WithTimeout(); var meta = await connection.GetStreamMetadataAsync(streamName).WithTimeout(); Assert.Equal(streamName, meta.Stream); Assert.False(meta.IsStreamDeleted); Assert.Equal(0, meta.MetastreamVersion); Assert.Equal(metadata.MaxCount, meta.StreamMetadata.MaxCount); Assert.Equal(metadata.MaxAge, meta.StreamMetadata.MaxAge); Assert.Equal(metadata.TruncateBefore, meta.StreamMetadata.TruncateBefore); Assert.Equal(metadata.CacheControl, meta.StreamMetadata.CacheControl); Assert.Equal(metadata.Acl.ReadRoles, meta.StreamMetadata.Acl.ReadRoles); Assert.Equal(metadata.Acl.WriteRoles, meta.StreamMetadata.Acl.WriteRoles); Assert.Equal(metadata.Acl.DeleteRoles, meta.StreamMetadata.Acl.DeleteRoles); Assert.Equal(metadata.Acl.MetaReadRoles, meta.StreamMetadata.Acl.MetaReadRoles); Assert.Equal(metadata.Acl.MetaWriteRoles, meta.StreamMetadata.Acl.MetaWriteRoles); }
public static async Task SetEventMaxAgeIfNull(this IEventStoreConnection connection, string streamName, TimeSpan maxAge) { for (int i = 0; i < 3; ++i) { var readRes = await connection.GetStreamMetadataAsync(streamName); if (readRes.IsStreamDeleted || readRes.StreamMetadata.MaxAge != null) { return; } var metadata = StreamMetadata.Create(maxAge: maxAge); try { await connection.SetStreamMetadataAsync(streamName, readRes.MetastreamVersion, metadata); return; } catch (WrongExpectedVersionException) { } await Task.Delay((i + 1) * 100); } }
/// <summary> /// Truncates the stream TCP. /// </summary> /// <param name="eventStoreConnection">The event store connection.</param> /// <param name="stream">The stream.</param> /// <param name="truncateBefore">The truncate before.</param> private static async Task TruncateStreamTcp(IEventStoreConnection eventStoreConnection, String stream, Int32 truncateBefore) { await eventStoreConnection.SetStreamMetadataAsync(stream, -2, StreamMetadata.Create(truncateBefore: truncateBefore)); Console.WriteLine($"truncate stream tcp {stream}"); }
async Task TryWriteInitialMetadata(Flow flow) { try { await _context.SetCheckpointStreamMetadata(flow, StreamMetadata.Create(maxCount: 1)); } catch (Exception error) { Log.Error(error, "Failed to set initial metadata of checkpoint stream for {Key}. The stream will contain data for every checkpoint, not just the latest.", flow); } }
async Task TryWriteDoneMetadata(Flow flow, long position) { try { await _context.SetCheckpointStreamMetadata(flow, StreamMetadata.Create(maxCount: 1, truncateBefore: position)); } catch (Exception error) { Log.Warning(error, "Failed to set done metadata of checkpoint stream for {Key}. The timeline can recover from this, but it warrants further investigation.", flow); } }
async Task SetStreamMaxCount() { var metadata = await _connection.GetStreamMetadataAsync(_streamName); if (!metadata.StreamMetadata.MaxCount.HasValue) { await _connection.SetStreamMetadataAsync( _streamName, ExpectedVersion.Any, StreamMetadata.Create(1) ); } }
public Task SoftDeleteTestStream() { var deleteRequest = CreateRequest(TestMetadataStream, "", "POST", "application/json"); deleteRequest.Headers.Add("ES-EventType", SystemEventTypes.StreamMetadata); deleteRequest.Headers.Add("ES-ExpectedVersion", ExpectedVersion.Any.ToString()); deleteRequest.Headers.Add("ES-EventId", Guid.NewGuid().ToString()); deleteRequest.Content = new ByteArrayContent(StreamMetadata .Create(truncateBefore: long.MaxValue) .AsJsonBytes()) { Headers = { ContentType = new MediaTypeHeaderValue("application/json") } }; return(GetRequestResponse(deleteRequest)); }
GetStreamMetadataAsync(string stream, UserCredentials userCredentials = null) { return(GetStreamMetadataAsRawBytesAsync(stream, userCredentials).ContinueWith(t => { ThrowIfNotNull(t.Exception); var res = t.Result; if (res.StreamMetadata == null || res.StreamMetadata.Length == 0) { return new StreamMetadataResult(res.Stream, res.IsStreamDeleted, res.MetastreamVersion, StreamMetadata.Create(), res.MetaEventCreated); } var metadata = StreamMetadata.FromJsonBytes(res.StreamMetadata); return new StreamMetadataResult(res.Stream, res.IsStreamDeleted, res.MetastreamVersion, metadata, res.MetaEventCreated); })); }
public static async Task SetDefaultStreamMetaData(this IEventStoreConnection conn, string stream) { try { var acl = new StreamAcl("$admins", "$admins", "$admins", "$admins", "$admins"); var metaData = StreamMetadata.Create(acl: acl); var result = await conn.SetStreamMetadataAsync(stream, ExpectedVersion.Any, metaData); return; } catch (Exception e) { Console.WriteLine(e.Message); throw; } }
GetStreamMetadataAsync(string stream, UserCredentials userCredentials = null) { return(GetStreamMetadataAsRawBytesAsync(stream, GetUserCredentials(_settings, userCredentials)) .ContinueWith(t => { if (t.Exception != null) { throw t.Exception.InnerException; } var res = t.Result; if (res.StreamMetadata == null || res.StreamMetadata.Length == 0) { return new StreamMetadataResult(res.Stream, res.IsStreamDeleted, res.MetastreamVersion, StreamMetadata.Create()); } var metadata = StreamMetadata.FromJsonBytes(res.StreamMetadata); return new StreamMetadataResult(res.Stream, res.IsStreamDeleted, res.MetastreamVersion, metadata); })); }
protected override async Task DeleteMessagesToAsync(string persistenceId, long toSequenceNr) { if (toSequenceNr == long.MaxValue) { var slice = await _conn.ReadStreamEventsBackwardAsync(persistenceId, StreamPosition.End, 1, false); if (slice.Events.Any()) { var @event = slice.Events.First(); var highestEventPosition = @event.OriginalEventNumber; await _conn.SetStreamMetadataAsync(persistenceId, ExpectedVersion.Any, StreamMetadata.Create(truncateBefore: highestEventPosition + 1)); } } else { await _conn.SetStreamMetadataAsync(persistenceId, ExpectedVersion.Any, StreamMetadata.Create(truncateBefore: toSequenceNr)); } }
public async Task What_happens_when_you_delete_a_stream_that_has_metadata() { await _node.StartAndWaitUntilInitialized(); using (var connection = EmbeddedEventStoreConnection.Create(_node, _connectionSettingsBuilder)) { string streamId = "stream-1"; var eventData = new EventData(Guid.NewGuid(), "type", false, null, null); await connection.AppendToStreamAsync(streamId, ExpectedVersion.NoStream, eventData); await connection .SetStreamMetadataAsync(streamId, ExpectedVersion.NoStream, StreamMetadata.Create(maxCount: 2)); await connection.DeleteStreamAsync(streamId, ExpectedVersion.Any, true); var metadata = await connection.GetStreamMetadataAsync(streamId); metadata.IsStreamDeleted.ShouldBe(true); } }
public async Task TestPerformance() { for (int i = 0; i < 100; i++) { await connection.AppendToStreamAsync(stream, i - 1, new EventData(Guid.NewGuid(), "test", true, Encoding.UTF8.GetBytes("{}"), StreamMetadata.Create().AsJsonBytes())); } }
public async Task detect_existing_streams_flush() { void CreateNode() { _node = new MiniNode <TLogFormat, TStreamId>( pathname: PathName, dbPath: Path.Combine(PathName, "mini-node-db"), inMemDb: false, streamExistenceFilterSize: 10_000, streamExistenceFilterCheckpointIntervalMs: 100, streamExistenceFilterCheckpointDelayMs: 0); } CreateNode(); await _node.Start(); // GIVEN some streams var normal = "detect_existing_streams_normal"; var meta = "detect_existing_streams_meta"; var committed = "detect_existing_streams_commited_transaction"; var uncommitted = "detect_existing_streams_uncommited_transaction"; EventData[] GenEvents() => Enumerable.Range(0, 10).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray(); var eventsByStream = new Dictionary <string, EventData[]>() { { normal, GenEvents() }, { committed, GenEvents() }, { uncommitted, GenEvents() }, }; var uncommittedTransId = 0L; using (var store = BuildConnection(_node)) { await store.ConnectAsync(); // normal Assert.AreEqual(9, await store .Apply(x => x.AppendToStreamAsync(normal, ExpectedVersion.NoStream, eventsByStream[normal])) .Apply(x => x.NextExpectedVersion)); // meta Assert.AreEqual(0, await store .Apply(x => x.SetStreamMetadataAsync("meta", ExpectedVersion.NoStream, StreamMetadata.Create(maxCount: 5))) .Apply(x => x.NextExpectedVersion)); if (LogFormatHelper <TLogFormat, TStreamId> .IsV2) { // committed Assert.AreEqual(9, await new TransactionalWriter(store, committed) .Apply(x => x.StartTransaction(-1)) .Apply(x => x.Write(eventsByStream[committed])) .Apply(x => x.Commit()) .Apply(x => x.NextExpectedVersion)); // uncommitted uncommittedTransId = await new TransactionalWriter(store, uncommitted) .Apply(x => x.StartTransaction(-1)) .Apply(x => x.Write(eventsByStream[committed])) .Apply(x => x.TransactionId); } // append an event to another stream because the last event is always going to get reinitialised Assert.AreEqual(0, await store .Apply(x => x.AppendToStreamAsync("another-stream", ExpectedVersion.NoStream, TestEvent.NewTestEvent(Guid.NewGuid()))) .Apply(x => x.NextExpectedVersion)); } // WHEN flush and restart await Task.Delay(500); await _node.Shutdown(keepDb : true); CreateNode(); await _node.Start(); // THEN the streams all exist using (var store = BuildConnection(_node)) { await store.ConnectAsync(); // normal Assert.AreEqual(10, await store .Apply(x => x.AppendToStreamAsync(normal, 9, TestEvent.NewTestEvent(Guid.NewGuid()))) .Apply(x => x.NextExpectedVersion)); Assert.AreEqual( eventsByStream[normal].Length + 1, await EventsStream.Count(store, normal)); // meta Assert.AreEqual(1, await store .Apply(x => x.SetStreamMetadataAsync("meta", 0, StreamMetadata.Create(maxCount: 6))) .Apply(x => x.NextExpectedVersion)); Assert.AreEqual(0, await EventsStream.Count(store, meta)); if (LogFormatHelper <TLogFormat, TStreamId> .IsV2) { // committed Assert.AreEqual(10, await store .Apply(x => x.AppendToStreamAsync(committed, 9, TestEvent.NewTestEvent(Guid.NewGuid()))) .Apply(x => x.NextExpectedVersion)); Assert.AreEqual( eventsByStream[committed].Length + 1, await EventsStream.Count(store, committed)); // uncommitted Assert.AreEqual(9, await new TransactionalWriter(store, uncommitted) .Apply(x => x.ContinueTransaction(uncommittedTransId)) .Apply(x => x.Commit()) .Apply(x => x.NextExpectedVersion)); Assert.AreEqual( eventsByStream[uncommitted].Length, await EventsStream.Count(store, uncommitted)); } } }
/// <summary> /// Start the subscriber. /// </summary> /// <param name="restart">Starting from a restart.</param> public void Start(bool restart = false) { if (_usingHeartbeats) { SendHeartbeat(); _heartbeatTimer.Start(); } lock (_subscriptionLock) { KillSubscription(); if (_liveOnly) { _subscription = _connection.SubscribeToStreamAsync(_streamName, true, (s, e) => EventAppeared(e), SubscriptionDropped).Result; } else { var catchUpSettings = new CatchUpSubscriptionSettings(_maxLiveQueueSize, _catchupPageSize, true, true); _subscription = _connection.SubscribeToStreamFrom(_streamName, _startingPosition, catchUpSettings, (s, e) => EventAppeared(e), LiveProcessingStarted, SubscriptionDropped); } } if (restart) { return; } if (_usingHeartbeats) { _connection.SetStreamMetadataAsync(_heartbeatStreamName, ExpectedVersion.Any, StreamMetadata.Create(2)); } var processor = new Thread(ProcessEvents) { IsBackground = true }; processor.Start(); }
public async Task What_happens_when_you_set_metadata_on_stream_that_does_not_exist() { await _node.StartAndWaitUntilInitialized(); using (var connection = EmbeddedEventStoreConnection.Create(_node, _connectionSettingsBuilder)) { string streamId = "does-not-exist"; var result = await connection .SetStreamMetadataAsync(streamId, ExpectedVersion.NoStream, StreamMetadata.Create(maxCount: 2)); var metadataResult = await connection.GetStreamMetadataAsync(streamId); metadataResult.IsStreamDeleted.ShouldBe(false); var events = await connection.ReadStreamEventsForwardAsync(streamId, StreamPosition.Start, 10, true); events.Status.ShouldBe(SliceReadStatus.StreamNotFound); } }
public async Task for_hard_deleted_stream_returns_default_with_stream_deletion(bool useSsl) { var streamName = $"{GetStreamName()}_{useSsl}"; var connection = _fixture.Connections[useSsl]; await connection.SetStreamMetadataAsync(streamName, ExpectedVersion.NoStream, StreamMetadata.Create()) .WithTimeout(); await connection.DeleteStreamAsync(streamName, ExpectedVersion.Any, true).WithTimeout(); var meta = await connection.GetStreamMetadataAsync(streamName).WithTimeout(); Assert.Equal(streamName, meta.Stream); Assert.True(meta.IsStreamDeleted); Assert.Equal(EventNumber.DeletedStream, meta.MetastreamVersion); Assert.False(meta.StreamMetadata.MaxCount.HasValue); Assert.False(meta.StreamMetadata.MaxAge.HasValue); Assert.False(meta.StreamMetadata.TruncateBefore.HasValue); Assert.False(meta.StreamMetadata.CacheControl.HasValue); }
private async Task AppendEventToStreamAsync() { await connection.AppendToStreamAsync(stream, ExpectedVersion.NoStream, new EventData(Guid.NewGuid(), "test", true, Encoding.UTF8.GetBytes("{}"), StreamMetadata.Create().AsJsonBytes())); }
private async Task AppendEventToStreamAsync() { await this.connection.AppendToStreamAsync(this.streamName, ExpectedVersion.NoStream, new EventData(Guid.NewGuid(), "TestEvent", true, Encoding.UTF8.GetBytes("{}"), StreamMetadata.Create().AsJsonBytes())).AnyContext(); }
public async Task TestPerformance() { var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new TestData { FirstName = "John", LastName = "Doe" })); for (var i = 0; i < 100; i++) { await this.connection.AppendToStreamAsync(this.streamName, i - 1, new EventData(Guid.NewGuid(), "TestEvent", true, data /*Encoding.UTF8.GetBytes("{}")*/, StreamMetadata.Create().AsJsonBytes())).AnyContext(); } }