private async Task CommitIfRelevant(ProjectionPosition p) { if (!BatchingPolicy.ShouldCommit(p)) { return; } await CommitState(); if (p.LooksLikeLatestAvailableInput()) { //performance testing: var now = DateTimeOffset.UtcNow; var timeSpentCatchingUp = now - _startedCatchingUpAt; Console.WriteLine($"Time taken to restore state of projection: {timeSpentCatchingUp.TotalSeconds} seconds"); } }
//This is a quick test for "ShouldCommit" public static void AssertShouldCommit() { var smallBatch = new CommitBatchPolicy(10); var bigBatch = new CommitBatchPolicy(1000); var reloadPositionCatchedUp = new ProjectionPosition(444, 440, 444); var rebuildFirstPositionCatchedUp = new ProjectionPosition(444, null, 444); var reloadCatchingUp900Uncomitted = new ProjectionPosition(999, 99, 8747235); if (smallBatch.ShouldCommit(reloadPositionCatchedUp) == false) { throw new InvalidOperationException("failed expectation: small batch to commit on " + nameof(reloadPositionCatchedUp)); } if (bigBatch.ShouldCommit(reloadPositionCatchedUp) == false) { throw new InvalidOperationException("failed expectation: big batch to commit on " + nameof(reloadPositionCatchedUp)); } if (smallBatch.ShouldCommit(rebuildFirstPositionCatchedUp) == false) { throw new InvalidOperationException("failed expectation: small batch to commit on " + nameof(rebuildFirstPositionCatchedUp)); } if (smallBatch.ShouldCommit(rebuildFirstPositionCatchedUp) == false) { throw new InvalidOperationException("failed expectation: big batch to commit on " + nameof(rebuildFirstPositionCatchedUp)); } if (smallBatch.ShouldCommit(reloadCatchingUp900Uncomitted) == false) { throw new InvalidOperationException("failed expectation: small batch to commit on " + nameof(reloadCatchingUp900Uncomitted)); } if (bigBatch.ShouldCommit(reloadCatchingUp900Uncomitted)) { throw new InvalidOperationException("failed expectation: big batch not to commit on " + nameof(reloadCatchingUp900Uncomitted)); } }
private async Task OnEvent(IAllStreamSubscription subscription, StreamMessage msg, CancellationToken cancelToken) { _lastReadPosition = msg.Position; try { var @event = await Deserialization.Deserialize(msg); var change = _projection.Apply(@event); if (change != string.Empty) { _dmlCollector.AppendLine(change); } await CommitIfRelevant(ProjectionPosition.From(subscription, msg, _lastCommitPosition)); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"WHOOPS: {e}"); } }
//public readonly int EventsPerCommitWhenInputBuffered; //^-- callbacks for "onCatchupStatus" are dispatched as soon as the latest event in //the inputstream has been buffered, this is an "almost there"-state public bool ShouldCommit(ProjectionPosition p) { return(p.LooksLikeLatestAvailableInput() || p.UncommitedLength >= EventsPerCommitWhenCatchingUp); }