Example #1
0
        private async Task <EventRange> loadEvents(EventRange range)
        {
            if (_cancellation.IsCancellationRequested)
            {
                return(null);
            }

            await _daemon.TryAction(this, async() =>
            {
                try
                {
                    await _fetcher.Load(range, _cancellation);
                }
                catch (Exception e)
                {
                    if (!_cancellation.IsCancellationRequested)
                    {
                        _logger.LogError(e, "Error loading events for {Range}", range);
                        throw new EventFetcherException(_projectionShard.Name, e);
                    }
                }

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Loaded events for {Range}", range);
                }
            }, _cancellation);

            return(range);
        }
Example #2
0
        public async Task Load(EventRange range, CancellationToken token)
        {
            // There's an assumption here that this method is only called sequentially
            // and never at the same time on the same instance

            try
            {
                range.Events = new List <IEvent>();

                using var session = querySession();
                _floor.Value      = range.SequenceFloor;
                _ceiling.Value    = range.SequenceCeiling;

                using var reader = await session.ExecuteReaderAsync(_command, token).ConfigureAwait(false);

                while (await reader.ReadAsync(token).ConfigureAwait(false))
                {
                    var @event = await _storage.ResolveAsync(reader, token).ConfigureAwait(false);

                    if (!await reader.IsDBNullAsync(_aggregateIndex, token).ConfigureAwait(false))
                    {
                        @event.AggregateTypeName = await reader.GetFieldValueAsync <string>(_aggregateIndex, token).ConfigureAwait(false);
                    }

                    range.Events.Add(@event);
                }
            }
            catch (Exception e)
            {
                throw new EventFetcherException(range.ShardName, _database, e);
            }

            Debug.WriteLine("here");
        }
Example #3
0
        private async Task <EventRangeGroup> groupEventRange(EventRange range)
        {
            if (_cancellation.IsCancellationRequested)
            {
                return(null);
            }

            EventRangeGroup group = null;

            await TryAction(async() =>
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Shard '{ProjectionShardIdentity}':Starting to group {Range}", ProjectionShardIdentity, range);
                }

                @group = await _source.GroupEvents(_store, _daemon.Database, range, _cancellation).ConfigureAwait(false);

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Shard '{ProjectionShardIdentity}': successfully grouped {Range}", ProjectionShardIdentity, range);
                }
            }, _cancellation, (logger, e) =>
            {
                logger.LogError(e, "Error while trying to group event range {EventRange} for projection shard {SProjectionShardIdentity}", range, ProjectionShardIdentity);
            }).ConfigureAwait(false);



            return(group);
        }
Example #4
0
        private async Task<EventRangeGroup> groupEventRange(EventRange range)
        {
            if (_cancellation.IsCancellationRequested) return null;

            EventRangeGroup group = null;

            await TryAction(async () =>
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Shard '{ShardName}':Starting to group {Range}", Name, range);
                }

                group = await _source.GroupEvents(_store, range, _cancellation);

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Shard '{ShardName}': successfully grouped {Range}", Name, range);
                }

            }, _cancellation, (logger, e) =>
            {
                logger.LogError(e, "Error while trying to group event range {EventRange} for projection shard {ShardName}", range, Name);
            });



            return group;
        }
        private void startRange(long floor, long ceiling)
        {
            var range = new EventRange(_shardName, floor, ceiling);

            LastEnqueued = range.SequenceCeiling;
            _inFlight.Enqueue(range);
            _agent.StartRange(range);
        }
Example #6
0
        public TenantedEventRange(IDocumentStore store, IProjection projection, EventRange range,
                                  CancellationToken shardCancellation) : base(range, shardCancellation)
        {
            _store      = (DocumentStore)store;
            _projection = projection;

            buildGroups();
        }
