Example #1
0
        public HttpResponseMessage GetStream(TenantId tenantId, string aggregateId)
        {
            if (string.IsNullOrEmpty(aggregateId))
            {
                return(null);
            }

            String identity = aggregateId;

            try
            {
                _identityConverter.ToIdentity(aggregateId);
            }
            catch (Exception)
            {
                //it could be a handle
                var handle = _handleMapper.TryTranslate(aggregateId);
                if (handle == null)
                {
                    throw new ArgumentException("Invalid aggregateId", "aggregateId");
                }

                identity = handle.AsString();
            }


            var commits = _commits.GetFrom("Jarvis", identity, 0, int.MaxValue);

            var commitsList = new List <CommitModel>();

            foreach (var commit in commits)
            {
                _enhancer.Enhance(commit);
                commitsList.Add(new CommitModel(commit));
            }
            EventStreamResult result = new EventStreamResult();

            result.Commits     = commitsList;
            result.AggregateId = identity;

            var all = result.ToJson()
                      .Replace("\"_t\"", "\"Type\"")
                      .Replace("ISODate(", "")
                      .Replace("CSUUID(", "")
                      .Replace("\")", "\"");

            var sc = new StringContent(all);

            sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var resp = new HttpResponseMessage {
                Content = sc
            };

            return(resp);
        }
        private PollingClient2.HandlingResult DispatchCommit(ICommit commit)
        {
            try
            {
                if (_stopRequested.IsCancellationRequested)
                {
                    CloseTplChain();
                    return(PollingClient2.HandlingResult.Stop);
                }

                if (commit.StreamId.StartsWith("system.empty"))
                {
                    _logger.DebugFormat("Found empty commit - {0}", commit.CheckpointToken);
                    return(PollingClient2.HandlingResult.MoveToNext);
                }
                _enhancer.Enhance(commit);
                var task = _buffer.SendAsync(commit);
                //wait till the commit is stored in tpl queue
                while (!task.Wait(2000))
                {
                    //maybe the mesh is full, but check for completion
                    if (_stopRequested.IsCancellationRequested)
                    {
                        _buffer.Complete();
                        return(PollingClient2.HandlingResult.Stop);
                    }
                }
                ;
                //Check if block postponed the message, if for some
                //reason the message is postponed, exit from the polling
                if (!task.Result)
                {
                    if (_postErrors > 2)
                    {
                        //TPL is somewhat stuck
                        _logger.ErrorFormat("CommitPollingClient {0}: Unable to dispatch commit {1} into TPL chain, buffer block did not accept message", _id, commit.CheckpointToken);
                        _stopRequested.Cancel();
                    }
                    _postErrors++;
                    _logger.WarnFormat("CommitPollingClient {0}: TPL did not accept message for commit {1}", _id, commit.CheckpointToken);
                    Thread.Sleep(1000); //let some time to flush some messages.
                    return(PollingClient2.HandlingResult.Retry);
                }
                _postErrors             = 0;
                _checkpointTokenCurrent = commit.CheckpointToken;
                return(PollingClient2.HandlingResult.MoveToNext);
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat(ex, "Error in dispatching commit {0} - {1}", commit.CheckpointToken, ex.Message);
                return(PollingClient2.HandlingResult.Stop);
            }
        }
Example #3
0
 public Task <bool> OnNextAsync(IChunk chunk)
 {
     _commitEnhancer.Enhance(chunk);
     if (chunk.Payload is Changeset cs)
     {
         if (_stopCondition(cs))
         {
             return(Task.FromResult(false));
         }
         _readModel.ProcessChangeset(cs);
     }
     return(Task.FromResult(true));
 }