Example #7
0
        public void StartRange(EventRange range)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Enqueued processing of " + range);
            }

            _loader.Post(range);
        }
        public TenantedEventRangeGroup(IDocumentStore store, IMartenDatabase daemonDatabase, IProjection projection,
                                       EventRange range,
                                       CancellationToken shardCancellation) : base(range, shardCancellation)
        {
            _store          = (DocumentStore)store;
            _daemonDatabase = daemonDatabase;
            _projection     = projection ?? throw new ArgumentNullException(nameof(projection));

            buildGroups();
        }
        public void EventRangeUpdated(EventRange range)
        {
            LastCommitted = range.SequenceCeiling;
            if (Equals(range, _inFlight.Peek()))
            {
                _inFlight.Dequeue();
            }

            enqueueNewEventRanges();
        }
Example #10
0
        public void StartRange(EventRange range)
        {
            if (_cancellation.IsCancellationRequested) return;

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Enqueued processing of {Range}", range);
            }

            _loader.Post(range);
        }
Example #11
0
        private async Task <EventRange> loadEvents(EventRange range)
        {
            var parameters = new ActionParameters(this, async() =>
            {
                await _fetcher !.Load(range, _cancellation).ConfigureAwait(false);

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Loaded events {Range} for {ProjectionShardIdentity}", range, ProjectionShardIdentity);
                }
            })
Example #12
0
        public void StartRange(EventRange range)
        {
            if (_cancellation.IsCancellationRequested)
            {
                return;
            }

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Enqueued processing {Range} for {ProjectionShardIdentity}", range, ProjectionShardIdentity);
            }

            _loader.Post(range);
        }
Example #13
0
        internal ProjectionUpdateBatch(EventGraph events, DocumentSessionBase session, EventRange range)
        {
            Range    = range;
            _session = session;
            Queue    = new ActionBlock <IStorageOperation>(processOperation,
                                                           new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 1, EnsureOrdered = true
            });

            startNewPage(session);

            var progressOperation = range.BuildProgressionOperation(events);

            Queue.Post(progressOperation);
        }
Example #14
0
        public async Task Load(EventRange range, CancellationToken token)
        {
            if (_session == null)
            {
                reset();
            }

            _cancellation.CancelAfter(5.Seconds());

            // There's an assumption here that this method is only called sequentially
            // and never at the same time on the same instance
            _statement.Range = range;

            range.Events = await _session.ExecuteHandlerAsync(_handler, token);
        }
Example #15
0
        public async Task Load(ShardName projectionShardName, EventRange range, CancellationToken token)
        {
            using var session = (QuerySession)_store.QuerySession();

            // There's an assumption here that this method is only called sequentially
            // and never at the same time on the same instance
            _statement.Range = range;

            try
            {
                range.Events = new List <IEvent>(await session.ExecuteHandlerAsync(_handler, token).ConfigureAwait(false));
            }
            catch (Exception e)
            {
                throw new EventFetcherException(projectionShardName, e);
            }
        }
        internal ProjectionUpdateBatch(EventGraph events, DaemonSettings settings,
                                       DocumentSessionBase session, EventRange range, CancellationToken token, ShardExecutionMode mode)
        {
            Range     = range;
            _settings = settings;
            _session  = session;
            _token    = token;
            _mode     = mode;
            Queue     = new ActionBlock <IStorageOperation>(processOperation,
                                                            new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 1, EnsureOrdered = true, CancellationToken = token
            });

            startNewPage(session);

            var progressOperation = range.BuildProgressionOperation(events);

            Queue.Post(progressOperation);
        }
Example #17
0
        private async Task <EventRange> loadEvents(EventRange range)
        {
            try
            {
                // TODO -- resiliency here.
                await _fetcher.Load(range, _cancellationSource.Token);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error loading events for " + range);
                // TODO -- retry? circuit breaker?
            }

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"Loaded events for {range}");
            }

            return(range);
        }
Example #18
0
        private T groupEventRange(EventRange range)
        {
            if (_token.IsCancellationRequested)
            {
                return(null);
            }

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"Shard '{ProjectionOrShardName}': Starting to slice {range}");
            }

            var group = applyGrouping(range);

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"Shard '{ProjectionOrShardName}': successfully sliced {range}");
            }

            return(group);
        }
Example #19
0
        public async Task Load(ShardName projectionShardName, EventRange range, CancellationToken token)
        {
            if (_session == null)
            {
                reset();
            }

            _cancellation.CancelAfter(5.Seconds());

            // There's an assumption here that this method is only called sequentially
            // and never at the same time on the same instance
            _statement.Range = range;

            try
            {
                range.Events = await _session.ExecuteHandlerAsync(_handler, token);
            }
            catch (Exception e)
            {
                throw new EventFetcherException(projectionShardName, e);
            }
        }
Example #20
0
        private async Task<EventRange> loadEvents(EventRange range)
        {
            var parameters = new ActionParameters(this, async () =>
            {
                await _fetcher.Load(_projectionShard.Name, range, _cancellation);

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Loaded events for {Range}", range);
                }
            })
            {
                LogAction = (logger, e) =>
                {
                    logger.LogError(e, "Error loading events for {Range}", range);
                }
            };


            await _daemon.TryAction(parameters);

            return range;
        }
Example #21
0
        private async Task <EventRange> loadEvents(EventRange range)
        {
            var parameters = new ActionParameters(this, async() =>
            {
                await _fetcher.Load(range, _cancellation).ConfigureAwait(false);

                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Loaded events {Range} for {ProjectionShardIdentity}", range, ProjectionShardIdentity);
                }
            })
            {
                LogAction = (logger, e) =>
                {
                    logger.LogError(e, "Error loading events {Range} for {ProjectionShardIdentity}", range, ProjectionShardIdentity);
                }
            };


            await _daemon.TryAction(parameters).ConfigureAwait(false);

            return(range);
        }
Example #22
0
        private async Task <T> groupEventRange(EventRange range)
        {
            if (_token.IsCancellationRequested)
            {
                return(null);
            }

            T group = null;

            await _updater.TryAction(() =>
            {
                try
                {
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug("Shard '{ShardName}':Starting to slice {Range}", Name, range);
                    }

                    group = applyGrouping(range);
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug("Shard '{ShardName}': successfully slice {Range}", Name, range);
                    }

                    return(Task.CompletedTask);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Error while trying to group event range {EventRange} for projection shard {ShardName}", range, Name);
                    throw;
                }
            }, _token);



            return(group);
        }
Example #23
0
        public TenantedEventRange(EventGraph graph, ITenancy tenancy, EventRange range)
        {
            Range = range;

            var byTenant = range.Events.GroupBy(x => x.TenantId);

            foreach (var group in byTenant)
            {
                var tenant = tenancy[group.Key];

                var actions = graph.StreamIdentity switch
                {
                    StreamIdentity.AsGuid => group.GroupBy(x => x.StreamId)
                    .Select(events => StreamAction.For(events.Key, events.ToList())),

                    StreamIdentity.AsString => group.GroupBy(x => x.StreamKey)
                    .Select(events => StreamAction.For(events.Key, events.ToList())),

                    _ => null
                };

                Groups.Add(new TenantActionGroup(tenant, actions));
            }
        }
Example #24
0
 protected bool Equals(EventRange other)
 {
     return(Equals(ShardName, other.ShardName) && SequenceFloor == other.SequenceFloor && SequenceCeiling == other.SequenceCeiling);
 }
Example #25
0
        public ProjectionUpdateBatch StartNewBatch(EventRange range)
        {
            var session = _store.LightweightSession();

            return(new ProjectionUpdateBatch(_store.Events, (DocumentSessionBase)session, range));
        }
Example #26
0
 protected abstract T applyGrouping(EventRange range);
Example #27
0
 protected EventRangeGroup(EventRange range, CancellationToken parent)
 {
     _parent = parent;
     Range   = range;
 }
Example #28
0
 protected override TenantedEventRange applyGrouping(EventRange range)
 {
     return(new(Store.Events, Store.Tenancy, range));
 }
Example #29
0
 protected bool Equals(EventRange other)
 {
     return(ProjectionOrShardName == other.ProjectionOrShardName && SequenceFloor == other.SequenceFloor && SequenceCeiling == other.SequenceCeiling);
 }