Example #4
0
        private Task <Boolean> DispatchChunk(IChunk chunk)
        {
            try
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.DebugFormat("CommitPollingClient {0}: CommitPollingClient2: Dispatched chunk {1}", _id, chunk.Position);
                }
                if (_stopRequested.IsCancellationRequested)
                {
                    CloseTplChain();
                    return(Task.FromResult(false));
                }

                if (chunk.PartitionId.StartsWith("system.empty"))
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.DebugFormat("CommitPollingClient {0}: CommitPollingClient2: Found empty commit - {1}", _id, chunk.Position);
                    }
                }
                else
                {
                    _enhancer.Enhance(chunk);
                }

                var task = _buffer.SendAsync(chunk);

                //TODO: CHECK if this is really needed, we could simply await
                //This was introduced because if you cancel the polling but the
                //mesh is full and the projections are stopped the mesh will never
                //accept the message again.
                //wait till the commit is stored in tpl queue
                while (!task.Wait(2000))
                {
                    //maybe the mesh is full, but check for completion
                    if (_stopRequested.IsCancellationRequested)
                    {
                        _buffer.Complete();
                        return(Task.FromResult(false));
                    }
                }

                //Check if block postponed the message, if for some
                //reason the message is postponed, exit from the polling
                if (!task.Result)
                {
                    //TPL is somewhat stuck
                    _logger.ErrorFormat("CommitPollingClient {0}: Unable to dispatch commit {1} into TPL chain, buffer block did not accept message", _id, chunk.Position);
                    _stopRequested.Cancel();
                    return(Task.FromResult(false)); //stop polling.
                }

                _lastDispatchedPosition = chunk.Position;
                return(Task.FromResult(true));
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat(ex, "CommitPollingClient2 {0}: Error in dispatching commit {1} - {2}", _id, chunk.Position, ex.Message);
                return(Task.FromResult(false));
            }
        }
Example #5
0
            private void DoPoll()
            {
                if (_persistStreams.IsDisposed)
                {
                    return;                             //no need to poll, someone disposed NEventStore wihout sh
                }
                if (Interlocked.CompareExchange(ref _isPolling, 1, 0) == 0)
                {
                    Int32 exceptionCount = 0;
                    do
                    {
                        try
                        {
                            skippedPoolCount = 0;
                            IEnumerable <ICommit> commits = _persistStreams.GetFrom(_checkpointToken);
                            Int64 i = 0;
                            foreach (var commit in commits)
                            {
                                if (_stopRequested.IsCancellationRequested)
                                {
                                    _subject.OnCompleted();
                                    return;
                                }

                                _enhancer.Enhance(commit);

                                _subject.OnNext(commit);
                                i++;
                                if (i % 1000 == 0)
                                {
                                    var minCheckpoint    = _tracker.GetMinCheckpoint();
                                    var actualCheckpoint = Int64.Parse(commit.CheckpointToken);
                                    while (actualCheckpoint > minCheckpoint + 1000)
                                    {
                                        Thread.Sleep(500);
                                        minCheckpoint = _tracker.GetMinCheckpoint();
                                    }
                                }
                                _checkpointToken = commit.CheckpointToken;
                            }
                        }
                        catch (Exception ex)
                        {
                            // These exceptions are expected to be transient
                            Logger.Error(ex.ToString());
                            exceptionCount++;
                            if (_persistStreams.IsDisposed || exceptionCount > 5)
                            {
                                //eventstore is stopped or too many exception, need to exit poll cycle.
                                skippedPoolCount = 0;
                            }
                            else
                            {
                                //conceptually if we have an exception and we are in manual poll we
                                //are potentially skipping some commit so we need to reschedule a poll
                                Interlocked.Increment(ref skippedPoolCount);
                            }
                        }
                    } while (skippedPoolCount > 0);
                    Interlocked.Exchange(ref _isPolling, 0);
                }
                else
                {
                    Interlocked.Increment(ref skippedPoolCount);
                    Logger.Debug("Poll skipped, count " + skippedPoolCount);
                }
            }
Example #6
0
 private ICommit Enhance(ICommit arg)
 {
     _enhancer.Enhance(arg);
     return(arg);
 }